2017-11-21 13:22:23 -05:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-02-25 09:32:16 -05:00
|
|
|
"os"
|
2018-06-18 08:09:23 -04:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-11-21 13:22:23 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/e2e/internal/fixtures"
|
2018-05-17 07:11:59 -04:00
|
|
|
"github.com/docker/cli/internal/test/environment"
|
2018-03-06 05:15:18 -05:00
|
|
|
"github.com/docker/cli/internal/test/output"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/fs"
|
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
"gotest.tools/v3/skip"
|
2017-11-21 13:22:23 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildFromContextDirectoryWithTag(t *testing.T) {
|
2022-09-22 10:31:28 -04:00
|
|
|
t.Setenv("DOCKER_BUILDKIT", "0")
|
2022-02-03 04:37:55 -05:00
|
|
|
|
2017-11-21 13:22:23 -05:00
|
|
|
dir := fs.NewDir(t, "test-build-context-dir",
|
2022-09-30 13:13:22 -04:00
|
|
|
fs.WithFile("run", "echo running", fs.WithMode(0o755)),
|
2017-11-21 13:22:23 -05:00
|
|
|
fs.WithDir("data", fs.WithFile("one", "1111")),
|
|
|
|
fs.WithFile("Dockerfile", fmt.Sprintf(`
|
2018-05-17 07:11:59 -04:00
|
|
|
FROM %s
|
|
|
|
COPY run /usr/bin/run
|
|
|
|
RUN run
|
|
|
|
COPY data /data
|
2017-11-21 13:22:23 -05:00
|
|
|
`, fixtures.AlpineImage)))
|
|
|
|
defer dir.Remove()
|
|
|
|
|
|
|
|
result := icmd.RunCmd(
|
|
|
|
icmd.Command("docker", "build", "-t", "myimage", "."),
|
|
|
|
withWorkingDir(dir))
|
2018-05-17 07:11:59 -04:00
|
|
|
defer icmd.RunCommand("docker", "image", "rm", "myimage")
|
2017-11-21 13:22:23 -05:00
|
|
|
|
2022-12-06 07:46:51 -05:00
|
|
|
const buildkitDisabledWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
|
2022-12-19 07:03:28 -05:00
|
|
|
BuildKit is currently disabled; enable it by removing the DOCKER_BUILDKIT=0
|
2022-12-06 07:46:51 -05:00
|
|
|
environment-variable.
|
2022-01-27 12:42:05 -05:00
|
|
|
`
|
|
|
|
|
2022-12-06 07:46:51 -05:00
|
|
|
result.Assert(t, icmd.Expected{Err: buildkitDisabledWarning})
|
2018-03-06 05:15:18 -05:00
|
|
|
output.Assert(t, result.Stdout(), map[int]func(string) error{
|
2024-02-09 08:55:03 -05:00
|
|
|
0: output.Prefix("Sending build context to Docker daemon"),
|
|
|
|
1: output.Suffix("Step 1/4 : FROM registry:5000/alpine:frozen"),
|
|
|
|
3: output.Suffix("Step 2/4 : COPY run /usr/bin/run"),
|
|
|
|
5: output.Suffix("Step 3/4 : RUN run"),
|
|
|
|
7: output.Suffix("running"),
|
|
|
|
// TODO(krissetto): ugly, remove when no longer testing against moby 24. see https://github.com/moby/moby/pull/46270
|
|
|
|
8: func(s string) error {
|
|
|
|
err := output.Contains("Removed intermediate container")(s) // moby >= v25
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return output.Contains("Removing intermediate container")(s) // moby < v25
|
|
|
|
},
|
2018-05-17 07:11:59 -04:00
|
|
|
10: output.Suffix("Step 4/4 : COPY data /data"),
|
|
|
|
12: output.Contains("Successfully built "),
|
|
|
|
13: output.Suffix("Successfully tagged myimage:latest"),
|
2017-11-21 13:22:23 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-06 05:15:18 -05:00
|
|
|
func TestTrustedBuild(t *testing.T) {
|
2018-05-17 07:11:59 -04:00
|
|
|
skip.If(t, environment.RemoteDaemon())
|
2022-09-22 10:31:28 -04:00
|
|
|
t.Setenv("DOCKER_BUILDKIT", "0")
|
2018-05-17 07:11:59 -04:00
|
|
|
|
2018-03-06 05:15:18 -05:00
|
|
|
dir := fixtures.SetupConfigFile(t)
|
|
|
|
defer dir.Remove()
|
|
|
|
image1 := fixtures.CreateMaskedTrustedRemoteImage(t, registryPrefix, "trust-build1", "latest")
|
|
|
|
image2 := fixtures.CreateMaskedTrustedRemoteImage(t, registryPrefix, "trust-build2", "latest")
|
2017-11-21 13:22:23 -05:00
|
|
|
|
2018-03-06 05:15:18 -05:00
|
|
|
buildDir := fs.NewDir(t, "test-trusted-build-context-dir",
|
|
|
|
fs.WithFile("Dockerfile", fmt.Sprintf(`
|
|
|
|
FROM %s as build-base
|
|
|
|
RUN echo ok > /foo
|
|
|
|
FROM %s
|
|
|
|
COPY --from=build-base foo bar
|
|
|
|
`, image1, image2)))
|
|
|
|
defer buildDir.Remove()
|
|
|
|
|
|
|
|
result := icmd.RunCmd(
|
|
|
|
icmd.Command("docker", "build", "-t", "myimage", "."),
|
|
|
|
withWorkingDir(buildDir),
|
|
|
|
fixtures.WithConfig(dir.Path()),
|
|
|
|
fixtures.WithTrust,
|
|
|
|
fixtures.WithNotary,
|
|
|
|
)
|
|
|
|
|
|
|
|
result.Assert(t, icmd.Expected{
|
|
|
|
Out: fmt.Sprintf("FROM %s@sha", image1[:len(image1)-7]),
|
|
|
|
Err: fmt.Sprintf("Tagging %s@sha", image1[:len(image1)-7]),
|
|
|
|
})
|
|
|
|
result.Assert(t, icmd.Expected{
|
|
|
|
Out: fmt.Sprintf("FROM %s@sha", image2[:len(image2)-7]),
|
|
|
|
})
|
2017-11-21 13:22:23 -05:00
|
|
|
}
|
|
|
|
|
2018-03-06 05:15:18 -05:00
|
|
|
func TestTrustedBuildUntrustedImage(t *testing.T) {
|
2018-05-17 07:11:59 -04:00
|
|
|
skip.If(t, environment.RemoteDaemon())
|
2022-09-22 10:31:28 -04:00
|
|
|
t.Setenv("DOCKER_BUILDKIT", "0")
|
2018-05-17 07:11:59 -04:00
|
|
|
|
2018-03-06 05:15:18 -05:00
|
|
|
dir := fixtures.SetupConfigFile(t)
|
|
|
|
defer dir.Remove()
|
|
|
|
buildDir := fs.NewDir(t, "test-trusted-build-context-dir",
|
|
|
|
fs.WithFile("Dockerfile", fmt.Sprintf(`
|
|
|
|
FROM %s
|
|
|
|
RUN []
|
|
|
|
`, fixtures.AlpineImage)))
|
|
|
|
defer buildDir.Remove()
|
2017-11-21 13:22:23 -05:00
|
|
|
|
2018-03-06 05:15:18 -05:00
|
|
|
result := icmd.RunCmd(
|
|
|
|
icmd.Command("docker", "build", "-t", "myimage", "."),
|
|
|
|
withWorkingDir(buildDir),
|
|
|
|
fixtures.WithConfig(dir.Path()),
|
|
|
|
fixtures.WithTrust,
|
|
|
|
fixtures.WithNotary,
|
|
|
|
)
|
|
|
|
|
|
|
|
result.Assert(t, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Err: "does not have trust data for",
|
|
|
|
})
|
2017-11-21 13:22:23 -05:00
|
|
|
}
|
|
|
|
|
2018-06-18 08:09:23 -04:00
|
|
|
func TestBuildIidFileSquash(t *testing.T) {
|
|
|
|
environment.SkipIfNotExperimentalDaemon(t)
|
2022-09-22 10:31:28 -04:00
|
|
|
t.Setenv("DOCKER_BUILDKIT", "0")
|
2022-02-03 04:37:55 -05:00
|
|
|
|
2018-06-18 08:09:23 -04:00
|
|
|
dir := fs.NewDir(t, "test-iidfile-squash")
|
|
|
|
defer dir.Remove()
|
|
|
|
iidfile := filepath.Join(dir.Path(), "idsquash")
|
|
|
|
buildDir := fs.NewDir(t, "test-iidfile-squash-build",
|
|
|
|
fs.WithFile("Dockerfile", fmt.Sprintf(`
|
|
|
|
FROM %s
|
2020-09-23 06:42:17 -04:00
|
|
|
ENV FOO=FOO
|
|
|
|
ENV BAR=BAR
|
2018-06-25 05:46:35 -04:00
|
|
|
RUN touch /fiip
|
2018-06-18 08:09:23 -04:00
|
|
|
RUN touch /foop`, fixtures.AlpineImage)),
|
|
|
|
)
|
|
|
|
defer buildDir.Remove()
|
|
|
|
|
|
|
|
imageTag := "testbuildiidfilesquash"
|
|
|
|
result := icmd.RunCmd(
|
|
|
|
icmd.Command("docker", "build", "--iidfile", iidfile, "--squash", "-t", imageTag, "."),
|
|
|
|
withWorkingDir(buildDir),
|
|
|
|
)
|
|
|
|
result.Assert(t, icmd.Success)
|
2022-02-25 09:32:16 -05:00
|
|
|
id, err := os.ReadFile(iidfile)
|
2018-06-18 08:09:23 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
result = icmd.RunCommand("docker", "image", "inspect", "-f", "{{.Id}}", imageTag)
|
|
|
|
result.Assert(t, icmd.Success)
|
|
|
|
assert.Check(t, is.Equal(string(id), strings.TrimSpace(result.Combined())))
|
|
|
|
}
|
|
|
|
|
2018-03-06 05:15:18 -05:00
|
|
|
func withWorkingDir(dir *fs.Dir) func(*icmd.Cmd) {
|
|
|
|
return func(cmd *icmd.Cmd) {
|
|
|
|
cmd.Dir = dir.Path()
|
2017-11-21 13:22:23 -05:00
|
|
|
}
|
|
|
|
}
|