/
inspect.go
429 lines (403 loc) · 12.1 KB
/
inspect.go
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package inspect
import (
"encoding/json"
"io"
"os"
"path/filepath"
"reflect"
"sort"
"testing"
"github.com/apptainer/apptainer/e2e/internal/e2e"
"github.com/apptainer/apptainer/e2e/internal/testhelper"
"github.com/apptainer/apptainer/internal/pkg/test/tool/exec"
"github.com/apptainer/apptainer/internal/pkg/test/tool/require"
"github.com/apptainer/apptainer/pkg/image"
"github.com/apptainer/apptainer/pkg/inspect"
)
type ctx struct {
env e2e.TestEnv
}
const (
containerTesterDEF = "testdata/inspector_container.def"
)
//nolint:maintidx
func (c ctx) apptainerInspect(t *testing.T) {
testDir, cleanup := e2e.MakeTempDir(t, c.env.TestDir, "inspect-", "")
defer cleanup(t)
sifImage := filepath.Join(testDir, "image.sif")
squashImage := filepath.Join(testDir, "image.sqs")
sandboxImage := filepath.Join(testDir, "sandbox")
c.env.RunApptainer(
t,
e2e.WithProfile(e2e.RootProfile),
e2e.WithCommand("build"),
e2e.WithArgs("-F", sifImage, containerTesterDEF),
e2e.PostRun(func(t *testing.T) {
if t.Failed() {
return
}
img, err := image.Init(sifImage, false)
if err != nil {
t.Fatalf("failed to open %s: %s", sifImage, err)
}
r, err := image.NewPartitionReader(img, image.RootFs, -1)
if err != nil {
t.Fatalf("failed to get root partition: %s", err)
}
f, err := os.Create(squashImage)
if err != nil {
t.Fatalf("failed to create %s: %s", squashImage, err)
}
defer f.Close()
if _, err := io.Copy(f, r); err != nil {
t.Fatalf("failed to copy squash image %s: %s", squashImage, err)
}
}),
e2e.ExpectExit(0),
)
require.Command(t, "unsquashfs")
// First try with -user-xattrs since unsquashfs 4.4 gives an error code if
// it can't set system xattrs while rootless.
cmd := exec.Command("unsquashfs", "-user-xattrs", "-d", sandboxImage, squashImage)
if res := cmd.Run(t); res.Error != nil {
t.Fatalf("Unexpected error while running command.\n%s", res)
}
compareLabel := func(label, expected string, appName string) func(*testing.T, *inspect.Metadata) {
return func(t *testing.T, meta *inspect.Metadata) {
v := meta.Attributes.Labels[label]
if appName != "" && meta.Attributes.Apps[appName] != nil {
v = meta.Attributes.Apps[appName].Labels[label]
}
if v != expected {
t.Errorf("unexpected %s label value, got %s instead of %s", label, v, expected)
}
}
}
tests := []struct {
name string
insType string // insType the type of 'inspect' flag, eg. '--deffile'
appName string // appName append --app <name> to the inspect command
compareFn func(*testing.T, *inspect.Metadata) // json is the path to a value that we will test
}{
{
name: "label maintainer",
insType: "--labels",
compareFn: compareLabel("MAINTAINER", "\"WestleyK <westley@sylabs.io>\"", ""),
},
{
name: "label_E2E",
insType: "--labels",
compareFn: compareLabel("E2E", "AWESOME", ""),
},
{
name: "label_HI",
insType: "--labels",
compareFn: compareLabel("HI", "\"HELLO WORLD\"", ""),
},
{
name: "label_e2e",
insType: "--labels",
compareFn: compareLabel("e2e", "awesome", ""),
},
{
name: "label_hi",
insType: "--labels",
compareFn: compareLabel("hi", "\"hello world\"", ""),
},
{
name: "build_label_first",
insType: "--labels",
compareFn: compareLabel("first.build.labels", "first", ""),
},
{
name: "build_label_second",
insType: "--labels",
compareFn: compareLabel("second.build.labels", "second", ""),
},
{
name: "label_org.label-schema.usage",
insType: "--labels",
compareFn: compareLabel("org.label-schema.usage", "/.singularity.d/runscript.help", ""),
},
{
name: "label_org.label-schema.usage.singularity.deffile.bootstrap",
insType: "--labels",
compareFn: compareLabel("org.label-schema.usage.singularity.deffile.bootstrap", "oras", ""),
},
{
name: "label_org.label-schema.usage.singularity.deffile.from",
insType: "--labels",
compareFn: compareLabel("org.label-schema.usage.singularity.deffile.from", "ghcr.io/apptainer/alpine:3.15.0", ""),
},
{
name: "label_org.label-schema.usage.apptainer.runscript.help",
insType: "--labels",
compareFn: compareLabel("org.label-schema.usage.apptainer.runscript.help", "/.singularity.d/runscript.help", ""),
},
{
name: "deffile",
insType: "--deffile",
compareFn: func(t *testing.T, meta *inspect.Metadata) {
out := "bootstrap:"
v := meta.Attributes.Deffile[0:len(out)]
if v != out {
t.Errorf("unexpected deffile output, got '%s' instead of '%s'", v, out)
}
},
},
{
name: "runscript",
insType: "--runscript",
compareFn: func(t *testing.T, meta *inspect.Metadata) {
out := "#!/bin/sh\n\ncat /.singularity.d/runscript.help"
v := meta.Attributes.Runscript
if v != out {
t.Errorf("unexpected runscript output, got %s instead of %s", v, out)
}
},
},
{
name: "startscript",
insType: "--startscript",
compareFn: func(t *testing.T, meta *inspect.Metadata) {
out := "#!/bin/sh\n\nexec \"$@\""
v := meta.Attributes.Startscript
if v != out {
t.Errorf("unexpected startscript output, got %s instead of %s", v, out)
}
},
},
{
name: "list apps",
insType: "--list-apps",
compareFn: func(t *testing.T, meta *inspect.Metadata) {
out := []string{"hello", "world"}
apps := make([]string, 0, len(meta.Attributes.Apps))
for app := range meta.Attributes.Apps {
apps = append(apps, app)
}
sort.Strings(apps)
if !reflect.DeepEqual(apps, out) {
t.Errorf("unexpected apps returned, got %v instead of %v", apps, out)
}
},
},
{
name: "test",
insType: "--test",
compareFn: func(t *testing.T, meta *inspect.Metadata) {
out := "#!/bin/sh\n\nls /\ntest -d /\ntest -d /etc"
v := meta.Attributes.Test
if v != out {
t.Errorf("unexpected testscript output, got %s instead of %s", v, out)
}
},
},
{
name: "helpfile",
insType: "--helpfile",
compareFn: func(t *testing.T, meta *inspect.Metadata) {
helpFile := "/.singularity.d/runscript.help"
out := "This is a e2e test container used for testing the 'inspect'\ncommand. This container \"inspector_container.sif\" should be placed\nin the \"e2e/testdata\" directory of Apptainer."
v := meta.Attributes.Helpfile
if v != out {
t.Errorf("unexpected %s output, got %s instead of %s", helpFile, v, out)
}
},
},
{
name: "environment",
insType: "--environment",
compareFn: func(t *testing.T, meta *inspect.Metadata) {
envFile := "/.singularity.d/env/90-environment.sh"
out := "#!/bin/sh\n# Custom environment shell code should follow\n\n\nexport test=\"testing\"\nexport e2e=\"e2e testing\""
v := meta.Attributes.Environment[envFile]
if v != out {
t.Errorf("unexpected environment for %s, got %s instead of %s", envFile, v, out)
}
envFile = "/.singularity.d/env/91-environment.sh"
out = "export hello=\"world\""
v = meta.Attributes.Environment[envFile]
if v != out {
t.Errorf("unexpected environment for %s, got %s instead of %s", envFile, v, out)
}
},
},
{
name: "label app hello",
insType: "--labels",
appName: "hello",
compareFn: compareLabel("HELLOTHISIS", "hello", "hello"),
},
{
name: "help app hello",
insType: "--helpfile",
appName: "hello",
compareFn: func(t *testing.T, meta *inspect.Metadata) {
out := "This is the help for hello!"
if a, ok := meta.Attributes.Apps["hello"]; ok {
v := a.Helpfile
if v != out {
t.Errorf("unexpected testscript output, got %s instead of %s", v, out)
}
} else {
t.Errorf("hello app not found")
}
},
},
{
name: "env app hello",
insType: "--environment",
appName: "hello",
compareFn: func(t *testing.T, meta *inspect.Metadata) {
envFile := "/scif/apps/hello/scif/env/90-environment.sh"
out := "HELLOTHISIS=hello\nexport HELLOTHISIS"
if a, ok := meta.Attributes.Apps["hello"]; ok {
v := a.Environment[envFile]
if v != out {
t.Errorf("unexpected environment for %s, got %s instead of %s", envFile, v, out)
}
} else {
t.Errorf("hello app not found")
}
},
},
{
name: "runscript app hello",
insType: "--runscript",
appName: "hello",
compareFn: func(t *testing.T, meta *inspect.Metadata) {
out := "#!/bin/sh\n\necho \"hello\""
if a, ok := meta.Attributes.Apps["hello"]; ok {
v := a.Runscript
if v != out {
t.Errorf("unexpected runscript output, got %s instead of %s", v, out)
}
} else {
t.Errorf("hello app not found")
}
},
},
{
name: "test app hello",
insType: "--test",
appName: "hello",
compareFn: func(t *testing.T, meta *inspect.Metadata) {
out := "#!/bin/sh\n\necho \"THIS IS A HELLO TEST\""
if a, ok := meta.Attributes.Apps["hello"]; ok {
v := a.Test
if v != out {
t.Errorf("unexpected testscript output, got %s instead of %s", v, out)
}
} else {
t.Errorf("hello app not found")
}
},
},
{
name: "runscript app world",
insType: "--runscript",
appName: "world",
compareFn: func(t *testing.T, meta *inspect.Metadata) {
out := "#!/bin/sh\n\necho \"world\""
if a, ok := meta.Attributes.Apps["world"]; ok {
v := a.Runscript
if v != out {
t.Errorf("unexpected runscript output, got %s instead of %s", v, out)
}
} else {
t.Errorf("world app not found")
}
},
},
}
for _, tt := range tests {
// Inspect the container, and get the output
compareOutput := func(t *testing.T, r *e2e.ApptainerCmdResult) {
meta := new(inspect.Metadata)
if err := json.Unmarshal(r.Stdout, meta); err != nil {
t.Errorf("unable to parse json output: %s", err)
}
tt.compareFn(t, meta)
}
args := []string{"--json", tt.insType}
if tt.appName != "" {
args = append(args, "--app", tt.appName)
}
c.env.RunApptainer(
t,
e2e.AsSubtest("SIF/"+tt.name),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("inspect"),
e2e.WithArgs(append(args, sifImage)...),
e2e.ExpectExit(0, compareOutput),
)
c.env.RunApptainer(
t,
e2e.AsSubtest("Squash/"+tt.name),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("inspect"),
e2e.WithArgs(append(args, squashImage)...),
e2e.ExpectExit(0, compareOutput),
)
c.env.RunApptainer(
t,
e2e.AsSubtest("Sandbox/"+tt.name),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("inspect"),
e2e.WithArgs(append(args, sandboxImage)...),
e2e.ExpectExit(0, compareOutput),
)
}
// test --all
compareAll := func(t *testing.T, r *e2e.ApptainerCmdResult) {
meta := new(inspect.Metadata)
if err := json.Unmarshal(r.Stdout, meta); err != nil {
t.Errorf("unable to parse json output: %s", err)
}
for _, tt := range tests {
tt.compareFn(t, meta)
}
}
c.env.RunApptainer(
t,
e2e.AsSubtest("SIF/all"),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("inspect"),
e2e.WithArgs("--all", sifImage),
e2e.ExpectExit(0, compareAll),
)
c.env.RunApptainer(
t,
e2e.AsSubtest("Squash/all"),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("inspect"),
e2e.WithArgs("--all", squashImage),
e2e.ExpectExit(0, compareAll),
)
c.env.RunApptainer(
t,
e2e.AsSubtest("Sandbox/all"),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("inspect"),
e2e.WithArgs("--all", sandboxImage),
e2e.ExpectExit(0, compareAll),
)
}
// E2ETests is the main func to trigger the test suite
func E2ETests(env e2e.TestEnv) testhelper.Tests {
c := ctx{
env: env,
}
return testhelper.Tests{
"inspect command": c.apptainerInspect,
}
}