From 0ef17f55ffd849e2ff79a4ee1da65680cde9af3d Mon Sep 17 00:00:00 2001 From: Jacques de Laval Date: Mon, 22 Feb 2021 16:32:49 +0100 Subject: [PATCH] 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"