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 <magnusmalm@gmail.com>
This commit is contained in:
Magnus Malm
2021-02-23 08:55:11 +01:00
parent 70773105ac
commit 4480010d4a
2 changed files with 13 additions and 3 deletions
+5
View File
@@ -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' ''
+8 -3
View File
@@ -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