@@ -27,6 +27,7 @@ import (
27
27
28
28
"github.com/containerd/platforms"
29
29
30
+ "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
30
31
"github.com/containerd/nerdctl/v2/pkg/testutil"
31
32
)
32
33
@@ -41,7 +42,7 @@ func TestBuild(t *testing.T) {
41
42
CMD ["echo", "nerdctl-build-test-string"]
42
43
` , testutil .CommonImage )
43
44
44
- buildCtx := createBuildContext (t , dockerfile )
45
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
45
46
46
47
base .Cmd ("build" , "-t" , imageName , buildCtx ).AssertOK ()
47
48
base .Cmd ("build" , buildCtx , "-t" , imageName ).AssertOK ()
@@ -66,7 +67,7 @@ func TestBuildIsShareableForCompatiblePlatform(t *testing.T) {
66
67
CMD ["echo", "nerdctl-build-test-string"]
67
68
` , testutil .CommonImage )
68
69
69
- buildCtx := createBuildContext (t , dockerfile )
70
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
70
71
71
72
base .Cmd ("build" , buildCtx , "-t" , imageName ).AssertErrNotContains ("tarball" )
72
73
@@ -99,7 +100,7 @@ RUN echo hello > /hello
99
100
CMD ["echo", "nerdctl-build-test-string"]
100
101
` , testutil .CommonImage )
101
102
102
- buildCtx := createBuildContext (t , dockerfile )
103
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
103
104
104
105
base .Cmd ("build" , "-t" , imageName , buildCtx ).AssertOK ()
105
106
base .Cmd ("build" , buildCtx , "-t" , imageName ).AssertOK ()
@@ -109,7 +110,7 @@ RUN echo hello2 > /hello2
109
110
CMD ["cat", "/hello2"]
110
111
` , imageName )
111
112
112
- buildCtx2 := createBuildContext (t , dockerfile2 )
113
+ buildCtx2 := helpers . CreateBuildContext (t , dockerfile2 )
113
114
114
115
base .Cmd ("build" , "-t" , imageName2 , buildCtx2 ).AssertOK ()
115
116
base .Cmd ("build" , buildCtx2 , "-t" , imageName2 ).AssertOK ()
@@ -142,7 +143,7 @@ RUN echo hello2 > /hello2
142
143
CMD ["cat", "/hello2"]
143
144
` , imageName )
144
145
145
- buildCtx2 := createBuildContext (t , dockerfile2 )
146
+ buildCtx2 := helpers . CreateBuildContext (t , dockerfile2 )
146
147
147
148
base .Cmd ("build" , "-t" , imageName2 , buildCtx2 ).AssertOK ()
148
149
base .Cmd ("build" , buildCtx2 , "-t" , imageName2 ).AssertOK ()
@@ -208,7 +209,7 @@ func TestBuildLocal(t *testing.T) {
208
209
COPY %s /` ,
209
210
testFileName )
210
211
211
- buildCtx := createBuildContext (t , dockerfile )
212
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
212
213
213
214
if err := os .WriteFile (filepath .Join (buildCtx , testFileName ), []byte (testContent ), 0644 ); err != nil {
214
215
t .Fatal (err )
@@ -234,13 +235,6 @@ COPY %s /`,
234
235
assert .Equal (t , string (data ), testContent )
235
236
}
236
237
237
- func createBuildContext (t * testing.T , dockerfile string ) string {
238
- tmpDir := t .TempDir ()
239
- err := os .WriteFile (filepath .Join (tmpDir , "Dockerfile" ), []byte (dockerfile ), 0644 )
240
- assert .NilError (t , err )
241
- return tmpDir
242
- }
243
-
244
238
func TestBuildWithBuildArg (t * testing.T ) {
245
239
testutil .RequiresBuild (t )
246
240
testutil .RegisterBuildCacheCleanup (t )
@@ -254,7 +248,7 @@ ENV TEST_STRING=$TEST_STRING
254
248
CMD echo $TEST_STRING
255
249
` , testutil .CommonImage )
256
250
257
- buildCtx := createBuildContext (t , dockerfile )
251
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
258
252
259
253
base .Cmd ("build" , buildCtx , "-t" , imageName ).AssertOK ()
260
254
base .Cmd ("run" , "--rm" , imageName ).AssertOutExactly ("1\n " )
@@ -297,7 +291,7 @@ func TestBuildWithIIDFile(t *testing.T) {
297
291
CMD ["echo", "nerdctl-build-test-string"]
298
292
` , testutil .CommonImage )
299
293
300
- buildCtx := createBuildContext (t , dockerfile )
294
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
301
295
fileName := filepath .Join (t .TempDir (), "id.txt" )
302
296
303
297
base .Cmd ("build" , "-t" , imageName , buildCtx , "--iidfile" , fileName ).AssertOK ()
@@ -320,7 +314,7 @@ func TestBuildWithLabels(t *testing.T) {
320
314
LABEL name=nerdctl-build-test-label
321
315
` , testutil .CommonImage )
322
316
323
- buildCtx := createBuildContext (t , dockerfile )
317
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
324
318
325
319
base .Cmd ("build" , "-t" , imageName , buildCtx , "--label" , "label=test" ).AssertOK ()
326
320
defer base .Cmd ("rmi" , imageName ).Run ()
@@ -343,7 +337,7 @@ func TestBuildMultipleTags(t *testing.T) {
343
337
dockerfile := fmt .Sprintf (`FROM %s
344
338
CMD ["echo", "%s"]
345
339
` , testutil .CommonImage , output )
346
- buildCtx := createBuildContext (t , dockerfile )
340
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
347
341
348
342
base .Cmd ("build" , "-t" , img , buildCtx ).AssertOK ()
349
343
base .Cmd ("build" , buildCtx , "-t" , img , "-t" , imgWithNoTag , "-t" , imgWithCustomTag ).AssertOK ()
@@ -396,7 +390,7 @@ CMD ["echo", "dockerfile"]
396
390
err = os .WriteFile (filepath .Join (tmpDir , "Containerfile" ), []byte (containerfile ), 0644 )
397
391
assert .NilError (t , err )
398
392
399
- buildCtx := createBuildContext (t , dockerfile )
393
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
400
394
401
395
base .Cmd ("build" , "-t" , imageName , buildCtx ).AssertOK ()
402
396
base .Cmd ("run" , "--rm" , imageName ).AssertOutExactly ("dockerfile\n " )
@@ -411,7 +405,7 @@ func TestBuildNoTag(t *testing.T) {
411
405
dockerfile := fmt .Sprintf (`FROM %s
412
406
CMD ["echo", "nerdctl-build-notag-string"]
413
407
` , testutil .CommonImage )
414
- buildCtx := createBuildContext (t , dockerfile )
408
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
415
409
416
410
base .Cmd ("build" , buildCtx ).AssertOK ()
417
411
base .Cmd ("images" ).AssertOutContains ("<none>" )
@@ -426,7 +420,7 @@ func TestBuildContextDockerImageAlias(t *testing.T) {
426
420
427
421
dockerfile := `FROM myorg/myapp
428
422
CMD ["echo", "nerdctl-build-myorg/myapp"]`
429
- buildCtx := createBuildContext (t , dockerfile )
423
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
430
424
431
425
base .Cmd ("build" , buildCtx , fmt .Sprintf ("--build-context=myorg/myapp=docker-image://%s" , testutil .CommonImage )).AssertOK ()
432
426
base .Cmd ("images" ).AssertOutContains ("<none>" )
@@ -451,7 +445,7 @@ func TestBuildContextWithCopyFromDir(t *testing.T) {
451
445
COPY --from=dir2 /%s /hello_from_dir2.txt
452
446
RUN ["cat", "/hello_from_dir2.txt"]` , testutil .CommonImage , filename )
453
447
454
- buildCtx := createBuildContext (t , dockerfile )
448
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
455
449
456
450
base .Cmd ("build" , buildCtx , fmt .Sprintf ("--build-context=dir2=%s" , dir2 )).AssertOK ()
457
451
base .Cmd ("images" ).AssertOutContains ("<none>" )
@@ -472,7 +466,7 @@ RUN echo $SOURCE_DATE_EPOCH >/source-date-epoch
472
466
CMD ["cat", "/source-date-epoch"]
473
467
` , testutil .CommonImage )
474
468
475
- buildCtx := createBuildContext (t , dockerfile )
469
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
476
470
477
471
const sourceDateEpochEnvStr = "1111111111"
478
472
base .Env = append (base .Env , "SOURCE_DATE_EPOCH=" + sourceDateEpochEnvStr )
@@ -493,7 +487,7 @@ func TestBuildNetwork(t *testing.T) {
493
487
RUN apk add --no-cache curl
494
488
RUN curl -I http://google.com
495
489
` , testutil .CommonImage )
496
- buildCtx := createBuildContext (t , dockerfile )
490
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
497
491
498
492
validCases := []struct {
499
493
name string
@@ -549,7 +543,7 @@ func TestBuildAttestation(t *testing.T) {
549
543
}
550
544
551
545
dockerfile := "FROM " + testutil .NginxAlpineImage
552
- buildCtx := createBuildContext (t , dockerfile )
546
+ buildCtx := helpers . CreateBuildContext (t , dockerfile )
553
547
554
548
// Test sbom
555
549
outputSBOMDir := t .TempDir ()
0 commit comments