From 2d5a2a7640d0f320d391a2fcc6a4bd0eb03b9f9f Mon Sep 17 00:00:00 2001 From: Ed Santiago Date: Wed, 3 Jun 2020 13:18:07 -0600 Subject: BATS and APIv2: more tests and tweaks - (minor): apiv2 tests: check for full ID Observation made while reviewing #6461: tests were checking only for a 12-character container/image ID in return value. It's actually 64, and we should test for that. This should also minimize confusion in a future maintainer. - podman pause/unpause: new test Runs a 'date/sleep' loop, pauses container, sleeps 3s, restarts, then confirms that there's a 3- to 6-second gap in the logs for the container. - podman healthcheck: new test run a container with healthcheck, test both healthy and unhealthy conditions - podman pod: check '{{.Pod}}' field in podman ps Hey, as long as we have a pod with two running containers, might as well confirm that 'podman ps' returns the expected pod ID. Signed-off-by: Ed Santiago --- test/apiv2/20-containers.at | 6 +- test/system/080-pause.bats | 58 ++++++++++++++++++++ test/system/200-pod.bats | 6 ++ test/system/220-healthcheck.bats | 116 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 test/system/080-pause.bats create mode 100644 test/system/220-healthcheck.bats diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index 8b535928a..60f6d97aa 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -19,7 +19,7 @@ t GET libpod/containers/json 200 length=0 t GET libpod/containers/json?all=true 200 \ length=1 \ - .[0].Id~[0-9a-f]\\{12\\} \ + .[0].Id~[0-9a-f]\\{64\\} \ .[0].Image=$IMAGE \ .[0].Command[0]="true" \ .[0].State~\\\(exited\\\|stopped\\\) \ @@ -33,7 +33,7 @@ t DELETE libpod/containers/$cid 204 CNAME=myfoo podman run --name $CNAME $IMAGE -td top t GET libpod/containers/json?all=true 200 \ - .[0].Id~[0-9a-f]\\{12\\} + .[0].Id~[0-9a-f]\\{64\\} cid=$(jq -r '.[0].Id' <<<"$output") # No such container @@ -45,7 +45,7 @@ t POST "libpod/commit?container=$CNAME&$cparam" '' 500 # Commit a new image from the container t POST "libpod/commit?container=$CNAME" '' 200 \ - .Id~[0-9a-f]\\{12\\} + .Id~[0-9a-f]\\{64\\} iid=$(jq -r '.Id' <<<"$output") t GET libpod/images/$iid/json 200 \ .RepoTags[0]=null \ diff --git a/test/system/080-pause.bats b/test/system/080-pause.bats new file mode 100644 index 000000000..4ec0906f4 --- /dev/null +++ b/test/system/080-pause.bats @@ -0,0 +1,58 @@ +#!/usr/bin/env bats -*- bats -*- +# +# tests for podman pause/unpause functionality +# + +load helpers + +@test "podman pause/unpause" { + skip_if_rootless "pause does not work rootless" + + cname=$(random_string 10) + run_podman run -d --name $cname $IMAGE \ + sh -c 'while :;do date +%s;sleep 1;done' + cid="$output" + # Wait for first time value + wait_for_output '[0-9]\{10,\}' $cid + + # Pause container, sleep a bit, unpause, sleep again to give process + # time to write a new post-restart time value. Pause by CID, unpause + # by name, just to exercise code paths. While paused, check 'ps' + # and 'inspect', then check again after restarting. + run_podman pause $cid + run_podman inspect --format '{{.State.Status}}' $cid + is "$output" "paused" "podman inspect .State.Status" + sleep 3 + run_podman ps -a --format '{{.ID}} {{.Names}} {{.Status}}' + is "$output" "${cid:0:12} $cname paused" "podman ps on paused container" + run_podman unpause $cname + run_podman ps -a --format '{{.ID}} {{.Names}} {{.Status}}' + is "$output" "${cid:0:12} $cname Up .*" "podman ps on resumed container" + sleep 1 + + # Get full logs, and iterate through them computing delta_t between entries + run_podman logs $cid + i=1 + max_delta=0 + while [ $i -lt ${#lines[*]} ]; do + this_delta=$(( ${lines[$i]} - ${lines[$(($i - 1))]} )) + if [ $this_delta -gt $max_delta ]; then + max_delta=$this_delta + fi + i=$(( $i + 1 )) + done + + # There should be a 3-4 second gap, *maybe* 5. Never 1 or 2, that + # would imply that the container never paused. + is "$max_delta" "[3456]" "delta t between paused and restarted" + + run_podman rm -f $cname + + # Pause/unpause on nonexistent name or id - these should all fail + run_podman 125 pause $cid + run_podman 125 pause $cname + run_podman 125 unpause $cid + run_podman 125 unpause $cname +} + +# vim: filetype=sh diff --git a/test/system/200-pod.bats b/test/system/200-pod.bats index 0d14ca990..9a6b39057 100644 --- a/test/system/200-pod.bats +++ b/test/system/200-pod.bats @@ -73,6 +73,12 @@ function teardown() { run_podman run -d --pod $podname $IMAGE nc -l -p $port cid1="$output" + # (While we're here, test the 'Pod' field of 'podman ps'. Expect two ctrs) + run_podman ps --format '{{.Pod}}' + newline=" +" + is "$output" "${podid:0:12}${newline}${podid:0:12}" "sdfdsf" + # Talker: send the message via common port on localhost message=$(random_string 15) run_podman run --rm --pod $podname $IMAGE \ diff --git a/test/system/220-healthcheck.bats b/test/system/220-healthcheck.bats new file mode 100644 index 000000000..e649ad3d2 --- /dev/null +++ b/test/system/220-healthcheck.bats @@ -0,0 +1,116 @@ +#!/usr/bin/env bats -*- bats -*- +# +# tests for podman healthcheck +# +# + +load helpers + + +# Helper function: run 'podman inspect' and check various given fields +function _check_health { + local testname="$1" + local tests="$2" + + run_podman inspect --format json healthcheck_c + + parse_table "$tests" | while read field expect;do + # (kludge to deal with parse_table and empty strings) + if [ "$expect" = "''" ]; then expect=""; fi + + actual=$(jq -r ".[0].State.Healthcheck.$field" <<<"$output") + is "$actual" "$expect" "$testname - .State.Healthcheck.$field" + done +} + + +@test "podman healthcheck" { + + # Create an image with a healthcheck script; said script will + # pass until the file /uh-oh gets created (by us, via exec) + cat >${PODMAN_TMPDIR}/healthcheck <&2 + exit 1 +else + echo "Life is Good on stdout" + echo "Life is Good on stderr" >&2 + exit 0 +fi +EOF + + cat >${PODMAN_TMPDIR}/entrypoint <${PODMAN_TMPDIR}/Containerfile <