From aa9a5fbb99a3149ecd07769727e36ec93df01718 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Wed, 17 Feb 2021 16:20:17 +0100 Subject: [PATCH 01/90] WIP: Unshare/Chroot test wrapper script --- test/run.sh | 15 +++++++++++++++ test/testwrapper.sh | 9 +++++++++ 2 files changed, 24 insertions(+) create mode 100755 test/run.sh create mode 100755 test/testwrapper.sh diff --git a/test/run.sh b/test/run.sh new file mode 100755 index 00000000..399bdb8b --- /dev/null +++ b/test/run.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +set -eux + +TEST_DIR=$(dirname "$0") +TESTS_ROOT="${TEST_DIR}/test_root" + +cp "${TEST_DIR}/testwrapper.sh" "${TESTS_ROOT}/usr/local/bin/" + +unshare \ + --user --map-root-user \ + --fork --pid --mount-proc \ + --mount \ + --mount-proc \ + chroot "$TESTS_ROOT" /usr/local/bin/testwrapper.sh "$@" diff --git a/test/testwrapper.sh b/test/testwrapper.sh new file mode 100755 index 00000000..ec4be152 --- /dev/null +++ b/test/testwrapper.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +set -eux + +export PATH=/sbin:/bin + +mount -t proc none /proc + +exec "$@" From 4f82cb826192cbbf34d1f85c73df6ac79659fb5d Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 18 Feb 2021 00:47:55 +0100 Subject: [PATCH 02/90] WIP: test makefile --- test/.gitignore | 2 ++ test/Makefile | 70 +++++++++++++++++++++++++++++++++++++++++ test/busybox-x86_64.md5 | 1 + test/run.sh | 14 ++++++--- test/testenter.sh | 26 +++++++++++++++ test/testwrapper.sh | 22 +++++++++++-- 6 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 test/.gitignore create mode 100644 test/Makefile create mode 100644 test/busybox-x86_64.md5 create mode 100755 test/testenter.sh diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 00000000..af5859fd --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,2 @@ +busybox-x86_64 +test_root diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 00000000..206ffb93 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,70 @@ +ARCH ?= x86_64 + +busybox-ver = 1.31.0 +busybox-bin = busybox-$(ARCH) + +testroot = test_root +testroot_bin = $(testroot)/bin +binaries = $(testroot_bin)/cat \ + $(testroot_bin)/date \ + $(testroot_bin)/echo \ + $(testroot_bin)/ls \ + $(testroot_bin)/mkdir \ + $(testroot_bin)/mkfifo \ + $(testroot_bin)/mknod \ + $(testroot_bin)/mount \ + $(testroot_bin)/printf \ + $(testroot_bin)/ps \ + $(testroot_bin)/sh \ + $(testroot_bin)/sleep \ + $(testroot_bin)/top \ + $(testroot_bin)/touch +dirs = $(testroot)/dev \ + $(testroot)/lib \ + $(testroot)/proc \ + $(testroot)/sbin \ + $(testroot)/sys \ + $(testroot)/tmp \ + $(testroot)/usr/local/lib + +$(testroot)/%: $(testroot) + mkdir -p $@ + +$(busybox-bin): + wget 'https://www.busybox.net/downloads/binaries/$(busybox-ver)-defconfig-multiarch-musl/$(busybox-bin)' + md5sum -c $(busybox-bin).md5 + +$(testroot): + mkdir $(testroot) + +$(testroot_bin)/$(busybox-bin): $(testroot_bin) $(busybox-bin) + cp $(busybox-bin) $(testroot_bin)/ + chmod +x $(testroot_bin)/$(busybox-bin) + +$(testroot_bin)/%: $(testroot_bin)/$(busybox-bin) + cd $(testroot_bin); \ + ln -s $(busybox-bin) $(notdir $@) + +$(testroot_bin)/testwrapper.sh: + cp testwrapper.sh $@ + +$(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 $< $@ + +$(testroot)/lib/ld-musl-x86_64.so.1: + cp test_root.backup/lib/ld-musl-x86_64.so.1 $@ + +$(testroot)/usr/local/lib/libite.so.5: + cp test_root.backup/usr/local/lib/libite.so.5 $@ + +$(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 + +.PHONY: clean +clean: + rm -rf $(testroot) + rm -f $(busybox-bin) diff --git a/test/busybox-x86_64.md5 b/test/busybox-x86_64.md5 new file mode 100644 index 00000000..0ecfec84 --- /dev/null +++ b/test/busybox-x86_64.md5 @@ -0,0 +1 @@ +ea7e2353af0067a926d3e021e504290e busybox-x86_64 diff --git a/test/run.sh b/test/run.sh index 399bdb8b..fff9e83e 100755 --- a/test/run.sh +++ b/test/run.sh @@ -1,15 +1,19 @@ #!/bin/sh -set -eux +set -eu TEST_DIR=$(dirname "$0") -TESTS_ROOT="${TEST_DIR}/test_root" +TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" -cp "${TEST_DIR}/testwrapper.sh" "${TESTS_ROOT}/usr/local/bin/" +echo "Hint: Execute './test/testenter.sh \$(pgrep -P $$) /bin/sh; reset' to enter the test namespace" -unshare \ +# Not supported by Busybox unshare: +# --cgroup --time +export PATH=/sbin:/bin +exec unshare \ --user --map-root-user \ --fork --pid --mount-proc \ --mount \ --mount-proc \ - chroot "$TESTS_ROOT" /usr/local/bin/testwrapper.sh "$@" + --uts --ipc --net \ + chroot "$TESTS_ROOT" /bin/testwrapper.sh "$@" diff --git a/test/testenter.sh b/test/testenter.sh new file mode 100755 index 00000000..8c4b11bb --- /dev/null +++ b/test/testenter.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +set -eu + +if [ "$#" -lt 2 ]; then + echo "Usage:" + echo " $0 [target-pid] [command [arguments]]" + exit 1 +fi +target="$1" +shift + +TEST_DIR=$(dirname "$0") +TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" + +export PATH=/sbin:/bin +nsenter \ + --preserve-credentials \ + --user \ + --mount \ + --uts \ + --ipc \ + --net \ + --pid \ + -w -t "$target" \ + chroot "$TESTS_ROOT" "$@" diff --git a/test/testwrapper.sh b/test/testwrapper.sh index ec4be152..19a9afee 100755 --- a/test/testwrapper.sh +++ b/test/testwrapper.sh @@ -1,9 +1,27 @@ #!/bin/sh -set -eux +set -eu -export PATH=/sbin:/bin +mount -n -t tmpfs none /dev +# mount -t devtmpfs none /dev + +# mknod -m 622 /dev/console c 5 1 +# mknod -m 666 /dev/null c 1 3 +# mknod -m 666 /dev/zero c 1 5 +# mknod -m 666 /dev/ptmx c 5 2 +# mknod -m 666 /dev/tty c 5 0 +# mknod -m 444 /dev/random c 1 8 +# mknod -m 444 /dev/urandom c 1 9 +# chown root:tty /dev/{console,ptmx,tty} mount -t proc none /proc +mount -t sysfs none /sys +mount -t tmpfs none /tmp + +# mkdir -p /dev/pts +# mount -t devpts none /dev/pts + +tty=/dev/$(cat /sys/class/tty/console/active) +mkfifo "$tty" exec "$@" From 4224c2e8fec878eca7e44aa5ac2e1270298174aa Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 18 Feb 2021 13:55:47 +0100 Subject: [PATCH 03/90] test: New test: test_start_stop_service Add a new test that verifies that Finit will: - Start a service that's been added to the config - Stop the service once it's removed from the config The second step is currently failing. Signed-off-by: Jacques de Laval --- test/Makefile | 12 ++++-- test/{testwrapper.sh => chrootsetup.sh} | 1 + test/runtest.sh | 52 +++++++++++++++++++++++++ test/test_start_stop_service/service.sh | 7 ++++ test/test_start_stop_service/test.sh | 28 +++++++++++++ test/{testenter.sh => testenv_exec.sh} | 0 test/{run.sh => testenv_start.sh} | 4 +- 7 files changed, 98 insertions(+), 6 deletions(-) rename test/{testwrapper.sh => chrootsetup.sh} (95%) create mode 100755 test/runtest.sh create mode 100755 test/test_start_stop_service/service.sh create mode 100755 test/test_start_stop_service/test.sh rename test/{testenter.sh => testenv_exec.sh} (100%) rename test/{run.sh => testenv_start.sh} (64%) 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 "$@" From 39edf5279228657340c9eb23af0479c5f6bc4bbb Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 18 Feb 2021 14:39:55 +0100 Subject: [PATCH 04/90] test: Add shellcheck Signed-off-by: Jacques de Laval --- test/Makefile | 6 +++++- test/runtest.sh | 10 +++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/test/Makefile b/test/Makefile index 8000ccbf..65a3646d 100644 --- a/test/Makefile +++ b/test/Makefile @@ -66,7 +66,11 @@ $(testroot)/usr/local/lib/libuev.so.2: .PHONY: test test: $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit - ./runtest.sh ./test_start_stop_service/test.sh + ./runtest.sh test_start_stop_service/test.sh + +.PHONY: lint +lint: + shellcheck $(shell find . -name '*.sh') .PHONY: clean clean: diff --git a/test/runtest.sh b/test/runtest.sh index 75ad2c16..5c95a696 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -3,14 +3,14 @@ set -eu texec() { - ./testenv_exec.sh $(pgrep -P $finit_ppid) "$@" + ./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" + printf "< $TEST_DIR > \e[1;%dm%s\e[0m %s\n" "$1" "$2" "$3" } assert() { @@ -40,13 +40,13 @@ trap teardown EXIT finit_ppid=$! for i in $(seq 1 50); do sleep 0.1 - finit_pid=$(pgrep -P "$finit_ppid") || continue + pgrep -P "$finit_ppid" > /dev/null || continue break done texec cat /dev/tty0 & sleep 1 -TEST_DIR=$(dirname $1) +TEST_DIR=$(dirname "$1") -source "$1" +. "$1" From cdd504622d120a8432f3289bedcb98306c32b54f Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 18 Feb 2021 15:05:45 +0100 Subject: [PATCH 05/90] test: Print test status after each test Signed-off-by: Jacques de Laval --- test/runtest.sh | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/test/runtest.sh b/test/runtest.sh index 5c95a696..b43f7ff0 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -6,11 +6,12 @@ texec() { ./testenv_exec.sh "$(pgrep -P "$finit_ppid")" "$@" } -fg_red='31' -fg_green='32' -fg_yellow='33' +color_reset='\e[0m' +fg_red='\e[1;31m' +fg_green='\e[1;32m' +fg_yellow='\e[1;33m' log() { - printf "< $TEST_DIR > \e[1;%dm%s\e[0m %s\n" "$1" "$2" "$3" + printf "< $TEST_DIR > %b%b%b %s\n" "$1" "$2" "$color_reset" "$3" } assert() { @@ -28,13 +29,25 @@ say() { } teardown() { + test_status="$?" + log "$color_reset" '--' '' + if [ "$test_status" -eq 0 ]; then + log "$fg_green" 'TEST PASS' '' + else + log "$fg_red" 'TEST FAIL' '' + fi texec kill 1 wait } +TEST_DIR=$(dirname "$1") + trap teardown EXIT +log "$color_reset" 'Test start' '' +log "$color_reset" '--' '' + # Setup ./testenv_start.sh finit & finit_ppid=$! @@ -47,6 +60,4 @@ done texec cat /dev/tty0 & sleep 1 -TEST_DIR=$(dirname "$1") - . "$1" From 5775a20eff3cc92db4fde7d8880001901329c937 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 18 Feb 2021 20:54:35 +0100 Subject: [PATCH 06/90] test: Copy dynamic libraries into chroot Signed-off-by: Jacques de Laval --- test/Makefile | 67 ++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/test/Makefile b/test/Makefile index 65a3646d..d343fbc2 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,7 +1,9 @@ ARCH ?= x86_64 -busybox-ver = 1.31.0 -busybox-bin = busybox-$(ARCH) +finit_bin = ../src/finit + +busybox_ver = 1.31.0 +busybox_bin = busybox-$(ARCH) testroot = test_root testroot_bin = $(testroot)/bin @@ -22,52 +24,57 @@ binaries = $(testroot_bin)/cat \ $(testroot_bin)/sleep \ $(testroot_bin)/top \ $(testroot_bin)/touch -dirs = $(testroot)/dev \ - $(testroot)/etc \ - $(testroot)/lib \ - $(testroot)/proc \ - $(testroot)/sbin \ - $(testroot)/sys \ - $(testroot)/tmp \ - $(testroot)/usr/local/lib +dirs = $(testroot)/bin \ + $(testroot)/dev \ + $(testroot)/etc \ + $(testroot)/lib \ + $(testroot)/lib64 \ + $(testroot)/proc \ + $(testroot)/sbin \ + $(testroot)/sys \ + $(testroot)/tmp \ + $(testroot)/usr/lib -$(testroot)/%: $(testroot) +$(dirs): mkdir -p $@ -$(busybox-bin): - wget 'https://www.busybox.net/downloads/binaries/$(busybox-ver)-defconfig-multiarch-musl/$(busybox-bin)' - md5sum -c $(busybox-bin).md5 +$(busybox_bin): + wget 'https://www.busybox.net/downloads/binaries/$(busybox_ver)-defconfig-multiarch-musl/$(busybox_bin)' + md5sum -c $(busybox_bin).md5 $(testroot): mkdir $(testroot) -$(testroot_bin)/$(busybox-bin): $(testroot_bin) $(busybox-bin) - cp $(busybox-bin) $(testroot_bin)/ - chmod +x $(testroot_bin)/$(busybox-bin) +$(testroot_bin)/$(busybox_bin): $(busybox_bin) + mkdir -p $(testroot_bin) + cp $(busybox_bin) $(testroot_bin)/ + chmod +x $(testroot_bin)/$(busybox_bin) -$(testroot_bin)/%: $(testroot_bin)/$(busybox-bin) +$(binaries): $(testroot_bin)/$(busybox_bin) cd $(testroot_bin); \ - ln -s $(busybox-bin) $(notdir $@) + ln -s $(busybox_bin) $(notdir $@) $(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 +libs_src = $(shell ldd $(finit_bin) | grep -v lib64 | grep '=>' | cut -d' ' -f3) +libs = $(foreach path,$(libs_src),$(testroot)/usr/lib/$(shell basename $(path))) +libs64_src = $(shell ldd $(finit_bin) | grep lib64 | grep '=>' | cut -d' ' -f3) +libs64 = $(foreach path,$(libs64_src),$(testroot)/lib64/$(shell basename $(path))) + +$(testroot)/sbin/finit: $(finit_bin) $(libs) $(libs64) cp $< $@ -$(testroot)/lib/ld-musl-x86_64.so.1: - cp test_root.backup/lib/ld-musl-x86_64.so.1 $@ - -$(testroot)/usr/local/lib/libite.so.5: - cp test_root.backup/usr/local/lib/libite.so.5 $@ - -$(testroot)/usr/local/lib/libuev.so.2: - cp test_root.backup/usr/local/lib/libuev.so.2 $@ - .PHONY: test test: $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit ./runtest.sh test_start_stop_service/test.sh +$(libs): $@ + cp $(shell ldd $(finit_bin) | cut -d' ' -f3 | grep $(notdir $@)) $@ + +$(libs64): $@ + cp $(shell ldd $(finit_bin) | cut -d' ' -f3 | grep $(notdir $@)) $@ + .PHONY: lint lint: shellcheck $(shell find . -name '*.sh') @@ -75,4 +82,4 @@ lint: .PHONY: clean clean: rm -rf $(testroot) - rm -f $(busybox-bin) + rm -f $(busybox_bin) From 5a4cb74493842e4affab258a619fe82965c88fc5 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 18 Feb 2021 21:29:51 +0100 Subject: [PATCH 07/90] test: Fix shellcheck warnings Signed-off-by: Jacques de Laval --- test/Makefile | 4 ++-- test/runtest.sh | 5 +++-- test/test_start_stop_service/service.sh | 2 +- test/test_start_stop_service/test.sh | 8 ++++---- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/test/Makefile b/test/Makefile index d343fbc2..668a7438 100644 --- a/test/Makefile +++ b/test/Makefile @@ -75,8 +75,8 @@ $(libs): $@ $(libs64): $@ cp $(shell ldd $(finit_bin) | cut -d' ' -f3 | grep $(notdir $@)) $@ -.PHONY: lint -lint: +.PHONY: test-check +test-check: shellcheck $(shell find . -name '*.sh') .PHONY: clean diff --git a/test/runtest.sh b/test/runtest.sh index b43f7ff0..75094567 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -51,7 +51,7 @@ log "$color_reset" '--' '' # Setup ./testenv_start.sh finit & finit_ppid=$! -for i in $(seq 1 50); do +for _ in $(seq 1 50); do sleep 0.1 pgrep -P "$finit_ppid" > /dev/null || continue break @@ -60,4 +60,5 @@ done texec cat /dev/tty0 & sleep 1 -. "$1" +# shellcheck source=/dev/null +. "$TEST" diff --git a/test/test_start_stop_service/service.sh b/test/test_start_stop_service/service.sh index e4ae0ede..6b267e7f 100755 --- a/test/test_start_stop_service/service.sh +++ b/test/test_start_stop_service/service.sh @@ -2,6 +2,6 @@ set -eu -while [ 1 ]; do +while true; do sleep 5 done diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index 2d6b5810..ffe6f465 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -11,8 +11,8 @@ say 'Reload Finit' texec kill -SIGHUP 1 sleep 1 -service_pids=( $(texec pgrep -P 1) ) -assert 'One service is running' ${#service_pids[@]} -eq 1 +service_pids=$(texec pgrep -P 1 | wc -l) +assert 'One service is running' "$service_pids" -eq 1 say 'Remove the service' texec sh -c 'echo > /etc/finit.conf' @@ -21,8 +21,8 @@ say 'Reload Finit' texec kill -SIGHUP 1 sleep 1 -service_pids=( $(texec pgrep -P 1) ) -assert 'Zero services are running' ${#service_pids[@]} -eq 0 +service_pids=$(texec pgrep -P 1 | wc -l) +assert 'Zero services are running' "$service_pids" -eq 0 # Teardown rm -f test_root/bin/service.sh From c80a755dc62be5329f630839fc5d7ee51d5524a2 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 18 Feb 2021 22:49:01 +0100 Subject: [PATCH 08/90] test: Clean up Makefile Signed-off-by: Jacques de Laval --- test/Makefile | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/test/Makefile b/test/Makefile index 668a7438..77bef491 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,9 +1,12 @@ +.PHONY: clean check test-check + ARCH ?= x86_64 -finit_bin = ../src/finit +FINITBIN ?= ../src/finit -busybox_ver = 1.31.0 -busybox_bin = busybox-$(ARCH) +BBVER ?= 1.31.0 +BBBIN = busybox-$(ARCH) +BBURL ?= https://www.busybox.net/downloads/binaries/$(BBVER)-defconfig-multiarch-musl/$(BBBIN) testroot = test_root testroot_bin = $(testroot)/bin @@ -35,51 +38,50 @@ dirs = $(testroot)/bin \ $(testroot)/tmp \ $(testroot)/usr/lib +all: check + $(dirs): mkdir -p $@ -$(busybox_bin): - wget 'https://www.busybox.net/downloads/binaries/$(busybox_ver)-defconfig-multiarch-musl/$(busybox_bin)' - md5sum -c $(busybox_bin).md5 +$(BBBIN): + wget -O $@ $(BBURL) + md5sum -c $@.md5 $(testroot): mkdir $(testroot) -$(testroot_bin)/$(busybox_bin): $(busybox_bin) +$(testroot_bin)/$(BBBIN): $(BBBIN) mkdir -p $(testroot_bin) - cp $(busybox_bin) $(testroot_bin)/ - chmod +x $(testroot_bin)/$(busybox_bin) + cp $(BBBIN) $(testroot_bin)/ + chmod +x $(testroot_bin)/$(BBBIN) -$(binaries): $(testroot_bin)/$(busybox_bin) +$(binaries): $(testroot_bin)/$(BBBIN) cd $(testroot_bin); \ - ln -s $(busybox_bin) $(notdir $@) + ln -s $(BBBIN) $(notdir $@) $(testroot_bin)/chrootsetup.sh: chrootsetup.sh cp $< $@ -libs_src = $(shell ldd $(finit_bin) | grep -v lib64 | grep '=>' | cut -d' ' -f3) +libs_src = $(shell ldd $(FINITBIN) | grep -v lib64 | grep '=>' | cut -d' ' -f3) libs = $(foreach path,$(libs_src),$(testroot)/usr/lib/$(shell basename $(path))) -libs64_src = $(shell ldd $(finit_bin) | grep lib64 | grep '=>' | cut -d' ' -f3) +libs64_src = $(shell ldd $(FINITBIN) | grep lib64 | grep '=>' | cut -d' ' -f3) libs64 = $(foreach path,$(libs64_src),$(testroot)/lib64/$(shell basename $(path))) -$(testroot)/sbin/finit: $(finit_bin) $(libs) $(libs64) +$(testroot)/sbin/finit: $(FINITBIN) $(libs) $(libs64) cp $< $@ -.PHONY: test -test: $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit - ./runtest.sh test_start_stop_service/test.sh - $(libs): $@ - cp $(shell ldd $(finit_bin) | cut -d' ' -f3 | grep $(notdir $@)) $@ + cp $(shell ldd $(FINITBIN) | cut -d' ' -f3 | grep $(notdir $@)) $@ $(libs64): $@ - cp $(shell ldd $(finit_bin) | cut -d' ' -f3 | grep $(notdir $@)) $@ + cp $(shell ldd $(FINITBIN) | cut -d' ' -f3 | grep $(notdir $@)) $@ + +check: $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit + ./runtest.sh test_start_stop_service/test.sh -.PHONY: test-check test-check: shellcheck $(shell find . -name '*.sh') -.PHONY: clean clean: rm -rf $(testroot) - rm -f $(busybox_bin) + rm -f $(BBBIN) From e72fd5a27162287cf63580434680d5ebfda687b2 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 20 Feb 2021 11:05:41 +0100 Subject: [PATCH 09/90] test: Fix and clean up dynamic library setup Signed-off-by: Jacques de Laval --- test/Makefile | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/test/Makefile b/test/Makefile index 77bef491..95addbbc 100644 --- a/test/Makefile +++ b/test/Makefile @@ -11,6 +11,7 @@ BBURL ?= https://www.busybox.net/downloads/binaries/$(BBVER)-defconfig-multiarch testroot = test_root testroot_bin = $(testroot)/bin binaries = $(testroot_bin)/cat \ + $(testroot_bin)/cp \ $(testroot_bin)/date \ $(testroot_bin)/echo \ $(testroot_bin)/kill \ @@ -30,13 +31,13 @@ binaries = $(testroot_bin)/cat \ dirs = $(testroot)/bin \ $(testroot)/dev \ $(testroot)/etc \ - $(testroot)/lib \ - $(testroot)/lib64 \ $(testroot)/proc \ $(testroot)/sbin \ $(testroot)/sys \ - $(testroot)/tmp \ - $(testroot)/usr/lib + $(testroot)/tmp + +_libs_src = $(shell ldd $(FINITBIN) | grep -Eo '/[^ ]+') +libs = $(foreach path,$(_libs_src),$(testroot)$(path)) all: check @@ -62,19 +63,12 @@ $(binaries): $(testroot_bin)/$(BBBIN) $(testroot_bin)/chrootsetup.sh: chrootsetup.sh cp $< $@ -libs_src = $(shell ldd $(FINITBIN) | grep -v lib64 | grep '=>' | cut -d' ' -f3) -libs = $(foreach path,$(libs_src),$(testroot)/usr/lib/$(shell basename $(path))) -libs64_src = $(shell ldd $(FINITBIN) | grep lib64 | grep '=>' | cut -d' ' -f3) -libs64 = $(foreach path,$(libs64_src),$(testroot)/lib64/$(shell basename $(path))) - -$(testroot)/sbin/finit: $(FINITBIN) $(libs) $(libs64) +$(testroot)/sbin/finit: $(FINITBIN) $(libs) cp $< $@ -$(libs): $@ - cp $(shell ldd $(FINITBIN) | cut -d' ' -f3 | grep $(notdir $@)) $@ - -$(libs64): $@ - cp $(shell ldd $(FINITBIN) | cut -d' ' -f3 | grep $(notdir $@)) $@ +$(libs): + mkdir -p $(dir $@) + cp $(shell echo $@ | sed s/^$(testroot)//) $@ check: $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit ./runtest.sh test_start_stop_service/test.sh From 061702b6e4a03f9f3c94bcdd671f2c7326f8037a Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 20 Feb 2021 11:09:22 +0100 Subject: [PATCH 10/90] .travis.yml: Run check after build Signed-off-by: Jacques de Laval --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 74c98092..03ffbf66 100644 --- a/.travis.yml +++ b/.travis.yml @@ -59,3 +59,4 @@ script: - size /tmp/sbin/initctl - size /tmp/sbin/reboot - LD_LIBRARY_PATH=/tmp/lib /tmp/sbin/initctl -h + - LD_LIBRARY_PATH=/tmp/lib make -C test/ check From ba621502aebc0a1e361c7b4eef594e3c7a3d1cc5 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 20 Feb 2021 11:53:20 +0100 Subject: [PATCH 11/90] .travis.yml: Print unshare/nsenter/chroot version for debugging Signed-off-by: Jacques de Laval --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 03ffbf66..649edf59 100644 --- a/.travis.yml +++ b/.travis.yml @@ -59,4 +59,7 @@ script: - size /tmp/sbin/initctl - size /tmp/sbin/reboot - LD_LIBRARY_PATH=/tmp/lib /tmp/sbin/initctl -h + - unshare -V + - nsenter -V + - chroot --version - LD_LIBRARY_PATH=/tmp/lib make -C test/ check From 99ad26236e880f4d11cfe45e8234d05fec09363e Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 20 Feb 2021 12:39:29 +0100 Subject: [PATCH 12/90] test: Fix unshare/nsenter not in PATH Signed-off-by: Jacques de Laval --- test/testenv_exec.sh | 4 +++- test/testenv_start.sh | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/test/testenv_exec.sh b/test/testenv_exec.sh index 8c4b11bb..f2929d48 100755 --- a/test/testenv_exec.sh +++ b/test/testenv_exec.sh @@ -13,8 +13,10 @@ shift TEST_DIR=$(dirname "$0") TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" +nsenter=$(which nsenter) + export PATH=/sbin:/bin -nsenter \ +"$nsenter" \ --preserve-credentials \ --user \ --mount \ diff --git a/test/testenv_start.sh b/test/testenv_start.sh index a0bb8646..3281d5b7 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -7,10 +7,12 @@ TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" >&2 echo "Hint: Execute '${TEST_DIR}/testenv_exec.sh \$(pgrep -P $$) /bin/sh; reset' to enter the test namespace" +unshare=$(which unshare) + # Not supported by Busybox unshare: # --cgroup --time export PATH=/sbin:/bin -exec unshare \ +exec "$unshare" \ --user --map-root-user \ --fork --pid --mount-proc \ --mount \ From 7965dbd13f1d1d16af0f24dc847770e56dbfee96 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 20 Feb 2021 13:06:28 +0100 Subject: [PATCH 13/90] test: Fix chroot not in PATH Signed-off-by: Jacques de Laval --- test/runtest.sh | 4 +++- test/testenv_exec.sh | 3 ++- test/testenv_start.sh | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/test/runtest.sh b/test/runtest.sh index 75094567..46aca966 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -41,7 +41,9 @@ teardown() { wait } -TEST_DIR=$(dirname "$1") +TEST="$1" +TEST_DIR=$(dirname "$TEST") +shift trap teardown EXIT diff --git a/test/testenv_exec.sh b/test/testenv_exec.sh index f2929d48..675748ea 100755 --- a/test/testenv_exec.sh +++ b/test/testenv_exec.sh @@ -14,6 +14,7 @@ TEST_DIR=$(dirname "$0") TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" nsenter=$(which nsenter) +chroot=$(which chroot) export PATH=/sbin:/bin "$nsenter" \ @@ -25,4 +26,4 @@ export PATH=/sbin:/bin --net \ --pid \ -w -t "$target" \ - chroot "$TESTS_ROOT" "$@" + "$chroot" "$TESTS_ROOT" "$@" diff --git a/test/testenv_start.sh b/test/testenv_start.sh index 3281d5b7..07a69d73 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -8,6 +8,7 @@ TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" >&2 echo "Hint: Execute '${TEST_DIR}/testenv_exec.sh \$(pgrep -P $$) /bin/sh; reset' to enter the test namespace" unshare=$(which unshare) +chroot=$(which chroot) # Not supported by Busybox unshare: # --cgroup --time @@ -18,4 +19,4 @@ exec "$unshare" \ --mount \ --mount-proc \ --uts --ipc --net \ - chroot "$TESTS_ROOT" /bin/chrootsetup.sh "$@" + "$chroot" "$TESTS_ROOT" /bin/chrootsetup.sh "$@" From dd6af87d6eca37774cc5efac17f27f844482bc02 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 20 Feb 2021 13:49:12 +0100 Subject: [PATCH 14/90] test: Print Environment and FS for debugging --- test/Makefile | 2 ++ test/runtest.sh | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/test/Makefile b/test/Makefile index 95addbbc..f1a03975 100644 --- a/test/Makefile +++ b/test/Makefile @@ -14,6 +14,8 @@ binaries = $(testroot_bin)/cat \ $(testroot_bin)/cp \ $(testroot_bin)/date \ $(testroot_bin)/echo \ + $(testroot_bin)/env \ + $(testroot_bin)/find \ $(testroot_bin)/kill \ $(testroot_bin)/ls \ $(testroot_bin)/mkdir \ diff --git a/test/runtest.sh b/test/runtest.sh index 46aca966..79a1461e 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -45,11 +45,19 @@ TEST="$1" TEST_DIR=$(dirname "$TEST") shift -trap teardown EXIT +# trap teardown EXIT log "$color_reset" 'Test start' '' log "$color_reset" '--' '' + +>&2 echo Environment: +>&2 ./testenv_start.sh env +>&2 echo -- +>&2 echo Filesystem: +>&2 ./testenv_start.sh find / +>&2 echo -- +exit # Setup ./testenv_start.sh finit & finit_ppid=$! From 01619586035902a1de2eb0d5347b056084f07c53 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 20 Feb 2021 14:08:37 +0100 Subject: [PATCH 15/90] test: Avoid shadwoing /tmp/lib in Travis CI Signed-off-by: Jacques de Laval --- test/chrootsetup.sh | 2 +- test/runtest.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/chrootsetup.sh b/test/chrootsetup.sh index ed8f5dd7..97d42161 100755 --- a/test/chrootsetup.sh +++ b/test/chrootsetup.sh @@ -16,7 +16,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 /tmp mount -t tmpfs none /etc # mkdir -p /dev/pts diff --git a/test/runtest.sh b/test/runtest.sh index 79a1461e..e22fd163 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -55,9 +55,9 @@ log "$color_reset" '--' '' >&2 ./testenv_start.sh env >&2 echo -- >&2 echo Filesystem: ->&2 ./testenv_start.sh find / +>&2 ./testenv_start.sh find / -path /sys -prune -o -print -path /proc -prune -o -print >&2 echo -- -exit + # Setup ./testenv_start.sh finit & finit_ppid=$! From ee71378dfdc6e33c0715c7978fb041468e625521 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 20 Feb 2021 14:20:09 +0100 Subject: [PATCH 16/90] test: Fix hardcoded Finit TTY pipe Signed-off-by: Jacques de Laval --- test/runtest.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/runtest.sh b/test/runtest.sh index e22fd163..c56b6fe3 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -67,7 +67,8 @@ for _ in $(seq 1 50); do break done -texec cat /dev/tty0 & +tty=/dev/$(texec cat /sys/class/tty/console/active) +texec cat "$tty" & sleep 1 # shellcheck source=/dev/null From 835f6a45d3e246c0152cdf65057ff4ad71bec4f4 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 20 Feb 2021 14:30:12 +0100 Subject: [PATCH 17/90] test: Re-enable teardown Signed-off-by: Jacques de Laval --- test/runtest.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtest.sh b/test/runtest.sh index c56b6fe3..9992717b 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -45,7 +45,7 @@ TEST="$1" TEST_DIR=$(dirname "$TEST") shift -# trap teardown EXIT +trap teardown EXIT log "$color_reset" 'Test start' '' log "$color_reset" '--' '' From a8f562a7f5e794f214f48d22fc6a7bb1688d515b Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sun, 21 Feb 2021 12:51:49 +0100 Subject: [PATCH 18/90] test: Minor clean up Signed-off-by: Jacques de Laval --- test/runtest.sh | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test/runtest.sh b/test/runtest.sh index 9992717b..00c3d3de 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -3,7 +3,7 @@ set -eu texec() { - ./testenv_exec.sh "$(pgrep -P "$finit_ppid")" "$@" + ./testenv_exec.sh "$finit_pid" "$@" } color_reset='\e[0m' @@ -15,17 +15,17 @@ log() { } assert() { - msg=$1 + __assert_msg=$1 shift if [ ! "$@" ]; then - log "$fg_red" ✘ "$msg" + log "$fg_red" ✘ "$__assert_msg ($*)" exit 1 fi - log "$fg_green" ✔ "$msg" + log "$fg_green" ✔ "$__assert_msg" } say() { - log "$fg_yellow" ● "$@" + log "$fg_yellow" • "$@" } teardown() { @@ -63,8 +63,10 @@ log "$color_reset" '--' '' finit_ppid=$! for _ in $(seq 1 50); do sleep 0.1 - pgrep -P "$finit_ppid" > /dev/null || continue - break + finit_pid=$(pgrep -P "$finit_ppid") + if [ "$finit_pid" ]; then + break + fi done tty=/dev/$(texec cat /sys/class/tty/console/active) From 407c524088d58a993b293ebeea38d54fdf725aed Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 14:34:22 +0100 Subject: [PATCH 19/90] test: Use FINIT_CONF from autoconf instead of hardcoded /etc/finit.conf Signed-off-by: Jacques de Laval --- test/runtest.sh | 3 +++ test/test_start_stop_service/test.sh | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/test/runtest.sh b/test/runtest.sh index 00c3d3de..bb20d02f 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -47,6 +47,8 @@ shift trap teardown EXIT +FINIT_CONF=$(grep FINIT_CONF "$(dirname "$0")/../config.h" | cut -d' ' -f3 | cut -d'"' -f2) + log "$color_reset" 'Test start' '' log "$color_reset" '--' '' @@ -57,6 +59,7 @@ log "$color_reset" '--' '' >&2 echo Filesystem: >&2 ./testenv_start.sh find / -path /sys -prune -o -print -path /proc -prune -o -print >&2 echo -- +>&2 echo "FINIT_CONF='$FINIT_CONF" # Setup ./testenv_start.sh finit & diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index ffe6f465..93e97223 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -5,7 +5,7 @@ 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' +texec sh -c "echo 'service [2345] kill:20 log /bin/service.sh' > $FINIT_CONF" say 'Reload Finit' texec kill -SIGHUP 1 @@ -15,7 +15,7 @@ service_pids=$(texec pgrep -P 1 | wc -l) assert 'One service is running' "$service_pids" -eq 1 say 'Remove the service' -texec sh -c 'echo > /etc/finit.conf' +texec sh -c "echo > $FINIT_CONF" say 'Reload Finit' texec kill -SIGHUP 1 From b73998d7c6b24b4f3cee58e2ac84b1a32fee600e Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 14:35:12 +0100 Subject: [PATCH 20/90] test: Reset PS env vars in test environment Signed-off-by: Jacques de Laval --- test/testenv_exec.sh | 5 +++++ test/testenv_start.sh | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/test/testenv_exec.sh b/test/testenv_exec.sh index 675748ea..374424d7 100755 --- a/test/testenv_exec.sh +++ b/test/testenv_exec.sh @@ -17,6 +17,11 @@ nsenter=$(which nsenter) chroot=$(which chroot) export PATH=/sbin:/bin +export PS1='\w \$ ' +export PS2='> ' +export PS3='#? ' +export PS4='+ ' + "$nsenter" \ --preserve-credentials \ --user \ diff --git a/test/testenv_start.sh b/test/testenv_start.sh index 07a69d73..afdbb34b 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -10,9 +10,14 @@ TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" unshare=$(which unshare) chroot=$(which chroot) +export PATH=/sbin:/bin +export PS1='\w \$ ' +export PS2='> ' +export PS3='#? ' +export PS4='+ ' + # Not supported by Busybox unshare: # --cgroup --time -export PATH=/sbin:/bin exec "$unshare" \ --user --map-root-user \ --fork --pid --mount-proc \ From 8f55bf140e86164a309c5ecdb750a0a49ba743ce Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 14:52:01 +0100 Subject: [PATCH 21/90] test: Add xtrace flag for debugging Signed-off-by: Jacques de Laval --- test/runtest.sh | 2 +- test/testenv_start.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/runtest.sh b/test/runtest.sh index bb20d02f..8715741d 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -1,6 +1,6 @@ #!/bin/sh -set -eu +set -eux texec() { ./testenv_exec.sh "$finit_pid" "$@" diff --git a/test/testenv_start.sh b/test/testenv_start.sh index afdbb34b..38673e0d 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -1,6 +1,6 @@ #!/bin/sh -set -eu +set -eux TEST_DIR=$(dirname "$0") TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" From 9d3d211b6bd1bb4f6c872f9f938168303508568b Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 15:22:32 +0100 Subject: [PATCH 22/90] test: Fix waiting for Finit PID Signed-off-by: Jacques de Laval --- test/runtest.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/runtest.sh b/test/runtest.sh index 8715741d..47d60fc0 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -66,10 +66,8 @@ log "$color_reset" '--' '' finit_ppid=$! for _ in $(seq 1 50); do sleep 0.1 - finit_pid=$(pgrep -P "$finit_ppid") - if [ "$finit_pid" ]; then - break - fi + finit_pid=$(pgrep -P "$finit_ppid") || continue + break done tty=/dev/$(texec cat /sys/class/tty/console/active) From a28bb6f9516acbb16a82fe8cd053f5fa86a59cbe Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 15:23:06 +0100 Subject: [PATCH 23/90] test: Create FINIT_CONF dir before writing to the file Signed-off-by: Jacques de Laval --- test/test_start_stop_service/test.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index 93e97223..a4d8b049 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -5,6 +5,7 @@ set -eu # Test say 'Set up a service' cp "$TEST_DIR"/service.sh test_root/bin +texec mkdir -p "$(dirname "$FINIT_CONF")" texec sh -c "echo 'service [2345] kill:20 log /bin/service.sh' > $FINIT_CONF" say 'Reload Finit' From 644273fc5ccdcec1c0890bfe4bf8d8ca35d72cb2 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Mon, 22 Feb 2021 15:47:59 +0100 Subject: [PATCH 24/90] test: Add 2 convenience shell functions for test development Signed-off-by: Magnus Malm --- test/runtest.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/runtest.sh b/test/runtest.sh index 47d60fc0..9813951d 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -6,6 +6,17 @@ texec() { ./testenv_exec.sh "$finit_pid" "$@" } +pause() { + echo "Press any key to continue... " + read -r REPLY +} + +toggle_finit_debug() { + say 'Toggle finit debug' + texec sh -c "/bin/initctl debug" + sleep 0.5 +} + color_reset='\e[0m' fg_red='\e[1;31m' fg_green='\e[1;32m' From 0ef17f55ffd849e2ff79a4ee1da65680cde9af3d Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 16:32:49 +0100 Subject: [PATCH 25/90] test: Retry asserting number of Finit children for a while Signed-off-by: Jacques de Laval --- test/runtest.sh | 38 +++++++++++++++++++++++----- test/test_start_stop_service/test.sh | 13 +++++----- test/testenv_start.sh | 2 +- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/test/runtest.sh b/test/runtest.sh index 9813951d..6b57f3e4 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -1,8 +1,9 @@ #!/bin/sh -set -eux +set -eu texec() { + # shellcheck disable=SC2154 ./testenv_exec.sh "$finit_pid" "$@" } @@ -30,11 +31,37 @@ assert() { shift if [ ! "$@" ]; then log "$fg_red" ✘ "$__assert_msg ($*)" - exit 1 + return 1 fi log "$fg_green" ✔ "$__assert_msg" } +retry() { + __retry_cmd=$1 + shift + case "$#" in + 2) + __retry_n=$1 + __retry_sleep=$2 + ;; + 1) + __retry_n=$1 + __retry_sleep=0.1 + ;; + *) + __retry_n=50 + __retry_sleep=0.1 + ;; + esac + + for _ in $(seq 1 "$__retry_n"); do + sleep "$__retry_sleep" + eval "$__retry_cmd" || continue + return 0 + done + return 1 +} + say() { log "$fg_yellow" • "$@" } @@ -75,11 +102,8 @@ log "$color_reset" '--' '' # Setup ./testenv_start.sh finit & finit_ppid=$! -for _ in $(seq 1 50); do - sleep 0.1 - finit_pid=$(pgrep -P "$finit_ppid") || continue - break -done +# shellcheck disable=SC2016 +retry 'finit_pid=$(pgrep -P '"$finit_ppid"')' tty=/dev/$(texec cat /sys/class/tty/console/active) texec cat "$tty" & diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index a4d8b049..e0547557 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -2,6 +2,11 @@ set -eu +assert_num_children() { + texec ps -opid,ppid,args + assert "$1 services are running" "$(texec pgrep -P 1 | wc -l)" -eq "$1" +} + # Test say 'Set up a service' cp "$TEST_DIR"/service.sh test_root/bin @@ -10,20 +15,16 @@ texec sh -c "echo 'service [2345] kill:20 log /bin/service.sh' > $FINIT_CONF" say 'Reload Finit' texec kill -SIGHUP 1 -sleep 1 -service_pids=$(texec pgrep -P 1 | wc -l) -assert 'One service is running' "$service_pids" -eq 1 +retry 'assert_num_children 1' say 'Remove the service' texec sh -c "echo > $FINIT_CONF" say 'Reload Finit' texec kill -SIGHUP 1 -sleep 1 -service_pids=$(texec pgrep -P 1 | wc -l) -assert 'Zero services are running' "$service_pids" -eq 0 +retry 'assert_num_children 0' # Teardown rm -f test_root/bin/service.sh diff --git a/test/testenv_start.sh b/test/testenv_start.sh index 38673e0d..afdbb34b 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -1,6 +1,6 @@ #!/bin/sh -set -eux +set -eu TEST_DIR=$(dirname "$0") TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" From 914955341d2df0134842abfdd4757fd058b553e5 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 16:39:06 +0100 Subject: [PATCH 26/90] test: Remove debug print Signed-off-by: Jacques de Laval --- test/test_start_stop_service/test.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index e0547557..7f33d179 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -3,7 +3,6 @@ set -eu assert_num_children() { - texec ps -opid,ppid,args assert "$1 services are running" "$(texec pgrep -P 1 | wc -l)" -eq "$1" } From adc9c40fe13344139c136e1d4f98c7257bd2ada8 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Mon, 22 Feb 2021 16:44:20 +0100 Subject: [PATCH 27/90] test: Include initctl in test run env Signed-off-by: Magnus Malm --- test/Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/Makefile b/test/Makefile index f1a03975..09d6bcd9 100644 --- a/test/Makefile +++ b/test/Makefile @@ -3,6 +3,7 @@ ARCH ?= x86_64 FINITBIN ?= ../src/finit +INITCTLBIN ?= ../src/initctl BBVER ?= 1.31.0 BBBIN = busybox-$(ARCH) @@ -68,11 +69,14 @@ $(testroot_bin)/chrootsetup.sh: chrootsetup.sh $(testroot)/sbin/finit: $(FINITBIN) $(libs) cp $< $@ +$(testroot)/bin/initctl: $(INITCTLBIN) $(libs) + cp $< $@ + $(libs): mkdir -p $(dir $@) cp $(shell echo $@ | sed s/^$(testroot)//) $@ -check: $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit +check: $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit $(testroot)/bin/initctl ./runtest.sh test_start_stop_service/test.sh test-check: From cd2fcac4cf6d243be7b2787bf66f8ef801e1bbe7 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Mon, 22 Feb 2021 16:46:25 +0100 Subject: [PATCH 28/90] test: Handle finit's default conf dirs/files better in test env Signed-off-by: Magnus Malm --- test/chrootsetup.sh | 2 ++ test/runtest.sh | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/test/chrootsetup.sh b/test/chrootsetup.sh index 97d42161..fe4e0e4c 100755 --- a/test/chrootsetup.sh +++ b/test/chrootsetup.sh @@ -22,6 +22,8 @@ mount -t tmpfs none /etc # mkdir -p /dev/pts # mount -t devpts none /dev/pts +mkdir -p "$FINIT_CONF_DIR" + tty=/dev/$(cat /sys/class/tty/console/active) mkfifo "$tty" diff --git a/test/runtest.sh b/test/runtest.sh index 6b57f3e4..203d27e7 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -85,7 +85,8 @@ shift trap teardown EXIT -FINIT_CONF=$(grep FINIT_CONF "$(dirname "$0")/../config.h" | cut -d' ' -f3 | cut -d'"' -f2) +export FINIT_CONF=$(grep FINIT_CONF "$(dirname "$0")/../config.h" | cut -d' ' -f3 | cut -d'"' -f2) +export FINIT_CONF_DIR="$(dirname "$FINIT_CONF")"/finit.d log "$color_reset" 'Test start' '' log "$color_reset" '--' '' @@ -97,7 +98,8 @@ log "$color_reset" '--' '' >&2 echo Filesystem: >&2 ./testenv_start.sh find / -path /sys -prune -o -print -path /proc -prune -o -print >&2 echo -- ->&2 echo "FINIT_CONF='$FINIT_CONF" +>&2 echo "FINIT_CONF='$FINIT_CONF'" +>&2 echo "FINIT_CONF_DIR='$FINIT_CONF_DIR/'" # Setup ./testenv_start.sh finit & From 6f8c24a8d3edc5068a40d0996d6ca63eb59da633 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 16:48:53 +0100 Subject: [PATCH 29/90] test: Only grep for specific child Signed-off-by: Jacques de Laval --- test/test_start_stop_service/test.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index 7f33d179..fc9a2852 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -3,7 +3,7 @@ set -eu assert_num_children() { - assert "$1 services are running" "$(texec pgrep -P 1 | wc -l)" -eq "$1" + assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" } # Test @@ -15,7 +15,7 @@ texec sh -c "echo 'service [2345] kill:20 log /bin/service.sh' > $FINIT_CONF" say 'Reload Finit' texec kill -SIGHUP 1 -retry 'assert_num_children 1' +retry 'assert_num_children 1 service.sh' say 'Remove the service' texec sh -c "echo > $FINIT_CONF" @@ -23,7 +23,7 @@ texec sh -c "echo > $FINIT_CONF" say 'Reload Finit' texec kill -SIGHUP 1 -retry 'assert_num_children 0' +retry 'assert_num_children 0 service.sh' # Teardown rm -f test_root/bin/service.sh From c2bb46f18c1dbae2ce5b67de194df2f456e86c6a Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 17:16:32 +0100 Subject: [PATCH 30/90] test: Run shellcheck on test scripts before running them Signed-off-by: Jacques de Laval --- test/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Makefile b/test/Makefile index 09d6bcd9..10108cc6 100644 --- a/test/Makefile +++ b/test/Makefile @@ -76,7 +76,7 @@ $(libs): mkdir -p $(dir $@) cp $(shell echo $@ | sed s/^$(testroot)//) $@ -check: $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit $(testroot)/bin/initctl +check: $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit $(testroot)/bin/initctl test-check ./runtest.sh test_start_stop_service/test.sh test-check: From ee31c8551f2f466e2fcbb34f7d0aa015ec49cb53 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 17:21:55 +0100 Subject: [PATCH 31/90] test: Remove unnecessary mkdir Signed-off-by: Jacques de Laval --- test/runtest.sh | 6 ++++-- test/test_start_stop_service/test.sh | 1 - 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/runtest.sh b/test/runtest.sh index 203d27e7..8be49826 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -85,8 +85,10 @@ shift trap teardown EXIT -export FINIT_CONF=$(grep FINIT_CONF "$(dirname "$0")/../config.h" | cut -d' ' -f3 | cut -d'"' -f2) -export FINIT_CONF_DIR="$(dirname "$FINIT_CONF")"/finit.d +FINIT_CONF=$(grep FINIT_CONF "$(dirname "$0")/../config.h" | cut -d' ' -f3 | cut -d'"' -f2) +export FINIT_CONF +FINIT_CONF_DIR="$(dirname "$FINIT_CONF")"/finit.d +export FINIT_CONF_DIR log "$color_reset" 'Test start' '' log "$color_reset" '--' '' diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index fc9a2852..0ba05b05 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -9,7 +9,6 @@ assert_num_children() { # Test say 'Set up a service' cp "$TEST_DIR"/service.sh test_root/bin -texec mkdir -p "$(dirname "$FINIT_CONF")" texec sh -c "echo 'service [2345] kill:20 log /bin/service.sh' > $FINIT_CONF" say 'Reload Finit' From e44d348f096c1d38da95c2b6dacabeb6e40157b0 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 20:22:09 +0100 Subject: [PATCH 32/90] test: Add test suite to Automake Signed-off-by: Jacques de Laval --- .gitignore | 3 +++ .travis.yml | 4 +++- Makefile.am | 3 +++ test/.gitignore | 3 ++- test/Makefile | 11 ++++++----- test/runtest.sh | 29 +++++----------------------- test/test_root/.gitignore | 10 ++++++++++ test/test_start_stop_service/test.sh | 6 +++++- test/testenv_exec.sh | 4 ++-- test/testenv_start.sh | 4 ++-- 10 files changed, 41 insertions(+), 36 deletions(-) create mode 100644 test/test_root/.gitignore diff --git a/.gitignore b/.gitignore index e57c87b4..f2d46e5f 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,9 @@ GTAGS /Makefile.in /stamp-h1 +/test-driver +/test-suite.log + # TroglOS build system /.config /.stamp diff --git a/.travis.yml b/.travis.yml index 649edf59..433c487c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -62,4 +62,6 @@ script: - unshare -V - nsenter -V - chroot --version - - LD_LIBRARY_PATH=/tmp/lib make -C test/ check + - LD_LIBRARY_PATH=/tmp/lib make check + - cat ./test-suite.log + - make -C test/ clean diff --git a/Makefile.am b/Makefile.am index 7d1d02e1..20ebb66e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -10,6 +10,9 @@ if DOC SUBDIRS += doc endif +AM_TESTS_ENVIRONMENT = make -C $(srcdir)/test test_root; +TESTS = test/test_start_stop_service/test.sh + # Compat hook install-dev: @make -C src install-pkgincludeHEADERS diff --git a/test/.gitignore b/test/.gitignore index af5859fd..a447bd04 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -1,2 +1,3 @@ busybox-x86_64 -test_root +*.log +*.trs diff --git a/test/Makefile b/test/Makefile index 10108cc6..0b047a97 100644 --- a/test/Makefile +++ b/test/Makefile @@ -51,8 +51,7 @@ $(BBBIN): wget -O $@ $(BBURL) md5sum -c $@.md5 -$(testroot): - mkdir $(testroot) +$(testroot): $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit $(testroot)/bin/initctl test-check $(testroot_bin)/$(BBBIN): $(BBBIN) mkdir -p $(testroot_bin) @@ -76,12 +75,14 @@ $(libs): mkdir -p $(dir $@) cp $(shell echo $@ | sed s/^$(testroot)//) $@ -check: $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit $(testroot)/bin/initctl test-check - ./runtest.sh test_start_stop_service/test.sh +check: $(testroot) + ./test_start_stop_service/test.sh test-check: shellcheck $(shell find . -name '*.sh') clean: - rm -rf $(testroot) + rm -rf $(testroot)/* rm -f $(BBBIN) + rm -f ./test_start_stop_service/*.log + rm -f ./test_start_stop_service/*.trs diff --git a/test/runtest.sh b/test/runtest.sh index 8be49826..452f2536 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -1,10 +1,8 @@ #!/bin/sh -set -eu - texec() { # shellcheck disable=SC2154 - ./testenv_exec.sh "$finit_pid" "$@" + "$TEST_DIR/testenv_exec.sh" "$finit_pid" "$@" } pause() { @@ -23,7 +21,7 @@ fg_red='\e[1;31m' fg_green='\e[1;32m' fg_yellow='\e[1;33m' log() { - printf "< $TEST_DIR > %b%b%b %s\n" "$1" "$2" "$color_reset" "$3" + printf "< TEST > %b%b%b %s\n" "$1" "$2" "$color_reset" "$3" } assert() { @@ -59,7 +57,7 @@ retry() { eval "$__retry_cmd" || continue return 0 done - return 1 + eval "$__retry_cmd" } say() { @@ -79,13 +77,9 @@ teardown() { wait } -TEST="$1" -TEST_DIR=$(dirname "$TEST") -shift - trap teardown EXIT -FINIT_CONF=$(grep FINIT_CONF "$(dirname "$0")/../config.h" | cut -d' ' -f3 | cut -d'"' -f2) +FINIT_CONF=$(grep FINIT_CONF "$TEST_DIR/../config.h" | cut -d' ' -f3 | cut -d'"' -f2) export FINIT_CONF FINIT_CONF_DIR="$(dirname "$FINIT_CONF")"/finit.d export FINIT_CONF_DIR @@ -93,18 +87,8 @@ export FINIT_CONF_DIR log "$color_reset" 'Test start' '' log "$color_reset" '--' '' - ->&2 echo Environment: ->&2 ./testenv_start.sh env ->&2 echo -- ->&2 echo Filesystem: ->&2 ./testenv_start.sh find / -path /sys -prune -o -print -path /proc -prune -o -print ->&2 echo -- ->&2 echo "FINIT_CONF='$FINIT_CONF'" ->&2 echo "FINIT_CONF_DIR='$FINIT_CONF_DIR/'" - # Setup -./testenv_start.sh finit & +"$TEST_DIR/testenv_start.sh" finit & finit_ppid=$! # shellcheck disable=SC2016 retry 'finit_pid=$(pgrep -P '"$finit_ppid"')' @@ -112,6 +96,3 @@ retry 'finit_pid=$(pgrep -P '"$finit_ppid"')' tty=/dev/$(texec cat /sys/class/tty/console/active) texec cat "$tty" & sleep 1 - -# shellcheck source=/dev/null -. "$TEST" diff --git a/test/test_root/.gitignore b/test/test_root/.gitignore new file mode 100644 index 00000000..08ad8a05 --- /dev/null +++ b/test/test_root/.gitignore @@ -0,0 +1,10 @@ +/bin +/dev +/etc +/lib64 +/proc +/sbin +/sys +/tmp +/usr +/var diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index 0ba05b05..a0b1d9c1 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -2,13 +2,17 @@ set -eu +TEST_DIR=$(dirname "$0")/.. +# shellcheck source=/dev/null +. "$TEST_DIR/runtest.sh" + assert_num_children() { assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" } # Test say 'Set up a service' -cp "$TEST_DIR"/service.sh test_root/bin +cp "$TEST_DIR"/test_start_stop_service/service.sh "$TEST_DIR"/test_root/bin texec sh -c "echo 'service [2345] kill:20 log /bin/service.sh' > $FINIT_CONF" say 'Reload Finit' diff --git a/test/testenv_exec.sh b/test/testenv_exec.sh index 374424d7..5243c142 100755 --- a/test/testenv_exec.sh +++ b/test/testenv_exec.sh @@ -13,8 +13,8 @@ shift TEST_DIR=$(dirname "$0") TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" -nsenter=$(which nsenter) -chroot=$(which chroot) +nsenter=$(command -v nsenter) +chroot=$(command -v chroot) export PATH=/sbin:/bin export PS1='\w \$ ' diff --git a/test/testenv_start.sh b/test/testenv_start.sh index afdbb34b..5c8cee8d 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -7,8 +7,8 @@ TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" >&2 echo "Hint: Execute '${TEST_DIR}/testenv_exec.sh \$(pgrep -P $$) /bin/sh; reset' to enter the test namespace" -unshare=$(which unshare) -chroot=$(which chroot) +unshare=$(command -v unshare) +chroot=$(command -v chroot) export PATH=/sbin:/bin export PS1='\w \$ ' From 11841ceb5d0be9df7cb2642a326fc2b11a3b8c1c Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 22:07:17 +0100 Subject: [PATCH 33/90] test: Clean up Automake integration of tests Signed-off-by: Jacques de Laval --- .travis.yml | 5 +- Makefile.am | 5 +- configure.ac | 3 +- test/.gitignore | 4 +- test/Makefile | 88 ------------------------- test/Makefile.am | 6 ++ test/busybox-x86_64.md5 | 1 - test/checkself.sh | 6 ++ test/test_root/.gitignore | 23 ++++++- test/test_root/Makefile | 79 ++++++++++++++++++++++ test/{ => test_root/bin}/chrootsetup.sh | 0 test/test_root/busybox-x86_64.md5 | 1 + 12 files changed, 123 insertions(+), 98 deletions(-) delete mode 100644 test/Makefile create mode 100644 test/Makefile.am delete mode 100644 test/busybox-x86_64.md5 create mode 100755 test/checkself.sh create mode 100644 test/test_root/Makefile rename test/{ => test_root/bin}/chrootsetup.sh (100%) create mode 100644 test/test_root/busybox-x86_64.md5 diff --git a/.travis.yml b/.travis.yml index 433c487c..2c669b40 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,6 +52,8 @@ script: - cat config.log - make clean - make V=1 -j5 + - LD_LIBRARY_PATH=/tmp/lib make check + - cat ./test/test-suite.log - make install-strip - tree /tmp - ldd /tmp/sbin/finit @@ -63,5 +65,4 @@ script: - nsenter -V - chroot --version - LD_LIBRARY_PATH=/tmp/lib make check - - cat ./test-suite.log - - make -C test/ clean + - cat ./test/test-suite.log diff --git a/Makefile.am b/Makefile.am index 20ebb66e..a1ccda65 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,5 @@ ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = plugins src +SUBDIRS = plugins src test doc_DATA = README.md LICENSE contrib/finit.conf EXTRA_DIST = README.md LICENSE ChangeLog.md contrib/finit.conf @@ -10,9 +10,6 @@ if DOC SUBDIRS += doc endif -AM_TESTS_ENVIRONMENT = make -C $(srcdir)/test test_root; -TESTS = test/test_start_stop_service/test.sh - # Compat hook install-dev: @make -C src install-pkgincludeHEADERS diff --git a/configure.ac b/configure.ac index 6a1c7f85..beeac4e7 100644 --- a/configure.ac +++ b/configure.ac @@ -11,7 +11,8 @@ AC_CONFIG_FILES([Makefile contrib/void/Makefile contrib/void/finit.d/Makefile contrib/void/finit.d/available/Makefile doc/Makefile plugins/Makefile - src/Makefile]) + src/Makefile + test/Makefile]) # Older versions of autoconf (<2.58) do not have AC_CONFIG_MACRO_DIR() #m4_include([m4/plugin.m4]) diff --git a/test/.gitignore b/test/.gitignore index a447bd04..151cdadf 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -1,3 +1,5 @@ -busybox-x86_64 +# GNU configure and build system *.log *.trs +/Makefile +/Makefile.in diff --git a/test/Makefile b/test/Makefile deleted file mode 100644 index 0b047a97..00000000 --- a/test/Makefile +++ /dev/null @@ -1,88 +0,0 @@ -.PHONY: clean check test-check - -ARCH ?= x86_64 - -FINITBIN ?= ../src/finit -INITCTLBIN ?= ../src/initctl - -BBVER ?= 1.31.0 -BBBIN = busybox-$(ARCH) -BBURL ?= https://www.busybox.net/downloads/binaries/$(BBVER)-defconfig-multiarch-musl/$(BBBIN) - -testroot = test_root -testroot_bin = $(testroot)/bin -binaries = $(testroot_bin)/cat \ - $(testroot_bin)/cp \ - $(testroot_bin)/date \ - $(testroot_bin)/echo \ - $(testroot_bin)/env \ - $(testroot_bin)/find \ - $(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)/bin \ - $(testroot)/dev \ - $(testroot)/etc \ - $(testroot)/proc \ - $(testroot)/sbin \ - $(testroot)/sys \ - $(testroot)/tmp - -_libs_src = $(shell ldd $(FINITBIN) | grep -Eo '/[^ ]+') -libs = $(foreach path,$(_libs_src),$(testroot)$(path)) - -all: check - -$(dirs): - mkdir -p $@ - -$(BBBIN): - wget -O $@ $(BBURL) - md5sum -c $@.md5 - -$(testroot): $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit $(testroot)/bin/initctl test-check - -$(testroot_bin)/$(BBBIN): $(BBBIN) - mkdir -p $(testroot_bin) - cp $(BBBIN) $(testroot_bin)/ - chmod +x $(testroot_bin)/$(BBBIN) - -$(binaries): $(testroot_bin)/$(BBBIN) - cd $(testroot_bin); \ - ln -s $(BBBIN) $(notdir $@) - -$(testroot_bin)/chrootsetup.sh: chrootsetup.sh - cp $< $@ - -$(testroot)/sbin/finit: $(FINITBIN) $(libs) - cp $< $@ - -$(testroot)/bin/initctl: $(INITCTLBIN) $(libs) - cp $< $@ - -$(libs): - mkdir -p $(dir $@) - cp $(shell echo $@ | sed s/^$(testroot)//) $@ - -check: $(testroot) - ./test_start_stop_service/test.sh - -test-check: - shellcheck $(shell find . -name '*.sh') - -clean: - rm -rf $(testroot)/* - rm -f $(BBBIN) - rm -f ./test_start_stop_service/*.log - rm -f ./test_start_stop_service/*.trs diff --git a/test/Makefile.am b/test/Makefile.am new file mode 100644 index 00000000..29f9741c --- /dev/null +++ b/test/Makefile.am @@ -0,0 +1,6 @@ +AM_TESTS_ENVIRONMENT = make -C test_root/; +TESTS = checkself.sh +TESTS += test_start_stop_service/test.sh + +clean-local: + make -C test_root/ clean diff --git a/test/busybox-x86_64.md5 b/test/busybox-x86_64.md5 deleted file mode 100644 index 0ecfec84..00000000 --- a/test/busybox-x86_64.md5 +++ /dev/null @@ -1 +0,0 @@ -ea7e2353af0067a926d3e021e504290e busybox-x86_64 diff --git a/test/checkself.sh b/test/checkself.sh new file mode 100755 index 00000000..07405ced --- /dev/null +++ b/test/checkself.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +set -eux + +# shellcheck disable=SC2046 +shellcheck $(find ./ -name '*.sh') diff --git a/test/test_root/.gitignore b/test/test_root/.gitignore index 08ad8a05..15385773 100644 --- a/test/test_root/.gitignore +++ b/test/test_root/.gitignore @@ -1,4 +1,25 @@ -/bin +/bin/busybox-x86_64 +/bin/cat +/bin/cp +/bin/date +/bin/echo +/bin/env +/bin/find +/bin/initctl +/bin/kill +/bin/ls +/bin/mkdir +/bin/mkfifo +/bin/mknod +/bin/mount +/bin/pgrep +/bin/printf +/bin/ps +/bin/rm +/bin/sh +/bin/sleep +/bin/top +/bin/touch /dev /etc /lib64 diff --git a/test/test_root/Makefile b/test/test_root/Makefile new file mode 100644 index 00000000..cfb687b1 --- /dev/null +++ b/test/test_root/Makefile @@ -0,0 +1,79 @@ +.PHONY: clean check + +ARCH ?= x86_64 + +FINITBIN ?= ../../src/finit +INITCTLBIN ?= ../../src/initctl + +BBVER ?= 1.31.0 +BBBIN = busybox-$(ARCH) +BBURL ?= https://www.busybox.net/downloads/binaries/$(BBVER)-defconfig-multiarch-musl/$(BBBIN) + +binaries = ./bin/cat \ + ./bin/cp \ + ./bin/date \ + ./bin/echo \ + ./bin/env \ + ./bin/find \ + ./bin/kill \ + ./bin/ls \ + ./bin/mkdir \ + ./bin/mkfifo \ + ./bin/mknod \ + ./bin/mount \ + ./bin/printf \ + ./bin/pgrep \ + ./bin/ps \ + ./bin/rm \ + ./bin/sh \ + ./bin/sleep \ + ./bin/top \ + ./bin/touch +dirs = ./dev \ + ./etc \ + ./proc \ + ./sbin \ + ./sys \ + ./tmp + +_libs_src = $(shell ldd $(FINITBIN) | grep -Eo '/[^ ]+') +libs = $(foreach path,$(_libs_src),.$(path)) + +all: $(binaries) $(dirs) ./sbin/finit ./bin/initctl + +$(dirs): + mkdir -p $@ + +./bin/$(BBBIN): + wget -O $@ $(BBURL) + chmod +x ./bin/$(BBBIN) + md5sum -c $(BBBIN).md5 + +$(binaries): ./bin/$(BBBIN) + cd ./bin; \ + rm -f $(notdir $@); \ + ln -s $(BBBIN) $(notdir $@) + +./sbin/finit: $(FINITBIN) $(libs) + cp $< $@ + +./bin/initctl: $(INITCTLBIN) $(libs) + cp $< $@ + +$(libs): + mkdir -p $(dir $@) + cp /$@ $@ + +clean: + rm -f ./bin/$(BBBIN) + rm -f ./bin/initctl + rm -rf $(binaries) + rm -rf ./dev + rm -rf ./etc + rm -rf ./lib64 + rm -rf ./proc + rm -rf ./sbin + rm -rf ./sys + rm -rf ./tmp + rm -rf ./usr + rm -rf ./var diff --git a/test/chrootsetup.sh b/test/test_root/bin/chrootsetup.sh similarity index 100% rename from test/chrootsetup.sh rename to test/test_root/bin/chrootsetup.sh diff --git a/test/test_root/busybox-x86_64.md5 b/test/test_root/busybox-x86_64.md5 new file mode 100644 index 00000000..9ad31f90 --- /dev/null +++ b/test/test_root/busybox-x86_64.md5 @@ -0,0 +1 @@ +ea7e2353af0067a926d3e021e504290e bin/busybox-x86_64 From 2c56e20941cae13b7c16ac7c501451c629689e39 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 22:12:34 +0100 Subject: [PATCH 34/90] test: Name test-lib appropriately Signed-off-by: Jacques de Laval --- test/{runtest.sh => lib.sh} | 0 test/test_start_stop_service/test.sh | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename test/{runtest.sh => lib.sh} (100%) diff --git a/test/runtest.sh b/test/lib.sh similarity index 100% rename from test/runtest.sh rename to test/lib.sh diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index a0b1d9c1..7fad918a 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -4,7 +4,7 @@ set -eu TEST_DIR=$(dirname "$0")/.. # shellcheck source=/dev/null -. "$TEST_DIR/runtest.sh" +. "$TEST_DIR/lib.sh" assert_num_children() { assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" From 07521e043a32f0e9ec01f8967d52a51f5b5e839d Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 22:30:10 +0100 Subject: [PATCH 35/90] test: Add standard dir for test assets Signed-off-by: Jacques de Laval --- test/test_root/.gitignore | 1 + test/test_root/Makefile | 2 ++ test/test_start_stop_service/test.sh | 4 ++-- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/test/test_root/.gitignore b/test/test_root/.gitignore index 15385773..bfa4c856 100644 --- a/test/test_root/.gitignore +++ b/test/test_root/.gitignore @@ -26,6 +26,7 @@ /proc /sbin /sys +/test_assets /tmp /usr /var diff --git a/test/test_root/Makefile b/test/test_root/Makefile index cfb687b1..29dd7d68 100644 --- a/test/test_root/Makefile +++ b/test/test_root/Makefile @@ -34,6 +34,7 @@ dirs = ./dev \ ./proc \ ./sbin \ ./sys \ + ./test_assets \ ./tmp _libs_src = $(shell ldd $(FINITBIN) | grep -Eo '/[^ ]+') @@ -74,6 +75,7 @@ clean: rm -rf ./proc rm -rf ./sbin rm -rf ./sys + rm -rf ./test_assets rm -rf ./tmp rm -rf ./usr rm -rf ./var diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index 7fad918a..d4146e4a 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -12,8 +12,8 @@ assert_num_children() { # Test say 'Set up a service' -cp "$TEST_DIR"/test_start_stop_service/service.sh "$TEST_DIR"/test_root/bin -texec sh -c "echo 'service [2345] kill:20 log /bin/service.sh' > $FINIT_CONF" +cp "$TEST_DIR"/test_start_stop_service/service.sh "$TEST_DIR"/test_root/test_assets/ +texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" say 'Reload Finit' texec kill -SIGHUP 1 From 17d0835d63d36c4bc9c37d47a31c009ccf6ea2e1 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Tue, 23 Feb 2021 00:32:36 +0100 Subject: [PATCH 36/90] test: Only echo last output of command in retry Signed-off-by: Jacques de Laval --- test/lib.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/lib.sh b/test/lib.sh index 452f2536..f73ff4b4 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -54,10 +54,13 @@ retry() { for _ in $(seq 1 "$__retry_n"); do sleep "$__retry_sleep" - eval "$__retry_cmd" || continue - return 0 + __retry_cmd_out=$(eval "$__retry_cmd") && \ + echo "$__retry_cmd_out" && \ + return 0 done - eval "$__retry_cmd" + __retry_cmd_exit="$?" + echo "$__retry_cmd_out" + return "$__retry_cmd_exit" } say() { @@ -90,8 +93,7 @@ log "$color_reset" '--' '' # Setup "$TEST_DIR/testenv_start.sh" finit & finit_ppid=$! -# shellcheck disable=SC2016 -retry 'finit_pid=$(pgrep -P '"$finit_ppid"')' +finit_pid=$(retry "pgrep -P $finit_ppid") tty=/dev/$(texec cat /sys/class/tty/console/active) texec cat "$tty" & From 70773105ac48b72ad84047290b93962aebb466bc Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Tue, 23 Feb 2021 01:09:46 +0100 Subject: [PATCH 37/90] test: Tell Finit to shut down rather than reboot in teardown Signed-off-by: Jacques de Laval --- test/lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lib.sh b/test/lib.sh index f73ff4b4..afa2d6f1 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -75,7 +75,7 @@ teardown() { else log "$fg_red" 'TEST FAIL' '' fi - texec kill 1 + texec kill -SIGUSR2 1 wait } From 4480010d4a46498097c8936bcbe46e99b21d3ed2 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Mon, 22 Feb 2021 23:43:15 +0100 Subject: [PATCH 38/90] test: Add support for teardown in tests If a test defines a function 'test_teardown(), that function will be called before the test env teardown is run. Signed-off-by: Magnus Malm --- test/lib.sh | 5 +++++ test/test_start_stop_service/test.sh | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/test/lib.sh b/test/lib.sh index afa2d6f1..953525a5 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -69,6 +69,11 @@ say() { teardown() { test_status="$?" + + if type test_teardown > /dev/null 2>&1 ; then + test_teardown + fi + log "$color_reset" '--' '' if [ "$test_status" -eq 0 ]; then log "$fg_green" 'TEST PASS' '' diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index d4146e4a..ae5fe8ff 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -10,6 +10,14 @@ assert_num_children() { assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" } +test_teardown() { + say "Test done $(date)" + say "Running test teardown..." + + # Teardown + rm -f test_root/bin/service.sh +} + # Test say 'Set up a service' cp "$TEST_DIR"/test_start_stop_service/service.sh "$TEST_DIR"/test_root/test_assets/ @@ -27,6 +35,3 @@ say 'Reload Finit' texec kill -SIGHUP 1 retry 'assert_num_children 0 service.sh' - -# Teardown -rm -f test_root/bin/service.sh From 3d01728d2e1c9b13dcd16953d4a1518cf48da8ad Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Mon, 22 Feb 2021 23:46:40 +0100 Subject: [PATCH 39/90] test: Add testenv_enter.sh to ease debugging testenv The script testenv_enter.sh, by default, runs /bin/sh in the environment currently setup. A bit easier than copy pasting the old hint text printed before test run. Signed-off-by: Magnus Malm --- test/lib.sh | 2 ++ test/testenv_enter.sh | 23 +++++++++++++++++++++++ test/testenv_start.sh | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100755 test/testenv_enter.sh diff --git a/test/lib.sh b/test/lib.sh index 953525a5..c87ccd5a 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -83,6 +83,7 @@ teardown() { texec kill -SIGUSR2 1 wait + rm "$TEST_DIR"/running_test.pid } trap teardown EXIT @@ -98,6 +99,7 @@ log "$color_reset" '--' '' # Setup "$TEST_DIR/testenv_start.sh" finit & finit_ppid=$! +echo "$finit_ppid" > "$TEST_DIR"/running_test.pid finit_pid=$(retry "pgrep -P $finit_ppid") tty=/dev/$(texec cat /sys/class/tty/console/active) diff --git a/test/testenv_enter.sh b/test/testenv_enter.sh new file mode 100755 index 00000000..a2507928 --- /dev/null +++ b/test/testenv_enter.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +set -eu + +cmd="" +if [ "$#" -lt 1 ]; then + cmd="/bin/sh" +else + cmd=$1 + shift +fi + +PID_FILE=$(dirname "$0")/running_test.pid + +if [ -f "$PID_FILE" ] ; then + target=$(cat "$PID_FILE") + target=$(pgrep -P "$target") +else + echo "No test running!" + exit 1 +fi + +"$(dirname "$0")"/testenv_exec.sh "$target" "$cmd" "$@" diff --git a/test/testenv_start.sh b/test/testenv_start.sh index 5c8cee8d..db529429 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -5,7 +5,7 @@ set -eu TEST_DIR=$(dirname "$0") TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" ->&2 echo "Hint: Execute '${TEST_DIR}/testenv_exec.sh \$(pgrep -P $$) /bin/sh; reset' to enter the test namespace" +>&2 echo "Hint: Execute 'test/testenv_enter.sh' to enter the test namespace" unshare=$(command -v unshare) chroot=$(command -v chroot) From 5e20abef8ddd15daefdb4e6323d5be8feb1711c0 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Tue, 23 Feb 2021 00:24:54 +0100 Subject: [PATCH 40/90] test: Print running test name with log() Signed-off-by: Magnus Malm --- test/lib.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/lib.sh b/test/lib.sh index c87ccd5a..fc85507c 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -21,7 +21,7 @@ fg_red='\e[1;31m' fg_green='\e[1;32m' fg_yellow='\e[1;33m' log() { - printf "< TEST > %b%b%b %s\n" "$1" "$2" "$color_reset" "$3" + printf "< $TEST_NAME > %b%b%b %s\n" "$1" "$2" "$color_reset" "$3" } assert() { @@ -92,6 +92,9 @@ FINIT_CONF=$(grep FINIT_CONF "$TEST_DIR/../config.h" | cut -d' ' -f3 | cut -d'"' export FINIT_CONF FINIT_CONF_DIR="$(dirname "$FINIT_CONF")"/finit.d export FINIT_CONF_DIR +TEST_NAME="$(dirname "$0")" +TEST_NAME=${TEST_NAME#*/} +export TEST_NAME log "$color_reset" 'Test start' '' log "$color_reset" '--' '' From 3ec788a6cf7dfd0af76552fd988bfba4e6beb6bc Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Tue, 23 Feb 2021 00:29:08 +0100 Subject: [PATCH 41/90] test: Minor tweaks of how/when to print testenv stuff Signed-off-by: Magnus Malm --- test/lib.sh | 10 +++++++--- test/testenv_start.sh | 2 -- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/test/lib.sh b/test/lib.sh index fc85507c..922b067f 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -96,13 +96,17 @@ TEST_NAME="$(dirname "$0")" TEST_NAME=${TEST_NAME#*/} export TEST_NAME -log "$color_reset" 'Test start' '' -log "$color_reset" '--' '' +# Setup test environment -# Setup "$TEST_DIR/testenv_start.sh" finit & finit_ppid=$! echo "$finit_ppid" > "$TEST_DIR"/running_test.pid + +>&2 echo "Hint: Execute 'test/testenv_enter.sh' to enter the test namespace" +>&2 echo "finit conf '$FINIT_CONF'" +>&2 echo "finit_conf dir '$FINIT_CONF_DIR'" +log "$color_reset" 'Setup of test environment done' '' + finit_pid=$(retry "pgrep -P $finit_ppid") tty=/dev/$(texec cat /sys/class/tty/console/active) diff --git a/test/testenv_start.sh b/test/testenv_start.sh index db529429..7dcf2869 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -5,8 +5,6 @@ set -eu TEST_DIR=$(dirname "$0") TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" ->&2 echo "Hint: Execute 'test/testenv_enter.sh' to enter the test namespace" - unshare=$(command -v unshare) chroot=$(command -v chroot) From 6d9374590faf6401751c72aa6417256081eb624f Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Tue, 23 Feb 2021 00:31:56 +0100 Subject: [PATCH 42/90] test: Add test start timestamp Signed-off-by: Magnus Malm --- test/test_start_stop_service/test.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index ae5fe8ff..5816854b 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -19,6 +19,7 @@ test_teardown() { } # Test +say "Test start $(date)" say 'Set up a service' cp "$TEST_DIR"/test_start_stop_service/service.sh "$TEST_DIR"/test_root/test_assets/ texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" From 8a2cc2aeb8f68be77e9e4d9148f8f758bc988dd1 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Tue, 23 Feb 2021 00:44:33 +0100 Subject: [PATCH 43/90] test: Move service.sh to test/common/ Signed-off-by: Magnus Malm --- test/{test_start_stop_service => common}/service.sh | 0 test/test_start_stop_service/test.sh | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename test/{test_start_stop_service => common}/service.sh (100%) diff --git a/test/test_start_stop_service/service.sh b/test/common/service.sh similarity index 100% rename from test/test_start_stop_service/service.sh rename to test/common/service.sh diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index 5816854b..c0e66564 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -21,7 +21,7 @@ test_teardown() { # Test say "Test start $(date)" say 'Set up a service' -cp "$TEST_DIR"/test_start_stop_service/service.sh "$TEST_DIR"/test_root/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" say 'Reload Finit' From bb095bfd5d9f4d102c8dde9b8c55e504938298f5 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Tue, 23 Feb 2021 00:49:10 +0100 Subject: [PATCH 44/90] test: Minor tweak on test's output Signed-off-by: Magnus Malm --- test/test_start_stop_service/test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index c0e66564..8637bc14 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -20,7 +20,7 @@ test_teardown() { # Test say "Test start $(date)" -say 'Set up a service' +say 'Add a dynamic service in /etc/finit.conf' cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" @@ -29,7 +29,7 @@ texec kill -SIGHUP 1 retry 'assert_num_children 1 service.sh' -say 'Remove the service' +say 'Remove the dynamic service from /etc/finit.conf' texec sh -c "echo > $FINIT_CONF" say 'Reload Finit' From eda9ae4c3e3e35698ddfa328909b6ad0c0efa678 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Tue, 23 Feb 2021 00:50:58 +0100 Subject: [PATCH 45/90] test: Rename start/stop service test to add/remove dynamic service Signed-off-by: Magnus Malm --- test/Makefile.am | 2 +- .../test.sh | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename test/{test_start_stop_service => test_add_remove_dynamic_service}/test.sh (100%) diff --git a/test/Makefile.am b/test/Makefile.am index 29f9741c..c8584af6 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,6 +1,6 @@ AM_TESTS_ENVIRONMENT = make -C test_root/; TESTS = checkself.sh -TESTS += test_start_stop_service/test.sh +TESTS += test_add_remove_dynamic_service/test.sh clean-local: make -C test_root/ clean diff --git a/test/test_start_stop_service/test.sh b/test/test_add_remove_dynamic_service/test.sh similarity index 100% rename from test/test_start_stop_service/test.sh rename to test/test_add_remove_dynamic_service/test.sh From 27ad3b5a7f3c55a8879451fa748ae6782882f94e Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Tue, 23 Feb 2021 01:05:07 +0100 Subject: [PATCH 46/90] test: Minor cleanup in lib.sh Signed-off-by: Magnus Malm --- test/lib.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/lib.sh b/test/lib.sh index 922b067f..82e6b1e2 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -90,14 +90,17 @@ trap teardown EXIT FINIT_CONF=$(grep FINIT_CONF "$TEST_DIR/../config.h" | cut -d' ' -f3 | cut -d'"' -f2) export FINIT_CONF + FINIT_CONF_DIR="$(dirname "$FINIT_CONF")"/finit.d export FINIT_CONF_DIR + TEST_NAME="$(dirname "$0")" TEST_NAME=${TEST_NAME#*/} export TEST_NAME # Setup test environment +# Setup test environment "$TEST_DIR/testenv_start.sh" finit & finit_ppid=$! echo "$finit_ppid" > "$TEST_DIR"/running_test.pid From b364fd7d6a2245b6af20bf919a99c96287f96113 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Tue, 23 Feb 2021 01:37:32 +0100 Subject: [PATCH 47/90] test: Add test for dynamically add/remove service in finit.d/ files Signed-off-by: Magnus Malm --- test/Makefile.am | 1 + test/common/service.conf | 1 + .../test.sh | 41 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 test/common/service.conf create mode 100755 test/test_add_remove_dynamic_service_sub_config/test.sh diff --git a/test/Makefile.am b/test/Makefile.am index c8584af6..20a60dca 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,6 +1,7 @@ AM_TESTS_ENVIRONMENT = make -C test_root/; TESTS = checkself.sh TESTS += test_add_remove_dynamic_service/test.sh +TESTS += test_add_remove_dynamic_service_sub_config/test.sh clean-local: make -C test_root/ clean diff --git a/test/common/service.conf b/test/common/service.conf new file mode 100644 index 00000000..3fd97586 --- /dev/null +++ b/test/common/service.conf @@ -0,0 +1 @@ +service [2345] kill:20 log /test_assets/service.sh diff --git a/test/test_add_remove_dynamic_service_sub_config/test.sh b/test/test_add_remove_dynamic_service_sub_config/test.sh new file mode 100755 index 00000000..a2cf9d85 --- /dev/null +++ b/test/test_add_remove_dynamic_service_sub_config/test.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +set -eu + +TEST_DIR=$(dirname "$0")/.. +# shellcheck source=/dev/null +. "$TEST_DIR/lib.sh" + +assert_num_children() { + assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" +} + +test_teardown() { + say "Test done $(date)" + say "Running test teardown..." + + # Teardown + rm -f test_root/bin/service.sh +} + +# Test +say "Test start $(date)" +# toggle_finit_debug +say "Add a dynamic service in $FINIT_CONF_DIR/service.conf" +cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ +texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh pid:/run/service.pid' > $FINIT_CONF_DIR/service.conf" + +say 'Reload Finit' +texec kill -SIGHUP 1 + +pause + +retry 'assert_num_children 1 service.sh' 5 + +say 'Remove the dynamic service from /etc/finit.d/service.conf' +texec sh -c "echo > $FINIT_CONF_DIR/service.conf" + +say 'Reload Finit' +texec kill -SIGHUP 1 + +retry 'assert_num_children 0 service.sh' 5 From 6bcb9e9030b7c61f05a9caed28f8b53212d7dc96 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Tue, 23 Feb 2021 11:20:38 +0100 Subject: [PATCH 48/90] test: Populate/cleanup /etc "manually" via Makefile/tests For now, do not mount /etc as tmpfs, but instead create the dir when building the rootfs via test/test_root/Makefile. Signed-off-by: Magnus Malm --- test/test_root/Makefile | 3 ++- test/test_root/bin/chrootsetup.sh | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/test_root/Makefile b/test/test_root/Makefile index 29dd7d68..0fa7304c 100644 --- a/test/test_root/Makefile +++ b/test/test_root/Makefile @@ -35,7 +35,8 @@ dirs = ./dev \ ./sbin \ ./sys \ ./test_assets \ - ./tmp + ./tmp \ + ./etc _libs_src = $(shell ldd $(FINITBIN) | grep -Eo '/[^ ]+') libs = $(foreach path,$(_libs_src),.$(path)) diff --git a/test/test_root/bin/chrootsetup.sh b/test/test_root/bin/chrootsetup.sh index fe4e0e4c..5f22b209 100755 --- a/test/test_root/bin/chrootsetup.sh +++ b/test/test_root/bin/chrootsetup.sh @@ -17,7 +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 +# mount -t tmpfs none /etc # mkdir -p /dev/pts # mount -t devpts none /dev/pts From 51fe6a15e9730bfd6d730a066dbd5d363288a74a Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Tue, 23 Feb 2021 15:27:33 +0100 Subject: [PATCH 49/90] test: Improve add/remove sub config test Signed-off-by: Magnus Malm --- test/test_add_remove_dynamic_service_sub_config/test.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/test_add_remove_dynamic_service_sub_config/test.sh b/test/test_add_remove_dynamic_service_sub_config/test.sh index a2cf9d85..97b82fc4 100755 --- a/test/test_add_remove_dynamic_service_sub_config/test.sh +++ b/test/test_add_remove_dynamic_service_sub_config/test.sh @@ -20,16 +20,14 @@ test_teardown() { # Test say "Test start $(date)" -# toggle_finit_debug + say "Add a dynamic service in $FINIT_CONF_DIR/service.conf" cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ -texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh pid:/run/service.pid' > $FINIT_CONF_DIR/service.conf" +texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF_DIR/service.conf" say 'Reload Finit' texec kill -SIGHUP 1 -pause - retry 'assert_num_children 1 service.sh' 5 say 'Remove the dynamic service from /etc/finit.d/service.conf' From 25ffc3192f78551b7cc07b5eff0edb01586e6d21 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Tue, 23 Feb 2021 15:30:42 +0100 Subject: [PATCH 50/90] test: Add two new tests * test_start_stop_service Test that we can start/stop (via initctl) a service configured in finit.conf * test_start_stop_service_sub_config Test that we can start/stop (via initctl) a service configured in finit.d/ files Signed-off-by: Magnus Malm --- test/Makefile.am | 2 + test/test_start_stop_service/test.sh | 40 ++++++++++++++++++ .../test.sh | 41 +++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100755 test/test_start_stop_service/test.sh create mode 100755 test/test_start_stop_service_sub_config/test.sh diff --git a/test/Makefile.am b/test/Makefile.am index 20a60dca..bb72e0b7 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -2,6 +2,8 @@ AM_TESTS_ENVIRONMENT = make -C test_root/; TESTS = checkself.sh TESTS += test_add_remove_dynamic_service/test.sh TESTS += test_add_remove_dynamic_service_sub_config/test.sh +TESTS += test_start_stop_service/test.sh +TESTS += test_start_stop_service_sub_config/test.sh clean-local: make -C test_root/ clean diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh new file mode 100755 index 00000000..7d0c2c5d --- /dev/null +++ b/test/test_start_stop_service/test.sh @@ -0,0 +1,40 @@ +#!/bin/sh + +set -eu + +TEST_DIR=$(dirname "$0")/.. +# shellcheck source=/dev/null +. "$TEST_DIR/lib.sh" + +assert_num_children() { + assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" +} + +test_teardown() { + say "Test done $(date)" + say "Running test teardown..." + + # Teardown + rm -f test_root/bin/service.sh +} + +# Test +say "Test start $(date)" +say 'Add service stanza in /etc/finit.conf' +cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ +texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" + +say 'Reload Finit' +texec sh -c "initctl reload" + +retry 'assert_num_children 1 service.sh' + +say 'Stop the service' +texec sh -c "initctl stop service.sh" + +retry 'assert_num_children 0 service.sh' + +say 'Start the service again' +texec sh -c "initctl start service.sh" + +retry 'assert_num_children 1 service.sh' diff --git a/test/test_start_stop_service_sub_config/test.sh b/test/test_start_stop_service_sub_config/test.sh new file mode 100755 index 00000000..87026274 --- /dev/null +++ b/test/test_start_stop_service_sub_config/test.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +set -eu + +TEST_DIR=$(dirname "$0")/.. +# shellcheck source=/dev/null +. "$TEST_DIR/lib.sh" + +assert_num_children() { + assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" +} + +test_teardown() { + say "Test done $(date)" + say "Running test teardown..." + + # Teardown + rm -f test_root/bin/service.sh +} + +# Test +say "Test start $(date)" +say 'Add service stanza in /etc/finit.d/service.conf' +cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ +# texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" +texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF_DIR/service.conf" + +say 'Reload Finit' +texec sh -c "initctl reload" + +retry 'assert_num_children 1 service.sh' + +say 'Stop the service' +texec sh -c "initctl stop service.sh" + +retry 'assert_num_children 0 service.sh' + +say 'Start the service again' +texec sh -c "initctl start service.sh" + +retry 'assert_num_children 1 service.sh' From 47dcd1cb8a98288f438128a5c3e5159d9f754d9b Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 23 Feb 2021 15:45:21 +0100 Subject: [PATCH 51/90] test: silence make in-out directory Signed-off-by: Joachim Wiberg --- test/Makefile.am | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/Makefile.am b/test/Makefile.am index bb72e0b7..b7542fd0 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,4 +1,5 @@ -AM_TESTS_ENVIRONMENT = make -C test_root/; +AM_TESTS_ENVIRONMENT = make --silent --no-print-directory -C test_root/; + TESTS = checkself.sh TESTS += test_add_remove_dynamic_service/test.sh TESTS += test_add_remove_dynamic_service_sub_config/test.sh From f72e0a646e6fb11e584e03f193f39f81d331a603 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 23 Feb 2021 15:45:50 +0100 Subject: [PATCH 52/90] test: fix make warning 'etc' given more than once Signed-off-by: Joachim Wiberg --- test/test_root/Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_root/Makefile b/test/test_root/Makefile index 0fa7304c..29dd7d68 100644 --- a/test/test_root/Makefile +++ b/test/test_root/Makefile @@ -35,8 +35,7 @@ dirs = ./dev \ ./sbin \ ./sys \ ./test_assets \ - ./tmp \ - ./etc + ./tmp _libs_src = $(shell ldd $(FINITBIN) | grep -Eo '/[^ ]+') libs = $(foreach path,$(_libs_src),.$(path)) From 05901a90aabf9ce0cf80bd09ce7653b590927694 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Tue, 23 Feb 2021 19:35:38 +0100 Subject: [PATCH 53/90] test: Fix distcheck Signed-off-by: Jacques de Laval --- test/Makefile.am | 21 ++++++++++++++++----- test/checkself.sh | 6 ++++-- test/lib.sh | 8 +++++--- test/setup_root.sh | 15 +++++++++++++++ test/teardown_root.sh | 8 ++++++++ test/test_root/Makefile | 13 +++---------- test/test_start_stop_service/test.sh | 4 ++-- test/testenv_exec.sh | 2 +- test/testenv_start.sh | 2 +- 9 files changed, 55 insertions(+), 24 deletions(-) create mode 100755 test/setup_root.sh create mode 100755 test/teardown_root.sh diff --git a/test/Makefile.am b/test/Makefile.am index 29f9741c..992c68c3 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,6 +1,17 @@ -AM_TESTS_ENVIRONMENT = make -C test_root/; -TESTS = checkself.sh -TESTS += test_start_stop_service/test.sh +EXTRA_DIST = lib.sh testenv_exec.sh testenv_start.sh +EXTRA_DIST += test_root/Makefile test_root/busybox-x86_64.md5 +EXTRA_DIST += test_root/bin/chrootsetup.sh +EXTRA_DIST += checkself.sh +EXTRA_DIST += setup_root.sh +EXTRA_DIST += teardown_root.sh +EXTRA_DIST += test_start_stop_service/test.sh +EXTRA_DIST += test_start_stop_service/service.sh -clean-local: - make -C test_root/ clean +AM_TESTS_ENVIRONMENT = TESTS_ROOT='$(builddir)/test_root/'; +AM_TESTS_ENVIRONMENT += export TESTS_ROOT; +AM_TESTS_ENVIRONMENT += export LD_LIBRARY_PATH; + +TESTS = checkself.sh +TESTS += setup_root.sh +TESTS += test_start_stop_service/test.sh +TESTS += teardown_root.sh diff --git a/test/checkself.sh b/test/checkself.sh index 07405ced..b42091e9 100755 --- a/test/checkself.sh +++ b/test/checkself.sh @@ -1,6 +1,8 @@ #!/bin/sh -set -eux +set -eu + +testdir="${srcdir:-$(dirname "$0")}" # shellcheck disable=SC2046 -shellcheck $(find ./ -name '*.sh') +shellcheck $(find "$testdir" -name '*.sh') diff --git a/test/lib.sh b/test/lib.sh index afa2d6f1..7902bae1 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -2,7 +2,7 @@ texec() { # shellcheck disable=SC2154 - "$TEST_DIR/testenv_exec.sh" "$finit_pid" "$@" + TESTS_ROOT=. "$TEST_DIR/testenv_exec.sh" "$finit_pid" "$@" } pause() { @@ -75,14 +75,16 @@ teardown() { else log "$fg_red" 'TEST FAIL' '' fi - texec kill -SIGUSR2 1 + if [ -n "${finit_pid+x}" ]; then + texec kill -SIGUSR2 1 + fi wait } trap teardown EXIT -FINIT_CONF=$(grep FINIT_CONF "$TEST_DIR/../config.h" | cut -d' ' -f3 | cut -d'"' -f2) +FINIT_CONF=$(grep FINIT_CONF "$TESTS_ROOT/../config.h" | cut -d' ' -f3 | cut -d'"' -f2) export FINIT_CONF FINIT_CONF_DIR="$(dirname "$FINIT_CONF")"/finit.d export FINIT_CONF_DIR diff --git a/test/setup_root.sh b/test/setup_root.sh new file mode 100755 index 00000000..a00f1244 --- /dev/null +++ b/test/setup_root.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +set -eu + +# shellcheck disable=SC2154 +"$srcdir"/../configure --prefix=/ +make +make DESTDIR="$(pwd)/test_root/" install + +mkdir -p test_root/bin +cp "${srcdir}/test_root/bin/chrootsetup.sh" test_root/bin +cp "${srcdir}/test_root/busybox-x86_64.md5" test_root/ +cp "${srcdir}/test_root/Makefile" test_root/ + +make -C test_root/ diff --git a/test/teardown_root.sh b/test/teardown_root.sh new file mode 100755 index 00000000..5cf2eb1d --- /dev/null +++ b/test/teardown_root.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +set -eu + +if [ -d test_root/var/lock ]; then + chmod +r test_root/var/lock +fi +make -C test_root/ clean diff --git a/test/test_root/Makefile b/test/test_root/Makefile index 29dd7d68..99b23c2e 100644 --- a/test/test_root/Makefile +++ b/test/test_root/Makefile @@ -2,8 +2,8 @@ ARCH ?= x86_64 -FINITBIN ?= ../../src/finit -INITCTLBIN ?= ../../src/initctl +FINITBIN ?= ./sbin/finit +INITCTLBIN ?= ./sbin/initctl BBVER ?= 1.31.0 BBBIN = busybox-$(ARCH) @@ -40,7 +40,7 @@ dirs = ./dev \ _libs_src = $(shell ldd $(FINITBIN) | grep -Eo '/[^ ]+') libs = $(foreach path,$(_libs_src),.$(path)) -all: $(binaries) $(dirs) ./sbin/finit ./bin/initctl +all: $(binaries) $(dirs) $(libs) $(dirs): mkdir -p $@ @@ -55,19 +55,12 @@ $(binaries): ./bin/$(BBBIN) rm -f $(notdir $@); \ ln -s $(BBBIN) $(notdir $@) -./sbin/finit: $(FINITBIN) $(libs) - cp $< $@ - -./bin/initctl: $(INITCTLBIN) $(libs) - cp $< $@ - $(libs): mkdir -p $(dir $@) cp /$@ $@ clean: rm -f ./bin/$(BBBIN) - rm -f ./bin/initctl rm -rf $(binaries) rm -rf ./dev rm -rf ./etc diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index d4146e4a..68eb7e1b 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -12,7 +12,7 @@ assert_num_children() { # Test say 'Set up a service' -cp "$TEST_DIR"/test_start_stop_service/service.sh "$TEST_DIR"/test_root/test_assets/ +cp "$TEST_DIR"/test_start_stop_service/service.sh "$TESTS_ROOT"/test_assets/ texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" say 'Reload Finit' @@ -29,4 +29,4 @@ texec kill -SIGHUP 1 retry 'assert_num_children 0 service.sh' # Teardown -rm -f test_root/bin/service.sh +rm -f "$TESTS_ROOT"/bin/service.sh diff --git a/test/testenv_exec.sh b/test/testenv_exec.sh index 5243c142..cfb51053 100755 --- a/test/testenv_exec.sh +++ b/test/testenv_exec.sh @@ -11,7 +11,7 @@ target="$1" shift TEST_DIR=$(dirname "$0") -TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" +TESTS_ROOT="${TESTS_ROOT:-$(pwd)/${TEST_DIR}/test_root}" nsenter=$(command -v nsenter) chroot=$(command -v chroot) diff --git a/test/testenv_start.sh b/test/testenv_start.sh index 5c8cee8d..ebf7258c 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -3,7 +3,7 @@ set -eu TEST_DIR=$(dirname "$0") -TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" +TESTS_ROOT="${TESTS_ROOT:-$(pwd)/${TEST_DIR}/test_root}" >&2 echo "Hint: Execute '${TEST_DIR}/testenv_exec.sh \$(pgrep -P $$) /bin/sh; reset' to enter the test namespace" From f21409856036ec307f57dbb4752b44bbb72ae846 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Wed, 24 Feb 2021 13:13:38 +0100 Subject: [PATCH 54/90] test: Minor refactor to reduce test code duplication Signed-off-by: Magnus Malm --- test/lib.sh | 4 ++++ test/test_add_remove_dynamic_service/test.sh | 4 ---- test/test_add_remove_dynamic_service_sub_config/test.sh | 4 ---- test/test_start_stop_service/test.sh | 4 ---- test/test_start_stop_service_sub_config/test.sh | 4 ---- 5 files changed, 4 insertions(+), 16 deletions(-) diff --git a/test/lib.sh b/test/lib.sh index 82e6b1e2..fa38a54e 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -1,5 +1,9 @@ #!/bin/sh +assert_num_children() { + assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" +} + texec() { # shellcheck disable=SC2154 "$TEST_DIR/testenv_exec.sh" "$finit_pid" "$@" diff --git a/test/test_add_remove_dynamic_service/test.sh b/test/test_add_remove_dynamic_service/test.sh index 8637bc14..ed093131 100755 --- a/test/test_add_remove_dynamic_service/test.sh +++ b/test/test_add_remove_dynamic_service/test.sh @@ -6,10 +6,6 @@ TEST_DIR=$(dirname "$0")/.. # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" -assert_num_children() { - assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" -} - test_teardown() { say "Test done $(date)" say "Running test teardown..." diff --git a/test/test_add_remove_dynamic_service_sub_config/test.sh b/test/test_add_remove_dynamic_service_sub_config/test.sh index 97b82fc4..2e7196f4 100755 --- a/test/test_add_remove_dynamic_service_sub_config/test.sh +++ b/test/test_add_remove_dynamic_service_sub_config/test.sh @@ -6,10 +6,6 @@ TEST_DIR=$(dirname "$0")/.. # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" -assert_num_children() { - assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" -} - test_teardown() { say "Test done $(date)" say "Running test teardown..." diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index 7d0c2c5d..e1e21c4c 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -6,10 +6,6 @@ TEST_DIR=$(dirname "$0")/.. # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" -assert_num_children() { - assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" -} - test_teardown() { say "Test done $(date)" say "Running test teardown..." diff --git a/test/test_start_stop_service_sub_config/test.sh b/test/test_start_stop_service_sub_config/test.sh index 87026274..897592c4 100755 --- a/test/test_start_stop_service_sub_config/test.sh +++ b/test/test_start_stop_service_sub_config/test.sh @@ -6,10 +6,6 @@ TEST_DIR=$(dirname "$0")/.. # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" -assert_num_children() { - assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" -} - test_teardown() { say "Test done $(date)" say "Running test teardown..." From 2c055cb954b07b068499d60d6fe60afecf0235e7 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Wed, 24 Feb 2021 13:20:13 +0100 Subject: [PATCH 55/90] test: Minor adjustments to improve consistency between test code structure Signed-off-by: Magnus Malm --- test/lib.sh | 6 ++++++ test/test_add_remove_dynamic_service/test.sh | 13 ++++++++----- .../test.sh | 16 +++++++++------- test/test_start_stop_service/test.sh | 13 ++++++++----- test/test_start_stop_service_sub_config/test.sh | 14 ++++++++------ 5 files changed, 39 insertions(+), 23 deletions(-) diff --git a/test/lib.sh b/test/lib.sh index fa38a54e..52b0c227 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -4,6 +4,12 @@ assert_num_children() { assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" } +remove_thing() { + THING="$(realpath "$1")" + say "Remove ${THING#*/test/*}" + rm -f "$THING" +} + texec() { # shellcheck disable=SC2154 "$TEST_DIR/testenv_exec.sh" "$finit_pid" "$@" diff --git a/test/test_add_remove_dynamic_service/test.sh b/test/test_add_remove_dynamic_service/test.sh index ed093131..50eb2235 100755 --- a/test/test_add_remove_dynamic_service/test.sh +++ b/test/test_add_remove_dynamic_service/test.sh @@ -3,21 +3,24 @@ set -eu TEST_DIR=$(dirname "$0")/.. +TESTENV_ROOT="$TEST_DIR"/test_root + # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" test_teardown() { say "Test done $(date)" - say "Running test teardown..." + say "Running test teardown." - # Teardown - rm -f test_root/bin/service.sh + remove_thing "$TESTENV_ROOT/$FINIT_CONF" + remove_thing "$TESTENV_ROOT"/test_assets/service.sh } -# Test say "Test start $(date)" -say 'Add a dynamic service in /etc/finit.conf' + cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ + +say "Add a dynamic service in $FINIT_CONF" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" say 'Reload Finit' diff --git a/test/test_add_remove_dynamic_service_sub_config/test.sh b/test/test_add_remove_dynamic_service_sub_config/test.sh index 2e7196f4..0f756701 100755 --- a/test/test_add_remove_dynamic_service_sub_config/test.sh +++ b/test/test_add_remove_dynamic_service_sub_config/test.sh @@ -3,28 +3,30 @@ set -eu TEST_DIR=$(dirname "$0")/.. +TESTENV_ROOT="$TEST_DIR"/test_root + # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" test_teardown() { say "Test done $(date)" - say "Running test teardown..." + say "Running test teardown." - # Teardown - rm -f test_root/bin/service.sh + remove_thing "$TESTENV_ROOT/$FINIT_CONF_DIR/service.conf" + remove_thing "$TESTENV_ROOT"/test_assets/service.sh } -# Test say "Test start $(date)" -say "Add a dynamic service in $FINIT_CONF_DIR/service.conf" cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ + +say "Add a dynamic service in $FINIT_CONF_DIR/service.conf" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF_DIR/service.conf" say 'Reload Finit' texec kill -SIGHUP 1 -retry 'assert_num_children 1 service.sh' 5 +retry 'assert_num_children 1 service.sh' say 'Remove the dynamic service from /etc/finit.d/service.conf' texec sh -c "echo > $FINIT_CONF_DIR/service.conf" @@ -32,4 +34,4 @@ texec sh -c "echo > $FINIT_CONF_DIR/service.conf" say 'Reload Finit' texec kill -SIGHUP 1 -retry 'assert_num_children 0 service.sh' 5 +retry 'assert_num_children 0 service.sh' diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index e1e21c4c..e55553d8 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -3,21 +3,24 @@ set -eu TEST_DIR=$(dirname "$0")/.. +TESTENV_ROOT="$TEST_DIR"/test_root + # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" test_teardown() { say "Test done $(date)" - say "Running test teardown..." + say "Running test teardown." - # Teardown - rm -f test_root/bin/service.sh + remove_thing "$TESTENV_ROOT/$FINIT_CONF" + remove_thing "$TESTENV_ROOT"/test_assets/service.sh } -# Test say "Test start $(date)" -say 'Add service stanza in /etc/finit.conf' + cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ + +say "Add service stanza in $FINIT_CONF" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" say 'Reload Finit' diff --git a/test/test_start_stop_service_sub_config/test.sh b/test/test_start_stop_service_sub_config/test.sh index 897592c4..cfc684fb 100755 --- a/test/test_start_stop_service_sub_config/test.sh +++ b/test/test_start_stop_service_sub_config/test.sh @@ -3,22 +3,24 @@ set -eu TEST_DIR=$(dirname "$0")/.. +TESTENV_ROOT="$TEST_DIR"/test_root + # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" test_teardown() { say "Test done $(date)" - say "Running test teardown..." + say "Running test teardown." - # Teardown - rm -f test_root/bin/service.sh + remove_thing "$TESTENV_ROOT/$FINIT_CONF_DIR/service.conf" + remove_thing "$TESTENV_ROOT"/test_assets/service.sh } -# Test say "Test start $(date)" -say 'Add service stanza in /etc/finit.d/service.conf' + cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ -# texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" + +say "Add service stanza in $FINIT_CONF_DIR/service.conf" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF_DIR/service.conf" say 'Reload Finit' From e77f3f4d74eb668ffa888024e455740bda3c254b Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Wed, 24 Feb 2021 14:31:18 +0100 Subject: [PATCH 56/90] test: Use initctl in favor of kill -SIGHUP when reloading finit Signed-off-by: Magnus Malm --- test/test_add_remove_dynamic_service/test.sh | 4 ++-- test/test_add_remove_dynamic_service_sub_config/test.sh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test_add_remove_dynamic_service/test.sh b/test/test_add_remove_dynamic_service/test.sh index 50eb2235..f109196d 100755 --- a/test/test_add_remove_dynamic_service/test.sh +++ b/test/test_add_remove_dynamic_service/test.sh @@ -24,7 +24,7 @@ say "Add a dynamic service in $FINIT_CONF" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" say 'Reload Finit' -texec kill -SIGHUP 1 +texec sh -c "initctl reload" retry 'assert_num_children 1 service.sh' @@ -32,6 +32,6 @@ say 'Remove the dynamic service from /etc/finit.conf' texec sh -c "echo > $FINIT_CONF" say 'Reload Finit' -texec kill -SIGHUP 1 +texec sh -c "initctl reload" retry 'assert_num_children 0 service.sh' diff --git a/test/test_add_remove_dynamic_service_sub_config/test.sh b/test/test_add_remove_dynamic_service_sub_config/test.sh index 0f756701..a6c1f554 100755 --- a/test/test_add_remove_dynamic_service_sub_config/test.sh +++ b/test/test_add_remove_dynamic_service_sub_config/test.sh @@ -24,7 +24,7 @@ say "Add a dynamic service in $FINIT_CONF_DIR/service.conf" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF_DIR/service.conf" say 'Reload Finit' -texec kill -SIGHUP 1 +texec sh -c "initctl reload" retry 'assert_num_children 1 service.sh' @@ -32,6 +32,6 @@ say 'Remove the dynamic service from /etc/finit.d/service.conf' texec sh -c "echo > $FINIT_CONF_DIR/service.conf" say 'Reload Finit' -texec kill -SIGHUP 1 +texec sh -c "initctl reload" retry 'assert_num_children 0 service.sh' From 76b2a846a25e3e0d299fd1d7b65fd0794e97eed0 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Wed, 24 Feb 2021 14:54:45 +0100 Subject: [PATCH 57/90] test: Slightly improve names of test directories Signed-off-by: Magnus Malm --- test/Makefile.am | 14 +++++++------- .../test.sh | 4 ++-- .../test.sh | 4 ++-- .../test.sh | 4 ++-- .../test.sh | 4 ++-- test/{test_root => test-root}/.gitignore | 0 test/{test_root => test-root}/Makefile | 0 test/{test_root => test-root}/bin/chrootsetup.sh | 0 test/{test_root => test-root}/busybox-x86_64.md5 | 0 test/testenv_exec.sh | 2 +- test/testenv_start.sh | 2 +- 11 files changed, 17 insertions(+), 17 deletions(-) rename test/{test_add_remove_dynamic_service_sub_config => add-remove-dynamic-service-sub-config}/test.sh (88%) rename test/{test_add_remove_dynamic_service => add-remove-dynamic-service}/test.sh (87%) rename test/{test_start_stop_service_sub_config => start-stop-service-sub-config}/test.sh (88%) rename test/{test_start_stop_service => start-stop-service}/test.sh (88%) rename test/{test_root => test-root}/.gitignore (100%) rename test/{test_root => test-root}/Makefile (100%) rename test/{test_root => test-root}/bin/chrootsetup.sh (100%) rename test/{test_root => test-root}/busybox-x86_64.md5 (100%) diff --git a/test/Makefile.am b/test/Makefile.am index b7542fd0..dc090d99 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,10 +1,10 @@ -AM_TESTS_ENVIRONMENT = make --silent --no-print-directory -C test_root/; +AM_TESTS_ENVIRONMENT = make --silent --no-print-directory -C test-root/; -TESTS = checkself.sh -TESTS += test_add_remove_dynamic_service/test.sh -TESTS += test_add_remove_dynamic_service_sub_config/test.sh -TESTS += test_start_stop_service/test.sh -TESTS += test_start_stop_service_sub_config/test.sh +TESTS = checkself.sh +TESTS += add-remove-dynamic-service/test.sh +TESTS += add-remove-dynamic-service-sub-config/test.sh +TESTS += start-stop-service/test.sh +TESTS += start-stop-service-sub-config/test.sh clean-local: - make -C test_root/ clean + make -C test-root/ clean diff --git a/test/test_add_remove_dynamic_service_sub_config/test.sh b/test/add-remove-dynamic-service-sub-config/test.sh similarity index 88% rename from test/test_add_remove_dynamic_service_sub_config/test.sh rename to test/add-remove-dynamic-service-sub-config/test.sh index a6c1f554..b541ac9f 100755 --- a/test/test_add_remove_dynamic_service_sub_config/test.sh +++ b/test/add-remove-dynamic-service-sub-config/test.sh @@ -3,7 +3,7 @@ set -eu TEST_DIR=$(dirname "$0")/.. -TESTENV_ROOT="$TEST_DIR"/test_root +TESTENV_ROOT="$TEST_DIR"/test-root # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" @@ -18,7 +18,7 @@ test_teardown() { say "Test start $(date)" -cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test-root/test_assets/ say "Add a dynamic service in $FINIT_CONF_DIR/service.conf" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF_DIR/service.conf" diff --git a/test/test_add_remove_dynamic_service/test.sh b/test/add-remove-dynamic-service/test.sh similarity index 87% rename from test/test_add_remove_dynamic_service/test.sh rename to test/add-remove-dynamic-service/test.sh index f109196d..3abf3614 100755 --- a/test/test_add_remove_dynamic_service/test.sh +++ b/test/add-remove-dynamic-service/test.sh @@ -3,7 +3,7 @@ set -eu TEST_DIR=$(dirname "$0")/.. -TESTENV_ROOT="$TEST_DIR"/test_root +TESTENV_ROOT="$TEST_DIR"/test-root # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" @@ -18,7 +18,7 @@ test_teardown() { say "Test start $(date)" -cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test-root/test_assets/ say "Add a dynamic service in $FINIT_CONF" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" diff --git a/test/test_start_stop_service_sub_config/test.sh b/test/start-stop-service-sub-config/test.sh similarity index 88% rename from test/test_start_stop_service_sub_config/test.sh rename to test/start-stop-service-sub-config/test.sh index cfc684fb..f2819679 100755 --- a/test/test_start_stop_service_sub_config/test.sh +++ b/test/start-stop-service-sub-config/test.sh @@ -3,7 +3,7 @@ set -eu TEST_DIR=$(dirname "$0")/.. -TESTENV_ROOT="$TEST_DIR"/test_root +TESTENV_ROOT="$TEST_DIR"/test-root # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" @@ -18,7 +18,7 @@ test_teardown() { say "Test start $(date)" -cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test-root/test_assets/ say "Add service stanza in $FINIT_CONF_DIR/service.conf" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF_DIR/service.conf" diff --git a/test/test_start_stop_service/test.sh b/test/start-stop-service/test.sh similarity index 88% rename from test/test_start_stop_service/test.sh rename to test/start-stop-service/test.sh index e55553d8..0e64ce46 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/start-stop-service/test.sh @@ -3,7 +3,7 @@ set -eu TEST_DIR=$(dirname "$0")/.. -TESTENV_ROOT="$TEST_DIR"/test_root +TESTENV_ROOT="$TEST_DIR"/test-root # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" @@ -18,7 +18,7 @@ test_teardown() { say "Test start $(date)" -cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test_root/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TEST_DIR"/test-root/test_assets/ say "Add service stanza in $FINIT_CONF" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" diff --git a/test/test_root/.gitignore b/test/test-root/.gitignore similarity index 100% rename from test/test_root/.gitignore rename to test/test-root/.gitignore diff --git a/test/test_root/Makefile b/test/test-root/Makefile similarity index 100% rename from test/test_root/Makefile rename to test/test-root/Makefile diff --git a/test/test_root/bin/chrootsetup.sh b/test/test-root/bin/chrootsetup.sh similarity index 100% rename from test/test_root/bin/chrootsetup.sh rename to test/test-root/bin/chrootsetup.sh diff --git a/test/test_root/busybox-x86_64.md5 b/test/test-root/busybox-x86_64.md5 similarity index 100% rename from test/test_root/busybox-x86_64.md5 rename to test/test-root/busybox-x86_64.md5 diff --git a/test/testenv_exec.sh b/test/testenv_exec.sh index 5243c142..b6306838 100755 --- a/test/testenv_exec.sh +++ b/test/testenv_exec.sh @@ -11,7 +11,7 @@ target="$1" shift TEST_DIR=$(dirname "$0") -TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" +TESTS_ROOT="$(pwd)/${TEST_DIR}/test-root" nsenter=$(command -v nsenter) chroot=$(command -v chroot) diff --git a/test/testenv_start.sh b/test/testenv_start.sh index 7dcf2869..7f0d97f0 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -3,7 +3,7 @@ set -eu TEST_DIR=$(dirname "$0") -TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" +TESTS_ROOT="$(pwd)/${TEST_DIR}/test-root" unshare=$(command -v unshare) chroot=$(command -v chroot) From a5ef5f3104303ac5dfac6af4fa532940d7ff3adc Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Wed, 24 Feb 2021 16:01:43 +0100 Subject: [PATCH 58/90] test: Fix make check Signed-off-by: Jacques de Laval --- test/Makefile.am | 2 ++ test/lib.sh | 6 ++---- test/setup_root.sh | 28 ++++++++++++++++++++-------- test/teardown_root.sh | 1 + test/test_root/Makefile | 1 - test/test_root/bin/chrootsetup.sh | 2 +- test/testenv_exec.sh | 1 - test/testenv_start.sh | 1 - 8 files changed, 26 insertions(+), 16 deletions(-) diff --git a/test/Makefile.am b/test/Makefile.am index 992c68c3..ba325e35 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -10,6 +10,8 @@ EXTRA_DIST += test_start_stop_service/service.sh AM_TESTS_ENVIRONMENT = TESTS_ROOT='$(builddir)/test_root/'; AM_TESTS_ENVIRONMENT += export TESTS_ROOT; AM_TESTS_ENVIRONMENT += export LD_LIBRARY_PATH; +AM_TESTS_ENVIRONMENT += PREFIX=$(prefix); +AM_TESTS_ENVIRONMENT += export PREFIX; TESTS = checkself.sh TESTS += setup_root.sh diff --git a/test/lib.sh b/test/lib.sh index 7902bae1..8356976f 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -84,10 +84,8 @@ teardown() { trap teardown EXIT -FINIT_CONF=$(grep FINIT_CONF "$TESTS_ROOT/../config.h" | cut -d' ' -f3 | cut -d'"' -f2) -export FINIT_CONF -FINIT_CONF_DIR="$(dirname "$FINIT_CONF")"/finit.d -export FINIT_CONF_DIR +# shellcheck source=/dev/null +. "$TESTS_ROOT/../test.env" log "$color_reset" 'Test start' '' log "$color_reset" '--' '' diff --git a/test/setup_root.sh b/test/setup_root.sh index a00f1244..a1a4d5ae 100755 --- a/test/setup_root.sh +++ b/test/setup_root.sh @@ -3,13 +3,25 @@ set -eu # shellcheck disable=SC2154 -"$srcdir"/../configure --prefix=/ -make -make DESTDIR="$(pwd)/test_root/" install +if [ ! -f "$srcdir"/../config.status ]; then + "$srcdir"/../configure --prefix=/ + make + make DESTDIR="$(pwd)/test_root/" install -mkdir -p test_root/bin -cp "${srcdir}/test_root/bin/chrootsetup.sh" test_root/bin -cp "${srcdir}/test_root/busybox-x86_64.md5" test_root/ -cp "${srcdir}/test_root/Makefile" test_root/ + mkdir -p test_root/bin + cp "${srcdir}/test_root/bin/chrootsetup.sh" test_root/bin + cp "${srcdir}/test_root/busybox-x86_64.md5" test_root/ + cp "${srcdir}/test_root/Makefile" test_root/ + make -C test_root/ + config_h='./config.h' + echo "export PATH=/bin:/sbin" > test.env +else + # make + make -C ../ DESTDIR="$(pwd)/test_root/" install + FINITBIN=../../src/finit make -C test_root/ + config_h='../config.h' + echo "export PATH=/bin:/sbin:$PREFIX/sbin:$PREFIX/bin" > test.env +fi -make -C test_root/ +echo "export FINIT_CONF=$(grep FINIT_CONF $config_h | cut -d' ' -f3 | cut -d'"' -f2)" >> test.env +echo "export FINIT_RCSD=$(grep FINIT_RCSD $config_h | cut -d' ' -f3 | cut -d'"' -f2)" >> test.env diff --git a/test/teardown_root.sh b/test/teardown_root.sh index 5cf2eb1d..e0649cce 100755 --- a/test/teardown_root.sh +++ b/test/teardown_root.sh @@ -6,3 +6,4 @@ if [ -d test_root/var/lock ]; then chmod +r test_root/var/lock fi make -C test_root/ clean +rm -f test.env diff --git a/test/test_root/Makefile b/test/test_root/Makefile index 99b23c2e..b77947e9 100644 --- a/test/test_root/Makefile +++ b/test/test_root/Makefile @@ -3,7 +3,6 @@ ARCH ?= x86_64 FINITBIN ?= ./sbin/finit -INITCTLBIN ?= ./sbin/initctl BBVER ?= 1.31.0 BBBIN = busybox-$(ARCH) diff --git a/test/test_root/bin/chrootsetup.sh b/test/test_root/bin/chrootsetup.sh index fe4e0e4c..e4810294 100755 --- a/test/test_root/bin/chrootsetup.sh +++ b/test/test_root/bin/chrootsetup.sh @@ -22,7 +22,7 @@ mount -t tmpfs none /etc # mkdir -p /dev/pts # mount -t devpts none /dev/pts -mkdir -p "$FINIT_CONF_DIR" +mkdir -p "$FINIT_RCSD" tty=/dev/$(cat /sys/class/tty/console/active) mkfifo "$tty" diff --git a/test/testenv_exec.sh b/test/testenv_exec.sh index cfb51053..72e1db70 100755 --- a/test/testenv_exec.sh +++ b/test/testenv_exec.sh @@ -16,7 +16,6 @@ TESTS_ROOT="${TESTS_ROOT:-$(pwd)/${TEST_DIR}/test_root}" nsenter=$(command -v nsenter) chroot=$(command -v chroot) -export PATH=/sbin:/bin export PS1='\w \$ ' export PS2='> ' export PS3='#? ' diff --git a/test/testenv_start.sh b/test/testenv_start.sh index ebf7258c..04278de6 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -10,7 +10,6 @@ TESTS_ROOT="${TESTS_ROOT:-$(pwd)/${TEST_DIR}/test_root}" unshare=$(command -v unshare) chroot=$(command -v chroot) -export PATH=/sbin:/bin export PS1='\w \$ ' export PS2='> ' export PS3='#? ' From 827f8262012f05aa3fb063fe29bf2df86b1149a3 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Wed, 24 Feb 2021 16:34:00 +0100 Subject: [PATCH 59/90] test: Remove commented code Signed-off-by: Jacques de Laval --- test/setup_root.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/test/setup_root.sh b/test/setup_root.sh index a1a4d5ae..4666d9d7 100755 --- a/test/setup_root.sh +++ b/test/setup_root.sh @@ -16,7 +16,6 @@ if [ ! -f "$srcdir"/../config.status ]; then config_h='./config.h' echo "export PATH=/bin:/sbin" > test.env else - # make make -C ../ DESTDIR="$(pwd)/test_root/" install FINITBIN=../../src/finit make -C test_root/ config_h='../config.h' From 70964ccb35ad5f04ee86ee8f5f8aa3b3bf85b798 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Wed, 24 Feb 2021 23:28:47 +0100 Subject: [PATCH 60/90] test: Only mess with PATH inside test environment --- test/setup-root.sh | 4 ++-- test/testenv_enter.sh | 3 +++ test/testenv_exec.sh | 3 +++ test/testenv_start.sh | 3 +++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/test/setup-root.sh b/test/setup-root.sh index 7f9ead5a..bc88fe0d 100755 --- a/test/setup-root.sh +++ b/test/setup-root.sh @@ -14,12 +14,12 @@ if [ ! -f "$srcdir"/../config.status ]; then cp "${srcdir}/test-root/Makefile" test-root/ make -C test-root/ config_h='./config.h' - echo "export PATH=/bin:/sbin" > test.env + echo "export TESTENV_PATH=/bin:/sbin" > test.env else make -C ../ DESTDIR="$(pwd)/test-root/" install FINITBIN=../../src/finit make -C test-root/ config_h='../config.h' - echo "export PATH=/bin:/sbin:$PREFIX/sbin:$PREFIX/bin" > test.env + echo "export TESTENV_PATH=/bin:/sbin:$PREFIX/sbin:$PREFIX/bin" > test.env fi echo "export FINIT_CONF=$(grep FINIT_CONF $config_h | cut -d' ' -f3 | cut -d'"' -f2)" >> test.env diff --git a/test/testenv_enter.sh b/test/testenv_enter.sh index 8a04fa43..ef385481 100755 --- a/test/testenv_enter.sh +++ b/test/testenv_enter.sh @@ -20,4 +20,7 @@ else exit 1 fi +PATH="$TESTENV_PATH" +export PATH + "$(dirname "$0")"/testenv_exec.sh "$target" "$cmd" "$@" diff --git a/test/testenv_exec.sh b/test/testenv_exec.sh index bb2494e1..aa0aafda 100755 --- a/test/testenv_exec.sh +++ b/test/testenv_exec.sh @@ -21,6 +21,9 @@ export PS2='> ' export PS3='#? ' export PS4='+ ' +PATH="$TESTENV_PATH" +export PATH + "$nsenter" \ --preserve-credentials \ --user \ diff --git a/test/testenv_start.sh b/test/testenv_start.sh index fb384027..cd1ea7c7 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -13,6 +13,9 @@ export PS2='> ' export PS3='#? ' export PS4='+ ' +PATH="$TESTENV_PATH" +export PATH + # Not supported by Busybox unshare: # --cgroup --time exec "$unshare" \ From 691bc58f0e4104a49dc59d8728aea29d28fe8d46 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 25 Feb 2021 00:53:30 +0100 Subject: [PATCH 61/90] test: Add support for starting Finit in debug mode Signed-off-by: Jacques de Laval --- test/lib.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/lib.sh b/test/lib.sh index 228680eb..f6db9dfd 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -16,7 +16,7 @@ pause() { toggle_finit_debug() { say 'Toggle finit debug' - texec sh -c "/bin/initctl debug" + texec initctl debug sleep 0.5 } @@ -104,7 +104,11 @@ export TEST_NAME # Setup test environment # Setup test environment -"$TEST_DIR/testenv_start.sh" finit & +if [ -n "${DEBUG:-}" ]; then + FINIT_ARGS="${FINIT_ARGS:-} finit.debug=on" +fi +# shellcheck disable=2086 +"$TEST_DIR/testenv_start.sh" finit ${FINIT_ARGS:-} & finit_ppid=$! echo "$finit_ppid" > "$TESTS_ROOT"/running_test.pid From 3c32cbfd9edfc7f3c2139fb56c13782198c2f3c9 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 25 Feb 2021 11:14:49 +0100 Subject: [PATCH 62/90] test: Make /var/lock readable in test teardown, so that it can be removed later Signed-off-by: Jacques de Laval --- test/lib.sh | 4 ++++ test/teardown-root.sh | 3 --- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/lib.sh b/test/lib.sh index f6db9dfd..ff021e12 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -89,6 +89,10 @@ teardown() { fi wait + + if [ -d "$TESTS_ROOT/var/lock" ]; then + chmod +r test-root/var/lock + fi rm -f "$TESTS_ROOT"/running_test.pid } diff --git a/test/teardown-root.sh b/test/teardown-root.sh index 88609938..664c481c 100755 --- a/test/teardown-root.sh +++ b/test/teardown-root.sh @@ -2,8 +2,5 @@ set -eu -if [ -d test-root/var/lock ]; then - chmod +r test-root/var/lock -fi make -C test-root/ clean rm -f test.env From cde580ffce6993e933f2e1fda49ae467dc7e61cb Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 25 Feb 2021 11:18:12 +0100 Subject: [PATCH 63/90] test: Unify setup-root.sh for make check and make distcheck Signed-off-by: Jacques de Laval --- test/Makefile.am | 4 ++++ test/setup-root.sh | 26 ++++++++++---------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/test/Makefile.am b/test/Makefile.am index d8cf90c7..23c6de37 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -14,6 +14,10 @@ AM_TESTS_ENVIRONMENT += export TESTS_ROOT; AM_TESTS_ENVIRONMENT += export LD_LIBRARY_PATH; AM_TESTS_ENVIRONMENT += PREFIX=$(prefix); AM_TESTS_ENVIRONMENT += export PREFIX; +AM_TESTS_ENVIRONMENT += top_srcdir=$(top_srcdir); +AM_TESTS_ENVIRONMENT += export top_srcdir; +AM_TESTS_ENVIRONMENT += top_builddir=$(top_builddir); +AM_TESTS_ENVIRONMENT += export top_builddir; TESTS = checkself.sh TESTS += setup-root.sh diff --git a/test/setup-root.sh b/test/setup-root.sh index bc88fe0d..e7c99427 100755 --- a/test/setup-root.sh +++ b/test/setup-root.sh @@ -2,25 +2,19 @@ set -eu -# shellcheck disable=SC2154 -if [ ! -f "$srcdir"/../config.status ]; then - "$srcdir"/../configure --prefix=/ - make - make DESTDIR="$(pwd)/test-root/" install - +if [ ! -d test-root ]; then mkdir -p test-root/bin + # shellcheck disable=SC2154 cp "${srcdir}/test-root/bin/chrootsetup.sh" test-root/bin cp "${srcdir}/test-root/busybox-x86_64.md5" test-root/ cp "${srcdir}/test-root/Makefile" test-root/ - make -C test-root/ - config_h='./config.h' - echo "export TESTENV_PATH=/bin:/sbin" > test.env -else - make -C ../ DESTDIR="$(pwd)/test-root/" install - FINITBIN=../../src/finit make -C test-root/ - config_h='../config.h' - echo "export TESTENV_PATH=/bin:/sbin:$PREFIX/sbin:$PREFIX/bin" > test.env fi -echo "export FINIT_CONF=$(grep FINIT_CONF $config_h | cut -d' ' -f3 | cut -d'"' -f2)" >> test.env -echo "export FINIT_RCSD=$(grep FINIT_RCSD $config_h | cut -d' ' -f3 | cut -d'"' -f2)" >> test.env +# shellcheck disable=SC2154 +make -C "$top_builddir" DESTDIR="$(pwd)/test-root/" install +FINITBIN="$(pwd)/$top_builddir/src/finit" make -C test-root/ # DESTDIR="$(pwd)/test-root/" +config_h="$top_builddir/config.h" +echo "export TESTENV_PATH=/bin:/sbin:$PREFIX/sbin:$PREFIX/bin" > test.env + +echo "export FINIT_CONF=$(grep FINIT_CONF "$config_h" | cut -d' ' -f3 | cut -d'"' -f2)" >> test.env +echo "export FINIT_RCSD=$(grep FINIT_RCSD "$config_h" | cut -d' ' -f3 | cut -d'"' -f2)" >> test.env From 48b732e53d4392a9a2107cba88074f3730be812d Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 25 Feb 2021 17:25:46 +0100 Subject: [PATCH 64/90] test: Let Autoconf generate env vars for tests This simplifies the setup script a lot, and also makes it easier to run tests independently. Signed-off-by: Jacques de Laval --- configure.ac | 3 +++ test/.gitignore | 2 ++ test/Makefile.am | 8 ++++---- test/checkself.sh | 2 +- test/setup-root.sh | 7 +------ test/teardown-root.sh | 6 ------ test/test.env.in | 6 ++++++ 7 files changed, 17 insertions(+), 17 deletions(-) delete mode 100755 test/teardown-root.sh create mode 100644 test/test.env.in diff --git a/configure.ac b/configure.ac index beeac4e7..f2eb4988 100644 --- a/configure.ac +++ b/configure.ac @@ -12,6 +12,7 @@ AC_CONFIG_FILES([Makefile doc/Makefile plugins/Makefile src/Makefile + test/test.env test/Makefile]) # Older versions of autoconf (<2.58) do not have AC_CONFIG_MACRO_DIR() @@ -101,10 +102,12 @@ AC_ARG_WITH(heading, AC_ARG_WITH(config, AS_HELP_STRING([--with-config=CONFIG], [Finit bootstrap config file, default /etc/finit.conf]), [conf=$withval], [conf=$sysconfdir/finit.conf]) +AC_SUBST(FINIT_CONF, $conf) AC_ARG_WITH(rcsd, AS_HELP_STRING([--with-rcsd=DIR], [Finit rcS.d/ directory, default /etc/finit.d]), [rcsd=$withval], [rcsd=$sysconfdir/finit.d]) +AC_SUBST(FINIT_RCSD, $rcsd) AC_ARG_WITH(rc-local, AS_HELP_STRING([--with-rc-local=FILE], [SysV init /etc/rc.local file, default /etc/rc.local]), diff --git a/test/.gitignore b/test/.gitignore index 151cdadf..1eb95bc4 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -3,3 +3,5 @@ *.trs /Makefile /Makefile.in + +/test.env diff --git a/test/Makefile.am b/test/Makefile.am index 23c6de37..91a9bbc0 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,7 +1,7 @@ EXTRA_DIST = lib.sh testenv_exec.sh testenv_start.sh EXTRA_DIST += test-root/Makefile test-root/busybox-x86_64.md5 EXTRA_DIST += test-root/bin/chrootsetup.sh -EXTRA_DIST += setup-root.sh teardown-root.sh +EXTRA_DIST += setup-root.sh EXTRA_DIST += common/service.conf common/service.sh EXTRA_DIST += checkself.sh EXTRA_DIST += add-remove-dynamic-service/test.sh @@ -12,8 +12,6 @@ EXTRA_DIST += start-stop-service-sub-config/test.sh AM_TESTS_ENVIRONMENT = TESTS_ROOT='$(abs_builddir)/test-root/'; AM_TESTS_ENVIRONMENT += export TESTS_ROOT; AM_TESTS_ENVIRONMENT += export LD_LIBRARY_PATH; -AM_TESTS_ENVIRONMENT += PREFIX=$(prefix); -AM_TESTS_ENVIRONMENT += export PREFIX; AM_TESTS_ENVIRONMENT += top_srcdir=$(top_srcdir); AM_TESTS_ENVIRONMENT += export top_srcdir; AM_TESTS_ENVIRONMENT += top_builddir=$(top_builddir); @@ -25,4 +23,6 @@ TESTS += add-remove-dynamic-service/test.sh TESTS += add-remove-dynamic-service-sub-config/test.sh TESTS += start-stop-service/test.sh TESTS += start-stop-service-sub-config/test.sh -TESTS += teardown-root.sh + +clean-local: + make -C test-root/ clean diff --git a/test/checkself.sh b/test/checkself.sh index b42091e9..8d75d8fb 100755 --- a/test/checkself.sh +++ b/test/checkself.sh @@ -5,4 +5,4 @@ set -eu testdir="${srcdir:-$(dirname "$0")}" # shellcheck disable=SC2046 -shellcheck $(find "$testdir" -name '*.sh') +shellcheck $(find "$testdir" -path "$testdir/test-root" -prune -false -o -name '*.sh') diff --git a/test/setup-root.sh b/test/setup-root.sh index e7c99427..ddc18df4 100755 --- a/test/setup-root.sh +++ b/test/setup-root.sh @@ -12,9 +12,4 @@ fi # shellcheck disable=SC2154 make -C "$top_builddir" DESTDIR="$(pwd)/test-root/" install -FINITBIN="$(pwd)/$top_builddir/src/finit" make -C test-root/ # DESTDIR="$(pwd)/test-root/" -config_h="$top_builddir/config.h" -echo "export TESTENV_PATH=/bin:/sbin:$PREFIX/sbin:$PREFIX/bin" > test.env - -echo "export FINIT_CONF=$(grep FINIT_CONF "$config_h" | cut -d' ' -f3 | cut -d'"' -f2)" >> test.env -echo "export FINIT_RCSD=$(grep FINIT_RCSD "$config_h" | cut -d' ' -f3 | cut -d'"' -f2)" >> test.env +FINITBIN="$(pwd)/$top_builddir/src/finit" make -C test-root/ diff --git a/test/teardown-root.sh b/test/teardown-root.sh deleted file mode 100755 index 664c481c..00000000 --- a/test/teardown-root.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh - -set -eu - -make -C test-root/ clean -rm -f test.env diff --git a/test/test.env.in b/test/test.env.in new file mode 100644 index 00000000..356e06df --- /dev/null +++ b/test/test.env.in @@ -0,0 +1,6 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ + +export TESTENV_PATH=/bin:/sbin:"@bindir@":"@sbindir@" +export FINIT_CONF="@FINIT_CONF@" +export FINIT_RCSD="@FINIT_RCSD@" From ea4fe2045d125e5ba2d4f7bbbf78e8bfebfe84b1 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Thu, 25 Feb 2021 17:38:56 +0100 Subject: [PATCH 65/90] test: Add sane default for TESTENV_ROOT; enable easier independent test runs Signed-off-by: Jacques de Laval --- test/Makefile.am | 4 ++-- test/add-remove-dynamic-service-sub-config/test.sh | 2 +- test/add-remove-dynamic-service/test.sh | 2 +- test/lib.sh | 11 +++++++---- test/start-stop-service-sub-config/test.sh | 2 +- test/start-stop-service/test.sh | 2 +- test/testenv_exec.sh | 4 ++-- test/testenv_start.sh | 4 ++-- 8 files changed, 17 insertions(+), 14 deletions(-) diff --git a/test/Makefile.am b/test/Makefile.am index 91a9bbc0..43445ee5 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -9,8 +9,8 @@ EXTRA_DIST += add-remove-dynamic-service-sub-config/test.sh EXTRA_DIST += start-stop-service/test.sh EXTRA_DIST += start-stop-service-sub-config/test.sh -AM_TESTS_ENVIRONMENT = TESTS_ROOT='$(abs_builddir)/test-root/'; -AM_TESTS_ENVIRONMENT += export TESTS_ROOT; +AM_TESTS_ENVIRONMENT = TESTENV_ROOT='$(abs_builddir)/test-root/'; +AM_TESTS_ENVIRONMENT += export TESTENV_ROOT; AM_TESTS_ENVIRONMENT += export LD_LIBRARY_PATH; AM_TESTS_ENVIRONMENT += top_srcdir=$(top_srcdir); AM_TESTS_ENVIRONMENT += export top_srcdir; diff --git a/test/add-remove-dynamic-service-sub-config/test.sh b/test/add-remove-dynamic-service-sub-config/test.sh index af90b983..7e319470 100755 --- a/test/add-remove-dynamic-service-sub-config/test.sh +++ b/test/add-remove-dynamic-service-sub-config/test.sh @@ -17,7 +17,7 @@ test_teardown() { say "Test start $(date)" -cp "$TEST_DIR"/common/service.sh "$TESTS_ROOT"/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TESTENV_ROOT"/test_assets/ say "Add a dynamic service in $FINIT_RCSD/service.conf" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_RCSD/service.conf" diff --git a/test/add-remove-dynamic-service/test.sh b/test/add-remove-dynamic-service/test.sh index e4b19c66..fc17f5ff 100755 --- a/test/add-remove-dynamic-service/test.sh +++ b/test/add-remove-dynamic-service/test.sh @@ -17,7 +17,7 @@ test_teardown() { say "Test start $(date)" -cp "$TEST_DIR"/common/service.sh "$TESTS_ROOT"/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TESTENV_ROOT"/test_assets/ say "Add a dynamic service in $FINIT_CONF" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" diff --git a/test/lib.sh b/test/lib.sh index ff021e12..282c51ab 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -90,16 +90,19 @@ teardown() { wait - if [ -d "$TESTS_ROOT/var/lock" ]; then + if [ -d "$TESTENV_ROOT/var/lock" ]; then chmod +r test-root/var/lock fi - rm -f "$TESTS_ROOT"/running_test.pid + rm -f "$TESTENV_ROOT"/running_test.pid } trap teardown EXIT +TESTENV_ROOT="${TESTENV_ROOT:-$(pwd)/${TEST_DIR}/test-root}" +export TESTENV_ROOT + # shellcheck source=/dev/null -. "$TESTS_ROOT/../test.env" +. "$TESTENV_ROOT/../test.env" TEST_NAME="$(dirname "$0")" TEST_NAME=${TEST_NAME#*/} @@ -114,7 +117,7 @@ fi # shellcheck disable=2086 "$TEST_DIR/testenv_start.sh" finit ${FINIT_ARGS:-} & finit_ppid=$! -echo "$finit_ppid" > "$TESTS_ROOT"/running_test.pid +echo "$finit_ppid" > "$TESTENV_ROOT"/running_test.pid >&2 echo "Hint: Execute 'test/testenv_enter.sh' to enter the test namespace" >&2 echo "finit conf '$FINIT_CONF'" diff --git a/test/start-stop-service-sub-config/test.sh b/test/start-stop-service-sub-config/test.sh index 0fe91094..80585ec5 100755 --- a/test/start-stop-service-sub-config/test.sh +++ b/test/start-stop-service-sub-config/test.sh @@ -17,7 +17,7 @@ test_teardown() { say "Test start $(date)" -cp "$TEST_DIR"/common/service.sh "$TESTS_ROOT"/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TESTENV_ROOT"/test_assets/ say "Add service stanza in $FINIT_RCSD/service.conf" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_RCSD/service.conf" diff --git a/test/start-stop-service/test.sh b/test/start-stop-service/test.sh index 8125f6df..c429a695 100755 --- a/test/start-stop-service/test.sh +++ b/test/start-stop-service/test.sh @@ -17,7 +17,7 @@ test_teardown() { say "Test start $(date)" -cp "$TEST_DIR"/common/service.sh "$TESTS_ROOT"/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TESTENV_ROOT"/test_assets/ say "Add service stanza in $FINIT_CONF" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" diff --git a/test/testenv_exec.sh b/test/testenv_exec.sh index aa0aafda..b467076e 100755 --- a/test/testenv_exec.sh +++ b/test/testenv_exec.sh @@ -11,7 +11,7 @@ target="$1" shift TEST_DIR=$(dirname "$0") -TESTS_ROOT="${TESTS_ROOT:-$(pwd)/${TEST_DIR}/test-root}" +TESTENV_ROOT="${TESTENV_ROOT:-$(pwd)/${TEST_DIR}/test-root}" nsenter=$(command -v nsenter) chroot=$(command -v chroot) @@ -33,4 +33,4 @@ export PATH --net \ --pid \ -w -t "$target" \ - "$chroot" "$TESTS_ROOT" "$@" + "$chroot" "$TESTENV_ROOT" "$@" diff --git a/test/testenv_start.sh b/test/testenv_start.sh index cd1ea7c7..ec824ed9 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -3,7 +3,7 @@ set -eu TEST_DIR=$(dirname "$0") -TESTS_ROOT="${TESTS_ROOT:-$(pwd)/${TEST_DIR}/test-root}" +TESTENV_ROOT="${TESTENV_ROOT:-$(pwd)/${TEST_DIR}/test-root}" unshare=$(command -v unshare) chroot=$(command -v chroot) @@ -24,4 +24,4 @@ exec "$unshare" \ --mount \ --mount-proc \ --uts --ipc --net \ - "$chroot" "$TESTS_ROOT" /bin/chrootsetup.sh "$@" + "$chroot" "$TESTENV_ROOT" /bin/chrootsetup.sh "$@" From 92c6de2c090b984e9706c0c6f171ae5f2b423950 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 10:13:44 +0100 Subject: [PATCH 66/90] test: tests use .sh extension, for .log/.trs transformation Signed-off-by: Joachim Wiberg --- test/Makefile.am | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Makefile.am b/test/Makefile.am index 43445ee5..8f563b2e 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -17,6 +17,7 @@ AM_TESTS_ENVIRONMENT += export top_srcdir; AM_TESTS_ENVIRONMENT += top_builddir=$(top_builddir); AM_TESTS_ENVIRONMENT += export top_builddir; +TEST_EXTENSIONS = .sh TESTS = checkself.sh TESTS += setup-root.sh TESTS += add-remove-dynamic-service/test.sh From 435211765c2102c40109ddcd36b1063c08c5af5f Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 17:14:26 +0100 Subject: [PATCH 67/90] Follow-up to f4a0b99: track modifications of service env: files Add support for monitoring environment files for services declared with the `env:[-]/path/to/file` option. The default path to such files has been chosen to follow Debian and Buildroot /etc/default/* Signed-off-by: Joachim Wiberg --- configure.ac | 12 ++++++++++++ doc/config.md | 28 ++++++++++++++++++++++++++++ src/conf.c | 20 ++++++++++++++------ src/service.c | 2 +- 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/configure.ac b/configure.ac index f2eb4988..3b08d5d8 100644 --- a/configure.ac +++ b/configure.ac @@ -113,6 +113,10 @@ AC_ARG_WITH(rc-local, AS_HELP_STRING([--with-rc-local=FILE], [SysV init /etc/rc.local file, default /etc/rc.local]), [rclocal=$withval], [rclocal=$sysconfdir/rc.local]) +AC_ARG_WITH(sysconfig, + AS_HELP_STRING([--with-sysconfig=DIR], [Enviroment config directory, default /etc/default]), + [sysconfig=$withval], [sysconfig=yes]) + AC_ARG_WITH(hostname, AS_HELP_STRING([--with-hostname=NAME], [If /etc/hostname is missing, default: noname]), [hostname=$withval], [hostname=noname]) @@ -169,6 +173,13 @@ AS_IF([test "x$with_rc_local" != "xno"], [ AC_EXPAND_DIR(rclocal_path, "$rclocal") AC_DEFINE_UNQUOTED(FINIT_RC_LOCAL, "$rclocal_path", [Compat SysV /etc/rc.local file to run last at boot.]) +AS_IF([test "x$with_sysconfig" != "xno"], [ + AS_IF([test "x$sysconfig" = "xyes"], [ + sysconfig=$sysconfdir/default + AC_EXPAND_DIR(sysconfig_path, "$sysconfig") + AC_DEFINE_UNQUOTED(FINIT_SYSCONFIG, "$sysconfig_path", + [System environment, default /etc/default])])]) + AS_IF([test "x$with_heading" != "xno"], [ AS_IF([test "x$heading" = "xyes"], [ heading="$PACKAGE_NAME v$PACKAGE_VERSION"])]) @@ -225,6 +236,7 @@ cat < examples in the `contrib/` directory, which can be used as a base. +Service Environment +------------------- + +Finit supports sourcing environment variables from `/etc/default/*`, or +similar `--with-sysconfig=DIR`. This is a common pattern from SysV init +scripts, where the start-stop script is a generic script for the given +service, `foo`, and the options for the service are sourced from the +file `/etc/default/foo`. Like this: + +* `/etc/default/foo`: + + FOO_OPTIONS=--extra-arg="bar" -s -x + +* `/etc/finit.conf`: + + service [2345] env:-/etc/default/foo foo -n $FOO_OPTIONS -- Example foo daemon + +Here the service `foo` is started with `-n`, to make sure it runs in the +foreground, and the with the options found in the environment file. With +the `ps` command we can see that the process is started with: + + foo -n --extra-arg=bar -s -x + +> **Note:** the leading `-` determines if Finit should treat a missing +> environment file as blocking the start of the service or not. When +> `-` is used, a missing environment file does *not* block the start. + + Service Wrapper Scripts ----------------------- diff --git a/src/conf.c b/src/conf.c index e866db7e..32c6b612 100644 --- a/src/conf.c +++ b/src/conf.c @@ -772,13 +772,13 @@ static void drop_changes(void) static int do_change(char *dir, char *name, uint32_t mask) { - char path[strlen(dir) + strlen(name) + 2]; + char fn[strlen(dir) + strlen(name) + 2]; struct conf_change *node; - snprintf(path, sizeof(path), "%s%s", dir, name); - _d("path: %s", path); + snprintf(fn, sizeof(fn), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); + _d("path: %s mask: %08x", fn, mask); - node = conf_find(path); + node = conf_find(fn); if (mask & (IN_DELETE | IN_MOVED_FROM)) { drop_change(node); return 0; @@ -793,13 +793,13 @@ static int do_change(char *dir, char *name, uint32_t mask) if (!node) return 1; - node->name = strdup(path); + node->name = strdup(fn); if (!node->name) { free(node); return 1; } - _d("Event registered for %s, mask 0x%x", path, mask); + _d("Event registered for %s, mask 0x%x", fn, mask); TAILQ_INSERT_HEAD(&conf_change_list, node, link); return 0; @@ -881,6 +881,14 @@ int conf_monitor(void) rc += iwatch_add(&iw_conf, FINIT_RCSD "/enabled/", IN_ONLYDIR | IN_DONT_FOLLOW); rc += iwatch_add(&iw_conf, FINIT_CONF, 0); + /* + * Systems with /etc/default, or similar, can also monitor changes + * in env files sourced by .conf files (above) + */ +#ifdef FINIT_SYSCONFIG + rc += iwatch_add(&iw_conf, FINIT_SYSCONFIG, IN_ONLYDIR); +#endif + return rc + conf_reload(); } diff --git a/src/service.c b/src/service.c index 7ba261e4..116f1528 100644 --- a/src/service.c +++ b/src/service.c @@ -1116,7 +1116,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) memcpy(svc->rlimit, rlimit, sizeof(svc->rlimit)); /* New, recently modified or unchanged ... used on reload. */ - if (file && conf_changed(file)) + if ((file && conf_changed(file)) || conf_changed(svc_getenv(svc))) svc_mark_dirty(svc); else svc_mark_clean(svc); From 3ae609c3410dc51f54d149c8c87136e99b39f80f Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 17:17:30 +0100 Subject: [PATCH 68/90] configure: minor refactor, simplify to reduce duplication Signed-off-by: Joachim Wiberg --- configure.ac | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/configure.ac b/configure.ac index 3b08d5d8..44cae27d 100644 --- a/configure.ac +++ b/configure.ac @@ -97,21 +97,19 @@ AC_PLUGIN([netlink], [yes], [Basic netlink plugin for IFUP/IFDN and GW eve # Check for extra arguments or packages AC_ARG_WITH(heading, AS_HELP_STRING([--with-heading=GREET], [Display GREET instead of Finit version at boot]), - [heading=$withval], [heading="$PACKAGE_NAME v$PACKAGE_VERSION"]) + [heading=$withval], [heading=yes]) AC_ARG_WITH(config, AS_HELP_STRING([--with-config=CONFIG], [Finit bootstrap config file, default /etc/finit.conf]), - [conf=$withval], [conf=$sysconfdir/finit.conf]) -AC_SUBST(FINIT_CONF, $conf) + [conf=$withval], [conf=yes]) AC_ARG_WITH(rcsd, AS_HELP_STRING([--with-rcsd=DIR], [Finit rcS.d/ directory, default /etc/finit.d]), - [rcsd=$withval], [rcsd=$sysconfdir/finit.d]) -AC_SUBST(FINIT_RCSD, $rcsd) + [rcsd=$withval], [rcsd=yes]) AC_ARG_WITH(rc-local, AS_HELP_STRING([--with-rc-local=FILE], [SysV init /etc/rc.local file, default /etc/rc.local]), - [rclocal=$withval], [rclocal=$sysconfdir/rc.local]) + [rclocal=$withval], [rclocal=yes]) AC_ARG_WITH(sysconfig, AS_HELP_STRING([--with-sysconfig=DIR], [Enviroment config directory, default /etc/default]), @@ -119,15 +117,15 @@ AC_ARG_WITH(sysconfig, AC_ARG_WITH(hostname, AS_HELP_STRING([--with-hostname=NAME], [If /etc/hostname is missing, default: noname]), - [hostname=$withval], [hostname=noname]) + [hostname=$withval], [hostname=yes]) AC_ARG_WITH(runlevel, AS_HELP_STRING([--with-runlevel=N], [Runlevel to switch to after bootstrap, default: 2]), - [runlevel=$withval], [runlevel=2]) + [runlevel=$withval], [runlevel=yes]) AC_ARG_WITH(random-seed, AS_HELP_STRING([--with-random-seed=FILE], [Save a random seed for /dev/urandom across reboots, default /var/lib/misc/random-seed]), - [random_seed=$withval], [random_seed=/var/lib/misc/random-seed]) + [random_seed=$withval], [random_seed=yes]) ### Enable features ########################################################################### @@ -156,16 +154,18 @@ AS_IF([test "x$enable_logrotate" != "xno"], [ ### With features ############################################################################## AS_IF([test "x$with_config" != "xno"], [ - AS_IF([test "x$config" = "xyes"], [ + AS_IF([test "x$conf" = "xyes"], [ conf=$sysconfdir/finit.conf])]) AC_EXPAND_DIR(conf_path, "$conf") AC_DEFINE_UNQUOTED(FINIT_CONF, "$conf_path", [Main Finit configuration file, default /etc/finit.conf]) +AC_SUBST(FINIT_CONF, "$conf_path") AS_IF([test "x$with_rcsd" != "xno"], [ AS_IF([test "x$rcsd" = "xyes"], [ rcsd=$sysconfdir/finit.d])]) AC_EXPAND_DIR(rcsd_path, "$rcsd") AC_DEFINE_UNQUOTED(FINIT_RCSD, "$rcsd_path", [Finit rcS.d/, default /etc/finit.d]) +AC_SUBST(FINIT_RCSD, "$rcsd_path") AS_IF([test "x$with_rc_local" != "xno"], [ AS_IF([test "x$rclocal" = "xyes"], [ @@ -192,7 +192,8 @@ AC_DEFINE_UNQUOTED(DEFHOST, "$hostname", [For /etc/hostname]) AS_IF([test "x$with_runlevel" != "xno"], [ AS_IF([test "x$runlevel" = "xyes"], [ - AC_DEFINE_UNQUOTED(RUNLEVEL, 2, [Default runlevel to start after S])]) + runlevel=2 + AC_DEFINE_UNQUOTED(RUNLEVEL, $runlevel, [Default runlevel to start after S])]) AC_DEFINE_UNQUOTED(RUNLEVEL, $runlevel, [Runlevel to start after S])]) AS_IF([test "x$with_random_seed" != "xno"], [ From 6f06c432ec6ffb3161f0439e72c4804bd3b154e3 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 17:18:03 +0100 Subject: [PATCH 69/90] Minor, insert '/' only if pasting components require it Signed-off-by: Joachim Wiberg --- plugins/pidfile.c | 4 ++-- src/exec.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/pidfile.c b/plugins/pidfile.c index 4377d736..c7f20458 100644 --- a/plugins/pidfile.c +++ b/plugins/pidfile.c @@ -68,7 +68,7 @@ static void pidfile_update_conds(char *dir, char *name, uint32_t mask) svc_t *svc; _d("Got dir: %s, name: %s, mask: %08x", dir, name, mask); - snprintf(fn, sizeof(fn), "%s/%s", dir, name); + snprintf(fn, sizeof(fn), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); if (fnmatch("*\\.pid", fn, 0) && fnmatch("*/pid", fn, 0)) return; @@ -131,7 +131,7 @@ static void pidfile_handle_dir(struct iwatch *iw, char *dir, char *name, int mas char path[strlen(dir) + strlen(name) + 2]; struct iwatch_path *iwp; - snprintf(path, sizeof(path), "%s/%s", dir, name); + snprintf(path, sizeof(path), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); _d("path: %s", path); iwp = iwatch_find_by_path(iw, path); diff --git a/src/exec.c b/src/exec.c index eba38738..874816d5 100644 --- a/src/exec.c +++ b/src/exec.c @@ -454,7 +454,7 @@ int run_parts(char *dir, char *cmd) int status; int exit_status; - snprintf(path, sizeof(path), "%s/%s", dir, name); + snprintf(path, sizeof(path), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); if (stat(path, &st)) { _d("Failed stat(%s): %s", path, strerror(errno)); continue; From a1f79e263aed24a1898cb2c8f41774bbc092218b Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 17:38:46 +0100 Subject: [PATCH 70/90] Refactor, add new helper fn paste() to concat directory compoents Signed-off-by: Joachim Wiberg --- plugins/pidfile.c | 6 +++--- src/conf.c | 2 +- src/exec.c | 6 +++--- src/helpers.h | 11 +++++++++++ src/pid.h | 2 +- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/plugins/pidfile.c b/plugins/pidfile.c index c7f20458..19a4b907 100644 --- a/plugins/pidfile.c +++ b/plugins/pidfile.c @@ -67,8 +67,8 @@ static void pidfile_update_conds(char *dir, char *name, uint32_t mask) char fn[PATH_MAX]; svc_t *svc; - _d("Got dir: %s, name: %s, mask: %08x", dir, name, mask); - snprintf(fn, sizeof(fn), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); + paste(fn, sizeof(fn), dir, name); + _d("path: %s, mask: %08x", fn, mask); if (fnmatch("*\\.pid", fn, 0) && fnmatch("*/pid", fn, 0)) return; @@ -131,7 +131,7 @@ static void pidfile_handle_dir(struct iwatch *iw, char *dir, char *name, int mas char path[strlen(dir) + strlen(name) + 2]; struct iwatch_path *iwp; - snprintf(path, sizeof(path), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); + paste(path, sizeof(path), dir, name); _d("path: %s", path); iwp = iwatch_find_by_path(iw, path); diff --git a/src/conf.c b/src/conf.c index 32c6b612..ae7def33 100644 --- a/src/conf.c +++ b/src/conf.c @@ -775,7 +775,7 @@ static int do_change(char *dir, char *name, uint32_t mask) char fn[strlen(dir) + strlen(name) + 2]; struct conf_change *node; - snprintf(fn, sizeof(fn), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); + paste(fn, sizeof(fn), dir, name); _d("path: %s mask: %08x", fn, mask); node = conf_find(fn); diff --git a/src/exec.c b/src/exec.c index 874816d5..155eaf2d 100644 --- a/src/exec.c +++ b/src/exec.c @@ -441,20 +441,20 @@ int run_parts(char *dir, char *cmd) } for (i = 0; i < num; i++) { + const char *name = e[i]->d_name; + char path[strlen(dir) + strlen(name) + 2]; struct stat st; - char path[512]; char *argv[4] = { "sh", "-c", path, NULL }; - char *name = e[i]->d_name; pid_t pid = 0; int status; int exit_status; - snprintf(path, sizeof(path), "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name); + paste(path, sizeof(path), dir, name); if (stat(path, &st)) { _d("Failed stat(%s): %s", path, strerror(errno)); continue; diff --git a/src/helpers.h b/src/helpers.h index e4fc1595..7c734d9f 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -103,6 +103,17 @@ static inline int create(char *path, mode_t mode, uid_t uid, gid_t gid) return 0; } +/* paste dir/file into buf */ +static inline int paste(char *buf, size_t len, const char *dir, const char *file) +{ + if (!dir) + dir = ""; + if (!file) + file = ""; + + return snprintf(buf, len, "%s%s%s", dir, fisslashdir(dir) ? "" : "/", file); +} + int ismnt (char *file, char *dir, char *mode); int fismnt (char *dir); diff --git a/src/pid.h b/src/pid.h index b0ca3325..6bf96ce2 100644 --- a/src/pid.h +++ b/src/pid.h @@ -62,7 +62,7 @@ static inline char *pid_runpath(const char *file, char *path, size_t len) else if (!strncmp(file, "/run/", 5)) file += 5; - rc = snprintf(path, len, "%s/%s", prefix, file); + rc = paste(path, len, prefix, file); if (rc < 0 || (size_t)rc >= len) _e("File path '%s' truncated, should end with '%s'", path, file); From e228174eb5cd111c52f927354e770540e2f65f7d Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 17:54:22 +0100 Subject: [PATCH 71/90] contrib: debian: update to use env: files for service arguments Signed-off-by: Joachim Wiberg --- contrib/debian/finit.d/available/Makefile.am | 2 +- contrib/debian/finit.d/available/acpid.conf | 2 +- contrib/debian/finit.d/available/modules.conf | 12 ++++++------ contrib/debian/finit.d/available/ntpclient.conf | 2 -- contrib/debian/finit.d/available/rsyslogd.conf | 2 +- contrib/debian/finit.d/available/sntpd.conf | 2 ++ contrib/debian/finit.d/available/sshd.conf | 2 +- contrib/debian/finit.d/available/syslogd.conf | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) delete mode 100644 contrib/debian/finit.d/available/ntpclient.conf create mode 100644 contrib/debian/finit.d/available/sntpd.conf diff --git a/contrib/debian/finit.d/available/Makefile.am b/contrib/debian/finit.d/available/Makefile.am index e971a708..bb8e5299 100644 --- a/contrib/debian/finit.d/available/Makefile.am +++ b/contrib/debian/finit.d/available/Makefile.am @@ -1,5 +1,5 @@ docsdir := @docdir@/contrib/debian/finit.d/available docs_DATA = acpid.conf console-setup.conf getty.conf keyboard-setup.conf \ - lightdm.conf modules.conf ntpclient.conf rsyslogd.conf \ + lightdm.conf modules.conf rsyslogd.conf sntpd.conf \ sshd.conf syslogd.conf xdm.conf EXTRA_DIST = $(docs_DATA) diff --git a/contrib/debian/finit.d/available/acpid.conf b/contrib/debian/finit.d/available/acpid.conf index a854b71f..8a255dac 100644 --- a/contrib/debian/finit.d/available/acpid.conf +++ b/contrib/debian/finit.d/available/acpid.conf @@ -1,3 +1,3 @@ # Handle PWR button to shutdown/reboot -- useful in Qemu (virt-manager) # Depends on rsyslogd having started. Redirect any output to log. -service [2345] log acpid -f -- ACPI daemon +service [2345] env:-/etc/default/acpid log acpid -f $OPTIONS -- ACPI daemon diff --git a/contrib/debian/finit.d/available/modules.conf b/contrib/debian/finit.d/available/modules.conf index f5ac914a..f45caec2 100644 --- a/contrib/debian/finit.d/available/modules.conf +++ b/contrib/debian/finit.d/available/modules.conf @@ -1,8 +1,8 @@ ## Linux kernel modules to load #module fbcon -module button -module evdev -module loop -module psmouse -module mousedev -module 8139cp +#module button +#module evdev +#module loop +#module psmouse +#module mousedev +#module 8139cp diff --git a/contrib/debian/finit.d/available/ntpclient.conf b/contrib/debian/finit.d/available/ntpclient.conf deleted file mode 100644 index ed058a67..00000000 --- a/contrib/debian/finit.d/available/ntpclient.conf +++ /dev/null @@ -1,2 +0,0 @@ -# Simple NTP client (SNTP) -service [2345] ntpclient -n -L pool.ntp.org -- SNTP client diff --git a/contrib/debian/finit.d/available/rsyslogd.conf b/contrib/debian/finit.d/available/rsyslogd.conf index f58beaed..5ca6e9f8 100644 --- a/contrib/debian/finit.d/available/rsyslogd.conf +++ b/contrib/debian/finit.d/available/rsyslogd.conf @@ -1,2 +1,2 @@ # Start rsyslogd as soon as possible, should always run -service [S12345] rsyslogd -n -- Reliable and extended syslog daemon +service [S12345] env:-/etc/default/rsyslog rsyslogd -n $RSYSLOGD_OPTIONS -- Reliable syslog daemon diff --git a/contrib/debian/finit.d/available/sntpd.conf b/contrib/debian/finit.d/available/sntpd.conf new file mode 100644 index 00000000..7b5a9fa7 --- /dev/null +++ b/contrib/debian/finit.d/available/sntpd.conf @@ -0,0 +1,2 @@ +# Simple NTP client (SNTP) +service [2345] env:-/etc/default/sntpd sntpd -n $SNTPD_OPTIONS -- SNTP client diff --git a/contrib/debian/finit.d/available/sshd.conf b/contrib/debian/finit.d/available/sshd.conf index 0bc6a63f..62c310ab 100644 --- a/contrib/debian/finit.d/available/sshd.conf +++ b/contrib/debian/finit.d/available/sshd.conf @@ -1,2 +1,2 @@ # Start SSH daemon as soon as basic networking comes up -service [2345] /usr/sbin/sshd -D -- OpenSSH daemon +service [2345] env:-/etc/default/ssh /usr/sbin/sshd -D $SSHD_OPTS -- OpenSSH daemon diff --git a/contrib/debian/finit.d/available/syslogd.conf b/contrib/debian/finit.d/available/syslogd.conf index e2feb808..6896ed10 100644 --- a/contrib/debian/finit.d/available/syslogd.conf +++ b/contrib/debian/finit.d/available/syslogd.conf @@ -1,2 +1,2 @@ # Start sysklogd as soon as possible, should always run if enabled -service [S123456789] syslogd -sF -r 10M:10 -- System logging daemon +service [S123456789] env:-/etc/default/syslogd syslogd -F $SYSLOGD_OPTIONS -- System logging daemon From a758d3dddc35a6e31550537285a59dceb446e8f8 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 18:15:05 +0100 Subject: [PATCH 72/90] contrib: alpine: update to use env: files for service arguments - Check for env in /etc/conf.d on Alpine - Update all services with their respective env: file - Disable default modules, noew modprobe plugin should handle this - Add watchdog service - Rename syslogd -> syslog, in case we want to integrate sysklogd Signed-off-by: Joachim Wiberg --- contrib/alpine/build.sh | 12 ++++++------ contrib/alpine/finit.d/available/Makefile.am | 5 +++-- contrib/alpine/finit.d/available/crond.conf | 2 +- contrib/alpine/finit.d/available/dropbear.conf | 2 +- contrib/alpine/finit.d/available/klogd.conf | 2 +- contrib/alpine/finit.d/available/modules.conf | 8 ++++---- contrib/alpine/finit.d/available/ntpd.conf | 4 ++-- contrib/alpine/finit.d/available/syslog.conf | 2 ++ contrib/alpine/finit.d/available/syslogd.conf | 2 -- contrib/alpine/finit.d/available/watchdog.conf | 1 + 10 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 contrib/alpine/finit.d/available/syslog.conf delete mode 100644 contrib/alpine/finit.d/available/syslogd.conf create mode 100644 contrib/alpine/finit.d/available/watchdog.conf diff --git a/contrib/alpine/build.sh b/contrib/alpine/build.sh index 744c0699..ea20792c 100755 --- a/contrib/alpine/build.sh +++ b/contrib/alpine/build.sh @@ -14,12 +14,12 @@ fi # The plugins are optional, but you may need D-Bus and X11 if you want # to run X-Window, the other configure flags are however required. PKG_CONFIG_LIBDIR=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig ./configure \ - --prefix=/usr --exec-prefix= \ - --sysconfdir=/etc --localstatedir=/var \ - --enable-dbus-plugin --enable-x11-common-plugin \ - --enable-alsa-utils-plugin \ - --enable-watchdog \ - --with-heading="Alpine Linux 3.13" --with-hostname=alpine + --prefix=/usr --exec-prefix= \ + --sysconfdir=/etc --localstatedir=/var \ + --enable-dbus-plugin --enable-x11-common-plugin \ + --enable-alsa-utils-plugin --enable-watchdog \ + --with-sysconfig=/etc/conf.d --with-hostname=alpine \ + --with-heading="Alpine Linux 3.13" echo echo "*** Building ..." diff --git a/contrib/alpine/finit.d/available/Makefile.am b/contrib/alpine/finit.d/available/Makefile.am index f712b81c..4ec27e04 100644 --- a/contrib/alpine/finit.d/available/Makefile.am +++ b/contrib/alpine/finit.d/available/Makefile.am @@ -1,4 +1,5 @@ docsdir := @docdir@/contrib/alpine/finit.d/available -docs_DATA = acpid.conf crond.conf dropbear.conf getty.conf keymap.conf \ - lxdm.conf modules.conf ntpd.conf klogd.conf syslogd.conf +docs_DATA = acpid.conf crond.conf dropbear.conf getty.conf keymap.conf \ + lxdm.conf modules.conf ntpd.conf klogd.conf syslog.conf \ + watchdog.conf EXTRA_DIST = $(docs_DATA) diff --git a/contrib/alpine/finit.d/available/crond.conf b/contrib/alpine/finit.d/available/crond.conf index 1ee0538a..687dc403 100644 --- a/contrib/alpine/finit.d/available/crond.conf +++ b/contrib/alpine/finit.d/available/crond.conf @@ -1 +1 @@ -service [2345] crond -f -S -c /etc/crontabs -- Cron daemon +service [2345] env:-/etc/conf.d/crond crond -f -S $CRON_OPTS -- Cron daemon diff --git a/contrib/alpine/finit.d/available/dropbear.conf b/contrib/alpine/finit.d/available/dropbear.conf index e8cd1212..11db0b73 100644 --- a/contrib/alpine/finit.d/available/dropbear.conf +++ b/contrib/alpine/finit.d/available/dropbear.conf @@ -1,2 +1,2 @@ # Start SSH daemon as soon as basic networking comes up -service [2345] dropbear -R -F -- Dropbear SSH daemon +service [2345] env:-/etc/conf.d/dropbear dropbear -R -F $DROPBEAR_OPTS -- Dropbear SSH daemon diff --git a/contrib/alpine/finit.d/available/klogd.conf b/contrib/alpine/finit.d/available/klogd.conf index 232f45c5..ad02d302 100644 --- a/contrib/alpine/finit.d/available/klogd.conf +++ b/contrib/alpine/finit.d/available/klogd.conf @@ -1,2 +1,2 @@ # Start klogd as soon as possible, after syslogd, should always run -service [S12345] klogd -n -- Kernel log daemon +service [S12345] env:-/etc/conf.d/klogd klogd -n $KLOGD_OPTS -- Kernel log daemon diff --git a/contrib/alpine/finit.d/available/modules.conf b/contrib/alpine/finit.d/available/modules.conf index 1f1ec5b3..7f3d85f0 100644 --- a/contrib/alpine/finit.d/available/modules.conf +++ b/contrib/alpine/finit.d/available/modules.conf @@ -1,6 +1,6 @@ ## Linux kernel modules to load #module fbcon -module button -module evdev -module mousedev -module 8139cp +#module button +#module evdev +#module mousedev +#module 8139cp diff --git a/contrib/alpine/finit.d/available/ntpd.conf b/contrib/alpine/finit.d/available/ntpd.conf index 1968b000..bf8f2d20 100644 --- a/contrib/alpine/finit.d/available/ntpd.conf +++ b/contrib/alpine/finit.d/available/ntpd.conf @@ -1,2 +1,2 @@ -# Silly Busybox ntpd has no option to use syslog when running in foreground -service [2345] log ntpd -n -N -p pool.ntp.org -- NTP daemon +# Busybox ntpd has no option to use syslog when running in foreground +service [2345] log env:-/etc/conf.d/ntpd ntpd -n $NTPD_OPTS -- NTP daemon diff --git a/contrib/alpine/finit.d/available/syslog.conf b/contrib/alpine/finit.d/available/syslog.conf new file mode 100644 index 00000000..c2916ce4 --- /dev/null +++ b/contrib/alpine/finit.d/available/syslog.conf @@ -0,0 +1,2 @@ +# Start syslogd as soon as possible, should always run +service [S12345] env:-/etc/conf.d/syslog syslogd -n $SYSLOGD_OPTS -- Syslog daemon diff --git a/contrib/alpine/finit.d/available/syslogd.conf b/contrib/alpine/finit.d/available/syslogd.conf deleted file mode 100644 index 95fc2e39..00000000 --- a/contrib/alpine/finit.d/available/syslogd.conf +++ /dev/null @@ -1,2 +0,0 @@ -# Start syslogd as soon as possible, should always run -service [S12345] syslogd -n -- Syslog daemon diff --git a/contrib/alpine/finit.d/available/watchdog.conf b/contrib/alpine/finit.d/available/watchdog.conf new file mode 100644 index 00000000..269b711f --- /dev/null +++ b/contrib/alpine/finit.d/available/watchdog.conf @@ -0,0 +1 @@ +service [S123456789] env:-/etc/conf.d/watchdog watchdog $WATCHDOG_OPTS $WATCHDOG_DEV -- System watchdog From 41092095f5d1f9ca7a4065ec09efeade8d323e69 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 25 Feb 2021 18:28:11 +0100 Subject: [PATCH 73/90] configure: allow custom directory for --with-sysconfig Signed-off-by: Joachim Wiberg --- configure.ac | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 44cae27d..7eb8dd0a 100644 --- a/configure.ac +++ b/configure.ac @@ -175,10 +175,10 @@ AC_DEFINE_UNQUOTED(FINIT_RC_LOCAL, "$rclocal_path", [Compat SysV /etc/rc.local f AS_IF([test "x$with_sysconfig" != "xno"], [ AS_IF([test "x$sysconfig" = "xyes"], [ - sysconfig=$sysconfdir/default - AC_EXPAND_DIR(sysconfig_path, "$sysconfig") - AC_DEFINE_UNQUOTED(FINIT_SYSCONFIG, "$sysconfig_path", - [System environment, default /etc/default])])]) + sysconfig=$sysconfdir/default]) + AC_EXPAND_DIR(sysconfig_path, "$sysconfig") + AC_DEFINE_UNQUOTED(FINIT_SYSCONFIG, "$sysconfig_path", + [System environment, default /etc/default])]) AS_IF([test "x$with_heading" != "xno"], [ AS_IF([test "x$heading" = "xyes"], [ From 9df5e7664d4590971fb27b068e8166b5b200c0c2 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 09:09:34 +0100 Subject: [PATCH 74/90] flog: new helper function for debugging to file Signed-off-by: Joachim Wiberg --- src/log.c | 22 ++++++++++++++++++++++ src/log.h | 1 + 2 files changed, 23 insertions(+) diff --git a/src/log.c b/src/log.c index dc574c9e..cc603a52 100644 --- a/src/log.c +++ b/src/log.c @@ -130,6 +130,28 @@ done: va_end(ap); } +/* + * Log to file, intended for debug only. + */ +void flog(char *file, const char *fmt, ...) +{ + char fn[80]; + va_list ap; + FILE *fp; + + snprintf(fn, sizeof(fn), "/tmp/%s.log", file); + fp = fopen(fn, "a"); + if (!fp) + return; + + va_start(ap, fmt); + vfprintf(fp, fmt, ap); + va_end(ap); + + fclose(fp); +} + + /** * Local Variables: * indent-tabs-mode: t diff --git a/src/log.h b/src/log.h index 6ade9a9b..1a371219 100644 --- a/src/log.h +++ b/src/log.h @@ -51,5 +51,6 @@ void log_exit (void); void log_debug (void); void logit (int prio, const char *fmt, ...); +void flog (char *file, const char *fmt, ...); #endif /* FINIT_LOG_H_ */ From 4e035f77f6226ddb12d70fe821f4f14e40699e14 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 09:11:56 +0100 Subject: [PATCH 75/90] Fix regression introduced in 7ef25ab, global rlimits not initialized Services that are instantiated by plugins, and thus don't have a .conf file, tried to use uninitialized rlimits. This caused the kernel to immediately kill those services for violating their own rlimits. Signed-off-by: Joachim Wiberg --- src/conf.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/conf.c b/src/conf.c index ae7def33..ffc674df 100644 --- a/src/conf.c +++ b/src/conf.c @@ -902,6 +902,14 @@ int conf_init(uev_ctx_t *ctx) /* default hostname */ hostname = strdup(DEFHOST); + /* + * Get current global limits, which may be overridden from both + * finit.conf, for Finit and its services like getty+watchdogd, + * and *.conf in finit.d/, for each service(s) listed there. + */ + for (int i = 0; i < RLIMIT_NLIMITS; i++) + getrlimit(i, &global_rlimit[i]); + /* prepare /etc watcher */ fd = iwatch_init(&iw_conf); if (fd < 0) From 6b7ae50fbb3399766d84de128fd3fe15045393a0 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Fri, 26 Feb 2021 11:24:12 +0100 Subject: [PATCH 76/90] test: Let Make generate shellcheck test from the list of test sources Not very readable yet, so it needs clean up! Signed-off-by: Jacques de Laval --- test/.gitignore | 1 + test/Makefile.am | 6 ++++++ test/checkself.sh | 8 -------- 3 files changed, 7 insertions(+), 8 deletions(-) delete mode 100755 test/checkself.sh diff --git a/test/.gitignore b/test/.gitignore index 1eb95bc4..f42a3c6e 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -5,3 +5,4 @@ /Makefile.in /test.env +/checkself.sh diff --git a/test/Makefile.am b/test/Makefile.am index 8f563b2e..1f707f2f 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -17,6 +17,11 @@ AM_TESTS_ENVIRONMENT += export top_srcdir; AM_TESTS_ENVIRONMENT += top_builddir=$(top_builddir); AM_TESTS_ENVIRONMENT += export top_builddir; +checkself.sh: + echo '#!/bin/sh' > $@ + echo shellcheck $(addprefix '"$$srcdir"'/,$(filter %$(TEST_EXTENSIONS),$(filter-out $@, $(EXTRA_DIST)))) >> $@ + chmod +x $@ + TEST_EXTENSIONS = .sh TESTS = checkself.sh TESTS += setup-root.sh @@ -27,3 +32,4 @@ TESTS += start-stop-service-sub-config/test.sh clean-local: make -C test-root/ clean + -rm checkself.sh diff --git a/test/checkself.sh b/test/checkself.sh deleted file mode 100755 index 8d75d8fb..00000000 --- a/test/checkself.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -set -eu - -testdir="${srcdir:-$(dirname "$0")}" - -# shellcheck disable=SC2046 -shellcheck $(find "$testdir" -path "$testdir/test-root" -prune -false -o -name '*.sh') From 2f439194e587f49ee52f21f96524e7544eba65e1 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Fri, 26 Feb 2021 15:35:07 +0100 Subject: [PATCH 77/90] test: Fix testenv-root lingering in build dir Signed-off-by: Jacques de Laval --- test/.gitignore | 1 + test/Makefile.am | 9 ++- test/{test-root => }/busybox-x86_64.md5 | 0 test/{test-root/bin => }/chrootsetup.sh | 0 test/lib.sh | 4 +- test/setup-root.sh | 13 +---- test/test-root/.gitignore | 32 ----------- test/test-root/Makefile | 73 ------------------------- test/testenv-root.mk | 67 +++++++++++++++++++++++ test/testenv_enter.sh | 2 +- test/testenv_exec.sh | 2 +- test/testenv_start.sh | 2 +- 12 files changed, 80 insertions(+), 125 deletions(-) rename test/{test-root => }/busybox-x86_64.md5 (100%) rename test/{test-root/bin => }/chrootsetup.sh (100%) delete mode 100644 test/test-root/.gitignore delete mode 100644 test/test-root/Makefile create mode 100644 test/testenv-root.mk diff --git a/test/.gitignore b/test/.gitignore index f42a3c6e..ef7327fc 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -6,3 +6,4 @@ /test.env /checkself.sh +/testenv-root diff --git a/test/Makefile.am b/test/Makefile.am index 1f707f2f..f2eb8b6e 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,6 +1,6 @@ EXTRA_DIST = lib.sh testenv_exec.sh testenv_start.sh -EXTRA_DIST += test-root/Makefile test-root/busybox-x86_64.md5 -EXTRA_DIST += test-root/bin/chrootsetup.sh +EXTRA_DIST += testenv-root.mk busybox-x86_64.md5 +EXTRA_DIST += chrootsetup.sh EXTRA_DIST += setup-root.sh EXTRA_DIST += common/service.conf common/service.sh EXTRA_DIST += checkself.sh @@ -9,7 +9,7 @@ EXTRA_DIST += add-remove-dynamic-service-sub-config/test.sh EXTRA_DIST += start-stop-service/test.sh EXTRA_DIST += start-stop-service-sub-config/test.sh -AM_TESTS_ENVIRONMENT = TESTENV_ROOT='$(abs_builddir)/test-root/'; +AM_TESTS_ENVIRONMENT = TESTENV_ROOT='$(abs_builddir)/testenv-root/'; AM_TESTS_ENVIRONMENT += export TESTENV_ROOT; AM_TESTS_ENVIRONMENT += export LD_LIBRARY_PATH; AM_TESTS_ENVIRONMENT += top_srcdir=$(top_srcdir); @@ -31,5 +31,4 @@ TESTS += start-stop-service/test.sh TESTS += start-stop-service-sub-config/test.sh clean-local: - make -C test-root/ clean - -rm checkself.sh + -rm -rf $(builddir)/testenv-root/ diff --git a/test/test-root/busybox-x86_64.md5 b/test/busybox-x86_64.md5 similarity index 100% rename from test/test-root/busybox-x86_64.md5 rename to test/busybox-x86_64.md5 diff --git a/test/test-root/bin/chrootsetup.sh b/test/chrootsetup.sh similarity index 100% rename from test/test-root/bin/chrootsetup.sh rename to test/chrootsetup.sh diff --git a/test/lib.sh b/test/lib.sh index 282c51ab..3970d12c 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -91,14 +91,14 @@ teardown() { wait if [ -d "$TESTENV_ROOT/var/lock" ]; then - chmod +r test-root/var/lock + chmod +r "$TESTENV_ROOT/var/lock" fi rm -f "$TESTENV_ROOT"/running_test.pid } trap teardown EXIT -TESTENV_ROOT="${TESTENV_ROOT:-$(pwd)/${TEST_DIR}/test-root}" +TESTENV_ROOT="${TESTENV_ROOT:-$(pwd)/${TEST_DIR}/testenv-root}" export TESTENV_ROOT # shellcheck source=/dev/null diff --git a/test/setup-root.sh b/test/setup-root.sh index ddc18df4..83c5e3d8 100755 --- a/test/setup-root.sh +++ b/test/setup-root.sh @@ -2,14 +2,7 @@ set -eu -if [ ! -d test-root ]; then - mkdir -p test-root/bin - # shellcheck disable=SC2154 - cp "${srcdir}/test-root/bin/chrootsetup.sh" test-root/bin - cp "${srcdir}/test-root/busybox-x86_64.md5" test-root/ - cp "${srcdir}/test-root/Makefile" test-root/ -fi - # shellcheck disable=SC2154 -make -C "$top_builddir" DESTDIR="$(pwd)/test-root/" install -FINITBIN="$(pwd)/$top_builddir/src/finit" make -C test-root/ +make -C "$top_builddir" DESTDIR="$TESTENV_ROOT" install +# shellcheck disable=SC2154 +FINITBIN="$(pwd)/$top_builddir/src/finit" DEST="$TESTENV_ROOT" make -f "$srcdir/testenv-root.mk" diff --git a/test/test-root/.gitignore b/test/test-root/.gitignore deleted file mode 100644 index bfa4c856..00000000 --- a/test/test-root/.gitignore +++ /dev/null @@ -1,32 +0,0 @@ -/bin/busybox-x86_64 -/bin/cat -/bin/cp -/bin/date -/bin/echo -/bin/env -/bin/find -/bin/initctl -/bin/kill -/bin/ls -/bin/mkdir -/bin/mkfifo -/bin/mknod -/bin/mount -/bin/pgrep -/bin/printf -/bin/ps -/bin/rm -/bin/sh -/bin/sleep -/bin/top -/bin/touch -/dev -/etc -/lib64 -/proc -/sbin -/sys -/test_assets -/tmp -/usr -/var diff --git a/test/test-root/Makefile b/test/test-root/Makefile deleted file mode 100644 index b77947e9..00000000 --- a/test/test-root/Makefile +++ /dev/null @@ -1,73 +0,0 @@ -.PHONY: clean check - -ARCH ?= x86_64 - -FINITBIN ?= ./sbin/finit - -BBVER ?= 1.31.0 -BBBIN = busybox-$(ARCH) -BBURL ?= https://www.busybox.net/downloads/binaries/$(BBVER)-defconfig-multiarch-musl/$(BBBIN) - -binaries = ./bin/cat \ - ./bin/cp \ - ./bin/date \ - ./bin/echo \ - ./bin/env \ - ./bin/find \ - ./bin/kill \ - ./bin/ls \ - ./bin/mkdir \ - ./bin/mkfifo \ - ./bin/mknod \ - ./bin/mount \ - ./bin/printf \ - ./bin/pgrep \ - ./bin/ps \ - ./bin/rm \ - ./bin/sh \ - ./bin/sleep \ - ./bin/top \ - ./bin/touch -dirs = ./dev \ - ./etc \ - ./proc \ - ./sbin \ - ./sys \ - ./test_assets \ - ./tmp - -_libs_src = $(shell ldd $(FINITBIN) | grep -Eo '/[^ ]+') -libs = $(foreach path,$(_libs_src),.$(path)) - -all: $(binaries) $(dirs) $(libs) - -$(dirs): - mkdir -p $@ - -./bin/$(BBBIN): - wget -O $@ $(BBURL) - chmod +x ./bin/$(BBBIN) - md5sum -c $(BBBIN).md5 - -$(binaries): ./bin/$(BBBIN) - cd ./bin; \ - rm -f $(notdir $@); \ - ln -s $(BBBIN) $(notdir $@) - -$(libs): - mkdir -p $(dir $@) - cp /$@ $@ - -clean: - rm -f ./bin/$(BBBIN) - rm -rf $(binaries) - rm -rf ./dev - rm -rf ./etc - rm -rf ./lib64 - rm -rf ./proc - rm -rf ./sbin - rm -rf ./sys - rm -rf ./test_assets - rm -rf ./tmp - rm -rf ./usr - rm -rf ./var diff --git a/test/testenv-root.mk b/test/testenv-root.mk new file mode 100644 index 00000000..da5c716c --- /dev/null +++ b/test/testenv-root.mk @@ -0,0 +1,67 @@ +.PHONY: clean check + +DEST ?= ./testenv-root +srcdir ?= ./ + +ARCH ?= x86_64 + +FINITBIN ?= ./sbin/finit + +BBVER ?= 1.31.0 +BBBIN = busybox-$(ARCH) +BBURL ?= https://www.busybox.net/downloads/binaries/$(BBVER)-defconfig-multiarch-musl/$(BBBIN) + +binaries = $(DEST)/bin/cat \ + $(DEST)/bin/cp \ + $(DEST)/bin/date \ + $(DEST)/bin/echo \ + $(DEST)/bin/env \ + $(DEST)/bin/find \ + $(DEST)/bin/kill \ + $(DEST)/bin/ls \ + $(DEST)/bin/mkdir \ + $(DEST)/bin/mkfifo \ + $(DEST)/bin/mknod \ + $(DEST)/bin/mount \ + $(DEST)/bin/printf \ + $(DEST)/bin/pgrep \ + $(DEST)/bin/ps \ + $(DEST)/bin/rm \ + $(DEST)/bin/sh \ + $(DEST)/bin/sleep \ + $(DEST)/bin/top \ + $(DEST)/bin/touch + +dirs = $(DEST)/bin \ + $(DEST)/dev \ + $(DEST)/etc \ + $(DEST)/proc \ + $(DEST)/sbin \ + $(DEST)/sys \ + $(DEST)/test_assets \ + $(DEST)/tmp + +_libs_src = $(shell ldd $(FINITBIN) | grep -Eo '/[^ ]+') +libs = $(foreach path,$(_libs_src),$(abspath $(DEST))$(path)) + +all: $(dirs) $(binaries) $(libs) $(DEST)/bin/chrootsetup.sh + +$(dirs): + mkdir -p $@ + +$(DEST)/bin/$(BBBIN): + wget -O $@ $(BBURL) + chmod +x $@ + # md5sum -c $(BBBIN).md5 + +$(DEST)/bin/chrootsetup.sh: + cp $(srcdir)/$(notdir $@) $@ + +$(binaries): $(DEST)/bin/$(BBBIN) + cd $(DEST)/bin; \ + rm -f $(notdir $@); \ + ln -s $(BBBIN) $(notdir $@) + +$(libs): + mkdir -p $(dir $@) + cp $(patsubst $(abspath $(DEST))%,%,$@) $@ diff --git a/test/testenv_enter.sh b/test/testenv_enter.sh index ef385481..fc2fef7d 100755 --- a/test/testenv_enter.sh +++ b/test/testenv_enter.sh @@ -10,7 +10,7 @@ else shift fi -PID_FILE=$(dirname "$0")/test-root/running_test.pid +PID_FILE=$(dirname "$0")/testenv-root/running_test.pid if [ -f "$PID_FILE" ] ; then target=$(cat "$PID_FILE") diff --git a/test/testenv_exec.sh b/test/testenv_exec.sh index b467076e..b2454bfe 100755 --- a/test/testenv_exec.sh +++ b/test/testenv_exec.sh @@ -11,7 +11,7 @@ target="$1" shift TEST_DIR=$(dirname "$0") -TESTENV_ROOT="${TESTENV_ROOT:-$(pwd)/${TEST_DIR}/test-root}" +TESTENV_ROOT="${TESTENV_ROOT:-$(pwd)/${TEST_DIR}/testenv-root}" nsenter=$(command -v nsenter) chroot=$(command -v chroot) diff --git a/test/testenv_start.sh b/test/testenv_start.sh index ec824ed9..cf3ac71d 100755 --- a/test/testenv_start.sh +++ b/test/testenv_start.sh @@ -3,7 +3,7 @@ set -eu TEST_DIR=$(dirname "$0") -TESTENV_ROOT="${TESTENV_ROOT:-$(pwd)/${TEST_DIR}/test-root}" +TESTENV_ROOT="${TESTENV_ROOT:-$(pwd)/${TEST_DIR}/testenv-root}" unshare=$(command -v unshare) chroot=$(command -v chroot) From 2e0c65816099ee777444bd832c090ff4c27b38b7 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Fri, 26 Feb 2021 15:59:55 +0100 Subject: [PATCH 78/90] test: Utilize distcheck in CI; it tests pretty much everything we want to test Signed-off-by: Jacques de Laval --- .travis.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2c669b40..54a2fa83 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,8 +52,6 @@ script: - cat config.log - make clean - make V=1 -j5 - - LD_LIBRARY_PATH=/tmp/lib make check - - cat ./test/test-suite.log - make install-strip - tree /tmp - ldd /tmp/sbin/finit @@ -64,5 +62,4 @@ script: - unshare -V - nsenter -V - chroot --version - - LD_LIBRARY_PATH=/tmp/lib make check - - cat ./test/test-suite.log + - LD_LIBRARY_PATH=/tmp/lib make distcheck From d258dcecd2e7bbff1cf1924f5657e63c86cfecd2 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Fri, 26 Feb 2021 16:19:03 +0100 Subject: [PATCH 79/90] test: ci: Print test log if tests fail Signed-off-by: Jacques de Laval --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 54a2fa83..8ec8475a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -62,4 +62,4 @@ script: - unshare -V - nsenter -V - chroot --version - - LD_LIBRARY_PATH=/tmp/lib make distcheck + - 'LD_LIBRARY_PATH=/tmp/lib make distcheck || cat finit-4.0-beta1/_build/sub/test/test-suite.log' From 061de4ddf310e0ec8a4a46e22df46d1d14ce38a7 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Fri, 26 Feb 2021 16:31:25 +0100 Subject: [PATCH 80/90] test: ci: Avoid shadowing failure with printing of test log Signed-off-by: Jacques de Laval --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8ec8475a..ba915ca7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -62,4 +62,4 @@ script: - unshare -V - nsenter -V - chroot --version - - 'LD_LIBRARY_PATH=/tmp/lib make distcheck || cat finit-4.0-beta1/_build/sub/test/test-suite.log' + - 'LD_LIBRARY_PATH=/tmp/lib make distcheck || (cat finit-4.0-beta1/_build/sub/test/test-suite.log'; exit 1) From f291c7ac9e869fab3224a90d54c9fe1c5ceef0e1 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Fri, 26 Feb 2021 16:33:40 +0100 Subject: [PATCH 81/90] test: ci: Fix syntax error in CI manifest Signed-off-by: Jacques de Laval --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ba915ca7..fa7638f6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -62,4 +62,4 @@ script: - unshare -V - nsenter -V - chroot --version - - 'LD_LIBRARY_PATH=/tmp/lib make distcheck || (cat finit-4.0-beta1/_build/sub/test/test-suite.log'; exit 1) + - 'LD_LIBRARY_PATH=/tmp/lib make distcheck || (cat finit-4.0-beta1/_build/sub/test/test-suite.log; exit 1)' From 8b680b1b141c0df2d6d61ec32b1b85928b2829ee Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Fri, 26 Feb 2021 16:55:20 +0100 Subject: [PATCH 82/90] test: ci: Add debug output for debugging Travis Signed-off-by: Jacques de Laval --- test/add-remove-dynamic-service/test.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/add-remove-dynamic-service/test.sh b/test/add-remove-dynamic-service/test.sh index fc17f5ff..950b8a4f 100755 --- a/test/add-remove-dynamic-service/test.sh +++ b/test/add-remove-dynamic-service/test.sh @@ -7,6 +7,19 @@ TEST_DIR=$(dirname "$0")/.. # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" +say 'environment:' +texec env +say 'initctl:' +texec busybox-x86_64 which initctl +say 'ldd initctl:' +texec sh -c 'LD_TRACE_LOADED_OBJECTS=1 initctl' +say 'finit:' +texec busybox-x86_64 which finit +say 'ldd finit:' +texec sh -c 'LD_TRACE_LOADED_OBJECTS=1 finit' +say 'tree:' +texec find / | grep -v '^/sys' | grep -v '^/proc' + test_teardown() { say "Test done $(date)" say "Running test teardown." From 255d90cee96fdbb0b4af308524f3c5906020f341 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 27 Feb 2021 13:45:05 +0100 Subject: [PATCH 83/90] test: Mount /tmp as tmpfs again, but copy any existing files in /tmp back into it Signed-off-by: Jacques de Laval --- test/add-remove-dynamic-service/test.sh | 13 ------------- test/chrootsetup.sh | 7 ++++++- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/test/add-remove-dynamic-service/test.sh b/test/add-remove-dynamic-service/test.sh index 950b8a4f..fc17f5ff 100755 --- a/test/add-remove-dynamic-service/test.sh +++ b/test/add-remove-dynamic-service/test.sh @@ -7,19 +7,6 @@ TEST_DIR=$(dirname "$0")/.. # shellcheck source=/dev/null . "$TEST_DIR/lib.sh" -say 'environment:' -texec env -say 'initctl:' -texec busybox-x86_64 which initctl -say 'ldd initctl:' -texec sh -c 'LD_TRACE_LOADED_OBJECTS=1 initctl' -say 'finit:' -texec busybox-x86_64 which finit -say 'ldd finit:' -texec sh -c 'LD_TRACE_LOADED_OBJECTS=1 finit' -say 'tree:' -texec find / | grep -v '^/sys' | grep -v '^/proc' - test_teardown() { say "Test done $(date)" say "Running test teardown." diff --git a/test/chrootsetup.sh b/test/chrootsetup.sh index 22581683..e30c75c4 100755 --- a/test/chrootsetup.sh +++ b/test/chrootsetup.sh @@ -16,7 +16,12 @@ mount -n -t tmpfs none /dev mount -t proc none /proc mount -t sysfs none /sys -# mount -t tmpfs none /tmp + +mkdir -p /tmp.shadow +mount --bind /tmp /tmp.shadow +mount -t tmpfs none /tmp +cp -a /tmp.shadow/* /tmp/ + # mount -t tmpfs none /etc # mkdir -p /dev/pts From 4aa17a84ab05108f7d0dd475b8347a60515032c8 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 27 Feb 2021 13:53:55 +0100 Subject: [PATCH 84/90] test: Minor clean up Signed-off-by: Jacques de Laval --- test/chrootsetup.sh | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/test/chrootsetup.sh b/test/chrootsetup.sh index e30c75c4..457c9ec5 100755 --- a/test/chrootsetup.sh +++ b/test/chrootsetup.sh @@ -2,17 +2,10 @@ set -eu +# It would have been nice to mount /dev as devtmpfs, but unfortunately +# that's not possible if you're not privileged. For now tmpfs will +# have to do. mount -n -t tmpfs none /dev -# mount -t devtmpfs none /dev - -# mknod -m 622 /dev/console c 5 1 -# mknod -m 666 /dev/null c 1 3 -# mknod -m 666 /dev/zero c 1 5 -# mknod -m 666 /dev/ptmx c 5 2 -# mknod -m 666 /dev/tty c 5 0 -# mknod -m 444 /dev/random c 1 8 -# mknod -m 444 /dev/urandom c 1 9 -# chown root:tty /dev/{console,ptmx,tty} mount -t proc none /proc mount -t sysfs none /sys @@ -22,11 +15,6 @@ mount --bind /tmp /tmp.shadow mount -t tmpfs none /tmp cp -a /tmp.shadow/* /tmp/ -# mount -t tmpfs none /etc - -# mkdir -p /dev/pts -# mount -t devpts none /dev/pts - mkdir -p "$FINIT_RCSD" tty=/dev/$(cat /sys/class/tty/console/active) From 7fe1189f7b66dd24001b4b8bad16e55728e64211 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 27 Feb 2021 14:50:49 +0100 Subject: [PATCH 85/90] test: Move test support files to separate dir and keep tests in /test Signed-off-by: Jacques de Laval --- test/.gitignore | 2 +- test/Makefile.am | 30 +++++++++---------- ... add-remove-dynamic-service-sub-config.sh} | 6 ++-- .../test.sh => add-remove-dynamic-service.sh} | 6 ++-- test/busybox-x86_64.md5 | 1 - test/setup-root.sh | 4 +-- ...st.sh => start-stop-service-sub-config.sh} | 6 ++-- .../test.sh => start-stop-service.sh} | 6 ++-- test/tenv/busybox-x86_64.md5 | 1 + test/{ => tenv}/chrootsetup.sh | 0 test/{testenv_enter.sh => tenv/enter.sh} | 9 +++--- test/{testenv_exec.sh => tenv/exec.sh} | 6 ++-- test/{ => tenv}/lib.sh | 30 +++++++------------ test/{testenv-root.mk => tenv/root.mk} | 14 +++++---- test/{testenv_start.sh => tenv/start.sh} | 6 ++-- 15 files changed, 62 insertions(+), 65 deletions(-) rename test/{add-remove-dynamic-service-sub-config/test.sh => add-remove-dynamic-service-sub-config.sh} (86%) rename test/{add-remove-dynamic-service/test.sh => add-remove-dynamic-service.sh} (85%) delete mode 100644 test/busybox-x86_64.md5 rename test/{start-stop-service-sub-config/test.sh => start-stop-service-sub-config.sh} (86%) rename test/{start-stop-service/test.sh => start-stop-service.sh} (85%) create mode 100644 test/tenv/busybox-x86_64.md5 rename test/{ => tenv}/chrootsetup.sh (100%) rename test/{testenv_enter.sh => tenv/enter.sh} (55%) rename test/{testenv_exec.sh => tenv/exec.sh} (78%) rename test/{ => tenv}/lib.sh (73%) rename test/{testenv-root.mk => tenv/root.mk} (84%) rename test/{testenv_start.sh => tenv/start.sh} (72%) diff --git a/test/.gitignore b/test/.gitignore index ef7327fc..04ae7baf 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -6,4 +6,4 @@ /test.env /checkself.sh -/testenv-root +/tenv-root diff --git a/test/Makefile.am b/test/Makefile.am index f2eb8b6e..d5518ab0 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,16 +1,16 @@ -EXTRA_DIST = lib.sh testenv_exec.sh testenv_start.sh -EXTRA_DIST += testenv-root.mk busybox-x86_64.md5 -EXTRA_DIST += chrootsetup.sh +EXTRA_DIST = tenv/lib.sh tenv/exec.sh tenv/start.sh +EXTRA_DIST += tenv/root.mk tenv/busybox-x86_64.md5 +EXTRA_DIST += tenv/chrootsetup.sh EXTRA_DIST += setup-root.sh EXTRA_DIST += common/service.conf common/service.sh EXTRA_DIST += checkself.sh -EXTRA_DIST += add-remove-dynamic-service/test.sh -EXTRA_DIST += add-remove-dynamic-service-sub-config/test.sh -EXTRA_DIST += start-stop-service/test.sh -EXTRA_DIST += start-stop-service-sub-config/test.sh +EXTRA_DIST += add-remove-dynamic-service.sh +EXTRA_DIST += add-remove-dynamic-service-sub-config.sh +EXTRA_DIST += start-stop-service.sh +EXTRA_DIST += start-stop-service-sub-config.sh -AM_TESTS_ENVIRONMENT = TESTENV_ROOT='$(abs_builddir)/testenv-root/'; -AM_TESTS_ENVIRONMENT += export TESTENV_ROOT; +AM_TESTS_ENVIRONMENT = TENV_ROOT='$(abs_builddir)/tenv-root/'; +AM_TESTS_ENVIRONMENT += export TENV_ROOT; AM_TESTS_ENVIRONMENT += export LD_LIBRARY_PATH; AM_TESTS_ENVIRONMENT += top_srcdir=$(top_srcdir); AM_TESTS_ENVIRONMENT += export top_srcdir; @@ -19,16 +19,16 @@ AM_TESTS_ENVIRONMENT += export top_builddir; checkself.sh: echo '#!/bin/sh' > $@ - echo shellcheck $(addprefix '"$$srcdir"'/,$(filter %$(TEST_EXTENSIONS),$(filter-out $@, $(EXTRA_DIST)))) >> $@ + echo shellcheck $(addprefix '"$$srcdir"'/,$(filter %.sh,$(filter-out $@, $(EXTRA_DIST)))) >> $@ chmod +x $@ TEST_EXTENSIONS = .sh TESTS = checkself.sh TESTS += setup-root.sh -TESTS += add-remove-dynamic-service/test.sh -TESTS += add-remove-dynamic-service-sub-config/test.sh -TESTS += start-stop-service/test.sh -TESTS += start-stop-service-sub-config/test.sh +TESTS += add-remove-dynamic-service.sh +TESTS += add-remove-dynamic-service-sub-config.sh +TESTS += start-stop-service.sh +TESTS += start-stop-service-sub-config.sh clean-local: - -rm -rf $(builddir)/testenv-root/ + -rm -rf $(builddir)/tenv-root/ diff --git a/test/add-remove-dynamic-service-sub-config/test.sh b/test/add-remove-dynamic-service-sub-config.sh similarity index 86% rename from test/add-remove-dynamic-service-sub-config/test.sh rename to test/add-remove-dynamic-service-sub-config.sh index 7e319470..24df838c 100755 --- a/test/add-remove-dynamic-service-sub-config/test.sh +++ b/test/add-remove-dynamic-service-sub-config.sh @@ -2,10 +2,10 @@ set -eu -TEST_DIR=$(dirname "$0")/.. +TEST_DIR=$(dirname "$0") # shellcheck source=/dev/null -. "$TEST_DIR/lib.sh" +. "$TEST_DIR/tenv/lib.sh" test_teardown() { say "Test done $(date)" @@ -17,7 +17,7 @@ test_teardown() { say "Test start $(date)" -cp "$TEST_DIR"/common/service.sh "$TESTENV_ROOT"/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TENV_ROOT"/test_assets/ say "Add a dynamic service in $FINIT_RCSD/service.conf" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_RCSD/service.conf" diff --git a/test/add-remove-dynamic-service/test.sh b/test/add-remove-dynamic-service.sh similarity index 85% rename from test/add-remove-dynamic-service/test.sh rename to test/add-remove-dynamic-service.sh index fc17f5ff..b3a8bbc5 100755 --- a/test/add-remove-dynamic-service/test.sh +++ b/test/add-remove-dynamic-service.sh @@ -2,10 +2,10 @@ set -eu -TEST_DIR=$(dirname "$0")/.. +TEST_DIR=$(dirname "$0") # shellcheck source=/dev/null -. "$TEST_DIR/lib.sh" +. "$TEST_DIR/tenv/lib.sh" test_teardown() { say "Test done $(date)" @@ -17,7 +17,7 @@ test_teardown() { say "Test start $(date)" -cp "$TEST_DIR"/common/service.sh "$TESTENV_ROOT"/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TENV_ROOT"/test_assets/ say "Add a dynamic service in $FINIT_CONF" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" diff --git a/test/busybox-x86_64.md5 b/test/busybox-x86_64.md5 deleted file mode 100644 index 9ad31f90..00000000 --- a/test/busybox-x86_64.md5 +++ /dev/null @@ -1 +0,0 @@ -ea7e2353af0067a926d3e021e504290e bin/busybox-x86_64 diff --git a/test/setup-root.sh b/test/setup-root.sh index 83c5e3d8..b4ca97da 100755 --- a/test/setup-root.sh +++ b/test/setup-root.sh @@ -3,6 +3,6 @@ set -eu # shellcheck disable=SC2154 -make -C "$top_builddir" DESTDIR="$TESTENV_ROOT" install +make -C "$top_builddir" DESTDIR="$TENV_ROOT" install # shellcheck disable=SC2154 -FINITBIN="$(pwd)/$top_builddir/src/finit" DEST="$TESTENV_ROOT" make -f "$srcdir/testenv-root.mk" +FINITBIN="$(pwd)/$top_builddir/src/finit" DEST="$TENV_ROOT" make -f "$srcdir/tenv/root.mk" diff --git a/test/start-stop-service-sub-config/test.sh b/test/start-stop-service-sub-config.sh similarity index 86% rename from test/start-stop-service-sub-config/test.sh rename to test/start-stop-service-sub-config.sh index 80585ec5..c91ce955 100755 --- a/test/start-stop-service-sub-config/test.sh +++ b/test/start-stop-service-sub-config.sh @@ -2,10 +2,10 @@ set -eu -TEST_DIR=$(dirname "$0")/.. +TEST_DIR=$(dirname "$0") # shellcheck source=/dev/null -. "$TEST_DIR/lib.sh" +. "$TEST_DIR/tenv/lib.sh" test_teardown() { say "Test done $(date)" @@ -17,7 +17,7 @@ test_teardown() { say "Test start $(date)" -cp "$TEST_DIR"/common/service.sh "$TESTENV_ROOT"/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TENV_ROOT"/test_assets/ say "Add service stanza in $FINIT_RCSD/service.conf" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_RCSD/service.conf" diff --git a/test/start-stop-service/test.sh b/test/start-stop-service.sh similarity index 85% rename from test/start-stop-service/test.sh rename to test/start-stop-service.sh index c429a695..64177133 100755 --- a/test/start-stop-service/test.sh +++ b/test/start-stop-service.sh @@ -2,10 +2,10 @@ set -eu -TEST_DIR=$(dirname "$0")/.. +TEST_DIR=$(dirname "$0") # shellcheck source=/dev/null -. "$TEST_DIR/lib.sh" +. "$TEST_DIR/tenv/lib.sh" test_teardown() { say "Test done $(date)" @@ -17,7 +17,7 @@ test_teardown() { say "Test start $(date)" -cp "$TEST_DIR"/common/service.sh "$TESTENV_ROOT"/test_assets/ +cp "$TEST_DIR"/common/service.sh "$TENV_ROOT"/test_assets/ say "Add service stanza in $FINIT_CONF" texec sh -c "echo 'service [2345] kill:20 log /test_assets/service.sh' > $FINIT_CONF" diff --git a/test/tenv/busybox-x86_64.md5 b/test/tenv/busybox-x86_64.md5 new file mode 100644 index 00000000..0ecfec84 --- /dev/null +++ b/test/tenv/busybox-x86_64.md5 @@ -0,0 +1 @@ +ea7e2353af0067a926d3e021e504290e busybox-x86_64 diff --git a/test/chrootsetup.sh b/test/tenv/chrootsetup.sh similarity index 100% rename from test/chrootsetup.sh rename to test/tenv/chrootsetup.sh diff --git a/test/testenv_enter.sh b/test/tenv/enter.sh similarity index 55% rename from test/testenv_enter.sh rename to test/tenv/enter.sh index fc2fef7d..f1188a3c 100755 --- a/test/testenv_enter.sh +++ b/test/tenv/enter.sh @@ -10,7 +10,9 @@ else shift fi -PID_FILE=$(dirname "$0")/testenv-root/running_test.pid +TEST_DIR=$(dirname "$0")/.. +TENV_ROOT="${TENV_ROOT:-$(pwd)/${TEST_DIR}/tenv-root}" +PID_FILE="$TENV_ROOT/running_test.pid" if [ -f "$PID_FILE" ] ; then target=$(cat "$PID_FILE") @@ -20,7 +22,6 @@ else exit 1 fi -PATH="$TESTENV_PATH" -export PATH +source "$TEST_DIR/test.env" -"$(dirname "$0")"/testenv_exec.sh "$target" "$cmd" "$@" +"$(dirname "$0")"/exec.sh "$target" "$cmd" "$@" diff --git a/test/testenv_exec.sh b/test/tenv/exec.sh similarity index 78% rename from test/testenv_exec.sh rename to test/tenv/exec.sh index b2454bfe..87e8197e 100755 --- a/test/testenv_exec.sh +++ b/test/tenv/exec.sh @@ -10,8 +10,8 @@ fi target="$1" shift -TEST_DIR=$(dirname "$0") -TESTENV_ROOT="${TESTENV_ROOT:-$(pwd)/${TEST_DIR}/testenv-root}" +TEST_DIR=$(dirname "$0"../) +TENV_ROOT="${TENV_ROOT:-$(pwd)/${TEST_DIR}/tenv-root}" nsenter=$(command -v nsenter) chroot=$(command -v chroot) @@ -33,4 +33,4 @@ export PATH --net \ --pid \ -w -t "$target" \ - "$chroot" "$TESTENV_ROOT" "$@" + "$chroot" "$TENV_ROOT" "$@" diff --git a/test/lib.sh b/test/tenv/lib.sh similarity index 73% rename from test/lib.sh rename to test/tenv/lib.sh index 3970d12c..2701d457 100755 --- a/test/lib.sh +++ b/test/tenv/lib.sh @@ -6,7 +6,7 @@ assert_num_children() { texec() { # shellcheck disable=SC2154 - "$TEST_DIR/testenv_exec.sh" "$finit_pid" "$@" + "$TEST_DIR/tenv/exec.sh" "$finit_pid" "$@" } pause() { @@ -25,7 +25,7 @@ fg_red='\e[1;31m' fg_green='\e[1;32m' fg_yellow='\e[1;33m' log() { - printf "< $TEST_NAME > %b%b%b %s\n" "$1" "$2" "$color_reset" "$3" + printf "< %s > %b%b%b %s\n" "$0" "$1" "$2" "$color_reset" "$3" } assert() { @@ -90,38 +90,30 @@ teardown() { wait - if [ -d "$TESTENV_ROOT/var/lock" ]; then - chmod +r "$TESTENV_ROOT/var/lock" + if [ -d "$TENV_ROOT/var/lock" ]; then + chmod +r "$TENV_ROOT/var/lock" fi - rm -f "$TESTENV_ROOT"/running_test.pid + rm -f "$TENV_ROOT"/running_test.pid } trap teardown EXIT -TESTENV_ROOT="${TESTENV_ROOT:-$(pwd)/${TEST_DIR}/testenv-root}" -export TESTENV_ROOT +TENV_ROOT="${TENV_ROOT:-$(pwd)/${TEST_DIR}/tenv-root}" +export TENV_ROOT # shellcheck source=/dev/null -. "$TESTENV_ROOT/../test.env" - -TEST_NAME="$(dirname "$0")" -TEST_NAME=${TEST_NAME#*/} -export TEST_NAME - -# Setup test environment +. "$TENV_ROOT/../test.env" # Setup test environment if [ -n "${DEBUG:-}" ]; then FINIT_ARGS="${FINIT_ARGS:-} finit.debug=on" fi # shellcheck disable=2086 -"$TEST_DIR/testenv_start.sh" finit ${FINIT_ARGS:-} & +"$TEST_DIR/tenv/start.sh" finit ${FINIT_ARGS:-} & finit_ppid=$! -echo "$finit_ppid" > "$TESTENV_ROOT"/running_test.pid +echo "$finit_ppid" > "$TENV_ROOT"/running_test.pid ->&2 echo "Hint: Execute 'test/testenv_enter.sh' to enter the test namespace" ->&2 echo "finit conf '$FINIT_CONF'" ->&2 echo "finit_conf dir '$FINIT_RCSD'" +>&2 echo "Hint: Execute 'TENV_ROOT=$TENV_ROOT $TEST_DIR/tenv/enter.sh' to enter the test namespace" log "$color_reset" 'Setup of test environment done' '' finit_pid=$(retry "pgrep -P $finit_ppid") diff --git a/test/testenv-root.mk b/test/tenv/root.mk similarity index 84% rename from test/testenv-root.mk rename to test/tenv/root.mk index da5c716c..0c4097cb 100644 --- a/test/testenv-root.mk +++ b/test/tenv/root.mk @@ -1,7 +1,7 @@ .PHONY: clean check -DEST ?= ./testenv-root -srcdir ?= ./ +DEST ?= ../tenv-root +srcdir ?= ../ ARCH ?= x86_64 @@ -49,13 +49,17 @@ all: $(dirs) $(binaries) $(libs) $(DEST)/bin/chrootsetup.sh $(dirs): mkdir -p $@ -$(DEST)/bin/$(BBBIN): +$(DEST)/bin/$(BBBIN).md5: + cp $(srcdir)/tenv/$(notdir $@) $@ + +$(DEST)/bin/$(BBBIN): $(DEST)/bin/$(BBBIN).md5 wget -O $@ $(BBURL) chmod +x $@ - # md5sum -c $(BBBIN).md5 + cd $(dir $@); \ + md5sum -c $(BBBIN).md5 $(DEST)/bin/chrootsetup.sh: - cp $(srcdir)/$(notdir $@) $@ + cp $(srcdir)/tenv/$(notdir $@) $@ $(binaries): $(DEST)/bin/$(BBBIN) cd $(DEST)/bin; \ diff --git a/test/testenv_start.sh b/test/tenv/start.sh similarity index 72% rename from test/testenv_start.sh rename to test/tenv/start.sh index cf3ac71d..962964de 100755 --- a/test/testenv_start.sh +++ b/test/tenv/start.sh @@ -2,8 +2,8 @@ set -eu -TEST_DIR=$(dirname "$0") -TESTENV_ROOT="${TESTENV_ROOT:-$(pwd)/${TEST_DIR}/testenv-root}" +TEST_DIR=$(dirname "$0"../) +TENV_ROOT="${TENV_ROOT:-$(pwd)/${TEST_DIR}/tenv-root}" unshare=$(command -v unshare) chroot=$(command -v chroot) @@ -24,4 +24,4 @@ exec "$unshare" \ --mount \ --mount-proc \ --uts --ipc --net \ - "$chroot" "$TESTENV_ROOT" /bin/chrootsetup.sh "$@" + "$chroot" "$TENV_ROOT" /bin/chrootsetup.sh "$@" From acf886f483ed1df6c547d1891a05fa75aa7245dc Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 27 Feb 2021 18:03:00 +0100 Subject: [PATCH 86/90] test: Document how to run the test suite Signed-off-by: Jacques de Laval --- test/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 test/README.md diff --git a/test/README.md b/test/README.md new file mode 100644 index 00000000..42fdf422 --- /dev/null +++ b/test/README.md @@ -0,0 +1,26 @@ +Tests +===== + +Finit comes with a set of tests and a small framework for running them. +Contributors are encouraged to write new tests when implementing features, or +fixing bugs. + +Each test is run in isolation, in it's own namespace. Finit will therefore +be able to be launched as PID 1. Since it's also running with it's own root +directory it will be able to function properly without having any super user +privileges in the host environment. + +Running tests +------------- + +To run the test suite, first [build](../doc/build.md) Finit - then simply run: + + make check + +`make check` will set up the required assets for the test environment, and then +run the full set of tests. The environment is not removed afterwards so at +this point individual tests can be executed without having to run the entire +test suite, which is handy when developing new tests or debugging existing +test. To execute an individual test, simply invoke the script containing it: + + ./test/name-of-the-test.sh From 7c45fd451e76997912c5b8b55b515edb59d2866a Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 27 Feb 2021 18:27:02 +0100 Subject: [PATCH 87/90] test: Remove some redundant exports Signed-off-by: Jacques de Laval --- test/Makefile.am | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/Makefile.am b/test/Makefile.am index d5518ab0..d0a0bd0e 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -12,8 +12,6 @@ EXTRA_DIST += start-stop-service-sub-config.sh AM_TESTS_ENVIRONMENT = TENV_ROOT='$(abs_builddir)/tenv-root/'; AM_TESTS_ENVIRONMENT += export TENV_ROOT; AM_TESTS_ENVIRONMENT += export LD_LIBRARY_PATH; -AM_TESTS_ENVIRONMENT += top_srcdir=$(top_srcdir); -AM_TESTS_ENVIRONMENT += export top_srcdir; AM_TESTS_ENVIRONMENT += top_builddir=$(top_builddir); AM_TESTS_ENVIRONMENT += export top_builddir; @@ -32,3 +30,4 @@ TESTS += start-stop-service-sub-config.sh clean-local: -rm -rf $(builddir)/tenv-root/ + -rm -f checkself.sh From aa096cde197544bbb4e002f6c4809e61c4a2ae79 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 27 Feb 2021 18:27:26 +0100 Subject: [PATCH 88/90] test: Only copy /tmp contents if there are any Signed-off-by: Jacques de Laval --- test/tenv/chrootsetup.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/tenv/chrootsetup.sh b/test/tenv/chrootsetup.sh index 457c9ec5..86951846 100755 --- a/test/tenv/chrootsetup.sh +++ b/test/tenv/chrootsetup.sh @@ -10,10 +10,14 @@ mount -n -t tmpfs none /dev mount -t proc none /proc mount -t sysfs none /sys -mkdir -p /tmp.shadow -mount --bind /tmp /tmp.shadow -mount -t tmpfs none /tmp -cp -a /tmp.shadow/* /tmp/ +if [ "$(ls -A /tmp)" ]; then + mkdir -p /tmp.shadow + mount --bind /tmp /tmp.shadow + mount -t tmpfs none /tmp + cp -a /tmp.shadow/* /tmp/ +else + mount -t tmpfs none /tmp +fi mkdir -p "$FINIT_RCSD" From e06e4175d3b245949d718661a9499107dece0f11 Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 27 Feb 2021 23:23:03 +0100 Subject: [PATCH 89/90] test: Add license and copyright to test framework Signed-off-by: Jacques de Laval --- test/tenv/chrootsetup.sh | 22 ++++++++++++++++++++++ test/tenv/exec.sh | 21 +++++++++++++++++++++ test/tenv/lib.sh | 21 +++++++++++++++++++++ test/tenv/root.mk | 22 +++++++++++++++++++++- test/tenv/start.sh | 21 +++++++++++++++++++++ 5 files changed, 106 insertions(+), 1 deletion(-) diff --git a/test/tenv/chrootsetup.sh b/test/tenv/chrootsetup.sh index 86951846..f465e897 100755 --- a/test/tenv/chrootsetup.sh +++ b/test/tenv/chrootsetup.sh @@ -1,4 +1,26 @@ #!/bin/sh +# Setup the things (mount /proc, /tmp, etc.) that needs to be set up from +# within the namespace/chroot and then start PID 1. +# +# Copyright (c) 2021 Jacques de Laval +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. set -eu diff --git a/test/tenv/exec.sh b/test/tenv/exec.sh index 87e8197e..17eadadb 100755 --- a/test/tenv/exec.sh +++ b/test/tenv/exec.sh @@ -1,4 +1,25 @@ #!/bin/sh +# Execute a given command from withing the test environment. +# +# Copyright (c) 2021 Jacques de Laval +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. set -eu diff --git a/test/tenv/lib.sh b/test/tenv/lib.sh index 2701d457..444cac5a 100755 --- a/test/tenv/lib.sh +++ b/test/tenv/lib.sh @@ -1,4 +1,25 @@ #!/bin/sh +# Small framework for executing tests in a test environment. +# +# Copyright (c) 2021 Jacques de Laval +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. assert_num_children() { assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" diff --git a/test/tenv/root.mk b/test/tenv/root.mk index 0c4097cb..7469dcf9 100644 --- a/test/tenv/root.mk +++ b/test/tenv/root.mk @@ -1,4 +1,24 @@ -.PHONY: clean check +# Root file system for test environment. +# +# Copyright (c) 2021 Jacques de Laval +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. DEST ?= ../tenv-root srcdir ?= ../ diff --git a/test/tenv/start.sh b/test/tenv/start.sh index 962964de..63558444 100755 --- a/test/tenv/start.sh +++ b/test/tenv/start.sh @@ -1,4 +1,25 @@ #!/bin/sh +# Start a new test environment. +# +# Copyright (c) 2021 Jacques de Laval +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. set -eu From 8a063e665defa0a346ca4ee501247cd0d8c2972b Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Sat, 27 Feb 2021 23:36:23 +0100 Subject: [PATCH 90/90] Update list of contributors Signed-off-by: Jacques de Laval --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index c43e34b8..cb0dadba 100644 --- a/AUTHORS +++ b/AUTHORS @@ -5,6 +5,7 @@ Major Contributors: * Tobias Waldekranz * Mattias Walström * Jonas Johansson + * Jacques de Laval Original Author(s): * Claudio Matsuoka