Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Test rework, next #3461

Closed
wants to merge 1 commit into from
Closed
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
88 changes: 47 additions & 41 deletions cmd/nerdctl/builder/builder_build_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ package builder

import (
"fmt"
"strings"
"testing"

"gotest.tools/v3/assert"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
testhelpers "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
)

func TestBuildContextWithOCILayout(t *testing.T) {
nerdtest.Setup()

testutil.RequiresBuild(t)
testutil.RegisterBuildCacheCleanup(t)

var dockerBuilderArgs []string
if testutil.IsDocker() {
// Default docker driver does not support OCI exporter.
Expand All @@ -38,48 +42,50 @@ func TestBuildContextWithOCILayout(t *testing.T) {
dockerBuilderArgs = []string{"buildx", "--builder", builderName}
}

base := testutil.NewBase(t)
imageName := testutil.Identifier(t)
ociLayout := "parent"
parentImageName := fmt.Sprintf("%s-%s", imageName, ociLayout)

teardown := func() {
base.Cmd("rmi", parentImageName, imageName).Run()
}
t.Cleanup(teardown)
teardown()

dockerfile := fmt.Sprintf(`FROM %s
testCase := &test.Case{
Description: "Build context OCI layout",
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rmi", fmt.Sprintf("%s-parent", data.Identifier()))
},
Setup: func(data test.Data, helpers test.Helpers) {
dockerfile := fmt.Sprintf(`FROM %s
LABEL layer=oci-layout-parent
CMD ["echo", "test-nerdctl-build-context-oci-layout-parent"]`, testutil.CommonImage)
buildCtx := helpers.CreateBuildContext(t, dockerfile)

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

// Create OCI archive from parent image.
base.Cmd("build", buildCtx, "--tag", parentImageName).AssertOK()
base.Cmd("image", "save", "--output", tarPath, parentImageName).AssertOK()

// Unpack OCI archive into OCI layout directory.
ociLayoutDir := t.TempDir()
err := helpers.ExtractTarFile(ociLayoutDir, tarPath)
assert.NilError(t, err)

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

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

buildArgs = append(buildArgs, "build", buildCtx, fmt.Sprintf("--build-context=%s=oci-layout://%s", ociLayout, ociLayoutDir), "--tag", imageName)
if testutil.IsDocker() {
// Need to load the container image from the builder to be able to run it.
buildArgs = append(buildArgs, "--load")
// FIXME: replace with a generic file creation helper - search for all occurrences of temp file creation
buildCtx := testhelpers.CreateBuildContext(t, dockerfile)
tarPath := fmt.Sprintf("%s/parent.tar", buildCtx)

helpers.Ensure("build", buildCtx, "--tag", fmt.Sprintf("%s-parent", data.Identifier()))
helpers.Ensure("image", "save", "--output", tarPath, fmt.Sprintf("%s-parent", data.Identifier()))
helpers.CustomCommand("tar", "Cxf", data.TempDir(), tarPath).Run(&test.Expected{})
},

Command: func(data test.Data, helpers test.Helpers) test.Command {
dockerfile := `FROM parent
CMD ["echo", "test-nerdctl-build-context-oci-layout"]`
buildCtx := testhelpers.CreateBuildContext(t, dockerfile)
var cmd test.Command
if testutil.IsDocker() {
cmd = helpers.Command(dockerBuilderArgs...)
} else {
cmd = helpers.Command()
}
cmd.WithArgs("build", buildCtx, fmt.Sprintf("--build-context=parent=oci-layout://%s", data.TempDir()), "--tag", data.Identifier())
if testutil.IsDocker() {
// Need to load the container image from the builder to be able to run it.
cmd.WithArgs("--load")
}
return cmd
},
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()), "test-nerdctl-build-context-oci-layout"), info)
},
}
},
}

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