-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6489 from edsantiago/bats
BATS and APIv2: more tests and tweaks
- Loading branch information
Showing
4 changed files
with
183 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |