Skip to content

Commit

Permalink
Merge pull request containers#13935 from edsantiago/bats_assert
Browse files Browse the repository at this point in the history
system tests: add assert(), and start using it
  • Loading branch information
openshift-merge-robot authored Apr 22, 2022
2 parents 26a51b2 + 97ee411 commit 82393e2
Show file tree
Hide file tree
Showing 26 changed files with 289 additions and 308 deletions.
10 changes: 5 additions & 5 deletions test/system/000-TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ load helpers
@test "podman subcmd - description of this particular test" {
args="some sort of argument list"
run_podman subcmd $args
is "$output" "what we expect" "output from 'podman subcmd $args'"
assert "$output" == "what we expect" "output from 'podman subcmd $args'"
}

# vim: filetype=sh
Expand Down Expand Up @@ -66,7 +66,7 @@ function teardown() {
# FIXME: example of dprint. This will trigger if PODMAN_TEST_DEBUG=FOO
# FIXME: ...or anything that matches the name assigned in the @test line.
dprint "podman logs $cid -> '$output'"
is "$output" "what are we expecting?" "description of this check"
assert "$output" == "what are we expecting?" "description of this check"

# Clean up
run_podman rm $cid
Expand All @@ -90,7 +90,7 @@ size | -\\\?[0-9]\\\+
run_podman history --format json $IMAGE

# FIXME: parse_table is what does all the work, giving us test cases.
parse_table "$tests" | while read field expect; do
while read field expect; do
# FIXME: this shows a drawback of BATS and bash: we can't include '|'
# FIXME: in the table, but we need to because some images don't
# FIXME: have a CID. So, yeah, this is ugly -- but rare.
Expand All @@ -104,10 +104,10 @@ size | -\\\?[0-9]\\\+
# FIXME: please be sure to note the third field!
# FIXME: that's the test name. Make it something useful! Include
# FIXME: loop variables whenever possible. Don't just say "my test"
is "$actual" "$expect\$" "jq .[$i].$field"
assert "$actual" =~ "$expect\$" "jq .[$i].$field"
i=$(expr $i + 1)
done
done
done < <(parse_table "$tests")
}


Expand Down
26 changes: 10 additions & 16 deletions test/system/001-basic.bats
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ function setup() {
# Test that build date is reasonable, e.g. after 2019-01-01
local built=$(expr "$output" : ".*Built: \+\(.*\)" | head -n1)
local built_t=$(date --date="$built" +%s)
if [ $built_t -lt 1546300800 ]; then
die "Preposterous 'Built' time in podman version: '$built'"
fi
assert "$built_t" -gt 1546300800 "Preposterous 'Built' time in podman version"
}

@test "podman info" {
Expand Down Expand Up @@ -114,29 +112,25 @@ See 'podman version --help'" "podman version --remote"

# By default, podman should include '--remote' in its help output
run_podman --help
is "$output" ".* --remote " "podman --help includes the --remote option"
assert "$output" =~ " --remote " "podman --help includes the --remote option"

# When it detects CONTAINER_HOST or _CONNECTION, --remote is not an option
CONTAINER_HOST=foobar run_podman --help
if grep -- " --remote " <<<"$output"; then
die "podman --help, with CONTAINER_HOST set, is showing --remote"
fi
assert "$output" !~ " --remote " \
"podman --help, with CONTAINER_HOST set, should not show --remote"

CONTAINER_CONNECTION=foobar run_podman --help
if grep -- " --remote " <<<"$output"; then
die "podman --help, with CONTAINER_CONNECTION set, is showing --remote"
fi
assert "$output" !~ " --remote " \
"podman --help, with CONTAINER_CONNECTION set, should not show --remote"

# When it detects --url or --connection, --remote is not an option
run_podman --url foobar --help
if grep -- " --remote " <<<"$output"; then
die "podman --help, with --url set, is showing --remote"
fi
assert "$output" !~ " --remote " \
"podman --help, with --url set, should not show --remote"

run_podman --connection foobar --help
if grep -- " --remote " <<<"$output"; then
die "podman --help, with --connection set, is showing --remote"
fi
assert "$output" !~ " --remote " \
"podman --help, with --connection set, should not show --remote"
}

