File: insert.bats

package info (click to toggle)
umoci 0.4.7%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,356 kB
  • sloc: sh: 436; makefile: 180; awk: 17
file content (371 lines) | stat: -rw-r--r-- 10,686 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env bats -t
# umoci: Umoci Modifies Open Containers' Images
# Copyright (C) 2016-2020 SUSE LLC
# Copyright (C) 2018 Cisco Systems
#
# 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.


load helpers

function setup() {
	setup_tmpdirs
	setup_image
}

function teardown() {
	teardown_tmpdirs
	teardown_image
}

@test "umoci insert" {
	# fail with too few arguments
	umoci insert --image "${IMAGE}:${TAG}"
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# ...and too many
	umoci insert --image "${IMAGE}:${TAG}" asdf 123 456
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Some things to insert.
	INSERTDIR="$(setup_tmpdir)"
	mkdir -p "${INSERTDIR}/test"
	touch "${INSERTDIR}/test/a"
	touch "${INSERTDIR}/test/b"
	chmod +x "${INSERTDIR}/test/b"

	# Make sure rootless mode works.
	mkdir -p "${INSERTDIR}/some/path"
	touch "${INSERTDIR}/some/path/hidden"
	chmod 000 "${INSERTDIR}/some/path"

	# Do a few inserts.
	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/test/a" /tester/a
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"

	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/test/b" /tester/b
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"

	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/test" /recursive
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"

	umoci insert --image "${IMAGE}:${TAG}" --tag "${TAG}-new" "${INSERTDIR}/some" /rootless
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"

	# Unpack after the inserts.
	new_bundle_rootfs
	umoci unpack --image "${IMAGE}:${TAG}-new" "$BUNDLE"
	[ "$status" -eq 0 ]
	bundle-verify "$BUNDLE"

	# ... and check to make sure it worked.
	[ -f "$ROOTFS/tester/a" ]
	[[ "$(stat -c '%f' "${INSERTDIR}/test/b")" == "$(stat -c '%f' "$ROOTFS/tester/b")" ]]
	[ -f "$ROOTFS/recursive/a" ]
	[ -f "$ROOTFS/recursive/b" ]

	# ... as well as the rootless portion.
	[ -d "$ROOTFS/rootless/path" ]
	[[ "$(stat -c '%f' "${INSERTDIR}/some/path")" == "$(stat -c '%f' "$ROOTFS/rootless/path")" ]]
	chmod a+rwx "$ROOTFS/rootless/path"
	[ -f "$ROOTFS/rootless/path/hidden" ]

	image-verify "${IMAGE}"
}

