summaryrefslogtreecommitdiff
path: root/test/system/220-healthcheck.bats
diff options
context:
space:
mode:
authorEd Santiago <santiago@redhat.com>2020-06-03 13:18:07 -0600
committerEd Santiago <santiago@redhat.com>2020-06-03 16:13:33 -0600
commit2d5a2a7640d0f320d391a2fcc6a4bd0eb03b9f9f (patch)
tree86b8bf35367dd52fc79989608f4cce47bee14e5a /test/system/220-healthcheck.bats
parent1f8c509fafb4ce41970c4f28ed55daec459c7520 (diff)
downloadpodman-2d5a2a7640d0f320d391a2fcc6a4bd0eb03b9f9f.tar.gz
podman-2d5a2a7640d0f320d391a2fcc6a4bd0eb03b9f9f.tar.bz2
podman-2d5a2a7640d0f320d391a2fcc6a4bd0eb03b9f9f.zip
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 <santiago@redhat.com>
Diffstat (limited to 'test/system/220-healthcheck.bats')
-rw-r--r--test/system/220-healthcheck.bats116
1 files changed, 116 insertions, 0 deletions
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 <<EOF
+#!/bin/sh
+
+if test -e /uh-oh; then
+ echo "Uh-oh on stdout!"
+ echo "Uh-oh on stderr!" >&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 <<EOF
+#!/bin/sh
+
+while :; do
+ sleep 1
+done
+EOF
+
+ cat >${PODMAN_TMPDIR}/Containerfile <<EOF
+FROM $IMAGE
+
+COPY healthcheck /healthcheck
+COPY entrypoint /entrypoint
+
+RUN chmod 755 /healthcheck /entrypoint
+
+CMD ["/entrypoint"]
+EOF
+
+ run_podman build -t healthcheck_i ${PODMAN_TMPDIR}
+
+ # Run that healthcheck image.
+ run_podman run -d --name healthcheck_c \
+ --health-cmd /healthcheck \
+ --health-interval 1s \
+ --health-retries 3 \
+ healthcheck_i
+
+ # We can't check for 'starting' because a 1-second interval is too
+ # short; it could run healthcheck before we get to our first check.
+ #
+ # So, just force a healthcheck run, then confirm that it's running.
+ run_podman healthcheck run healthcheck_c
+ is "$output" "healthy" "output from 'podman healthcheck run'"
+
+ _check_health "All healthy" "
+Status | healthy
+FailingStreak | 0
+Log[-1].ExitCode | 0
+Log[-1].Output |
+"
+
+ # Force a failure
+ run_podman exec healthcheck_c touch /uh-oh
+ sleep 2
+
+ _check_health "First failure" "
+Status | healthy
+FailingStreak | [123]
+Log[-1].ExitCode | 1
+Log[-1].Output |
+"
+
+ # After three successive failures, container should no longer be healthy
+ sleep 5
+ _check_health "Three or more failures" "
+Status | unhealthy
+FailingStreak | [3456]
+Log[-1].ExitCode | 1
+Log[-1].Output |
+"
+
+ # healthcheck should now fail, with exit status 1 and 'unhealthy' output
+ run_podman 1 healthcheck run healthcheck_c
+ is "$output" "unhealthy" "output from 'podman healthcheck run'"
+
+ # Clean up
+ run_podman rm -f healthcheck_c
+ run_podman rmi healthcheck_i
+}
+
+# vim: filetype=sh