Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
3cky committed Apr 11, 2018
1 parent fb3d6d6 commit a6d204b
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 1,686 deletions.
12 changes: 7 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/extern_GPL)
include(CheckFunctionExists)
include(CheckLibraryExists)
include(GNUInstallDirs)
include(FindUnixCommands)
include(FindSystemd)

#TODO ISC_Posix, prog_libtool
Expand Down Expand Up @@ -98,11 +99,12 @@ add_custom_target(uninstall
${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
)

# unittest target
option(TESTS "Enable unittests" OFF)
if(TESTS)
add_executable(test_basic tests/test_basics.c)
endif()
# integration tests target
enable_testing()
add_test(
NAME itests
COMMAND ${BASH} -c "cd ${CMAKE_CURRENT_SOURCE_DIR}/tests && ./run_itests.sh $<TARGET_FILE:mbusd>"
)

## Please find Packaging stuff following
#@source http://xit0.org/2013/04/cmake-use-git-branch-and-commit-details-in-project/
Expand Down
35 changes: 35 additions & 0 deletions tests/environment/mbusd_runner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

MBUSD_BIN=${MBUSD_BIN:=mbusd}
MBUSD_ARGS="-d -L - -v9 -p /tmp/pts0 -s 19200 -P 1025"
MBUSD_PID=/tmp/mbusd.pid

# check argument count
## https://stackoverflow.com/questions/4341630/checking-for-the-correct-number-of-arguments
if [ "$#" -ne 1 ]; then
echo "[E] usage: $0 <start|stop>" >&2
exit 1
fi

check_preconditions() {
# check if mbusd location is set
if ! [ -x "$MBUSD_BIN" ]; then
echo "[E] executable binary not found: ${MBUSD_BIN}" >&2
exit 1
fi
}

check_preconditions
CURRENT_DIR="$(dirname "$(realpath "$0")")"
. $CURRENT_DIR/subprocess_helper.sh

case "$1" in
up|start)
CMD="$MBUSD_BIN $MBUSD_ARGS &"
run_cmd_save_pid "$CMD" $MBUSD_PID
;;
down|stop)
kill_pid $MBUSD_PID
;;
esac

Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ def _start_rtu_server(self, framer=ModbusRtuFramer):
# store = ModbusSlaveContext(..., zero_mode=True)
#---------------------------------------------------------------------------#
store = ModbusSlaveContext(
di = ModbusSequentialDataBlock(0, [12]*100), # discrete input
co = ModbusSequentialDataBlock(0, [13]*100), # coils
hr = ModbusSequentialDataBlock(0, [14]*100), # holding reg
ir = ModbusSequentialDataBlock(0, [15]*100)) #
di = ModbusSequentialDataBlock(0, [True]*8), # discrete inputs
co = ModbusSequentialDataBlock(0, [False]*8), # coils
hr = ModbusSequentialDataBlock(0, [0]*8), # holding regs
ir = ModbusSequentialDataBlock(0, list(range(8))), # input regs
zero_mode=True) # request(0-7) will map to the address (0-7)
context = ModbusServerContext(slaves=store, single=True)

#---------------------------------------------------------------------------#
Expand Down Expand Up @@ -127,7 +128,7 @@ def start(self):


def kill(self):
print("Going to terminat the process, this could throw exceptins")
print("Going to terminate the process, this could throw exceptions")
if self.p is not None:
self.p.terminate()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
#!/usr/bin/env bash

MBUS_SERVER_PID=/tmp/modbus_server.pid
RTU_SLAVE_PID=/tmp/rtu_slave.pid

CURRENT_DIR="$(dirname "$(realpath "$0")")"
. $CURRENT_DIR/subprocess_helper.sh

check_preconditions() {
#TODO check if python module 'pymodbus' is installed
#python -c "import foo"
true
python -c "import pymodbus" || exit 1
}

# check argument count
## https://stackoverflow.com/questions/4341630/checking-for-the-correct-number-of-arguments
if [ "$#" -ne 1 ]; then
echo "Usage: $0 up|down" >&2
echo "[E] usage: $0 <start|stop>" >&2
exit 1
fi

check_preconditions
case "$1" in
up|start)
#TOOO obtain current directory
CMD="python ${CURRENT_DIR}/modbus_server_mock.py &"
run_cmd_save_pid "$CMD" $MBUS_SERVER_PID
CMD="python ${CURRENT_DIR}/rtu_slave.py &"
run_cmd_save_pid "$CMD" $RTU_SLAVE_PID
;;
down|stop)
kill_pid $MBUS_SERVER_PID
kill_pid $RTU_SLAVE_PID
;;
esac

2 changes: 1 addition & 1 deletion tests/environment/socat_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ check_preconditions() {
# check argument count
## https://stackoverflow.com/questions/4341630/checking-for-the-correct-number-of-arguments
if [ "$#" -ne 1 ]; then
echo "Usage: $0 up|down" >&2
echo "[E] usage: $0 <start|stop>" >&2
exit 1
fi

Expand Down
10 changes: 8 additions & 2 deletions tests/environment/subprocess_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ run_cmd_save_pid() {
local _PID_FILE=$2

if [[ -e $_PID_FILE ]]; then
echo "[D] pid_file ($_PID_FILE) exists, done"
return 1
_PID=`cat $_PID_FILE`
if ps -p $_PID > /dev/null; then
echo "[D] PID file ($_PID_FILE) exists, done"
return 1
else
echo "[D] removing stale PID file ($_PID_FILE)"
rm -f $_PID_FILE
fi
fi

echo "[I] running $_CMD in background..."
Expand Down
75 changes: 0 additions & 75 deletions tests/greatest/CONTRIBUTING.md

This file was deleted.

13 changes: 0 additions & 13 deletions tests/greatest/LICENSE

This file was deleted.

Loading

0 comments on commit a6d204b

Please sign in to comment.