Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions cmd/nerdctl/builder/builder_build_oci_layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package builder

import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"gotest.tools/v3/assert"

"github.com/containerd/nerdctl/mod/tigron/expect"
"github.com/containerd/nerdctl/mod/tigron/require"
"github.com/containerd/nerdctl/mod/tigron/test"

Expand All @@ -44,17 +44,17 @@ func TestBuildContextWithOCILayout(t *testing.T) {
),
Cleanup: func(data test.Data, helpers test.Helpers) {
if nerdtest.IsDocker() {
helpers.Anyhow("buildx", "stop", data.Identifier("-container"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leading dashes are unnecessary. Identifier does separate seed string with dash.

helpers.Anyhow("buildx", "rm", "--force", data.Identifier("-container"))
helpers.Anyhow("buildx", "stop", data.Identifier("container"))
helpers.Anyhow("buildx", "rm", "--force", data.Identifier("container"))
}
helpers.Anyhow("rmi", "-f", data.Identifier("-parent"))
helpers.Anyhow("rmi", "-f", data.Identifier("-child"))
helpers.Anyhow("rmi", "-f", data.Identifier("parent"))
helpers.Anyhow("rmi", "-f", data.Identifier("child"))
},
Setup: func(data test.Data, helpers test.Helpers) {
// Default docker driver does not support OCI exporter.
// Reference: https://docs.docker.com/build/exporters/oci-docker/
if nerdtest.IsDocker() {
name := data.Identifier("-container")
name := data.Identifier("container")
helpers.Ensure("buildx", "create", "--name", name, "--driver=docker-container")
dockerBuilderArgs = []string{"buildx", "--builder", name}
}
Expand All @@ -63,33 +63,35 @@ func TestBuildContextWithOCILayout(t *testing.T) {
LABEL layer=oci-layout-parent
CMD ["echo", "test-nerdctl-build-context-oci-layout-parent"]`, testutil.CommonImage)

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

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

Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
dockerfile := `FROM parent
CMD ["echo", "test-nerdctl-build-context-oci-layout"]`

buildCtx := data.TempDir()
err := os.WriteFile(filepath.Join(buildCtx, "Dockerfile"), []byte(dockerfile), 0o600)
assert.NilError(helpers.T(), err)
data.Temp().Save(dockerfile, "Dockerfile")

var cmd test.TestableCommand
if nerdtest.IsDocker() {
cmd = helpers.Command(dockerBuilderArgs...)
} else {
cmd = helpers.Command()
}
cmd.WithArgs("build", buildCtx, fmt.Sprintf("--build-context=parent=oci-layout://%s", filepath.Join(buildCtx, "parent")), "--tag", data.Identifier("-child"))
cmd.WithArgs(
"build",
data.Temp().Path(),
fmt.Sprintf("--build-context=parent=oci-layout://%s", filepath.Join(data.Temp().Path(), "parent")),
"--tag",
data.Identifier("child"),
)
if nerdtest.IsDocker() {
// Need to load the container image from the builder to be able to run it.
cmd.WithArgs("--load")
Expand All @@ -99,7 +101,14 @@ CMD ["echo", "test-nerdctl-build-context-oci-layout"]`
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: func(stdout string, info string, t *testing.T) {
assert.Assert(t, strings.Contains(helpers.Capture("run", "--rm", data.Identifier("-child")), "test-nerdctl-build-context-oci-layout"), info)
assert.Assert(
t,
strings.Contains(
helpers.Capture("run", "--rm", data.Identifier("child")),
"test-nerdctl-build-context-oci-layout",
),
info,
)
},
}
},
Expand Down
Loading