test/system: check for leaks in teardown suite

At the end of all tests always check for leaks. That should make us more
robust against adding tests at the end that would leak stuff otherwise.

TODO: something seems wrong with bats when returning an error in
teardown_suite(), it prints a warning:
bats warning: Executed <NUM+1> instead of expected <NUM> tests
And also the output is formatted weirdly in this case where the podman
args are split over multiple lines.
But the test fails as expected so I don't think it is a problem.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2024-06-07 13:41:19 +02:00
parent 81c90f51c2
commit 4e0cd49148
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
2 changed files with 48 additions and 32 deletions

View File

@ -217,39 +217,8 @@ function basic_teardown() {
# As these podman commands are slow we do not want to do this by default
# and only provide this as opt in option. (#22909)
if [[ "$BATS_TEST_COMPLETED" -eq 1 ]] && [ $exit_code -eq 0 ] && [ -n "$PODMAN_BATS_LEAK_CHECK" ]; then
run_podman volume ls -q
assert "$output" == "" "Leaked volumes!!!"
leak_check
exit_code=$((exit_code + $?))
run_podman network ls -q
# podman always exists
assert "$output" == "podman" "Leaked networks!!!"
exit_code=$((exit_code + $?))
run_podman pod ps -q
assert "$output" == "" "Leaked pods!!!"
exit_code=$((exit_code + $?))
run_podman ps -a -q
assert "$output" == "" "Leaked containers!!!"
exit_code=$((exit_code + $?))
run_podman images --all --format '{{.Repository}}:{{.Tag}} {{.ID}}'
for line in "${lines[@]}"; do
set $line
if [[ "$1" == "$PODMAN_TEST_IMAGE_FQN" ]]; then
found_needed_image=1
elif [[ "$1" == "$PODMAN_SYSTEMD_IMAGE_FQN" ]]; then
# This is a big image, don't force unnecessary pulls
:
else
exit_code=$((exit_code + 1))
echo "Leaked image $1 $2"
fi
done
# Make sure desired image is present
if [[ -z "$found_needed_image" ]]; then
exit_code=$((exit_code + 1))
die "$PODMAN_TEST_IMAGE_FQN was removed"
fi
fi
# Some error happened (either in teardown itself or the actual test failed)
@ -294,6 +263,43 @@ function restore_image() {
run_podman restore $archive
}
function leak_check() {
run_podman volume ls -q
assert "$output" == "" "Leaked volumes!!!"
local exit_code=$?
run_podman network ls -q
# podman always exists
assert "$output" == "podman" "Leaked networks!!!"
exit_code=$((exit_code + $?))
run_podman pod ps -q
assert "$output" == "" "Leaked pods!!!"
exit_code=$((exit_code + $?))
run_podman ps -a -q
assert "$output" == "" "Leaked containers!!!"
exit_code=$((exit_code + $?))
run_podman images --all --format '{{.Repository}}:{{.Tag}} {{.ID}}'
for line in "${lines[@]}"; do
set $line
if [[ "$1" == "$PODMAN_TEST_IMAGE_FQN" ]]; then
found_needed_image=1
elif [[ "$1" == "$PODMAN_SYSTEMD_IMAGE_FQN" ]]; then
# This is a big image, don't force unnecessary pulls
:
else
exit_code=$((exit_code + 1))
echo "Leaked image $1 $2"
fi
done
# Make sure desired image is present
if [[ -z "$found_needed_image" ]]; then
exit_code=$((exit_code + 1))
die "$PODMAN_TEST_IMAGE_FQN was removed"
fi
return $exit_code
}
function clean_setup() {
local actions=(
"pod rm -t 0 --all --force --ignore"

View File

@ -37,4 +37,14 @@ function setup_suite() {
# Run at the very end of all tests. Useful for cleanup of non-BATS tmpdirs.
function teardown_suite() {
stop_registry
local exit_code=$?
# After all tests make sure there are no leaks and cleanup if there are
leak_check
if [ $? -gt 0 ]; then
exit_code=$((exit_code + 1))
clean_setup
fi
return $exit_code
}