From 4848b3853ef5517c4da4f2bcd6c8b959dae33411 Mon Sep 17 00:00:00 2001
From: John Hodge <tpg@ucc.asn.au>
Date: Sun, 19 Sep 2021 15:08:09 +0800
Subject: [PATCH] tests - Support running as `root` (for CI)

---
 tests/TEST_basic.sh | 21 ++++++++++++++-------
 tests/_common.sh    |  4 ++--
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/tests/TEST_basic.sh b/tests/TEST_basic.sh
index 481e119..76c6490 100755
--- a/tests/TEST_basic.sh
+++ b/tests/TEST_basic.sh
@@ -4,14 +4,21 @@ TESTNAME=basic
 
 . _common.sh
 
-# Ensure that the databse doesn't already contain the current user
-# - 1. Protects against running the test against the production database
-# - 2. Allows adding the current user as an admin
-if $DISPENSE acct "$USER"; then
-	FAIL "Database contains '$USER'"
+# (CI runs as root, and root is auto-added by databse creation)
+if [[ "$USER" -ne "root" ]]; then
+	# Ensure that the databse doesn't already contain the current user
+	# - 1. Protects against running the test against the production database
+	# - 2. Allows adding the current user as an admin
+	if $DISPENSE acct "$USER"; then
+		FAIL "Database contains '$USER'"
+	fi
+	# Add the current user as an admin
+	sqlite3 "${BASEDIR}cokebank.db" "INSERT INTO accounts (acct_name,acct_is_admin,acct_uid) VALUES ('${USER}',1,1);"
+else
+	if $DISPENSE acct accmurph; then
+		FAIL "Database contains 'accmurph' (running as root, might be using prodution)"
+	fi
 fi
-# Add the current user as an admin
-sqlite3 "${BASEDIR}cokebank.db" "INSERT INTO accounts (acct_name,acct_is_admin,acct_uid) VALUES ('${USER}',1,1);"
 
 # Try to add a new user
 TRY_COMMAND "$DISPENSE user add unittest_user0"
diff --git a/tests/_common.sh b/tests/_common.sh
index fa2f107..04d1657 100644
--- a/tests/_common.sh
+++ b/tests/_common.sh
@@ -41,13 +41,13 @@ TRY_COMMAND() {
 	fi
 }
 
-DISPENSE="../dispense -H localhost -P ${PORT}"
+DISPENSE="../dispense -f /dev/null -H localhost -P ${PORT}"
 
 LD_LIBRARY_PATH=.. ../dispsrv -f ${BASEDIR}cfg_server.conf --dont-daemonise > ${BASEDIR}server.log 2>&1 &
 server_pid=$!
 
 cleanup() {
-	if pidof dispsrv | grep ${server_pid}; then
+	if pidof dispsrv | grep ${server_pid} > /dev/null; then
 		LOG "Killing ${server_pid}"
 		kill ${server_pid}; true
 	else
-- 
GitLab