Skip to content

Commit 5c240d7

Browse files
committed
Test rework, part 4
Signed-off-by: apostasie <spam_blackhole@farcloser.world>
1 parent 49214a2 commit 5c240d7

File tree

74 files changed

+6194
-3354
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+6194
-3354
lines changed

cmd/nerdctl/builder/builder_build_linux_test.go

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@ package builder
1818

1919
import (
2020
"fmt"
21+
"strings"
2122
"testing"
2223

2324
"gotest.tools/v3/assert"
2425

25-
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
26+
testhelpers "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2627
"github.com/containerd/nerdctl/v2/pkg/testutil"
28+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
29+
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
2730
)
2831

2932
func TestBuildContextWithOCILayout(t *testing.T) {
33+
nerdtest.Setup()
34+
3035
testutil.RequiresBuild(t)
3136
testutil.RegisterBuildCacheCleanup(t)
32-
3337
var dockerBuilderArgs []string
3438
if testutil.IsDocker() {
3539
// Default docker driver does not support OCI exporter.
@@ -38,48 +42,50 @@ func TestBuildContextWithOCILayout(t *testing.T) {
3842
dockerBuilderArgs = []string{"buildx", "--builder", builderName}
3943
}
4044

41-
base := testutil.NewBase(t)
42-
imageName := testutil.Identifier(t)
43-
ociLayout := "parent"
44-
parentImageName := fmt.Sprintf("%s-%s", imageName, ociLayout)
45-
46-
teardown := func() {
47-
base.Cmd("rmi", parentImageName, imageName).Run()
48-
}
49-
t.Cleanup(teardown)
50-
teardown()
51-
52-
dockerfile := fmt.Sprintf(`FROM %s
45+
testCase := &test.Case{
46+
Description: "Build context OCI layout",
47+
Cleanup: func(data test.Data, helpers test.Helpers) {
48+
helpers.Anyhow("rmi", fmt.Sprintf("%s-parent", data.Identifier()))
49+
},
50+
Setup: func(data test.Data, helpers test.Helpers) {
51+
dockerfile := fmt.Sprintf(`FROM %s
5352
LABEL layer=oci-layout-parent
5453
CMD ["echo", "test-nerdctl-build-context-oci-layout-parent"]`, testutil.CommonImage)
55-
buildCtx := helpers.CreateBuildContext(t, dockerfile)
56-
57-
tarPath := fmt.Sprintf("%s/%s.tar", buildCtx, ociLayout)
58-
59-
// Create OCI archive from parent image.
60-
base.Cmd("build", buildCtx, "--tag", parentImageName).AssertOK()
61-
base.Cmd("image", "save", "--output", tarPath, parentImageName).AssertOK()
6254

63-
// Unpack OCI archive into OCI layout directory.
64-
ociLayoutDir := t.TempDir()
65-
err := helpers.ExtractTarFile(ociLayoutDir, tarPath)
66-
assert.NilError(t, err)
67-
68-
dockerfile = fmt.Sprintf(`FROM %s
69-
CMD ["echo", "test-nerdctl-build-context-oci-layout"]`, ociLayout)
70-
buildCtx = helpers.CreateBuildContext(t, dockerfile)
71-
72-
var buildArgs = []string{}
73-
if testutil.IsDocker() {
74-
buildArgs = dockerBuilderArgs
75-
}
76-
77-
buildArgs = append(buildArgs, "build", buildCtx, fmt.Sprintf("--build-context=%s=oci-layout://%s", ociLayout, ociLayoutDir), "--tag", imageName)
78-
if testutil.IsDocker() {
79-
// Need to load the container image from the builder to be able to run it.
80-
buildArgs = append(buildArgs, "--load")
55+
// FIXME: replace with a generic file creation helper - search for all occurrences of temp file creation
56+
buildCtx := testhelpers.CreateBuildContext(t, dockerfile)
57+
tarPath := fmt.Sprintf("%s/parent.tar", buildCtx)
58+
59+
helpers.Ensure("build", buildCtx, "--tag", fmt.Sprintf("%s-parent", data.Identifier()))
60+
helpers.Ensure("image", "save", "--output", tarPath, fmt.Sprintf("%s-parent", data.Identifier()))
61+
helpers.CustomCommand("tar", "Cxf", data.TempDir(), tarPath).Run(&test.Expected{})
62+
},
63+
64+
Command: func(data test.Data, helpers test.Helpers) test.Command {
65+
dockerfile := `FROM parent
66+
CMD ["echo", "test-nerdctl-build-context-oci-layout"]`
67+
buildCtx := testhelpers.CreateBuildContext(t, dockerfile)
68+
var cmd test.Command
69+
if testutil.IsDocker() {
70+
cmd = helpers.Command(dockerBuilderArgs...)
71+
} else {
72+
cmd = helpers.Command()
73+
}
74+
cmd.WithArgs("build", buildCtx, fmt.Sprintf("--build-context=parent=oci-layout://%s", data.TempDir()), "--tag", data.Identifier())
75+
if testutil.IsDocker() {
76+
// Need to load the container image from the builder to be able to run it.
77+
cmd.WithArgs("--load")
78+
}
79+
return cmd
80+
},
81+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
82+
return &test.Expected{
83+
Output: func(stdout string, info string, t *testing.T) {
84+
assert.Assert(t, strings.Contains(helpers.Capture("run", "--rm", data.Identifier()), "test-nerdctl-build-context-oci-layout"), info)
85+
},
86+
}
87+
},
8188
}
8289

83-
base.Cmd(buildArgs...).AssertOK()
84-
base.Cmd("run", "--rm", imageName).AssertOutContains("test-nerdctl-build-context-oci-layout")
90+
testCase.Run(t)
8591
}

0 commit comments

Comments
 (0)