@test "umoci insert [invalid arguments]" {
	# Some things to insert.
	INSERTDIR="$(setup_tmpdir)"
	touch "$INSERTDIR/foobar"

	# Missing --image, source, and target argument.
	umoci insert
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Missing --image and target argument.
	umoci insert "$INSERTDIR"
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Missing source and target argument.
	umoci insert --image "${IMAGE}:${TAG}"
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Missing target argument.
	umoci insert --image "${IMAGE}:${TAG}" "$INSERTDIR"
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Unknown flag argument.
	umoci insert --this-is-an-invalid-argument \
		--image "${IMAGE}:${TAG}" "$INSERTDIR" /foo/bar
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Empty positional arguments.
	umoci insert --image "${IMAGE}:${TAG}" "$INSERTDIR" ""
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	umoci insert --image "${IMAGE}:${TAG}" "" /foo/bar
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Empty positional arguments (--whiteout).
	umoci insert --image "${IMAGE}:${TAG}" --whiteout ""
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Too many positional arguments.
	umoci insert --image "${IMAGE}:${TAG}" "$INSERTDIR" /foo/bar this-is-an-invalid-argument
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Too many positional arguments (--whiteout).
	umoci insert --image "${IMAGE}:${TAG}" --whiteout /foo/bar this-is-an-invalid-argument
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Empty image path.
	umoci insert --image ":${TAG}" "$INSERTDIR" /foo/bar
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Non-existent image path.
	umoci insert --image "${IMAGE}-doesnotexist:${TAG}" "$INSERTDIR" /foo/bar
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Empty image source tag.
	umoci insert --image "${IMAGE}:" "$INSERTDIR" /foo/bar
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Non-existent image source tag.
	umoci insert --image "${IMAGE}:${TAG}-doesnotexist" "$INSERTDIR" /foo/bar
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Invalid image source tag.
	umoci insert --image "${IMAGE}:${INVALID_TAG}" "$INSERTDIR" /foo/bar
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Empty image destination tag.
	umoci insert --image "${IMAGE}:${TAG}" --tag "" "$INSERTDIR" /foo/bar
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Invalid image destination tag.
	umoci insert --image "${IMAGE}:${TAG}" --tag "${INVALID_TAG}" "$INSERTDIR" /foo/bar
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Insertion of a file with a .wh. prefix must fail.
	umoci insert --image "${IMAGE}:${TAG}" "$INSERTDIR" /foo/bar/.wh.invalid-path
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"

	# Whiteout of a file with a .wh. prefix must fail.
	umoci insert --image "${IMAGE}:${TAG}" --whiteout /foo/bar/.wh.invalid-whiteout
	[ "$status" -ne 0 ]
	image-verify "${IMAGE}"
}

@test "umoci insert --opaque" {
	# Some things to insert.
	INSERTDIR="$(setup_tmpdir)"
	mkdir -p "${INSERTDIR}/etc"
	touch "${INSERTDIR}/etc/foo"

	# Insert our /etc.
	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/etc" /etc
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"

	# Make sure that the /etc/foo is there.
	new_bundle_rootfs
	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
	[ "$status" -eq 0 ]
	bundle-verify "$BUNDLE"

	# Make sure that it's merged!
	[ -f "$ROOTFS/etc/shadow" ]
	[ -f "$ROOTFS/etc/foo" ]

	# Now make it opaque to make sure it isn't included.
	INSERTDIR="$(setup_tmpdir)"
	mkdir -p "${INSERTDIR}/etc"
	touch "${INSERTDIR}/etc/bar"
	touch "${INSERTDIR}/should_be_fine"

	# Insert our /etc.
	umoci insert --image "${IMAGE}:${TAG}" --opaque "${INSERTDIR}/etc" /etc
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"
	# And try to make a file opaque just to see what happens (should be nothing).
	umoci insert --image "${IMAGE}:${TAG}" --opaque "${INSERTDIR}/should_be_fine" /should_be_fine
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"

	# Make sure that now only /etc/bar is around.
	new_bundle_rootfs
	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
	[ "$status" -eq 0 ]
	bundle-verify "$BUNDLE"

	# Make sure that it's _not_ merged!
	! [ -f "$ROOTFS/etc/shadow" ]
	! [ -f "$ROOTFS/etc/foo" ]
	# And that bar is there.
	[ -f "$ROOTFS/etc/bar" ]
	# And that should_be_fine is around.
	[ -f "$ROOTFS/should_be_fine" ]

	image-verify "${IMAGE}"
}

@test "umoci insert --whiteout" {
	# Some things to insert.
	INSERTDIR="$(setup_tmpdir)"
	touch "${INSERTDIR}/rm_file"
	mkdir "${INSERTDIR}/rm_dir"

	# Add our things.
	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/rm_file" /rm_file
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"
	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/rm_dir" /rm_dir
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"

	# Unpack after the inserts.
	new_bundle_rootfs
	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
	[ "$status" -eq 0 ]
	bundle-verify "$BUNDLE"

	[ -d "$ROOTFS/etc" ]
	[ -d "$ROOTFS/rm_dir" ]
	[ -f "$ROOTFS/rm_file" ]

	# Directory whiteout.
	umoci insert --image "${IMAGE}:${TAG}" --whiteout /rm_dir
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"

	# (Another) directory whiteout.
	umoci insert --image "${IMAGE}:${TAG}" --whiteout /etc
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"

	# File whiteout.
	umoci insert --image "${IMAGE}:${TAG}" --whiteout /rm_file
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"

	# Unpack after the inserts.
	new_bundle_rootfs
	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
	[ "$status" -eq 0 ]
	bundle-verify "$BUNDLE"

	! [ -d "$ROOTFS/etc" ]
	! [ -d "$ROOTFS/rm_dir" ]
	! [ -f "$ROOTFS/rm_file" ]

	image-verify "${IMAGE}"
}

