[PATCH 2/3] init-d-script-test.sh: test script for init-d-script

Trek trek00 at inbox.ru
Tue Apr 26 22:33:28 BST 2022


It checks the exit value and running status of a daemon managed
by the init-d-script framework in common use cases.
---
 debian/init-d-script-test.sh | 92 ++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)
 create mode 100644 debian/init-d-script-test.sh

diff --git a/debian/init-d-script-test.sh b/debian/init-d-script-test.sh
new file mode 100644
index 00000000..8e83b9f3
--- /dev/null
+++ b/debian/init-d-script-test.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+# usage: init-d-script-test.sh [ init-d-script ]
+
+INIT_D_SCRIPT=${1:-debian/init-d-script}
+SHELL=/bin/sh
+#TMPDIR=/tmp
+
+
+log () { echo "*** $@"; }
+
+# usage: try action exitvalue isrunning
+try ()
+{
+  # redirect to /dev/null in order to not enable VERBOSE
+  [ no = $verbose ] && in=3 out=3 || in=0 out=1
+
+  $SHELL $INIT_D_SCRIPT $dir/script $1 <&$in >&$out
+
+  [ $? -ne $2 ] && err=1 && log $1 failed on exit value
+
+  (read pid <$dir/pid && kill -0 $pid) 2>/dev/null || false
+
+  [ $? -ne $3 ] && err=1 && log $1 failed on running status
+}
+
+
+[ ! -t 1 ] && echo this test needs a valid tty && exit 2
+
+dir=$(mktemp -d)
+[ -d "$dir" ] || exit 2
+
+err=0
+exec 3<>/dev/null
+
+cat >$dir/script <<-EOF
+	DAEMON=/bin/sleep
+	DAEMON_ARGS=10
+	START_ARGS="--background --make-pidfile"
+	PIDFILE=$dir/pid
+	alias do_reload=do_reload_sigusr1
+EOF
+
+for verbose in yes no
+do
+  rm -f $dir/pid
+  killall -q sleep
+  log testing with VERBOSE=$verbose
+
+  try start 0 0
+  try start 0 0
+  try status 0 0
+  try try-restart 0 0
+  try restart 0 0
+  try stop 0 1
+  try stop 0 1
+  try status 3 1
+  try try-restart 0 1
+  try restart 0 0
+  try force-reload 0 1	# sleep gets killed here, as it does not handle SIGHUP
+done
+
+log testing errors
+
+# program is dead but pidfile exists
+echo 123456789 >$dir/pid
+killall -q sleep
+try status 1 1
+try reload 1 1
+rm -f $dir/pid
+
+# start-stop-daemon invalid arguments
+cat >$dir/script <<-EOF
+	DAEMON=/bin/true
+	START_ARGS=--notexistant
+	STOP_ARGS=--notexistant
+EOF
+
+try start 1 1
+try stop 1 1
+
+# package removed but not purged
+cat >$dir/script <<-EOF
+	DAEMON=/notexistant
+EOF
+
+try start 0 1
+
+
+[ $err -eq 0 ] && log OK || log FAILED!
+
+rm -fr $dir
+exit $err
-- 
2.20.1



More information about the Debian-init-diversity mailing list