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 <jacques@de-laval.se>
This commit is contained in:
Jacques de Laval
2021-02-18 13:55:47 +01:00
parent 4f82cb8261
commit 4224c2e8fe
7 changed files with 98 additions and 6 deletions
+8 -4
View File
@@ -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:
@@ -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
+52
View File
@@ -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"
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
set -eu
while [ 1 ]; do
sleep 5
done
+28
View File
@@ -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
+2 -2
View File
@@ -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 "$@"