Skip to content

Commit f736de3

Browse files
committed
Revamp Data
Many tests require temporary resources (Dockerfiles, compose.yml, etc), and routinely redo the same thing over and over (create separate temp dir, write file, remove temp dir). At best this adds a lot of boilerplate / helper functions to the test - at worst, the resources are not properly cleaned-up, or not well isolated from other test. This PR does introduce a set of helpers to fasttrack temp files manipulation, and rejiggle the Data interface to more clearly separate labels (shared with subtests) and temporary resources. Signed-off-by: apostasie <[email protected]>
1 parent c02abf5 commit f736de3

Some content is hidden

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

55 files changed

+1060
-872
lines changed

cmd/nerdctl/builder/builder_build_oci_layout_test.go

+30-21
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ package builder
1818

1919
import (
2020
"fmt"
21-
"os"
2221
"path/filepath"
2322
"strings"
2423
"testing"
2524

2625
"gotest.tools/v3/assert"
2726

27+
"github.com/containerd/nerdctl/mod/tigron/expect"
2828
"github.com/containerd/nerdctl/mod/tigron/require"
2929
"github.com/containerd/nerdctl/mod/tigron/test"
3030

@@ -44,17 +44,17 @@ func TestBuildContextWithOCILayout(t *testing.T) {
4444
),
4545
Cleanup: func(data test.Data, helpers test.Helpers) {
4646
if nerdtest.IsDocker() {
47-
helpers.Anyhow("buildx", "stop", data.Identifier("-container"))
48-
helpers.Anyhow("buildx", "rm", "--force", data.Identifier("-container"))
47+
helpers.Anyhow("buildx", "stop", data.Identifier("container"))
48+
helpers.Anyhow("buildx", "rm", "--force", data.Identifier("container"))
4949
}
50-
helpers.Anyhow("rmi", "-f", data.Identifier("-parent"))
51-
helpers.Anyhow("rmi", "-f", data.Identifier("-child"))
50+
helpers.Anyhow("rmi", "-f", data.Identifier("parent"))
51+
helpers.Anyhow("rmi", "-f", data.Identifier("child"))
5252
},
5353
Setup: func(data test.Data, helpers test.Helpers) {
5454
// Default docker driver does not support OCI exporter.
5555
// Reference: https://docs.docker.com/build/exporters/oci-docker/
5656
if nerdtest.IsDocker() {
57-
name := data.Identifier("-container")
57+
name := data.Identifier("container")
5858
helpers.Ensure("buildx", "create", "--name", name, "--driver=docker-container")
5959
dockerBuilderArgs = []string{"buildx", "--builder", name}
6060
}
@@ -63,33 +63,35 @@ func TestBuildContextWithOCILayout(t *testing.T) {
6363
LABEL layer=oci-layout-parent
6464
CMD ["echo", "test-nerdctl-build-context-oci-layout-parent"]`, testutil.CommonImage)
6565

66-
buildCtx := data.TempDir()
67-
err := os.WriteFile(filepath.Join(buildCtx, "Dockerfile"), []byte(dockerfile), 0o600)
68-
assert.NilError(helpers.T(), err)
66+
data.Temp().Save(dockerfile, "Dockerfile")
67+
dest := data.Temp().Dir("parent")
68+
tarPath := data.Temp().Path("parent.tar")
6969

70-
tarPath := filepath.Join(buildCtx, "parent.tar")
71-
dest := filepath.Join(buildCtx, "parent")
72-
assert.NilError(helpers.T(), os.MkdirAll(dest, 0o700))
73-
helpers.Ensure("build", buildCtx, "--tag", data.Identifier("-parent"))
74-
helpers.Ensure("image", "save", "--output", tarPath, data.Identifier("-parent"))
75-
helpers.Custom("tar", "Cxf", dest, tarPath).Run(&test.Expected{})
70+
helpers.Ensure("build", data.Temp().Path(), "--tag", data.Identifier("parent"))
71+
helpers.Ensure("image", "save", "--output", tarPath, data.Identifier("parent"))
72+
helpers.Custom("tar", "Cxf", dest, tarPath).Run(&test.Expected{
73+
ExitCode: expect.ExitCodeSuccess,
74+
})
7675
},
7776

7877
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
7978
dockerfile := `FROM parent
8079
CMD ["echo", "test-nerdctl-build-context-oci-layout"]`
81-
82-
buildCtx := data.TempDir()
83-
err := os.WriteFile(filepath.Join(buildCtx, "Dockerfile"), []byte(dockerfile), 0o600)
84-
assert.NilError(helpers.T(), err)
80+
data.Temp().Save(dockerfile, "Dockerfile")
8581

8682
var cmd test.TestableCommand
8783
if nerdtest.IsDocker() {
8884
cmd = helpers.Command(dockerBuilderArgs...)
8985
} else {
9086
cmd = helpers.Command()
9187
}
92-
cmd.WithArgs("build", buildCtx, fmt.Sprintf("--build-context=parent=oci-layout://%s", filepath.Join(buildCtx, "parent")), "--tag", data.Identifier("-child"))
88+
cmd.WithArgs(
89+
"build",
90+
data.Temp().Path(),
91+
fmt.Sprintf("--build-context=parent=oci-layout://%s", filepath.Join(data.Temp().Path(), "parent")),
92+
"--tag",
93+
data.Identifier("child"),
94+
)
9395
if nerdtest.IsDocker() {
9496
// Need to load the container image from the builder to be able to run it.
9597
cmd.WithArgs("--load")
@@ -99,7 +101,14 @@ CMD ["echo", "test-nerdctl-build-context-oci-layout"]`
99101
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
100102
return &test.Expected{
101103
Output: func(stdout string, info string, t *testing.T) {
102-
assert.Assert(t, strings.Contains(helpers.Capture("run", "--rm", data.Identifier("-child")), "test-nerdctl-build-context-oci-layout"), info)
104+
assert.Assert(
105+
t,
106+
strings.Contains(
107+
helpers.Capture("run", "--rm", data.Identifier("child")),
108+
"test-nerdctl-build-context-oci-layout",
109+
),
110+
info,
111+
)
103112
},
104113
}
105114
},

0 commit comments

Comments
 (0)