github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/integration/build/build_squash_test.go (about) 1 package build 2 3 import ( 4 "bytes" 5 "context" 6 "io" 7 "io/ioutil" 8 "strings" 9 "testing" 10 11 "github.com/docker/docker/api/types" 12 "github.com/docker/docker/integration/internal/container" 13 "github.com/docker/docker/internal/test/daemon" 14 "github.com/docker/docker/internal/test/fakecontext" 15 "github.com/docker/docker/pkg/stdcopy" 16 "gotest.tools/assert" 17 is "gotest.tools/assert/cmp" 18 "gotest.tools/skip" 19 ) 20 21 func TestBuildSquashParent(t *testing.T) { 22 skip.If(t, testEnv.DaemonInfo.OSType == "windows") 23 skip.If(t, !testEnv.DaemonInfo.ExperimentalBuild) 24 skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon") 25 d := daemon.New(t, daemon.WithExperimental) 26 d.StartWithBusybox(t) 27 defer d.Stop(t) 28 29 client := d.NewClientT(t) 30 31 dockerfile := ` 32 FROM busybox 33 RUN echo hello > /hello 34 RUN echo world >> /hello 35 RUN echo hello > /remove_me 36 ENV HELLO world 37 RUN rm /remove_me 38 ` 39 40 // build and get the ID that we can use later for history comparison 41 ctx := context.Background() 42 source := fakecontext.New(t, "", fakecontext.WithDockerfile(dockerfile)) 43 defer source.Close() 44 45 name := "test" 46 resp, err := client.ImageBuild(ctx, 47 source.AsTarReader(t), 48 types.ImageBuildOptions{ 49 Remove: true, 50 ForceRemove: true, 51 Tags: []string{name}, 52 }) 53 assert.NilError(t, err) 54 _, err = io.Copy(ioutil.Discard, resp.Body) 55 resp.Body.Close() 56 assert.NilError(t, err) 57 58 inspect, _, err := client.ImageInspectWithRaw(ctx, name) 59 assert.NilError(t, err) 60 origID := inspect.ID 61 62 // build with squash 63 resp, err = client.ImageBuild(ctx, 64 source.AsTarReader(t), 65 types.ImageBuildOptions{ 66 Remove: true, 67 ForceRemove: true, 68 Squash: true, 69 Tags: []string{name}, 70 }) 71 assert.NilError(t, err) 72 _, err = io.Copy(ioutil.Discard, resp.Body) 73 resp.Body.Close() 74 assert.NilError(t, err) 75 76 cid := container.Run(t, ctx, client, 77 container.WithImage(name), 78 container.WithCmd("/bin/sh", "-c", "cat /hello"), 79 ) 80 reader, err := client.ContainerLogs(ctx, cid, types.ContainerLogsOptions{ 81 ShowStdout: true, 82 }) 83 assert.NilError(t, err) 84 85 actualStdout := new(bytes.Buffer) 86 actualStderr := ioutil.Discard 87 _, err = stdcopy.StdCopy(actualStdout, actualStderr, reader) 88 assert.NilError(t, err) 89 assert.Check(t, is.Equal(strings.TrimSpace(actualStdout.String()), "hello\nworld")) 90 91 container.Run(t, ctx, client, 92 container.WithImage(name), 93 container.WithCmd("/bin/sh", "-c", "[ ! -f /remove_me ]"), 94 ) 95 container.Run(t, ctx, client, 96 container.WithImage(name), 97 container.WithCmd("/bin/sh", "-c", `[ "$(echo $HELLO)" == "world" ]`), 98 ) 99 100 origHistory, err := client.ImageHistory(ctx, origID) 101 assert.NilError(t, err) 102 testHistory, err := client.ImageHistory(ctx, name) 103 assert.NilError(t, err) 104 105 inspect, _, err = client.ImageInspectWithRaw(ctx, name) 106 assert.NilError(t, err) 107 assert.Check(t, is.Len(testHistory, len(origHistory)+1)) 108 assert.Check(t, is.Len(inspect.RootFS.Layers, 2)) 109 }