Skip to content

Commit 1298f19

Browse files
committed
more BATS tests
- run: --name (includes 'podman container exists' tests) - run: --pull (always, never, missing) - build: new test for ADD URL (#4420) - exec: new test for issue #4785 (pipe getting lost) - diff: new test - selinux (mostly copied from docker-autotest) Plus a bug fix: the wait_for_output() helper would continue checking, eventually timing out, even if the container had already exited (probably because of an error). Fix: as part of the loop, run 'podman inspect' and bail out if container is not running. Include exit code and logs. Signed-off-by: Ed Santiago <[email protected]>
1 parent 9e2e4d7 commit 1298f19

File tree

6 files changed

+193
-3
lines changed

6 files changed

+193
-3
lines changed

test/system/030-run.bats

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,55 @@ echo $rand | 0 | $rand
8585
run_podman 1 run --rm $IMAGE sh -c /bin/false
8686
}
8787

88+
@test "podman run --name" {
89+
randomname=$(random_string 30)
90+
91+
# Assume that 4 seconds gives us enough time for 3 quick tests (or at
92+
# least for the 'ps'; the 'container exists' should pass even in the
93+
# unlikely case that the container exits before we get to them)
94+
run_podman run -d --name $randomname $IMAGE sleep 4
95+
cid=$output
96+
97+
run_podman ps --format '{{.Names}}--{{.ID}}'
98+
is "$output" "$randomname--${cid:0:12}"
99+
100+
run_podman container exists $randomname
101+
run_podman container exists $cid
102+
103+
# Done with live-container tests; now let's test after container finishes
104+
run_podman wait $cid
105+
106+
# Container still exists even after stopping:
107+
run_podman container exists $randomname
108+
run_podman container exists $cid
109+
110+
# ...but not after being removed:
111+
run_podman rm $cid
112+
run_podman 1 container exists $randomname
113+
run_podman 1 container exists $cid
114+
}
115+
116+
@test "podman run --pull" {
117+
skip_if_remote "podman-remote does not emit 'Trying to pull' msgs"
118+
119+
run_podman run --pull=missing $IMAGE true
120+
is "$output" "" "--pull=missing [present]: no output"
121+
122+
run_podman run --pull=never $IMAGE true
123+
is "$output" "" "--pull=never [present]: no output"
124+
125+
# Now test with busybox, which we don't have present
126+
run_podman 125 run --pull=never busybox true
127+
is "$output" "Error: unable to find a name and tag match for busybox in repotags: no such image" "--pull=never [busybox/missing]: error"
128+
129+
run_podman run --pull=missing busybox true
130+
is "$output" "Trying to pull .*" "--pull=missing [busybox/missing]: fetches"
131+
132+
run_podman run --pull=always busybox true
133+
is "$output" "Trying to pull .*" "--pull=always [busybox/present]: fetches"
134+
135+
run_podman rm -a
136+
run_podman rmi busybox
137+
}
138+
88139
# vim: filetype=sh

test/system/070-build.bats

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ EOF
4040

4141
# Make an empty test directory, with a subdirectory used for tar
4242
tmpdir=$PODMAN_TMPDIR/build-test
43-
run mkdir -p $tmpdir/subtest || die "Could not mkdir $tmpdir/subtest"
43+
mkdir -p $tmpdir/subtest || die "Could not mkdir $tmpdir/subtest"
4444

4545
echo "This is the ORIGINAL file" > $tmpdir/subtest/myfile1
4646
run tar -C $tmpdir -cJf $tmpdir/myfile.tar.xz subtest
@@ -80,6 +80,25 @@ EOF
8080
run_podman rmi -f build_test $iid
8181
}
8282

83+
@test "podman build - URLs" {
84+
tmpdir=$PODMAN_TMPDIR/build-test
85+
mkdir -p $tmpdir
86+
87+
cat >$tmpdir/Dockerfile <<EOF
88+
FROM $IMAGE
89+
ADD https://github.com/containers/libpod/blob/master/README.md /tmp/
90+
EOF
91+
run_podman build -t add_url $tmpdir
92+
run_podman run --rm add_url stat /tmp/README.md
93+
run_podman rmi -f add_url
94+
95+
# Now test COPY. That should fail.
96+
sed -i -e 's/ADD/COPY/' $tmpdir/Dockerfile
97+
run_podman 125 build -t copy_url $tmpdir
98+
is "$output" ".*error building at STEP .*: source can't be a URL for COPY"
99+
}
100+
101+
83102
function teardown() {
84103
# A timeout or other error in 'build' can leave behind stale images
85104
# that podman can't even see and which will cascade into subsequent

test/system/075-exec.bats

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,20 @@ load helpers
4949
run_podman rm -f $cid
5050
}
5151

