Skip to content

Commit 4e0cd49

Browse files
committed
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 <[email protected]>
1 parent 81c90f5 commit 4e0cd49

File tree

2 files changed

+48
-32
lines changed

2 files changed

+48
-32
lines changed

test/system/helpers.bash

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -217,39 +217,8 @@ function basic_teardown() {
217217
# As these podman commands are slow we do not want to do this by default
218218
# and only provide this as opt in option. (#22909)
219219
if [[ "$BATS_TEST_COMPLETED" -eq 1 ]] && [ $exit_code -eq 0 ] && [ -n "$PODMAN_BATS_LEAK_CHECK" ]; then
220-
run_podman volume ls -q
221-
assert "$output" == "" "Leaked volumes!!!"
220+
leak_check
222221
exit_code=$((exit_code + $?))
223-
run_podman network ls -q
224-
# podman always exists
225-
assert "$output" == "podman" "Leaked networks!!!"
226-
exit_code=$((exit_code + $?))
227-
run_podman pod ps -q
228-
assert "$output" == "" "Leaked pods!!!"
229-
exit_code=$((exit_code + $?))
230-
run_podman ps -a -q
231-
assert "$output" == "" "Leaked containers!!!"
232-
exit_code=$((exit_code + $?))
233-
234-
run_podman images --all --format '{{.Repository}}:{{.Tag}} {{.ID}}'
235-
for line in "${lines[@]}"; do
236-
set $line
237-
if [[ "$1" == "$PODMAN_TEST_IMAGE_FQN" ]]; then
238-
found_needed_image=1
239-
elif [[ "$1" == "$PODMAN_SYSTEMD_IMAGE_FQN" ]]; then
240-
# This is a big image, don't force unnecessary pulls
241-
:
242-
else
243-
exit_code=$((exit_code + 1))
244-
echo "Leaked image $1 $2"
245-
fi
246-
done
247-
248-
# Make sure desired image is present
249-
if [[ -z "$found_needed_image" ]]; then
250-
exit_code=$((exit_code + 1))
251-
die "$PODMAN_TEST_IMAGE_FQN was removed"
252-
fi
253222
fi
254223

255224
# Some error happened (either in teardown itself or the actual test failed)
@@ -294,6 +263,43 @@ function restore_image() {
294263
run_podman restore $archive
295264
}
296265

266+
function leak_check() {
267+
run_podman volume ls -q
268+
assert "$output" == "" "Leaked volumes!!!"
269+
local exit_code=$?
270+
run_podman network ls -q
271+
# podman always exists
272+
assert "$output" == "podman" "Leaked networks!!!"
273+
exit_code=$((exit_code + $?))
274+
run_podman pod ps -q
275+
assert "$output" == "" "Leaked pods!!!"
276+
exit_code=$((exit_code + $?))
277+
run_podman ps -a -q
278+
assert "$output" == "" "Leaked containers!!!"
279+
exit_code=$((exit_code + $?))
280+
281+
run_podman images --all --format '{{.Repository}}:{{.Tag}} {{.ID}}'
282+
for line in "${lines[@]}"; do
283+
set $line
284+
if [[ "$1" == "$PODMAN_TEST_IMAGE_FQN" ]]; then
285+
found_needed_image=1
286+
elif [[ "$1" == "$PODMAN_SYSTEMD_IMAGE_FQN" ]]; then
287+
# This is a big image, don't force unnecessary pulls
288+
:
289+
else
290+
exit_code=$((exit_code + 1))
291+
echo "Leaked image $1 $2"
292+
fi
293+
done
294+
295+
# Make sure desired image is present
296+
if [[ -z "$found_needed_image" ]]; then
297+
exit_code=$((exit_code + 1))
298+
die "$PODMAN_TEST_IMAGE_FQN was removed"
299+
fi
300+
return $exit_code
301+
}
302+
297303
function clean_setup() {
298304
local actions=(
299305
"pod rm -t 0 --all --force --ignore"

test/system/setup_suite.bash

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,14 @@ function setup_suite() {
3737
# Run at the very end of all tests. Useful for cleanup of non-BATS tmpdirs.
3838
function teardown_suite() {
3939
stop_registry
40+
local exit_code=$?
41+
42+
# After all tests make sure there are no leaks and cleanup if there are
43+
leak_check
44+
if [ $? -gt 0 ]; then
45+
exit_code=$((exit_code + 1))
46+
clean_setup
47+
fi
48+
49+
return $exit_code
4050
}

0 commit comments

Comments
 (0)