diff --git a/e2e/image/build_test.go b/e2e/image/build_test.go index 7d89a6f3b5..6ded9ae8e9 100644 --- a/e2e/image/build_test.go +++ b/e2e/image/build_test.go @@ -2,11 +2,16 @@ package image import ( "fmt" + "io/ioutil" + "path/filepath" + "strings" "testing" "github.com/docker/cli/e2e/internal/fixtures" "github.com/docker/cli/internal/test/environment" "github.com/docker/cli/internal/test/output" + "gotest.tools/assert" + is "gotest.tools/assert/cmp" "gotest.tools/fs" "gotest.tools/icmd" "gotest.tools/skip" @@ -103,6 +108,33 @@ func TestTrustedBuildUntrustedImage(t *testing.T) { }) } +func TestBuildIidFileSquash(t *testing.T) { + environment.SkipIfNotExperimentalDaemon(t) + 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 + ENV FOO FOO + ENV BAR BAR + 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) + id, err := ioutil.ReadFile(iidfile) + 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()))) +} + func withWorkingDir(dir *fs.Dir) func(*icmd.Cmd) { return func(cmd *icmd.Cmd) { cmd.Dir = dir.Path() diff --git a/internal/test/environment/testenv.go b/internal/test/environment/testenv.go index 3e4ef8495d..42043c1bdf 100644 --- a/internal/test/environment/testenv.go +++ b/internal/test/environment/testenv.go @@ -1,12 +1,17 @@ package environment import ( + "context" "os" "strings" + "testing" "time" + "github.com/docker/docker/client" "github.com/pkg/errors" + "gotest.tools/assert" "gotest.tools/poll" + "gotest.tools/skip" ) // Setup a new environment @@ -74,5 +79,21 @@ func boolFromString(val string) bool { } } +func dockerClient(t *testing.T) client.APIClient { + t.Helper() + c, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.37")) + assert.NilError(t, err) + return c +} + // DefaultPollSettings used with gotestyourself/poll var DefaultPollSettings = poll.WithDelay(100 * time.Millisecond) + +// SkipIfNotExperimentalDaemon returns whether the test docker daemon is in experimental mode +func SkipIfNotExperimentalDaemon(t *testing.T) { + t.Helper() + c := dockerClient(t) + info, err := c.Info(context.Background()) + assert.NilError(t, err) + skip.If(t, !info.ExperimentalBuild, "running against a non-experimental daemon") +}