|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package builder |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "gotest.tools/v3/assert" |
| 27 | + |
| 28 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 29 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 30 | + "github.com/containerd/nerdctl/v2/pkg/testutil/test" |
| 31 | +) |
| 32 | + |
| 33 | +func TestBuildContextWithOCILayout(t *testing.T) { |
| 34 | + nerdtest.Setup() |
| 35 | + |
| 36 | + var dockerBuilderArgs []string |
| 37 | + |
| 38 | + testCase := &test.Case{ |
| 39 | + Require: test.Require( |
| 40 | + nerdtest.Build, |
| 41 | + test.Not(test.Windows), |
| 42 | + ), |
| 43 | + Cleanup: func(data test.Data, helpers test.Helpers) { |
| 44 | + if nerdtest.IsDocker() { |
| 45 | + helpers.Anyhow("buildx", "stop", data.Identifier("-container")) |
| 46 | + helpers.Anyhow("buildx", "rm", "--force", data.Identifier("-container")) |
| 47 | + } |
| 48 | + helpers.Anyhow("rmi", "-f", data.Identifier("-parent")) |
| 49 | + helpers.Anyhow("rmi", "-f", data.Identifier("-child")) |
| 50 | + }, |
| 51 | + Setup: func(data test.Data, helpers test.Helpers) { |
| 52 | + // Default docker driver does not support OCI exporter. |
| 53 | + // Reference: https://docs.docker.com/build/exporters/oci-docker/ |
| 54 | + if nerdtest.IsDocker() { |
| 55 | + name := data.Identifier("-container") |
| 56 | + helpers.Ensure("buildx", "create", "--name", name, "--driver=docker-container") |
| 57 | + dockerBuilderArgs = []string{"buildx", "--builder", name} |
| 58 | + } |
| 59 | + |
| 60 | + dockerfile := fmt.Sprintf(`FROM %s |
| 61 | +LABEL layer=oci-layout-parent |
| 62 | +CMD ["echo", "test-nerdctl-build-context-oci-layout-parent"]`, testutil.CommonImage) |
| 63 | + |
| 64 | + buildCtx := data.TempDir() |
| 65 | + err := os.WriteFile(filepath.Join(buildCtx, "Dockerfile"), []byte(dockerfile), 0o600) |
| 66 | + assert.NilError(helpers.T(), err) |
| 67 | + |
| 68 | + tarPath := filepath.Join(buildCtx, "parent.tar") |
| 69 | + dest := filepath.Join(buildCtx, "parent") |
| 70 | + assert.NilError(helpers.T(), os.MkdirAll(dest, 0o700)) |
| 71 | + helpers.Ensure("build", buildCtx, "--tag", data.Identifier("-parent")) |
| 72 | + helpers.Ensure("image", "save", "--output", tarPath, data.Identifier("-parent")) |
| 73 | + helpers.Custom("tar", "Cxf", dest, tarPath).Run(&test.Expected{}) |
| 74 | + }, |
| 75 | + |
| 76 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 77 | + dockerfile := `FROM parent |
| 78 | +CMD ["echo", "test-nerdctl-build-context-oci-layout"]` |
| 79 | + |
| 80 | + buildCtx := data.TempDir() |
| 81 | + err := os.WriteFile(filepath.Join(buildCtx, "Dockerfile"), []byte(dockerfile), 0o600) |
| 82 | + assert.NilError(helpers.T(), err) |
| 83 | + |
| 84 | + var cmd test.TestableCommand |
| 85 | + if nerdtest.IsDocker() { |
| 86 | + cmd = helpers.Command(dockerBuilderArgs...) |
| 87 | + } else { |
| 88 | + cmd = helpers.Command() |
| 89 | + } |
| 90 | + cmd.WithArgs("build", buildCtx, fmt.Sprintf("--build-context=parent=oci-layout://%s", filepath.Join(buildCtx, "parent")), "--tag", data.Identifier("-child")) |
| 91 | + if nerdtest.IsDocker() { |
| 92 | + // Need to load the container image from the builder to be able to run it. |
| 93 | + cmd.WithArgs("--load") |
| 94 | + } |
| 95 | + return cmd |
| 96 | + }, |
| 97 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 98 | + return &test.Expected{ |
| 99 | + Output: func(stdout string, info string, t *testing.T) { |
| 100 | + assert.Assert(t, strings.Contains(helpers.Capture("run", "--rm", data.Identifier("-child")), "test-nerdctl-build-context-oci-layout"), info) |
| 101 | + }, |
| 102 | + } |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + testCase.Run(t) |
| 107 | +} |
0 commit comments