# Check that just calling "podman-remote" prints the usage message even
Expand Down
21 changes: 9 additions & 12 deletions test/system/015-help.bats
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function check_help() {

# The line immediately after 'Usage:' gives us a 1-line synopsis
usage=$(echo "$full_help" | grep -A1 '^Usage:' | tail -1)
[ -n "$usage" ] || die "podman $cmd: no Usage message found"
assert "$usage" != "" "podman $cmd: no Usage message found"

# e.g. 'podman ps' should not show 'podman container ps' in usage
# Trailing space in usage handles 'podman system renumber' which
Expand All @@ -42,14 +42,12 @@ function check_help() {
fi

# We had someone write upper-case '[OPTIONS]' once. Prevent it.
if expr "$usage" : '.*\[OPTION' >/dev/null; then
die "'options' string must be lower-case in usage: $usage"
fi
assert "$usage" !~ '\[OPTION' \
"'options' string must be lower-case in usage"

# We had someone do 'podman foo ARG [options]' one time. Yeah, no.
if expr "$usage" : '.*[A-Z].*\[option' >/dev/null; then
die "'options' must precede arguments in usage: $usage"
fi
assert "$usage" !~ '[A-Z].*\[option' \
"'options' must precede arguments in usage"

# Cross-check: if usage includes '[options]', there must be a
# longer 'Options:' section in the full --help output; vice-versa,
Expand Down Expand Up @@ -169,16 +167,15 @@ function check_help() {

# This can happen if the output of --help changes, such as between
# the old command parser and cobra.
[ $count -gt 0 ] || \
die "Internal error: no commands found in 'podman help $@' list"
assert "$count" -gt 0 \
"Internal error: no commands found in 'podman help $*' list"

# Sanity check: make sure the special loops above triggered at least once.
# (We've had situations where a typo makes the conditional never run)
if [ -z "$*" ]; then
for i in subcommands required_args takes_no_args fixed_args; do
if [[ -z ${found[$i]} ]]; then
die "Internal error: '$i' subtest did not trigger"
fi
assert "${found[$i]}" != "" \
"Internal error: '$i' subtest did not trigger"
done
fi
}
Expand Down
53 changes: 28 additions & 25 deletions test/system/030-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ echo $rand | 0 | $rand
skip_if_remote "TODO Fix this for remote case"

run_podman run --rm --uidmap 0:100:10000 $IMAGE mount
run grep /sys/kernel <(echo "$output")
is "$output" "" "unwanted /sys/kernel in 'mount' output"
assert "$output" !~ /sys/kernel "unwanted /sys/kernel in 'mount' output"

run_podman run --rm --net host --uidmap 0:100:10000 $IMAGE mount
run grep /sys/kernel <(echo "$output")
is "$output" "" "unwanted /sys/kernel in 'mount' output (with --net=host)"
assert "$output" !~ /sys/kernel \
"unwanted /sys/kernel in 'mount' output (with --net=host)"
}

# 'run --rm' goes through different code paths and may lose exit status.
Expand Down Expand Up @@ -692,10 +691,8 @@ json-file | f
# This operation should take
# exactly 10 seconds. Give it some leeway.
delta_t=$(( $t1 - $t0 ))
[ $delta_t -gt 8 ] ||\
die "podman stop: ran too quickly! ($delta_t seconds; expected >= 10)"
[ $delta_t -le 14 ] ||\
die "podman stop: took too long ($delta_t seconds; expected ~10)"
assert "$delta_t" -gt 8 "podman stop: ran too quickly!"
assert "$delta_t" -le 14 "podman stop: took too long"

run_podman rm $cid
}
Expand Down Expand Up @@ -753,34 +750,40 @@ EOF

