-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathstructure_test_tests.sh
More file actions
executable file
·313 lines (262 loc) · 9.89 KB
/
Copy pathstructure_test_tests.sh
File metadata and controls
executable file
·313 lines (262 loc) · 9.89 KB
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
#!/usr/bin/env bash
# Copyright 2017 Google Inc. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#Structure test tests. The tests to test the structure tests.
#End to end tests to make sure the structure tests do what we expect them
#to do on a known quantity, the latest debian docker image.
failures=0
# Get the architecture to load the right configurations
go_architecture=$(go env GOARCH)
# Get the absolute path of the tests directory
test_dir="$( cd "$(dirname "$0")" || return >/dev/null 2>&1 ; pwd -P )"
test_config_dir="${test_dir}/${go_architecture}"
# If a configuration folder for the architecture doesn't exist, default to amd64
test -d ${test_config_dir} || test_config_dir="${test_dir}/amd64"
function HEADER() {
local msg="$1"
echo ""
echo "###############"
echo "# $msg"
echo "###############"
echo ""
}
function build_image() {
_file=$1
_tag=$2
_dir=$3
docker build -q -f "$_dir/$_file" --tag "$_tag" "$_dir" > /dev/null
}
HEADER "Determine the runtime"
DOCKER=""
if which docker > /dev/null; then
DOCKER=$(which docker)
echo "Using docker at $(which docker)"
elif which podman > /dev/null; then
DOCKER=$(which podman)
echo "Using podman at $(which podman)"
else
echo "Could not find a runtime to run tests"
exit 1
fi
docker() {
$DOCKER "$@"
}
HEADER "Build the newest 'container structure test' binary"
cp -f "${test_dir}/Dockerfile" "${test_dir}/../Dockerfile"
make DOCKER=$DOCKER
make DOCKER=$DOCKER cross
make DOCKER=$DOCKER image
# Run the ubuntu tests, they should always pass on 22.04
test_image="ubuntu:22.04"
docker pull "$test_image" > /dev/null
HEADER "Positive Test Case"
res=$(./out/container-structure-test test --image "$test_image" --config "${test_config_dir}/ubuntu_22_04_test.yaml")
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: Success test case failed"
echo "$res"
failures=$((failures +1))
else
echo "PASS: Success test case passed"
fi
HEADER "Container Run Options Test Cases"
test_containeropts_user_image="test.local/ubuntu-unprivileged:latest"
build_image "Dockerfile.unprivileged" "$test_containeropts_user_image" "$test_dir"
res=$(./out/container-structure-test test --image "$test_containeropts_user_image" --config "${test_config_dir}/ubuntu_22_04_containeropts_user_test.yaml")
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: Run options (user) test case failed"
echo "$res"
failures=$((failures +1))
else
echo "PASS: Run option (user) test case passed"
fi
docker rmi "$test_containeropts_user_image" > /dev/null
test_containeropts_cap_image="test.local/ubuntu-cap:latest"
build_image "Dockerfile.cap" "$test_containeropts_cap_image" "$test_dir"
res=$(./out/container-structure-test test --image "$test_containeropts_cap_image" --config "${test_config_dir}/ubuntu_22_04_containeropts_test.yaml")
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: Run options (capabilities, bindMounts) test case failed"
echo "$res"
failures=$((failures +1))
else
echo "PASS: Run options (capabilities, bindMounts) test case passed"
fi
docker rmi "$test_containeropts_cap_image" > /dev/null
res=$(FOO='keepitsecret!' BAR='keepitsafe!' ./out/container-structure-test test --image "$test_image" --config "${test_config_dir}/ubuntu_22_04_containeropts_env_test.yaml")
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: Run options (envVars) test case failed"
echo "$res"
failures=$((failures +1))
else
echo "PASS: Run options (envVars) test case passed"
fi
res=$(./out/container-structure-test test --image "$test_image" --config "${test_config_dir}/ubuntu_22_04_containeropts_envfile_test.yaml")
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: Run options (envFile) test case failed"
echo "$res"
failures=$((failures +1))
else
echo "PASS: Run options (envFile) test case passed"
fi
res=$(./out/container-structure-test test --image "$test_image" --config "${test_config_dir}/ubuntu_22_04_containeropts_network_none_test.yaml")
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: Run options (network none) test case failed"
echo "$res"
failures=$((failures +1))
else
echo "PASS: Run options (network none) test case passed"
fi
res=$(./out/container-structure-test test --image "$test_image" --config "${test_config_dir}/ubuntu_22_04_containeropts_network_host_test.yaml")
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: Run options (network host) test case failed"
echo "$res"
failures=$((failures +1))
else
echo "PASS: Run options (network host) test case passed"
fi
HEADER "Metadata Test Case"
# test image metadata
run_metadata_tests=true
if $run_metadata_tests ;
then
test_metadata_image=test.local/debian8-with-metadata:latest
test_metadata_tar=debian8-with-metadata.tar
test_metadata_dir=debian8-with-metadata
build_image "Dockerfile.metadata" "$test_metadata_image" "$test_dir"
res=$(./out/container-structure-test test --image "$test_metadata_image" --config "${test_config_dir}/ubuntu_22_04_metadata_test.yaml")
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: Metadata success test case for docker driver"
echo "$res"
failures=$((failures +1))
else
echo "PASS: Metadata success test case for docker driver"
fi
docker save "$test_metadata_image" -o "$test_metadata_tar" > /dev/null
res=$(./out/container-structure-test test --driver tar --image "$test_metadata_tar" --config "${test_config_dir}/ubuntu_22_04_metadata_test.yaml")
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: Metadata success test case for tar driver"
echo "$res"
failures=$((failures +1))
else
echo "PASS: Metadata success test case for tar driver"
fi
mkdir -p "$test_metadata_dir"
tar -C "$test_metadata_dir" -xf "$test_metadata_tar" > /dev/null
test_metadata_json=$(jq -r '.[0].Config' "$test_metadata_dir/manifest.json")
res=$(./out/container-structure-test test --driver host --force --metadata "$test_metadata_dir/$test_metadata_json" --config "${test_config_dir}/ubuntu_22_04_metadata_test.yaml")
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: Metadata success test case for host driver"
echo "$res"
failures=$((failures +1))
else
echo "PASS: Metadata success test case for host driver"
fi
rm -rf "$test_metadata_dir"
rm "$test_metadata_tar"
docker rmi "$test_metadata_image" > /dev/null
fi
HEADER "Failure Test Case"
# Run some bogus tests, they should fail as expected
res=$(./out/container-structure-test test --image "$test_image" --config "${test_config_dir}/ubuntu_22_04_failure_test.yaml")
code=$?
if ! [[ ("$res" =~ "FAIL" && "$code" == "1") ]];
then
echo "FAIL: Failure test case did not fail"
echo "$res"
failures=$((failures +1))
else
echo "PASS: Failure test failed"
fi
HEADER "OCI layout test case"
go install github.com/google/go-containerregistry/cmd/crane
tmp="$(mktemp -d)"
"$(go env GOPATH)/bin/crane" pull "$test_image" --format=oci "$tmp" --platform="linux/$go_architecture"
res=$(./out/container-structure-test test --image-from-oci-layout="$tmp" --config "${test_config_dir}/ubuntu_22_04_test.yaml" 2>&1)
code=$?
if ! [[ ("$res" =~ index\ does\ not\ contain\ a\ reference\ annotation\.\ \-\-default\-image\-tag\ must\ be\ provided\. && "$code" == "1") ]];
then
echo "FAIL: oci failing test case"
echo "$res"
echo "$code"
failures=$((failures +1))
else
echo "PASS: oci failing test case"
fi
res=$(./out/container-structure-test test --image-from-oci-layout="$tmp" --default-image-tag="test.local/$test_image" --config "${test_config_dir}/ubuntu_22_04_test.yaml" 2>&1)
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: oci success test case"
echo "$res"
echo "$code"
failures=$((failures +1))
else
echo "PASS: oci success test case"
fi
HEADER "Platform test cases"
docker run --rm --privileged tonistiigi/binfmt --install all > /dev/null
res=$(./out/container-structure-test test --image "$test_image" --platform="linux/$go_architecture" --config "${test_config_dir}/ubuntu_22_04_test.yaml" 2>&1)
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: current host platform test case"
echo "$res"
echo "$code"
failures=$((failures +1))
else
echo "PASS: current host platform test case"
fi
res=$(./out/container-structure-test test --image "$test_image" --platform="linux/riscv64" --config "${test_config_dir}/ubuntu_22_04_test.yaml" 2>&1)
code=$?
if ! [[ ("$res" =~ image\ with\ reference.+was\ found\ but\ does\ not\ provide\ the\ specified\ platform\ \(linux\/riscv64\) || "$res" =~ image\ with\ reference.+was\ found\ but\ its\ platform\ \(linux\/${go_architecture}\)\ does\ not\ match\ the\ specified\ platform\ \(linux\/riscv64\)) && "$code" == "1" ]];
then
echo "FAIL: platform failing test case"
echo "$res"
echo "$code"
failures=$((failures +1))
else
echo "PASS: platform failing test case"
fi
test_config_dir="${test_dir}/s390x"
res=$(./out/container-structure-test test --image "$test_image" --platform="linux/s390x" --pull --config "${test_config_dir}/ubuntu_22_04_test.yaml" 2>&1)
code=$?
if ! [[ ("$res" =~ "PASS" && "$code" == "0") ]];
then
echo "FAIL: platform w/ --pull test case"
echo "$res"
echo "$code"
failures=$((failures +1))
else
echo "PASS: platform w/ --pull test case"
fi
if [ $failures -gt 0 ]; then
echo "Some tests did not pass. $failures"
exit 1
fi