Skip to content

Commit

Permalink
Migrate container tests
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 12, 2024
1 parent 0953eba commit ece4022
Show file tree
Hide file tree
Showing 35 changed files with 1,418 additions and 939 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
project:
name: Project Checks
name: checks
runs-on: ubuntu-24.04
timeout-minutes: 20
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
# Apparently does not
# timeout-minutes: ${{ fromJSON(env.SHORT_TIMEOUT) }}
timeout-minutes: 5
name: unit ${{ matrix.goos }}
name: unit | ${{ matrix.goos }}
runs-on: "${{ matrix.os }}"
strategy:
matrix:
Expand Down
88 changes: 46 additions & 42 deletions cmd/nerdctl/builder/builder_build_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ 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) {
testutil.RequiresBuild(t)
testutil.RegisterBuildCacheCleanup(t)
nerdtest.Setup()

var dockerBuilderArgs []string
if testutil.IsDocker() {
Expand All @@ -38,48 +40,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{
Require: nerdtest.Build,
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rmi", "-f", 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.Custom("tar", "Cxf", data.TempDir(), tarPath).Run(&test.Expected{})
},

Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
dockerfile := `FROM parent
CMD ["echo", "test-nerdctl-build-context-oci-layout"]`
buildCtx := testhelpers.CreateBuildContext(t, dockerfile)
var cmd test.TestableCommand
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 ece4022

Please sign in to comment.