From 209aad64803d474788b24ee4794013f5d400122b Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 19 Feb 2017 17:32:19 +0800 Subject: [PATCH] Add a basic unit test --- .gitignore | 2 ++ tests/TEST_basic.sh | 22 +++++++++++++++++++ tests/_common.sh | 52 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100755 tests/TEST_basic.sh create mode 100644 tests/_common.sh diff --git a/.gitignore b/.gitignore index 6e59002..fdff355 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ dispsrv dispense cokebank.log + +/tests/rundir diff --git a/tests/TEST_basic.sh b/tests/TEST_basic.sh new file mode 100755 index 0000000..f2f1301 --- /dev/null +++ b/tests/TEST_basic.sh @@ -0,0 +1,22 @@ +#!/bin/bash +set -eux +TESTNAME=basic + +. _common.sh + +if $DISPENSE acct tpg; then + FAIL "Database contains '$USER'" +fi + +sqlite3 "${BASEDIR}cokebank.db" "INSERT INTO accounts (acct_name,acct_is_admin,acct_uid) VALUES ('${USER}',1,1);" + +TRY_COMMAND "$DISPENSE user add unittest_user0" + +LOG "Checking for test user" +TRY_COMMAND $DISPENSE acct unittest_user0 | grep ': $ 0.00' + +TRY_COMMAND $DISPENSE acct unittest_user0 +100 Unit_test +TRY_COMMAND $DISPENSE acct unittest_user0 | grep ': $ 1.00' +TRY_COMMAND $DISPENSE acct unittest_user0 -100 Unit_test +TRY_COMMAND $DISPENSE acct unittest_user0 | grep ': $ 0.00' +LOG "Success" diff --git a/tests/_common.sh b/tests/_common.sh new file mode 100644 index 0000000..0019b99 --- /dev/null +++ b/tests/_common.sh @@ -0,0 +1,52 @@ + +BASEDIR=rundir/${TESTNAME}/ +PORT=22222 + +mkdir -p ${BASEDIR} +rm -f ${BASEDIR}cokebank.db + +cat << EOF > ${BASEDIR}cfg_server.conf +# AUTOGENERATED Test ${TESTNAME} +server_port ${PORT} +cokebank_database ${BASEDIR}cokebank.db +items_file ${BASEDIR}cfg_items.conf +door_serial_port /dev/null + +coke_modbus_address 0.0.0.0 +coke_modbus_port 502 +test_mode no + +disable_syslog yes +coke_dummy_mode yes +EOF + +echo "# AUTOGENERATED Test ${TESTNAME}" > ${BASEDIR}cfg_items.conf + +LOG() { + echo "TEST ${TESTNAME}: "$* +} +FAIL() { + echo "TEST ${TESTNAME} FAIL: "$* + exit 1 +} +TRY_COMMAND() { + cmd="$*" + if ! $cmd ; then + FAIL "Command \`$cmd\` failed" + fi +} + +DISPENSE="../dispense -H localhost -P ${PORT}" + +LD_LIBRARY_PATH=.. ../dispsrv -f ${BASEDIR}cfg_server.conf --dont-daemonise > ${BASEDIR}server.log 2>&1 & +server_pid=$! + +cleanup() { + LOG "Killing ${server_pid}" + kill ${server_pid} +} +trap cleanup EXIT + +LOG "Server running on PID ${server_pid}" +sleep 1 +echo "" | nc localhost ${PORT} -- 2.20.1