test: Retry asserting number of Finit children for a while

Signed-off-by: Jacques de Laval <jacques@de-laval.se>
This commit is contained in:
Jacques de Laval committed 2021-02-22 16:32:49 +01:00
1 parent 644273fc5c
commit 0ef17f55ff
3 files changed
+39 -14

No files matched your search

+31 -7
View File
@@ -1,8 +1,9 @@
#!/bin/sh #!/bin/sh
set -eux set -eu
texec() { texec() {
# shellcheck disable=SC2154
./testenv_exec.sh "$finit_pid" "$@" ./testenv_exec.sh "$finit_pid" "$@"
} }
@@ -30,11 +31,37 @@ assert() {
shift shift
if [ ! "$@" ]; then if [ ! "$@" ]; then
log "$fg_red" ✘ "$__assert_msg ($*)" log "$fg_red" ✘ "$__assert_msg ($*)"
exit 1 return 1
fi fi
log "$fg_green" ✔ "$__assert_msg" 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() { say() {
log "$fg_yellow" • "$@" log "$fg_yellow" • "$@"
} }
@@ -75,11 +102,8 @@ log "$color_reset" '--' ''
# Setup # Setup
./testenv_start.sh finit & ./testenv_start.sh finit &
finit_ppid=$! finit_ppid=$!
for _ in $(seq 1 50); do # shellcheck disable=SC2016
sleep 0.1 retry 'finit_pid=$(pgrep -P '"$finit_ppid"')'
finit_pid=$(pgrep -P "$finit_ppid") || continue
break
done
tty=/dev/$(texec cat /sys/class/tty/console/active) tty=/dev/$(texec cat /sys/class/tty/console/active)
texec cat "$tty" & texec cat "$tty" &
+7 -6
View File
@@ -2,6 +2,11 @@
set -eu set -eu
assert_num_children() {
texec ps -opid,ppid,args
assert "$1 services are running" "$(texec pgrep -P 1 | wc -l)" -eq "$1"
}
# Test # Test
say 'Set up a service' say 'Set up a service'
cp "$TEST_DIR"/service.sh test_root/bin 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' say 'Reload Finit'
texec kill -SIGHUP 1 texec kill -SIGHUP 1
sleep 1
service_pids=$(texec pgrep -P 1 | wc -l) retry 'assert_num_children 1'
assert 'One service is running' "$service_pids" -eq 1
say 'Remove the service' say 'Remove the service'
texec sh -c "echo > $FINIT_CONF" texec sh -c "echo > $FINIT_CONF"
say 'Reload Finit' say 'Reload Finit'
texec kill -SIGHUP 1 texec kill -SIGHUP 1
sleep 1
service_pids=$(texec pgrep -P 1 | wc -l) retry 'assert_num_children 0'
assert 'Zero services are running' "$service_pids" -eq 0
# Teardown # Teardown
rm -f test_root/bin/service.sh rm -f test_root/bin/service.sh
+1 -1
View File
@@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
set -eux set -eu
TEST_DIR=$(dirname "$0") TEST_DIR=$(dirname "$0")
TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root" TESTS_ROOT="$(pwd)/${TEST_DIR}/test_root"