@test "podman run defaultenv" {
run_podman run --rm $IMAGE printenv
is "$output" ".*TERM=xterm" "output matches TERM"
is "$output" ".*container=podman" "output matches container=podman"
assert "$output" =~ "TERM=xterm" "env includes TERM"
assert "$output" =~ "container=podman" "env includes container=podman"

run_podman run --unsetenv=TERM --rm $IMAGE printenv
is "$output" ".*container=podman" "output matches container=podman"
run grep TERM <<<$output
is "$output" "" "unwanted TERM environment variable despite --unsetenv=TERM"
assert "$output" =~ "container=podman" "env includes container=podman"
assert "$output" != "TERM" "unwanted TERM environment variable despite --unsetenv=TERM"

run_podman run --unsetenv-all --rm $IMAGE /bin/printenv
run grep TERM <<<$output
is "$output" "" "unwanted TERM environment variable despite --unsetenv-all"
run grep container <<<$output
is "$output" "" "unwanted container environment variable despite --unsetenv-all"
run grep PATH <<<$output
is "$output" "" "unwanted PATH environment variable despite --unsetenv-all"
for v in TERM container PATH; do
assert "$output" !~ "$v" "variable present despite --unsetenv-all"
done

run_podman run --unsetenv-all --env TERM=abc --rm $IMAGE /bin/printenv
is "$output" ".*TERM=abc" "missing TERM environment variable despite TERM being set on commandline"
assert "$output" =~ "TERM=abc" \
"missing TERM environment variable despite TERM being set on commandline"
}

@test "podman run - no /etc/hosts" {
if [[ -z "$container" ]]; then
skip "Test is too dangerous to run in a non-container environment"
fi
skip_if_rootless "cannot move /etc/hosts file as a rootless user"
tmpfile=$PODMAN_TMPDIR/hosts
mv /etc/hosts $tmpfile

local hosts_tmp=/etc/hosts.RENAME-ME-BACK-TO-JUST-HOSTS
if [[ -e $hosts_tmp ]]; then
die "Internal error: leftover backup hosts file: $hosts_tmp"
fi
mv /etc/hosts $hosts_tmp
run_podman '?' run --rm --add-host "foo.com:1.2.3.4" $IMAGE cat "/etc/hosts"
mv $tmpfile /etc/hosts
is "$status" 0 "podman run without /etc/hosts file should work"
is "$output" "1.2.3.4 foo.com.*" "users can add hosts even without /etc/hosts"
mv $hosts_tmp /etc/hosts
assert "$status" = 0 \
"podman run without /etc/hosts file should work"
assert "$output" =~ "^1\.2\.3\.4 foo.com.*" \
"users can add hosts even without /etc/hosts"
}

# rhbz#1854566 : $IMAGE has incorrect permission 555 on the root '/' filesystem
Expand Down
5 changes: 2 additions & 3 deletions test/system/035-logs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,8 @@ $s_after"
retries=$((retries - 1))
sleep 0.1
done
if [[ $retries -eq 0 ]]; then
die "Timed out waiting for before&after in podman logs: $output"
fi
assert $retries -gt 0 \
"Timed out waiting for before&after in podman logs: $output"

run_podman logs --until $before test
is "$output" "" "podman logs --until before"
Expand Down
5 changes: 2 additions & 3 deletions test/system/040-ps.bats
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ RUN sleep 30
EOF
local t1=$SECONDS
local delta_t=$((t1 - t0))
if [[ $delta_t -gt 10 ]]; then
die "podman build did not get killed within 10 seconds (actual time: $delta_t seconds)"
fi
assert $delta_t -le 10 \
"podman build did not get killed within 10 seconds"

run_podman ps -a
is "${#lines[@]}" "1" "podman ps -a does not see buildah containers"
Expand Down
5 changes: 2 additions & 3 deletions test/system/045-start.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ load helpers
is "$output" ".*$cid_none_implicit" "started: container with no --restart"
is "$output" ".*$cid_none_explicit" "started: container with --restart=no"
is "$output" ".*$cid_on_failure" "started: container with --restart=on-failure"
if [[ $output =~ $cid_always ]]; then
die "podman start --all restarted a running container"
fi
assert "$output" !~ "$cid_always" \
"podman start --all should not restart a running container"

run_podman wait $cid_none_implicit $cid_none_explicit $cid_on_failure

Expand Down
17 changes: 5 additions & 12 deletions test/system/050-stop.bats
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ load helpers
# The initial SIGTERM is ignored, so this operation should take
# exactly 10 seconds. Give it some leeway.
delta_t=$(( $t1 - $t0 ))
[ $delta_t -gt 8 ] ||\
die "podman stop: ran too quickly! ($delta_t seconds; expected >= 10)"
[ $delta_t -le 14 ] ||\
die "podman stop: took too long ($delta_t seconds; expected ~10)"
assert $delta_t -gt 8 "podman stop: ran too quickly!"
assert $delta_t -le 14 "podman stop: took too long"

