From 74189d6caf345b2cf800f356001d620a6d80cefd Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 18 Jun 2018 16:06:16 +0200 Subject: [PATCH] test: moved all setup/teardown into the python realm --- .../environment/notneeded_rtu_slave_runner.sh | 28 ------------- tests/run_itests.py | 40 +++++++++++++------ tests/run_itests.sh | 20 +--------- 3 files changed, 29 insertions(+), 59 deletions(-) delete mode 100755 tests/environment/notneeded_rtu_slave_runner.sh diff --git a/tests/environment/notneeded_rtu_slave_runner.sh b/tests/environment/notneeded_rtu_slave_runner.sh deleted file mode 100755 index b14db06..0000000 --- a/tests/environment/notneeded_rtu_slave_runner.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env bash - -RTU_SLAVE_PID=/tmp/rtu_slave.pid - -CURRENT_DIR="$(dirname "$(realpath "$0")")" -. $CURRENT_DIR/subprocess_helper.sh - -check_preconditions() { - 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 "[E] usage: $0 " >&2 - exit 1 -fi - -check_preconditions -case "$1" in - up|start) - CMD="python ${CURRENT_DIR}/rtu_slave.py &" - run_cmd_save_pid "$CMD" $RTU_SLAVE_PID - ;; - down|stop) - kill_pid $RTU_SLAVE_PID - ;; -esac diff --git a/tests/run_itests.py b/tests/run_itests.py index fa2f5c8..3625cbb 100755 --- a/tests/run_itests.py +++ b/tests/run_itests.py @@ -3,6 +3,9 @@ import random import unittest import sys +import logging +from subprocess import Popen +from os.path import isfile from pymodbus.client.sync import ModbusTcpClient from pymodbus.pdu import ExceptionResponse @@ -11,31 +14,41 @@ from pymodbus.bit_write_message import WriteMultipleCoilsResponse, WriteSingleCo from pymodbus.register_read_message import ReadInputRegistersResponse, ReadHoldingRegistersResponse from pymodbus.register_write_message import WriteMultipleRegistersResponse, WriteSingleRegisterResponse -from environment.rtu_slave import ModbusSerialServer - -MBUSD_PORT = 1025 - +MBUSD_PORT = 1025 +MBUSD_BINARY = "../output.dir/mbusd" class TestModbusRequests(unittest.TestCase): + log = logging.getLogger("TestModbusRequests") @classmethod def setUpClass(cls): - cls.log = logging.getLogger("TestModbusRequests") + cls.log.debug("1. run socat") + cls.socat = Popen(["socat", "-d", "-d", "pty,raw,echo=0,link=/tmp/pts0", "pty,raw,echo=0,link=/tmp/pts1"]) + cls.log.debug("2. run rtu_slave") + from environment.rtu_slave import ModbusSerialServer cls.mbs = ModbusSerialServer() cls.mbs.start() + cls.log.debug("3. run mbusd to be tested with the binary:%s" % MBUSD_BINARY) + cls.mbusd_main = Popen([MBUSD_BINARY, "-d", "-L", "-v9", "-p/tmp/pts0", "-s19200", "-P" + str(MBUSD_PORT)]) + + cls.log.debug("4. connect the modbus TCP client to mbusd") cls.client = ModbusTcpClient('127.0.0.1', port=MBUSD_PORT) cls.client.connect() @classmethod def tearDownClass(cls): cls.log.info("test teardown") + + cls.log.debug("4. kill tcp_client") cls.client.close() - try: - cls.mbs.kill() - except e: - cls.log.info("Fetched exception during mbs.kill") + cls.log.debug("3. kill mbusd") + cls.mbusd_main.kill() + cls.log.debug("2. kill rtu_slave") + cls.mbs.kill() + cls.log.debug("1. kill socat") + cls.socat.kill() def test_coils(self): @@ -104,12 +117,15 @@ class TestModbusRequests(unittest.TestCase): self.assertEqual(result.original_code, 5, result) # fc05 Write Single Coil self.assertEqual(result.exception_code, 2, result) # Illegal Data Address - if __name__ == '__main__': - import logging stdout_handler = logging.StreamHandler(sys.stdout) logging.basicConfig(level=logging.DEBUG, format=u'[%(asctime)s] %(name)-26s-%(levelname)-5s %(funcName)-20s:%(lineno)-4d \033[35m%(message)s\033[0m', datefmt='%d.%m. %H:%M:%S', handlers=[stdout_handler]) - unittest.main(verbosity=2) \ No newline at end of file + if len(sys.argv) != 2 or not isfile(sys.argv[1]): + logging.error("usage: ./run_itests.py ") + sys.exit(1) + MBUSD_BINARY = sys.argv[1] + + unittest.main(verbosity=2, argv=[sys.argv[0]]) diff --git a/tests/run_itests.sh b/tests/run_itests.sh index ad77920..bf7dc36 100755 --- a/tests/run_itests.sh +++ b/tests/run_itests.sh @@ -8,24 +8,6 @@ if ! [ -x "$1" ]; then fi export MBUSD_BIN=$1 - -function setup() { - echo "[I] do test environment setup" - $CWD/environment/socat_runner.sh start || return 1 - # $CWD/environment/rtu_slave_runner.sh start || return 1 - $CWD/environment/mbusd_runner.sh start || return 1 -} - -function teardown() { - echo "[I] do test environment teardown" - $CWD/environment/mbusd_runner.sh stop - # $CWD/environment/rtu_slave_runner.sh stop - $CWD/environment/socat_runner.sh stop -} -trap teardown EXIT - -setup || (echo "failed to setup" && exit 1) - -$CWD/run_itests.py || exit 1 +$CWD/run_itests.py "$MBUSD_BIN" || exit 1 exit 0