Tests - Tweaking
[tpg/opendispense2.git] / tests / _common.sh
1 #
2 # NOTE: Not a script, to be included by scripts
3 #
4 USER=$(id -un)
5 BASEDIR=rundir/${TESTNAME}/
6 PORT=22222
7
8 mkdir -p ${BASEDIR}
9 rm -f ${BASEDIR}cokebank.db
10
11 # Template configuration (disables door and the coke machine)
12 cat << EOF > ${BASEDIR}cfg_server.conf
13 # AUTOGENERATED Test ${TESTNAME}
14 server_port ${PORT}
15 cokebank_database ${BASEDIR}cokebank.db
16 items_file ${BASEDIR}cfg_items.conf
17 door_serial_port /dev/null
18
19 coke_modbus_address 0.0.0.0
20 coke_modbus_port 502
21 test_mode no
22
23 disable_syslog yes
24 coke_dummy_mode yes
25 EOF
26
27 echo "# AUTOGENERATED Test ${TESTNAME}" > ${BASEDIR}cfg_items.conf
28 echo "pseudo    0       0       Test item" >> ${BASEDIR}cfg_items.conf
29
30 LOG() {
31         echo "TEST ${TESTNAME}: "$*
32 }
33 FAIL() {
34         echo "TEST ${TESTNAME} FAIL: "$*
35         exit 1
36 }
37 TRY_COMMAND() {
38         cmd="$*"
39         echo ">> $cmd"
40         if ! $cmd ; then
41                 FAIL "Command \`$cmd\` failed"
42         fi
43 }
44
45 DISPENSE="../dispense -f /dev/null -H localhost -P ${PORT}"
46
47 LD_LIBRARY_PATH=.. ../dispsrv -f ${BASEDIR}cfg_server.conf --dont-daemonise > ${BASEDIR}server.log 2>&1 &
48 server_pid=$!
49
50 cleanup() {
51         if pidof dispsrv | grep ${server_pid} > /dev/null; then
52                 LOG "Killing ${server_pid}"
53                 kill ${server_pid}; true
54         else
55                 LOG "Server already terminated"
56         fi
57 }
58 trap cleanup EXIT
59
60 LOG "Server running on PID ${server_pid}"
61 sleep 1
62 # - Make sure that the server started
63 if ! (echo "" | nc localhost ${PORT}); then
64         LOG "Server not responding on ${PORT}"
65         LOG "Server log contents:"
66         cat ${BASEDIR}server.log
67         exit 1
68 fi
69
70 if [[ "x$USER" == "xroot" ]]; then
71         FAIL "Running as root"
72 fi
73 # Ensure that the database doesn't already contain the current user
74 # - 1. Protects against running the test against the production database
75 # - 2. Allows adding the current user as an admin
76 if $DISPENSE acct "${USER}" 2> /dev/null; then
77         FAIL "Database contains '$USER', are you running on production?"
78 fi
79 # Add the current user as an admin
80 sqlite3 "${BASEDIR}cokebank.db" "INSERT INTO accounts (acct_name,acct_is_admin,acct_uid) VALUES ('${USER}',1,1);"

UCC git Repository :: git.ucc.asn.au