@test "umoci insert --history.*" {
	# Some things to insert.
	INSERTDIR="$(setup_tmpdir)"
	mkdir -p "${INSERTDIR}/etc"
	touch "${INSERTDIR}/etc/foo"

	# umoci-insert will overwrite the tag.
	umoci tag --image "${IMAGE}:${TAG}" "${TAG}-new"
	[ "$status" -eq 0 ]

	# Insert something into the image, setting history values.
	now="$(date --iso-8601=seconds --utc)"
	umoci insert --image "${IMAGE}:${TAG}-new" \
		--history.author="Some Author <jane@blogg.com>" \
		--history.comment="Made a_small_change." \
		--history.created_by="insert ${INSERTDIR}" \
		--history.created="$now" "${INSERTDIR}/etc" /etc
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"

	# Make sure that the history was modified.
	umoci stat --image "${IMAGE}:${TAG}" --json
	[ "$status" -eq 0 ]
	numLinesA="$(echo "$output" | jq -SMr '.history | length')"

	umoci stat --image "${IMAGE}:${TAG}-new" --json
	[ "$status" -eq 0 ]
	numLinesB="$(echo "$output" | jq -SMr '.history | length')"

	# Number of lines should be greater.
	[ "$numLinesB" -gt "$numLinesA" ]
	# The final layer should not be an empty_layer now.
	[[ "$(echo "$output" | jq -SMr '.history[-1].empty_layer')" == "null" ]]
	# The author should've changed to --history.author.
	[[ "$(echo "$output" | jq -SMr '.history[-1].author')" == "Some Author <jane@blogg.com>" ]]
	# The comment should be added.
	[[ "$(echo "$output" | jq -SMr '.history[-1].comment')" == "Made a_small_change." ]]
	# The created_by should be set.
	[[ "$(echo "$output" | jq -SMr '.history[-1].created_by')" == "insert ${INSERTDIR}" ]]
	# The created should be set.
	[[ "$(date --iso-8601=seconds --utc --date="$(echo "$output" | jq -SMr '.history[-1].created')")" == "$now" ]]

	image-verify "${IMAGE}"
}

@test "umoci insert --no-history" {
	# Some things to insert.
	INSERTDIR="$(setup_tmpdir)"
	mkdir -p "${INSERTDIR}/etc"
	touch "${INSERTDIR}/etc/foo"

	# umoci-insert will overwrite the tag.
	umoci tag --image "${IMAGE}:${TAG}" "${TAG}-new"
	[ "$status" -eq 0 ]

	# Insert something into the image, but with no history change.
	umoci insert --no-history --image "${IMAGE}:${TAG}-new" "${INSERTDIR}/etc" /etc
	[ "$status" -eq 0 ]
	image-verify "${IMAGE}"

	# Make sure we *did not* add a new history entry.
	umoci stat --image "${IMAGE}:${TAG}" --json
	[ "$status" -eq 0 ]
	hashA="$(sha256sum <<<"$output")"

	umoci stat --image "${IMAGE}:${TAG}-new" --json
	[ "$status" -eq 0 ]
	hashB="$(sha256sum <<<"$output")"

	# umoci-stat output should be identical.
	[[ "$hashA" == "$hashB" ]]

	image-verify "${IMAGE}"
}