diff --git a/test/Makefile b/test/Makefile index 206ffb93..8000ccbf 100644 --- a/test/Makefile +++ b/test/Makefile @@ -8,18 +8,22 @@ testroot_bin = $(testroot)/bin binaries = $(testroot_bin)/cat \ $(testroot_bin)/date \ $(testroot_bin)/echo \ + $(testroot_bin)/kill \ $(testroot_bin)/ls \ $(testroot_bin)/mkdir \ $(testroot_bin)/mkfifo \ $(testroot_bin)/mknod \ $(testroot_bin)/mount \ $(testroot_bin)/printf \ + $(testroot_bin)/pgrep \ $(testroot_bin)/ps \ + $(testroot_bin)/rm \ $(testroot_bin)/sh \ $(testroot_bin)/sleep \ $(testroot_bin)/top \ $(testroot_bin)/touch dirs = $(testroot)/dev \ + $(testroot)/etc \ $(testroot)/lib \ $(testroot)/proc \ $(testroot)/sbin \ @@ -45,8 +49,8 @@ $(testroot_bin)/%: $(testroot_bin)/$(busybox-bin) cd $(testroot_bin); \ ln -s $(busybox-bin) $(notdir $@) -$(testroot_bin)/testwrapper.sh: - cp testwrapper.sh $@ +$(testroot_bin)/chrootsetup.sh: chrootsetup.sh + cp $< $@ $(testroot)/sbin/finit: ../src/finit $(testroot)/lib/ld-musl-x86_64.so.1 $(testroot)/usr/local/lib/libite.so.5 $(testroot)/usr/local/lib/libuev.so.2 cp $< $@ @@ -61,8 +65,8 @@ $(testroot)/usr/local/lib/libuev.so.2: cp test_root.backup/usr/local/lib/libuev.so.2 $@ .PHONY: test -test: $(binaries) $(testroot_bin)/testwrapper.sh $(dirs) $(testroot)/sbin/finit - ./run.sh finit +test: $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit + ./runtest.sh ./test_start_stop_service/test.sh .PHONY: clean clean: diff --git a/test/testwrapper.sh b/test/chrootsetup.sh similarity index 95% rename from test/testwrapper.sh rename to test/chrootsetup.sh index 19a9afee..ed8f5dd7 100755 --- a/test/testwrapper.sh +++ b/test/chrootsetup.sh @@ -17,6 +17,7 @@ mount -n -t tmpfs none /dev mount -t proc none /proc mount -t sysfs none /sys mount -t tmpfs none /tmp +mount -t tmpfs none /etc # mkdir -p /dev/pts # mount -t devpts none /dev/pts diff --git a/test/runtest.sh b/test/runtest.sh new file mode 100755 index 00000000..75ad2c16 --- /dev/null +++ b/test/runtest.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +set -eu + +texec() { + ./testenv_exec.sh $(pgrep -P $finit_ppid) "$@" +} + +fg_red='31' +fg_green='32' +fg_yellow='33' +log() { + printf "< TEST > \e[1;%dm%s\e[0m %s\n" "$1" "$2" "$3" +} + +assert() { + msg=$1 + shift + if [ ! "$@" ]; then + log "$fg_red" ✘ "$msg" + exit 1 + fi + log "$fg_green" ✔ "$msg" +} + +say() { + log "$fg_yellow" ● "$@" +} + +teardown() { + texec kill 1 + + wait +} + +trap teardown EXIT + +# Setup +./testenv_start.sh finit & +finit_ppid=$! +for i in $(seq 1 50); do + sleep 0.1 + finit_pid=$(pgrep -P "$finit_ppid") || continue + break +done + +texec cat /dev/tty0 & +sleep 1 + +TEST_DIR=$(dirname $1) + +source "$1" diff --git a/test/test_start_stop_service/service.sh b/test/test_start_stop_service/service.sh new file mode 100755 index 00000000..e4ae0ede --- /dev/null +++ b/test/test_start_stop_service/service.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +set -eu + +while [ 1 ]; do + sleep 5 +done diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh new file mode 100755 index 00000000..2d6b5810 --- /dev/null +++ b/test/test_start_stop_service/test.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +set -eu + +# Test +say 'Set up a service' +cp "$TEST_DIR"/service.sh test_root/bin +texec sh -c 'echo "service [2345] kill:20 log /bin/service.sh" > /etc/finit.conf' + +say 'Reload Finit' +texec kill -SIGHUP 1 +sleep 1 + +service_pids=( $(texec pgrep -P 1) ) +assert 'One service is running' ${#service_pids[@]} -eq 1 + +say 'Remove the service' +texec sh -c 'echo > /etc/finit.conf' + +say 'Reload Finit' +texec kill -SIGHUP 1 +sleep 1 + +service_pids=( $(texec pgrep -P 1) ) +assert 'Zero services are running' ${#service_pids[@]} -eq 0 + +# Teardown +rm -f test_root/bin/service.sh diff --git a/test/testenter.sh b/test/testenv_exec.sh similarity index 100% rename from test/testenter.sh rename to test/testenv_exec.sh diff --git a/test/run.sh b/test/testenv_start.sh similarity index 64% rename from test/run.sh rename to test/testenv_start.sh index fff9e83e..a0bb8646 100755 --- a/test/run.sh +++ b/test/testenv_start.sh @@ -5,7 +5,7 @@ set -eu TEST_DIR=$(dirname "$0") TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" -echo "Hint: Execute './test/testenter.sh \$(pgrep -P $$) /bin/sh; reset' to enter the test namespace" +>&2 echo "Hint: Execute '${TEST_DIR}/testenv_exec.sh \$(pgrep -P $$) /bin/sh; reset' to enter the test namespace" # Not supported by Busybox unshare: # --cgroup --time @@ -16,4 +16,4 @@ exec unshare \ --mount \ --mount-proc \ --uts --ipc --net \ - chroot "$TESTS_ROOT" /bin/testwrapper.sh "$@" + chroot "$TESTS_ROOT" /bin/chrootsetup.sh "$@"