Skip to content

Commit c8df501

Browse files
committed
Move a bunch of helpers
AddIntFlag AddDurationFlag AddStringFlag checkExperimental createBuildContext testComposeUp requiresStargz newJWEKeyPair rmiAll Signed-off-by: apostasie <[email protected]>
1 parent 3b8a098 commit c8df501

26 files changed

+367
-312
lines changed

cmd/nerdctl/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func newBuilderPruneCommand() *cobra.Command {
5858
SilenceErrors: true,
5959
}
6060

61-
AddStringFlag(buildPruneCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
61+
helpers.AddStringFlag(buildPruneCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
6262

6363
buildPruneCommand.Flags().BoolP("all", "a", false, "Remove all unused build cache, not just dangling ones")
6464
buildPruneCommand.Flags().BoolP("force", "f", false, "Do not prompt for confirmation")
@@ -136,7 +136,7 @@ func newBuilderDebugCommand() *cobra.Command {
136136
var buildDebugCommand = &cobra.Command{
137137
Use: "debug",
138138
Short: shortHelp,
139-
PreRunE: checkExperimental("`nerdctl builder debug`"),
139+
PreRunE: helpers.CheckExperimental("`nerdctl builder debug`"),
140140
RunE: builderDebugAction,
141141
SilenceUsage: true,
142142
SilenceErrors: true,

cmd/nerdctl/builder_build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container
4444
SilenceUsage: true,
4545
SilenceErrors: true,
4646
}
47-
AddStringFlag(buildCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
47+
helpers.AddStringFlag(buildCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
4848
buildCommand.Flags().StringArrayP("tag", "t", nil, "Name and optionally a tag in the 'name:tag' format")
4949
buildCommand.Flags().StringP("file", "f", "", "Name of the Dockerfile")
5050
buildCommand.Flags().String("target", "", "Set the target build stage to build")

cmd/nerdctl/builder_build_linux_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"gotest.tools/v3/assert"
2424

25+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2526
"github.com/containerd/nerdctl/v2/pkg/testutil"
2627
)
2728

@@ -51,7 +52,7 @@ func TestBuildContextWithOCILayout(t *testing.T) {
5152
dockerfile := fmt.Sprintf(`FROM %s
5253
LABEL layer=oci-layout-parent
5354
CMD ["echo", "test-nerdctl-build-context-oci-layout-parent"]`, testutil.CommonImage)
54-
buildCtx := createBuildContext(t, dockerfile)
55+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
5556

5657
tarPath := fmt.Sprintf("%s/%s.tar", buildCtx, ociLayout)
5758

@@ -66,7 +67,7 @@ CMD ["echo", "test-nerdctl-build-context-oci-layout-parent"]`, testutil.CommonIm
6667

6768
dockerfile = fmt.Sprintf(`FROM %s
6869
CMD ["echo", "test-nerdctl-build-context-oci-layout"]`, ociLayout)
69-
buildCtx = createBuildContext(t, dockerfile)
70+
buildCtx = helpers.CreateBuildContext(t, dockerfile)
7071

7172
var buildArgs = []string{}
7273
if testutil.IsDocker() {

cmd/nerdctl/builder_build_test.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/containerd/platforms"
2929

30+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
3031
"github.com/containerd/nerdctl/v2/pkg/testutil"
3132
)
3233

@@ -41,7 +42,7 @@ func TestBuild(t *testing.T) {
4142
CMD ["echo", "nerdctl-build-test-string"]
4243
`, testutil.CommonImage)
4344

44-
buildCtx := createBuildContext(t, dockerfile)
45+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
4546

4647
base.Cmd("build", "-t", imageName, buildCtx).AssertOK()
4748
base.Cmd("build", buildCtx, "-t", imageName).AssertOK()
@@ -66,7 +67,7 @@ func TestBuildIsShareableForCompatiblePlatform(t *testing.T) {
6667
CMD ["echo", "nerdctl-build-test-string"]
6768
`, testutil.CommonImage)
6869

69-
buildCtx := createBuildContext(t, dockerfile)
70+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
7071

7172
base.Cmd("build", buildCtx, "-t", imageName).AssertErrNotContains("tarball")
7273

@@ -99,7 +100,7 @@ RUN echo hello > /hello
99100
CMD ["echo", "nerdctl-build-test-string"]
100101
`, testutil.CommonImage)
101102

102-
buildCtx := createBuildContext(t, dockerfile)
103+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
103104

104105
base.Cmd("build", "-t", imageName, buildCtx).AssertOK()
105106
base.Cmd("build", buildCtx, "-t", imageName).AssertOK()
@@ -109,7 +110,7 @@ RUN echo hello2 > /hello2
109110
CMD ["cat", "/hello2"]
110111
`, imageName)
111112

112-
buildCtx2 := createBuildContext(t, dockerfile2)
113+
buildCtx2 := helpers.CreateBuildContext(t, dockerfile2)
113114

114115
base.Cmd("build", "-t", imageName2, buildCtx2).AssertOK()
115116
base.Cmd("build", buildCtx2, "-t", imageName2).AssertOK()
@@ -142,7 +143,7 @@ RUN echo hello2 > /hello2
142143
CMD ["cat", "/hello2"]
143144
`, imageName)
144145

145-
buildCtx2 := createBuildContext(t, dockerfile2)
146+
buildCtx2 := helpers.CreateBuildContext(t, dockerfile2)
146147

147148
base.Cmd("build", "-t", imageName2, buildCtx2).AssertOK()
148149
base.Cmd("build", buildCtx2, "-t", imageName2).AssertOK()
@@ -208,7 +209,7 @@ func TestBuildLocal(t *testing.T) {
208209
COPY %s /`,
209210
testFileName)
210211

211-
buildCtx := createBuildContext(t, dockerfile)
212+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
212213

213214
if err := os.WriteFile(filepath.Join(buildCtx, testFileName), []byte(testContent), 0644); err != nil {
214215
t.Fatal(err)
@@ -234,13 +235,6 @@ COPY %s /`,
234235
assert.Equal(t, string(data), testContent)
235236
}
236237

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-
244238
func TestBuildWithBuildArg(t *testing.T) {
245239
testutil.RequiresBuild(t)
246240
testutil.RegisterBuildCacheCleanup(t)
@@ -254,7 +248,7 @@ ENV TEST_STRING=$TEST_STRING
254248
CMD echo $TEST_STRING
255249
`, testutil.CommonImage)
256250

257-
buildCtx := createBuildContext(t, dockerfile)
251+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
258252

259253
base.Cmd("build", buildCtx, "-t", imageName).AssertOK()
260254
base.Cmd("run", "--rm", imageName).AssertOutExactly("1\n")
@@ -297,7 +291,7 @@ func TestBuildWithIIDFile(t *testing.T) {
297291
CMD ["echo", "nerdctl-build-test-string"]
298292
`, testutil.CommonImage)
299293

300-
buildCtx := createBuildContext(t, dockerfile)
294+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
301295
fileName := filepath.Join(t.TempDir(), "id.txt")
302296

303297
base.Cmd("build", "-t", imageName, buildCtx, "--iidfile", fileName).AssertOK()
@@ -320,7 +314,7 @@ func TestBuildWithLabels(t *testing.T) {
320314
LABEL name=nerdctl-build-test-label
321315
`, testutil.CommonImage)
322316

323-
buildCtx := createBuildContext(t, dockerfile)
317+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
324318

325319
base.Cmd("build", "-t", imageName, buildCtx, "--label", "label=test").AssertOK()
326320
defer base.Cmd("rmi", imageName).Run()
@@ -343,7 +337,7 @@ func TestBuildMultipleTags(t *testing.T) {
343337
dockerfile := fmt.Sprintf(`FROM %s
344338
CMD ["echo", "%s"]
345339
`, testutil.CommonImage, output)
346-
buildCtx := createBuildContext(t, dockerfile)
340+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
347341

348342
base.Cmd("build", "-t", img, buildCtx).AssertOK()
349343
base.Cmd("build", buildCtx, "-t", img, "-t", imgWithNoTag, "-t", imgWithCustomTag).AssertOK()
@@ -396,7 +390,7 @@ CMD ["echo", "dockerfile"]
396390
err = os.WriteFile(filepath.Join(tmpDir, "Containerfile"), []byte(containerfile), 0644)
397391
assert.NilError(t, err)
398392

399-
buildCtx := createBuildContext(t, dockerfile)
393+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
400394

401395
base.Cmd("build", "-t", imageName, buildCtx).AssertOK()
402396
base.Cmd("run", "--rm", imageName).AssertOutExactly("dockerfile\n")
@@ -411,7 +405,7 @@ func TestBuildNoTag(t *testing.T) {
411405
dockerfile := fmt.Sprintf(`FROM %s
412406
CMD ["echo", "nerdctl-build-notag-string"]
413407
`, testutil.CommonImage)
414-
buildCtx := createBuildContext(t, dockerfile)
408+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
415409

416410
base.Cmd("build", buildCtx).AssertOK()
417411
base.Cmd("images").AssertOutContains("<none>")
@@ -426,7 +420,7 @@ func TestBuildContextDockerImageAlias(t *testing.T) {
426420

427421
dockerfile := `FROM myorg/myapp
428422
CMD ["echo", "nerdctl-build-myorg/myapp"]`
429-
buildCtx := createBuildContext(t, dockerfile)
423+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
430424

431425
base.Cmd("build", buildCtx, fmt.Sprintf("--build-context=myorg/myapp=docker-image://%s", testutil.CommonImage)).AssertOK()
432426
base.Cmd("images").AssertOutContains("<none>")
@@ -451,7 +445,7 @@ func TestBuildContextWithCopyFromDir(t *testing.T) {
451445
COPY --from=dir2 /%s /hello_from_dir2.txt
452446
RUN ["cat", "/hello_from_dir2.txt"]`, testutil.CommonImage, filename)
453447

454-
buildCtx := createBuildContext(t, dockerfile)
448+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
455449

456450
base.Cmd("build", buildCtx, fmt.Sprintf("--build-context=dir2=%s", dir2)).AssertOK()
457451
base.Cmd("images").AssertOutContains("<none>")
@@ -472,7 +466,7 @@ RUN echo $SOURCE_DATE_EPOCH >/source-date-epoch
472466
CMD ["cat", "/source-date-epoch"]
473467
`, testutil.CommonImage)
474468

475-
buildCtx := createBuildContext(t, dockerfile)
469+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
476470

477471
const sourceDateEpochEnvStr = "1111111111"
478472
base.Env = append(base.Env, "SOURCE_DATE_EPOCH="+sourceDateEpochEnvStr)
@@ -493,7 +487,7 @@ func TestBuildNetwork(t *testing.T) {
493487
RUN apk add --no-cache curl
494488
RUN curl -I http://google.com
495489
`, testutil.CommonImage)
496-
buildCtx := createBuildContext(t, dockerfile)
490+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
497491

498492
validCases := []struct {
499493
name string
@@ -549,7 +543,7 @@ func TestBuildAttestation(t *testing.T) {
549543
}
550544

551545
dockerfile := "FROM " + testutil.NginxAlpineImage
552-
buildCtx := createBuildContext(t, dockerfile)
546+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
553547

554548
// Test sbom
555549
outputSBOMDir := t.TempDir()

cmd/nerdctl/builder_linux_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"gotest.tools/v3/assert"
2828

29+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2930
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3031
"github.com/containerd/nerdctl/v2/pkg/testutil"
3132
)
@@ -39,7 +40,7 @@ func TestBuilderPrune(t *testing.T) {
3940
dockerfile := fmt.Sprintf(`FROM %s
4041
CMD ["echo", "nerdctl-test-builder-prune"]`, testutil.CommonImage)
4142

42-
buildCtx := createBuildContext(t, dockerfile)
43+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
4344

4445
testCases := []struct {
4546
name string
@@ -71,7 +72,7 @@ func TestBuilderDebug(t *testing.T) {
7172
CMD ["echo", "nerdctl-builder-debug-test-string"]
7273
`, testutil.CommonImage)
7374

74-
buildCtx := createBuildContext(t, dockerfile)
75+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
7576

7677
base.Cmd("builder", "debug", buildCtx).CmdOption(testutil.WithStdin(bytes.NewReader([]byte("c\n")))).AssertOK()
7778
}
@@ -132,7 +133,7 @@ namespace = "%s"`, testutil.Namespace)
132133
err := os.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644)
133134
assert.NilError(t, err)
134135

135-
buildCtx := createBuildContext(t, dockerfile)
136+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
136137

137138
buildCmd := []string{"build", buildCtx}
138139
switch tc.pull {

cmd/nerdctl/compose_up_linux_test.go

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"github.com/containerd/log"
3131

32+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
3233
"github.com/containerd/nerdctl/v2/pkg/composer/serviceparser"
3334
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3435
"github.com/containerd/nerdctl/v2/pkg/testutil"
@@ -37,7 +38,7 @@ import (
3738

3839
func TestComposeUp(t *testing.T) {
3940
base := testutil.NewBase(t)
40-
testComposeUp(t, base, fmt.Sprintf(`
41+
helpers.ComposeUp(t, base, fmt.Sprintf(`
4142
version: '3.1'
4243
4344
services:
@@ -72,57 +73,6 @@ volumes:
7273
`, testutil.WordpressImage, testutil.MariaDBImage))
7374
}
7475

75-
func testComposeUp(t *testing.T, base *testutil.Base, dockerComposeYAML string, opts ...string) {
76-
comp := testutil.NewComposeDir(t, dockerComposeYAML)
77-
defer comp.CleanUp()
78-
79-
projectName := comp.ProjectName()
80-
t.Logf("projectName=%q", projectName)
81-
82-
base.ComposeCmd(append(append([]string{"-f", comp.YAMLFullPath()}, opts...), "up", "-d")...).AssertOK()
83-
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()
84-
base.Cmd("volume", "inspect", fmt.Sprintf("%s_db", projectName)).AssertOK()
85-
base.Cmd("network", "inspect", fmt.Sprintf("%s_default", projectName)).AssertOK()
86-
87-
checkWordpress := func() error {
88-
resp, err := nettestutil.HTTPGet("http://127.0.0.1:8080", 10, false)
89-
if err != nil {
90-
return err
91-
}
92-
respBody, err := io.ReadAll(resp.Body)
93-
if err != nil {
94-
return err
95-
}
96-
if !strings.Contains(string(respBody), testutil.WordpressIndexHTMLSnippet) {
97-
t.Logf("respBody=%q", respBody)
98-
return fmt.Errorf("respBody does not contain %q", testutil.WordpressIndexHTMLSnippet)
99-
}
100-
return nil
101-
}
102-
103-
var wordpressWorking bool
104-
for i := 0; i < 30; i++ {
105-
t.Logf("(retry %d)", i)
106-
err := checkWordpress()
107-
if err == nil {
108-
wordpressWorking = true
109-
break
110-
}
111-
// NOTE: "<h1>Error establishing a database connection</h1>" is expected for the first few iterations
112-
t.Log(err)
113-
time.Sleep(3 * time.Second)
114-
}
115-
116-
if !wordpressWorking {
117-
t.Fatal("wordpress is not working")
118-
}
119-
t.Log("wordpress seems functional")
120-
121-
base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
122-
base.Cmd("volume", "inspect", fmt.Sprintf("%s_db", projectName)).AssertFail()
123-
base.Cmd("network", "inspect", fmt.Sprintf("%s_default", projectName)).AssertFail()
124-
}
125-
12676
func TestComposeUpBuild(t *testing.T) {
12777
testutil.RequiresBuild(t)
12878
testutil.RegisterBuildCacheCleanup(t)
@@ -505,7 +455,7 @@ func TestComposeUpWithBypass4netns(t *testing.T) {
505455
testutil.RequireKernelVersion(t, ">= 5.9.0-0")
506456
testutil.RequireSystemService(t, "bypass4netnsd")
507457
base := testutil.NewBase(t)
508-
testComposeUp(t, base, fmt.Sprintf(`
458+
helpers.ComposeUp(t, base, fmt.Sprintf(`
509459
version: '3.1'
510460
511461
services:

cmd/nerdctl/container_run_mount_linux_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/containerd/containerd/v2/core/mount"
3030

31+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
3132
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3233
"github.com/containerd/nerdctl/v2/pkg/testutil"
3334
)
@@ -125,7 +126,7 @@ func TestRunAnonymousVolumeWithBuild(t *testing.T) {
125126
VOLUME /foo
126127
`, testutil.AlpineImage)
127128

128-
buildCtx := createBuildContext(t, dockerfile)
129+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
129130

130131
base.Cmd("build", "-t", imageName, buildCtx).AssertOK()
131132
base.Cmd("run", "--rm", "-v", "/foo", testutil.AlpineImage,
@@ -147,7 +148,7 @@ RUN mkdir -p /mnt && echo hi > /mnt/initial_file
147148
CMD ["cat", "/mnt/initial_file"]
148149
`, testutil.AlpineImage)
149150

150-
buildCtx := createBuildContext(t, dockerfile)
151+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
151152

152153
base.Cmd("build", "-t", imageName, buildCtx).AssertOK()
153154

@@ -175,7 +176,7 @@ VOLUME /mnt
175176
CMD ["cat", "/mnt/initial_file"]
176177
`, testutil.AlpineImage)
177178

178-
buildCtx := createBuildContext(t, dockerfile)
179+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
179180

180181
base.Cmd("build", "-t", imageName, buildCtx).AssertOK()
181182
//AnonymousVolume
@@ -208,7 +209,7 @@ CMD ["readlink", "/mnt/passwd"]
208209
`, testutil.AlpineImage)
209210
const expected = "../../../../../../../../../../../../../../../../../../etc/passwd\n"
210211

211-
buildCtx := createBuildContext(t, dockerfile)
212+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
212213

213214
base.Cmd("build", "-t", imageName, buildCtx).AssertOK()
214215

@@ -235,7 +236,7 @@ func TestRunCopyingUpInitialContentsShouldNotResetTheCopiedContents(t *testing.T
235236
RUN echo -n "rev0" > /mnt/file
236237
`, testutil.AlpineImage)
237238

238-
buildCtx := createBuildContext(t, dockerfile)
239+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
239240

240241
base.Cmd("build", "-t", imageName, buildCtx).AssertOK()
241242

0 commit comments

Comments
 (0)