forked from awslabs/soci-snapshotter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_standalone_test.go
More file actions
355 lines (285 loc) · 9.84 KB
/
convert_standalone_test.go
File metadata and controls
355 lines (285 loc) · 9.84 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
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
/*
Copyright The Soci Snapshotter Authors.
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.
*/
package integration
import (
"encoding/json"
"path/filepath"
"strings"
"testing"
"github.com/awslabs/soci-snapshotter/soci"
"github.com/awslabs/soci-snapshotter/util/dockershell"
"github.com/awslabs/soci-snapshotter/util/testutil"
"github.com/containerd/platforms"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
func TestStandaloneConvertBasic(t *testing.T) {
regConfig := newRegistryConfig()
sh, done := newShellWithRegistry(t, regConfig)
defer done()
rebootContainerd(t, sh, getContainerdConfigToml(t, false), getSnapshotterConfigToml(t))
imageRef := nginxImage
mirrorImg := regConfig.mirror(imageRef)
srcRef := mirrorImg.ref
srcDigest := getImageDigest(sh, srcRef)
baseDir, err := testutil.TempDir(sh)
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer sh.X("rm", "-rf", baseDir)
inputDir := filepath.Join(baseDir, "input")
inputTar := filepath.Join(baseDir, "input.tar")
copyImage(sh, dockerhub(imageRef), mirrorImg)
exportToOCIDir(sh, srcRef, inputDir)
sh.X("tar", "-cf", inputTar, "-C", inputDir, ".")
testCases := []struct {
name string
input string
output string
format string
}{
{"dir-to-tar", inputDir, filepath.Join(baseDir, "dt.tar"), "oci-archive"},
{"tar-to-tar", inputTar, filepath.Join(baseDir, "tt.tar"), "oci-archive"},
{"dir-to-dir", inputDir, filepath.Join(baseDir, "dd"), "oci-dir"},
{"tar-to-dir", inputTar, filepath.Join(baseDir, "td"), "oci-dir"},
}
stopContainerd(t, sh)
for _, tc := range testCases {
sh.X("soci", "convert",
"--standalone",
"--format", tc.format,
"--min-layer-size=0",
tc.input,
tc.output,
)
}
rebootContainerd(t, sh, getContainerdConfigToml(t, false), getSnapshotterConfigToml(t))
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
dstRef := srcRef + "-standalone-" + tc.name
importPath := tc.output
if tc.format == "oci-dir" {
// ctr images import requires a tar, so tar the directory output first
importPath = tc.output + ".tar"
sh.X("tar", "-cf", importPath, "-C", tc.output, ".")
}
sh.X("ctr", "images", "import", "--no-unpack", "--index-name", dstRef, importPath)
dstDigest := getImageDigest(sh, dstRef)
validateConversion(t, sh, srcDigest, dstDigest)
})
}
}
func TestStandaloneConvertSpecificPlatform(t *testing.T) {
regConfig := newRegistryConfig()
sh, done := newShellWithRegistry(t, regConfig)
defer done()
rebootContainerd(t, sh, getContainerdConfigToml(t, false), getSnapshotterConfigToml(t))
imageRef := nginxImage
mirrorImg := regConfig.mirror(imageRef)
platformStr := platforms.Format(mirrorImg.platform)
srcRef := mirrorImg.ref
srcDigest := getImageDigest(sh, srcRef)
baseDir, err := testutil.TempDir(sh)
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer sh.X("rm", "-rf", baseDir)
inputDir := filepath.Join(baseDir, "input")
outputTar := filepath.Join(baseDir, "output.tar")
copyImage(sh, dockerhub(imageRef), mirrorImg)
exportToOCIDir(sh, srcRef, inputDir)
stopContainerd(t, sh)
sh.X("soci", "convert",
"--standalone",
"--platform", platformStr,
"--min-layer-size=0",
inputDir,
outputTar,
)
rebootContainerd(t, sh, getContainerdConfigToml(t, false), getSnapshotterConfigToml(t))
dstRef := srcRef + "-standalone-platform"
sh.X("ctr", "images", "import", "--no-unpack", "--index-name", dstRef, outputTar)
dstDigest := getImageDigest(sh, dstRef)
validateConversion(t, sh, srcDigest, dstDigest)
}
func TestStandaloneInvalidConversion(t *testing.T) {
regConfig := newRegistryConfig()
sh, done := newShellWithRegistry(t, regConfig)
defer done()
t.Run("nonexistent input", func(t *testing.T) {
output, err := sh.CombinedOLog("soci", "convert",
"--standalone",
"/tmp/nonexistent-input",
"/tmp/output.tar",
)
if err == nil {
t.Fatal("expected error for nonexistent input")
}
outputStr := string(output)
if !strings.Contains(outputStr, "failed to access input") {
t.Fatalf("expected error about failed input access, got: %s", outputStr)
}
})
t.Run("invalid format", func(t *testing.T) {
output, err := sh.CombinedOLog("soci", "convert",
"--standalone",
"--format", "invalid",
"/tmp/some-input.tar",
"/tmp/output.tar",
)
if err == nil {
t.Fatal("expected error for invalid format")
}
outputStr := string(output)
if !strings.Contains(outputStr, "unsupported output format") {
t.Fatalf("expected error about unsupported format, got: %s", outputStr)
}
})
t.Run("missing destination", func(t *testing.T) {
output, err := sh.CombinedOLog("soci", "convert",
"--standalone",
"/tmp/some-input.tar",
)
if err == nil {
t.Fatal("expected error for missing destination")
}
outputStr := string(output)
if !strings.Contains(outputStr, "destination needs to be specified") {
t.Errorf("expected error about missing destination, got: %s", outputStr)
}
})
}
func TestStandaloneConvertIdempotent(t *testing.T) {
regConfig := newRegistryConfig()
sh, done := newShellWithRegistry(t, regConfig)
defer done()
rebootContainerd(t, sh, getContainerdConfigToml(t, false), getSnapshotterConfigToml(t))
imageRef := nginxImage
mirrorImg := regConfig.mirror(imageRef)
srcRef := mirrorImg.ref
baseDir, err := testutil.TempDir(sh)
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer sh.X("rm", "-rf", baseDir)
inputDir := filepath.Join(baseDir, "input")
outputTar1 := filepath.Join(baseDir, "output1.tar")
outputTar2 := filepath.Join(baseDir, "output2.tar")
copyImage(sh, dockerhub(imageRef), mirrorImg)
exportToOCIDir(sh, srcRef, inputDir)
stopContainerd(t, sh)
// First conversion
sh.X("soci", "convert",
"--standalone",
"--min-layer-size=0",
inputDir,
outputTar1,
)
// Second conversion of the already-converted image
sh.X("soci", "convert",
"--standalone",
"--min-layer-size=0",
outputTar1,
outputTar2,
)
rebootContainerd(t, sh, getContainerdConfigToml(t, false), getSnapshotterConfigToml(t))
dstRef1 := srcRef + "-standalone-soci1"
dstRef2 := srcRef + "-standalone-soci2"
sh.X("ctr", "images", "import", "--no-unpack", "--index-name", dstRef1, outputTar1)
sh.X("ctr", "images", "import", "--no-unpack", "--index-name", dstRef2, outputTar2)
digest1 := getImageDigest(sh, dstRef1)
digest2 := getImageDigest(sh, dstRef2)
if digest1 != digest2 {
t.Errorf("converting a SOCI-enabled image should be idempotent, but digests differ: %s vs %s", digest1, digest2)
}
}
func exportToOCIDir(sh *dockershell.Shell, imageRef, outputDir string, saveArgs ...string) {
exportTar := outputDir + ".export.tar"
sh.X(append(append([]string{"nerdctl", "save"}, saveArgs...), "-o", exportTar, imageRef)...)
sh.X("mkdir", "-p", outputDir)
sh.X("tar", "-xf", exportTar, "-C", outputDir)
sh.X("rm", "-f", exportTar)
}
func TestStandaloneConvertAllPlatforms(t *testing.T) {
regConfig := newRegistryConfig()
sh, done := newShellWithRegistry(t, regConfig)
defer done()
rebootContainerd(t, sh, getContainerdConfigToml(t, false), getSnapshotterConfigToml(t))
// Pull and save the multi-arch image directly from its public registry.
srcRef := dockerhub(nginxImage).ref
baseDir, err := testutil.TempDir(sh)
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer sh.X("rm", "-rf", baseDir)
inputDir := filepath.Join(baseDir, "input")
outputTar := filepath.Join(baseDir, "output.tar")
sh.X("nerdctl", "pull", "-q", "--all-platforms", srcRef)
exportToOCIDir(sh, srcRef, inputDir, "--all-platforms")
srcDigest := getImageDigest(sh, srcRef)
var srcPlatforms []string
for _, m := range readIndex(t, sh, srcDigest).Manifests {
if m.Platform != nil {
srcPlatforms = append(srcPlatforms, platforms.Format(*m.Platform))
}
}
if len(srcPlatforms) < 2 {
t.Skipf("expected multi-arch input, got %d platforms", len(srcPlatforms))
}
stopContainerd(t, sh)
sh.X("soci", "convert",
"--standalone",
"--all-platforms",
"--min-layer-size=0",
inputDir,
outputTar,
)
rebootContainerd(t, sh, getContainerdConfigToml(t, false), getSnapshotterConfigToml(t))
dstRef := srcRef + "-standalone-allplatforms"
sh.X("ctr", "images", "import", "--no-unpack", "--index-name", dstRef, outputTar)
dstDigest := getImageDigest(sh, dstRef)
validateConversion(t, sh, srcDigest, dstDigest)
imgs, sociIdx := map[string]bool{}, map[string]bool{}
for _, m := range readIndex(t, sh, dstDigest).Manifests {
if m.Platform == nil {
continue
}
key := platforms.Format(*m.Platform)
switch m.ArtifactType {
case soci.SociIndexArtifactTypeV2:
sociIdx[key] = true
case "":
imgs[key] = true
}
}
for _, p := range srcPlatforms {
if !imgs[p] {
t.Errorf("converted output missing image manifest for %s", p)
}
if !sociIdx[p] {
t.Errorf("converted output missing SOCI index for %s", p)
}
}
}
// readIndex fetches an OCI image index from the content store, unwrapping a
// single-manifest wrapper (as produced by `ctr images import --index-name`).
func readIndex(t *testing.T, sh *dockershell.Shell, indexDigest string) ocispec.Index {
t.Helper()
var idx ocispec.Index
if err := json.Unmarshal(sh.O("ctr", "content", "get", indexDigest), &idx); err != nil {
t.Fatalf("parse index %s: %v", indexDigest, err)
}
if len(idx.Manifests) == 1 && idx.Manifests[0].MediaType == ocispec.MediaTypeImageIndex {
return readIndex(t, sh, idx.Manifests[0].Digest.String())
}
return idx
}