forked from opencontainers/umoci
/
create.bats
219 lines (183 loc) · 7.14 KB
/
create.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
#!/usr/bin/env bats -t
# umoci: Umoci Modifies Open Containers' Images
# Copyright (C) 2016-2020 SUSE LLC
#
# 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 init [missing args]" {
umoci init
[ "$status" -ne 0 ]
}
@test "umoci init --layout [empty]" {
# We are making a new image.
IMAGE="$(setup_tmpdir)/image"
# Create an empty layout.
umoci init --layout "$IMAGE"
[ "$status" -eq 0 ]
image-verify "$IMAGE"
# Make sure that there's no references or blobs.
sane_run find "$IMAGE/blobs" -type f
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
# Note that this is _very_ dodgy at the moment because of how complicated
# the reference handling is now.
# XXX: Make sure to update this for 1.0.0-rc6 where the refname changed.
sane_run jq -SMr '.manifests[]? | .annotations["org.opencontainers.ref.name"] | strings' "$IMAGE/index.json"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
# Make sure that the required files exist.
[ -f "$IMAGE/oci-layout" ]
[ -d "$IMAGE/blobs" ]
[ -d "$IMAGE/blobs/sha256" ]
[ -f "$IMAGE/index.json" ]
# Make sure that attempting to create a new layout will fail.
umoci init --layout "$IMAGE"
[ "$status" -ne 0 ]
image-verify "$IMAGE"
}
@test "umoci new [missing args]" {
umoci new
[ "$status" -ne 0 ]
}
@test "umoci new --image" {
# We are making a new image.
IMAGE="$(setup_tmpdir)/image" TAG="latest"
# Create an empty layout.
umoci init --layout "$IMAGE"
[ "$status" -eq 0 ]
image-verify "$IMAGE"
# Create a new image.
umoci new --image "${IMAGE}:${TAG}"
[ "$status" -eq 0 ]
# XXX: oci-image-tool validate doesn't like empty images (without layers)
#image-verify "$IMAGE"
# Modify the config.
umoci config --image "${IMAGE}:${TAG}" --config.user "1234:1332"
[ "$status" -eq 0 ]
# XXX: oci-image-tool validate doesn't like empty images (without layers)
#image-verify "$IMAGE"
# Unpack the image.
new_bundle_rootfs
umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
[ "$status" -eq 0 ]
bundle-verify "$BUNDLE"
# Make sure that the rootfs is empty.
sane_run find "$ROOTFS"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 1 ]
# Make sure that the config applied.
sane_run jq -SM '.process.user.uid' "$BUNDLE/config.json"
[ "$status" -eq 0 ]
[ "$output" -eq 1234 ]
# Make sure numeric config was actually set.
sane_run jq -SM '.process.user.gid' "$BUNDLE/config.json"
[ "$status" -eq 0 ]
[ "$output" -eq 1332 ]
# Make sure additionalGids were not set.
sane_run jq -SMr '.process.user.additionalGids' "$BUNDLE/config.json"
[ "$status" -eq 0 ]
[[ "$output" == "null" ]]
# Check that the history looks sane.
umoci stat --image "${IMAGE}:${TAG}" --json
[ "$status" -eq 0 ]
# There should be no non-empty_layers.
[[ "$(echo "$output" | jq -SM '[.history[] | .empty_layer == null] | any')" == "false" ]]
# XXX: oci-image-tool validate doesn't like empty images (without layers)
#image-verify "$IMAGE"
}
# Given the bad experiences we've had with Go compiler changes resulting in
# inconsistent archive output, this is a simple test to check whether a Go
# compiler update will change our expected hashes seriously. We want to be as
# reproducible-builds friendly as possible.
#
# Note that we cannot do anything here that involves a JSON map because this
# translates to a Go map which then causes *guaranteed* non-deterministic
# behaviour and thus non-deterministic archives. Do you ever get the feeling
# that sometimes you have to cut your losses and actually do something better
# than to sit and suffer? Well, this is how it feels.
#
# Oh and it turns out that this issue can be triggered by more than just
# archive/tar. It turns out that pgzip has changed their output in the past
# <https://github.com/klauspost/compress/pull/105> which means that our
# dependencies can also trigger this issue. And the best part is that you have
# to decide whether you want better compression or consistent output between
# versions.
@test "umoci [archive/tar regressions]" {
# Setup up $IMAGE.
IMAGE="$(setup_tmpdir)/image"
# Create a new image with no tags.
umoci init --layout "$IMAGE"
[ "$status" -eq 0 ]
image-verify "$IMAGE"
# Create a new image with another tag.
umoci new --image "${IMAGE}:${TAG}"
[ "$status" -eq 0 ]
# XXX: oci-image-tool validate doesn't like empty images (without layers)
#image-verify "$IMAGE"
# Unpack the image.
new_bundle_rootfs
umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
[ "$status" -eq 0 ]
bundle-verify "$BUNDLE"
# Create some files. We have to make sure all of the {atime,mtime} are
# consistent as well as the image metadata -- so pick a specific date/time.
epoch="1997-03-25T13:40:00+01:00"
mkdir -p "$ROOTFS"/{usr/bin,etc,var/run}
mkfifo "$ROOTFS/var/run/test.fifo"
echo "#!/usr/bin/true" > "$ROOTFS/usr/bin/bash" ; chmod a+x "$ROOTFS/usr/bin/bash"
ln "$ROOTFS/usr/bin/bash" "$ROOTFS/usr/bin/sh"
ln "$ROOTFS/usr/bin/bash" "$ROOTFS/usr/bin/ash"
ln -s /usr/bin "$ROOTFS/bin"
ln -s /var/run "$ROOTFS/run"
# Make sure everything has an {mtime,atime} of $epoch. We have to do this
# before and after the chmod to make sure we can set the times in
# --rootless mode.
find "$ROOTFS" -print0 | xargs -0 touch --no-dereference --date="$epoch"
chmod a-rwx "$ROOTFS/var" && touch --date="$epoch" "$ROOTFS/var"
# Repack the image.
umoci repack --image "${IMAGE}:${TAG}" \
--history.created="$epoch" "$BUNDLE"
[ "$status" -eq 0 ]
image-verify "$IMAGE"
# Modify the configuration.
umoci config --image "${IMAGE}:${TAG}" \
--config.user="1000:100" --config.cmd="/bin/sh" \
--os="linux" --architecture="amd64" \
--created="$epoch" --history.created="$epoch"
[ "$status" -eq 0 ]
image-verify "$IMAGE"
# Remove unused blobs.
umoci gc --layout "$IMAGE"
[ "$status" -eq 0 ]
image-verify "$IMAGE"
# To allow us to debug issues with the checksums, output them to the log.
find "$IMAGE" -type f -print0 | xargs -0 sha256sum
# Verify that the hashes of the blobs and index match (blobs are
# content-addressable so using hashes is a bit silly, but whatever).
known_hashes=(
"4e15e3699a98dddc67cb2e9aa5a6135cdf8ffcbdc7963daf959395f73dd52849 $IMAGE/index.json"
"54d5cbf998d9b1185628128d83af76ca18425f037ef9f79e6900f7e26985c169 $IMAGE/blobs/sha256/54d5cbf998d9b1185628128d83af76ca18425f037ef9f79e6900f7e26985c169"
"e7013826daf8b5d68f82c5b790ca5e9de222a834f2cb3fe3532030161bd72083 $IMAGE/blobs/sha256/e7013826daf8b5d68f82c5b790ca5e9de222a834f2cb3fe3532030161bd72083"
"f4a39a97d97aa834da7ad2d92940f9636a57e3d9b3cc7c53242451b02a6cea89 $IMAGE/blobs/sha256/f4a39a97d97aa834da7ad2d92940f9636a57e3d9b3cc7c53242451b02a6cea89"
)
sha256sum -c <(printf '%s\n' "${known_hashes[@]}")
image-verify "$IMAGE"
}