run_podman rm $cid
}
Expand Down Expand Up @@ -103,8 +101,7 @@ load helpers

# The 'stop' command should return almost instantaneously
delta_t=$(( $t1 - $t0 ))
[ $delta_t -le 2 ] ||\
die "podman stop: took too long ($delta_t seconds; expected <= 2)"
assert $delta_t -le 2 "podman stop: took too long"

run_podman rm $cid
done
Expand Down Expand Up @@ -138,9 +135,7 @@ load helpers
break
fi
timeout=$((timeout - 1))
if [[ $timeout -eq 0 ]]; then
die "Timed out waiting for container to receive SIGERM"
fi
assert $timeout -gt 0 "Timed out waiting for container to receive SIGTERM"
sleep 0.5
done

Expand All @@ -154,9 +149,7 @@ load helpers
# Time check: make sure we were able to run 'ps' before the container
# exited. If this takes too long, it means ps had to wait for lock.
local delta_t=$(( $SECONDS - t0 ))
if [[ $delta_t -gt 5 ]]; then
die "Operations took too long ($delta_t seconds)"
fi
assert $delta_t -le 5 "Operations took too long"

run_podman kill stopme
run_podman wait stopme
Expand Down
28 changes: 13 additions & 15 deletions test/system/065-cp.bats
Original file line number Diff line number Diff line change
Expand Up @@ -589,20 +589,22 @@ ${randomcontent[1]}" "$description"
# RUNNING container
# NOTE: /dest does not exist yet but is expected to be created during copy
run_podman cp cpcontainer:/tmp/sub/weirdlink $destdir/dest
run cat $destdir/dest/containerfile0 $destdir/dest/containerfile1
is "${lines[0]}" "${randomcontent[0]}" "eval symlink - running container"
is "${lines[1]}" "${randomcontent[1]}" "eval symlink - running container"
for i in 0 1; do
assert "$(< $destdir/dest/containerfile$i)" = "${randomcontent[$i]}" \
"eval symlink - running container - file $i/1"
done

run_podman kill cpcontainer
run_podman rm -t 0 -f cpcontainer
run rm -rf $srcdir/dest
rm -rf $srcdir/dest

# CREATED container
run_podman create --name cpcontainer $cpimage
run_podman cp cpcontainer:/tmp/sub/weirdlink $destdir/dest
run cat $destdir/dest/containerfile0 $destdir/dest/containerfile1
is "${lines[0]}" "${randomcontent[0]}" "eval symlink - created container"
is "${lines[1]}" "${randomcontent[1]}" "eval symlink - created container"
for i in 0 1; do
assert "$(< $destdir/dest/containerfile$i)" = "${randomcontent[$i]}" \
"eval symlink - created container - file $i/1"
done
run_podman rm -t 0 -f cpcontainer
run_podman rmi $cpimage
}
Expand Down Expand Up @@ -924,20 +926,16 @@ ${randomcontent[1]}" "$description"

# Copy file.
$PODMAN cp cpcontainer:/tmp/file.txt - > $srcdir/stdout.tar
if [ $? -ne 0 ]; then
die "Command failed: podman cp ... - | cat"
fi

tar xvf $srcdir/stdout.tar -C $srcdir
is "$(< $srcdir/file.txt)" "$rand_content"
run 1 ls $srcdir/empty.txt
is "$(< $srcdir/file.txt)" "$rand_content" "File contents: file.txt"
if [[ -e "$srcdir/empty.txt" ]]; then
die "File should not exist, but does: empty.txt"
fi
rm -f $srcdir/*

# Copy directory.
$PODMAN cp cpcontainer:/tmp - > $srcdir/stdout.tar
if [ $? -ne 0 ]; then
die "Command failed: podman cp ... - | cat : $output"
fi

tar xvf $srcdir/stdout.tar -C $srcdir
is "$(< $srcdir/tmp/file.txt)" "$rand_content"
Expand Down
Loading

0 comments on commit 82393e2

Please sign in to comment.