Skip to content

Commit

Permalink
Test rework, part 4
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <spam_blackhole@farcloser.world>
  • Loading branch information
apostasie committed Oct 3, 2024
1 parent 49214a2 commit a80b265
Show file tree
Hide file tree
Showing 74 changed files with 6,193 additions and 3,354 deletions.
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

0 comments on commit a80b265

Please sign in to comment.