-
Notifications
You must be signed in to change notification settings - Fork 767
/
run.bats
382 lines (329 loc) · 15 KB
/
run.bats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env bats
load helpers
@test "run" {
if ! which runc ; then
skip "no runc in PATH"
fi
runc --version
createrandom ${TESTDIR}/randomfile
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json alpine)
root=$(buildah mount $cid)
buildah config --workingdir /tmp $cid
run_buildah --log-level=error run $cid pwd
expect_output "/tmp"
buildah config --workingdir /root $cid
run_buildah --log-level=error run $cid pwd
expect_output "/root"
cp ${TESTDIR}/randomfile $root/tmp/
buildah run $cid cp /tmp/randomfile /tmp/other-randomfile
test -s $root/tmp/other-randomfile
cmp ${TESTDIR}/randomfile $root/tmp/other-randomfile
seq 100000 | buildah run $cid -- sh -c 'while read i; do echo $i; done'
buildah unmount $cid
buildah rm $cid
}
@test "run--args" {
if ! which runc ; then
skip "no runc in PATH"
fi
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json alpine)
# This should fail, because buildah run doesn't have a -n flag.
run_buildah 1 --log-level=error run -n $cid echo test
# This should succeed, because buildah run stops caring at the --, which is preserved as part of the command.
run_buildah --log-level=error run $cid echo -- -n test
expect_output -- "-- -n test"
# This should succeed, because buildah run stops caring at the --, which is not part of the command.
run_buildah --log-level=error run $cid -- echo -n -- test
expect_output -- "-- test"
# This should succeed, because buildah run stops caring at the --.
run_buildah --log-level=error run $cid -- echo -- -n test --
expect_output -- "-- -n test --"
# This should succeed, because buildah run stops caring at the --.
run_buildah --log-level=error run $cid -- echo -n "test"
expect_output "test"
buildah rm $cid
}
@test "run-cmd" {
if ! which runc ; then
skip "no runc in PATH"
fi
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json alpine)
buildah config --workingdir /tmp $cid
# Configured entrypoint/cmd shouldn't modify behaviour of run with no arguments
# empty entrypoint, configured cmd, empty run arguments
buildah config --entrypoint "" $cid
buildah config --cmd pwd $cid
run_buildah 1 --log-level=error run $cid
expect_output --substring "command must be specified" "empty entrypoint, cmd, no args"
# empty entrypoint, configured cmd, empty run arguments, end parsing option
buildah config --entrypoint "" $cid
buildah config --cmd pwd $cid
run_buildah 1 --log-level=error run $cid --
expect_output --substring "command must be specified" "empty entrypoint, cmd, no args, --"
# configured entrypoint, empty cmd, empty run arguments
buildah config --entrypoint pwd $cid
buildah config --cmd "" $cid
run_buildah 1 --log-level=error run $cid
expect_output --substring "command must be specified" "entrypoint, empty cmd, no args"
# configured entrypoint, empty cmd, empty run arguments, end parsing option
buildah config --entrypoint pwd $cid
buildah config --cmd "" $cid
run_buildah 1 --log-level=error run $cid --
expect_output --substring "command must be specified" "entrypoint, empty cmd, no args, --"
# configured entrypoint only, empty run arguments
buildah config --entrypoint pwd $cid
run_buildah 1 --log-level=error run $cid
expect_output --substring "command must be specified" "entrypoint, no args"
# configured entrypoint only, empty run arguments, end parsing option
buildah config --entrypoint pwd $cid
run_buildah 1 --log-level=error run $cid --
expect_output --substring "command must be specified" "entrypoint, no args, --"
# cofigured cmd only, empty run arguments
buildah config --cmd pwd $cid
run_buildah 1 --log-level=error run $cid
expect_output --substring "command must be specified" "cmd, no args"
# configured cmd only, empty run arguments, end parsing option
buildah config --cmd pwd $cid
run_buildah 1 --log-level=error run $cid --
expect_output --substring "command must be specified" "cmd, no args, --"
# configured entrypoint, configured cmd, empty run arguments
buildah config --entrypoint "pwd" $cid
buildah config --cmd "whoami" $cid
run_buildah 1 --log-level=error run $cid
expect_output --substring "command must be specified" "entrypoint, cmd, no args"
# configured entrypoint, configured cmd, empty run arguments, end parsing option
buildah config --entrypoint "pwd" $cid
buildah config --cmd "whoami" $cid
run_buildah 1 --log-level=error run $cid --
expect_output --substring "command must be specified" "entrypoint, cmd, no args"
# Configured entrypoint/cmd shouldn't modify behaviour of run with argument
# Note: entrypoint and cmd can be invalid in below tests as they should never execute
# empty entrypoint, configured cmd, configured run arguments
buildah config --entrypoint "" $cid
buildah config --cmd "/invalid/cmd" $cid
run_buildah --log-level=error run $cid -- pwd
expect_output "/tmp" "empty entrypoint, invalid cmd, pwd"
# configured entrypoint, empty cmd, configured run arguments
buildah config --entrypoint "/invalid/entrypoint" $cid
buildah config --cmd "" $cid
run_buildah --log-level=error run $cid -- pwd
expect_output "/tmp" "invalid entrypoint, empty cmd, pwd"
# configured entrypoint only, configured run arguments
buildah config --entrypoint "/invalid/entrypoint" $cid
run_buildah --log-level=error run $cid -- pwd
expect_output "/tmp" "invalid entrypoint, no cmd(??), pwd"
# cofigured cmd only, configured run arguments
buildah config --cmd "/invalid/cmd" $cid
run_buildah --log-level=error run $cid -- pwd
expect_output "/tmp" "invalid cmd, no entrypoint(??), pwd"
# configured entrypoint, configured cmd, configured run arguments
buildah config --entrypoint "/invalid/entrypoint" $cid
buildah config --cmd "/invalid/cmd" $cid
run_buildah --log-level=error run $cid -- pwd
expect_output "/tmp" "invalid cmd & entrypoint, pwd"
buildah rm $cid
}
function configure_and_check_user() {
local setting=$1
local expect_u=$2
local expect_g=$3
run_buildah config -u "$setting" $cid
run_buildah --log-level=error run -- $cid id -u
expect_output "$expect_u" "id -u ($setting)"
run_buildah --log-level=error run -- $cid id -g
expect_output "$expect_g" "id -g ($setting)"
}
@test "run-user" {
if ! which runc ; then
skip "no runc in PATH"
fi
eval $(go env)
echo CGO_ENABLED=${CGO_ENABLED}
if test "$CGO_ENABLED" -ne 1; then
skip "CGO_ENABLED = '$CGO_ENABLED'"
fi
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json alpine)
root=$(buildah mount $cid)
testuser=jimbo
testbogususer=nosuchuser
testgroup=jimbogroup
testuid=$RANDOM
testotheruid=$RANDOM
testgid=$RANDOM
testgroupid=$RANDOM
echo "$testuser:x:$testuid:$testgid:Jimbo Jenkins:/home/$testuser:/bin/sh" >> $root/etc/passwd
echo "$testgroup:x:$testgroupid:" >> $root/etc/group
configure_and_check_user "" 0 0
configure_and_check_user "${testuser}" $testuid $testgid
configure_and_check_user "${testuid}" $testuid $testgid
configure_and_check_user "${testuser}:${testgroup}" $testuid $testgroupid
configure_and_check_user "${testuid}:${testgroup}" $testuid $testgroupid
configure_and_check_user "${testotheruid}:${testgroup}" $testotheruid $testgroupid
configure_and_check_user "${testotheruid}" $testotheruid 0
configure_and_check_user "${testuser}:${testgroupid}" $testuid $testgroupid
configure_and_check_user "${testuid}:${testgroupid}" $testuid $testgroupid
buildah config -u ${testbogususer} $cid
run_buildah 1 --log-level=error run -- $cid id -u
expect_output --substring "unknown user" "id -u (bogus user)"
run_buildah 1 --log-level=error run -- $cid id -g
expect_output --substring "unknown user" "id -g (bogus user)"
ln -vsf /etc/passwd $root/etc/passwd
buildah config -u ${testuser}:${testgroup} $cid
run_buildah 1 --log-level=error run -- $cid id -u
echo "$output"
expect_output --substring "unknown user" "run as unknown user"
buildah unmount $cid
buildah rm $cid
}
@test "run --hostname" {
if test "$BUILDAH_ISOLATION" = "rootless" ; then
skip "rootless"
fi
if ! which runc ; then
skip "no runc in PATH"
fi
runc --version
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json alpine)
run_buildah --log-level=error run $cid hostname
[ "$output" != "foobar" ]
run_buildah --log-level=error run --hostname foobar $cid hostname
expect_output "foobar"
buildah rm $cid
}
@test "run --volume" {
if ! which runc ; then
skip "no runc in PATH"
fi
zflag=
if which selinuxenabled > /dev/null 2> /dev/null ; then
if selinuxenabled ; then
zflag=z
fi
fi
runc --version
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json alpine)
mkdir -p ${TESTDIR}/was-empty
# As a baseline, this should succeed.
run_buildah --log-level=error run -v ${TESTDIR}/was-empty:/var/not-empty${zflag:+:${zflag}} $cid touch /var/not-empty/testfile
# Parsing options that with comma, this should succeed.
run_buildah --log-level=error run -v ${TESTDIR}/was-empty:/var/not-empty:rw,rshared${zflag:+,${zflag}} $cid touch /var/not-empty/testfile
# If we're parsing the options at all, this should be read-only, so it should fail.
run_buildah 1 --log-level=error run -v ${TESTDIR}/was-empty:/var/not-empty:ro${zflag:+,${zflag}} $cid touch /var/not-empty/testfile
# Even if the parent directory doesn't exist yet, this should succeed.
run_buildah --log-level=error run -v ${TESTDIR}/was-empty:/var/multi-level/subdirectory $cid touch /var/multi-level/subdirectory/testfile
# And check the same for file volumes.
run_buildah --log-level=error run -v ${TESTDIR}/was-empty/testfile:/var/different-multi-level/subdirectory/testfile $cid touch /var/different-multi-level/subdirectory/testfile
}
@test "run --mount" {
if ! which runc ; then
skip "no runc in PATH"
fi
zflag=
if which selinuxenabled > /dev/null 2> /dev/null ; then
if selinuxenabled ; then
zflag=z
fi
fi
runc --version
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json alpine)
mkdir -p ${TESTDIR}/was:empty
# As a baseline, this should succeed.
run_buildah --log-level=error run --mount type=tmpfs,dst=/var/tmpfs-not-empty $cid touch /var/tmpfs-not-empty/testfile
run_buildah --log-level=error run --mount type=bind,src=${TESTDIR}/was:empty,dst=/var/not-empty${zflag:+,${zflag}} $cid touch /var/not-empty/testfile
# If we're parsing the options at all, this should be read-only, so it should fail.
run_buildah 1 --log-level=error run --mount type=bind,src=${TESTDIR}/was:empty,dst=/var/not-empty,ro${zflag:+,${zflag}} $cid touch /var/not-empty/testfile
# Even if the parent directory doesn't exist yet, this should succeed.
run_buildah --log-level=error run --mount type=bind,src=${TESTDIR}/was:empty,dst=/var/multi-level/subdirectory $cid touch /var/multi-level/subdirectory/testfile
# And check the same for file volumes.
run_buildah --log-level=error run --mount type=bind,src=${TESTDIR}/was:empty/testfile,dst=/var/different-multi-level/subdirectory/testfile $cid touch /var/different-multi-level/subdirectory/testfile
}
@test "run symlinks" {
if ! which runc ; then
skip "no runc in PATH"
fi
runc --version
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json alpine)
mkdir -p ${TESTDIR}/tmp
ln -s tmp ${TESTDIR}/tmp2
export TMPDIR=${TESTDIR}/tmp2
run_buildah --log-level=error run $cid id
}
@test "run --cap-add/--cap-drop" {
if ! which runc ; then
skip "no runc in PATH"
fi
runc --version
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json alpine)
# Try with default caps.
run_buildah --log-level=error run $cid grep ^CapEff /proc/self/status
defaultcaps="$output"
# Try adding DAC_OVERRIDE.
run_buildah --log-level=error run --cap-add CAP_DAC_OVERRIDE $cid grep ^CapEff /proc/self/status
addedcaps="$output"
# Try dropping DAC_OVERRIDE.
run_buildah --log-level=error run --cap-drop CAP_DAC_OVERRIDE $cid grep ^CapEff /proc/self/status
droppedcaps="$output"
# Okay, now the "dropped" and "added" should be different.
test "$addedcaps" != "$droppedcaps"
# And one or the other should be different from the default, with the other being the same.
if test "$defaultcaps" == "$addedcaps" ; then
test "$defaultcaps" != "$droppedcaps"
fi
if test "$defaultcaps" == "$droppedcaps" ; then
test "$defaultcaps" != "$addedcaps"
fi
}
@test "Check if containers run with correct open files/processes limits" {
if ! which runc ; then
skip "no runc in PATH"
fi
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json alpine)
run_buildah --log-level=error run $cid awk '/open files/{print $4}' /proc/self/limits
expect_output "1048576" "limits: open files (unlimited)"
run_buildah --log-level=error run $cid awk '/processes/{print $3}' /proc/self/limits
expect_output "1048576" "limits: processes (unlimited)"
buildah rm $cid
cid=$(buildah from --ulimit nofile=300:400 --pull --signature-policy ${TESTSDIR}/policy.json alpine)
run_buildah --log-level=error run $cid awk '/open files/{print $4}' /proc/self/limits
expect_output "300" "limits: open files (w/file limit)"
run_buildah --log-level=error run $cid awk '/processes/{print $3}' /proc/self/limits
expect_output "1048576" "limits: processes (w/file limit)"
buildah rm $cid
cid=$(buildah from --ulimit nproc=100:200 --ulimit nofile=300:400 --pull --signature-policy ${TESTSDIR}/policy.json alpine)
run_buildah --log-level=error run $cid awk '/open files/{print $4}' /proc/self/limits
expect_output "300" "limits: open files (w/file & proc limits)"
run_buildah --log-level=error run $cid awk '/processes/{print $3}' /proc/self/limits
expect_output "100" "limits: processes (w/file & proc limits)"
buildah rm $cid
}
@test "run-builtin-volume-omitted" {
# This image is known to include a volume, but not include the mountpoint
# in the image.
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json docker.io/library/registry@sha256:a25e4660ed5226bdb59a5e555083e08ded157b1218282840e55d25add0223390)
mnt=$(buildah mount $cid)
# By default, the mountpoint should not be there.
run test -d "$mnt"/var/lib/registry
echo "$output"
[ "$status" -ne 0 ]
# We'll create the mountpoint for "run".
run_buildah --log-level=error run $cid ls -1 /var/lib
expect_output --substring "registry"
# Double-check that the mountpoint is there.
test -d "$mnt"/var/lib/registry
}
@test "run-exit-status" {
if ! which runc ; then
skip "no runc in PATH"
fi
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json alpine)
run_buildah 42 run ${cid} sh -c 'exit 42'
}
@test "Verify /run/.containerenv exist" {
if ! which runc ; then
skip "no runc in PATH"
fi
cid=$(buildah from --pull --signature-policy ${TESTSDIR}/policy.json alpine)
# test a standard mount to /run/.containerenv
run_buildah --log-level=error run $cid ls -1 /run/.containerenv
expect_output --substring "/run/.containerenv"
}