52+
# Issue #4785 - piping to exec statement - fixed in #4818
53+
@test "podman exec - cat from stdin" {
54+
skip_if_remote
55+
56+
run_podman run -d $IMAGE sh -c 'while [ ! -e /stop ]; do sleep 0.1;done'
57+
cid="$output"
58+
59+
echo_string=$(random_string 20)
60+
run_podman exec -i $cid cat < <(echo $echo_string)
61+
is "$output" "$echo_string" "output read back from 'exec cat'"
62+
63+
run_podman exec $cid touch /stop
64+
run_podman wait $cid
65+
run_podman rm $cid
66+
}
67+
5268
# vim: filetype=sh

test/system/140-diff.bats

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bats -*- bats -*-
2+
#
3+
# Tests for podman diff
4+
#
5+
6+
load helpers
7+
8+
@test "podman diff" {
9+
rand_file=$(random_string 10)
10+
run_podman run $IMAGE sh -c "touch /$rand_file;rm /etc/services"
11+
run_podman diff --format json -l
12+
13+
# Expected results for each type of diff
14+
declare -A expect=(
15+
[added]="/$rand_file"
16+
[changed]="/etc"
17+
[deleted]="/etc/services"
18+
)
19+
20+
for field in ${!expect[@]}; do
21+
result=$(jq -r -c ".${field}[]" <<<"$output")
22+
is "$result" "${expect[$field]}" "$field"
23+
done
24+
25+
run_podman rm -l
26+
}
27+
28+
# vim: filetype=sh

test/system/410-selinux.bats

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env bats -*- bats -*-
2+
#
3+
# 410-selinux - podman selinux tests
4+
#
5+
6+
load helpers
7+
8+
9+
function check_label() {
10+
if [ ! -e /usr/sbin/selinuxenabled ] || ! /usr/sbin/selinuxenabled; then
11+
skip "selinux disabled or not available"
12+
fi
13+
14+
local args="$1"; shift # command-line args for run
15+
16+
# FIXME: it'd be nice to specify the command to run, e.g. 'ls -dZ /',
17+
# but alpine ls (from busybox) doesn't support -Z
18+
run_podman run --rm $args $IMAGE cat -v /proc/self/attr/current
19+
20+
# FIXME: on some CI systems, 'run --privileged' emits a spurious
21+
# warning line about dup devices. Ignore it.
22+
local context="$output"
23+
if [ ${#lines[@]} -gt 1 ]; then
24+
if expr "${lines[0]}" : "WARNING: .* type, major" >/dev/null; then
25+
echo "# ${lines[0]} [ignored]" >&3
26+
context="${lines[1]}"
27+
else
28+
die "FAILED: too much output, expected one single line"
29+
fi
30+
fi
31+
32+
is "$context" ".*_u:system_r:.*" "SELinux role should always be system_r"
33+
34+
# e.g. system_u:system_r:container_t:s0:c45,c745 -> "container_t"
35+
type=$(cut -d: -f3 <<<"$context")
36+
is "$type" "$1" "SELinux type"
37+
38+
if [ -n "$2" ]; then
39+
# e.g. from the above example -> "s0:c45,c745"
40+
range=$(cut -d: -f4,5 <<<"$context")
41+
is "$range" "$2" "SELinux range"
42+
fi
43+
}
44+
45+
46+
@test "podman selinux: confined container" {
47+
check_label "" "container_t"
48+
}
49+
50+
@test "podman selinux: container with label=disable" {
51+
skip_if_rootless
52+
53+
check_label "--security-opt label=disable" "spc_t"
54+
}
55+
56+
@test "podman selinux: privileged container" {
57+
skip_if_rootless
58+
59+
check_label "--privileged --userns=host" "spc_t"
60+
}
61+
62+
@test "podman selinux: container with overridden range" {
63+
check_label "--security-opt label=level:s0:c1,c2" "container_t" "s0:c1,c2"
64+
}
65+
66+
# vim: filetype=sh

test/system/helpers.bash

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,24 @@ function wait_for_output {
192192
fi
193193
done
194194

195-
[ -n "$cid" ] || die "FATAL: wait_for_ready: no container name/ID in '$*'"
195+
[ -n "$cid" ] || die "FATAL: wait_for_output: no container name/ID in '$*'"
196196

197197
t1=$(expr $SECONDS + $how_long)
198198
while [ $SECONDS -lt $t1 ]; do
199199
run_podman logs $cid
200-
if expr "$output" : ".*$expect" >/dev/null; then
200+
logs=$output
201+
if expr "$logs" : ".*$expect" >/dev/null; then
201202
return
202203
fi
203204

205+
# Barf if container is not running
206+
run_podman inspect --format '{{.State.Running}}' $cid
207+
if [ $output != "true" ]; then
208+
run_podman inspect --format '{{.State.ExitCode}}' $cid
209+
exitcode=$output
210+
die "Container exited (status: $exitcode) before we saw '$expect': $logs"
211+
fi
212+
204213
sleep $sleep_delay
205214
done
206215

@@ -258,6 +267,7 @@ function skip_if_not_systemd() {
258267
# die # Abort with helpful message
259268
#########
260269
function die() {
270+
# FIXME: handle multi-line output
261271
echo "#/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv" >&2
262272
echo "#| FAIL: $*" >&2
263273
echo "#\\^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" >&2

0 commit comments

Comments
 (0)