github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/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 "github.com/gotestyourself/gotestyourself/assert" 17 is "github.com/gotestyourself/gotestyourself/assert/cmp" 18 ) 19 20 func TestBuildSquashParent(t *testing.T) { 21 d := daemon.New(t, daemon.WithExperimental) 22 d.StartWithBusybox(t) 23 defer d.Stop(t) 24 25 client, err := d.NewClient() 26 assert.NilError(t, err) 27 28 dockerfile := ` 29 FROM busybox 30 RUN echo hello > /hello 31 RUN echo world >> /hello 32 RUN echo hello > /remove_me 33 ENV HELLO world 34 RUN rm /remove_me 35 ` 36 37 // build and get the ID that we can use later for history comparison 38 ctx := context.Background() 39 source := fakecontext.New(t, "", fakecontext.WithDockerfile(dockerfile)) 40 defer source.Close() 41 42 name := "test" 43 resp, err := client.ImageBuild(ctx, 44 source.AsTarReader(t), 45 types.ImageBuildOptions{ 46 Remove: true, 47 ForceRemove: true, 48 Tags: []string{name}, 49 }) 50 assert.NilError(t, err) 51 _, err = io.Copy(ioutil.Discard, resp.Body) 52 resp.Body.Close() 53 assert.NilError(t, err) 54 55 inspect, _, err := client.ImageInspectWithRaw(ctx, name) 56 assert.NilError(t, err) 57 origID := inspect.ID 58 59 // build with squash 60 resp, err = client.ImageBuild(ctx, 61 source.AsTarReader(t), 62 types.ImageBuildOptions{ 63 Remove: true, 64 ForceRemove: true, 65 Squash: true, 66 Tags: []string{name}, 67 }) 68 assert.NilError(t, err) 69 _, err = io.Copy(ioutil.Discard, resp.Body) 70 resp.Body.Close() 71 assert.NilError(t, err) 72 73 cid := container.Run(t, ctx, client, 74 container.WithImage(name), 75 container.WithCmd("/bin/sh", "-c", "cat /hello"), 76 ) 77 reader, err := client.ContainerLogs(ctx, cid, types.ContainerLogsOptions{ 78 ShowStdout: true, 79 }) 80 assert.NilError(t, err) 81 82 actualStdout := new(bytes.Buffer) 83 actualStderr := ioutil.Discard 84 _, err = stdcopy.StdCopy(actualStdout, actualStderr, reader) 85 assert.NilError(t, err) 86 assert.Check(t, is.Equal(strings.TrimSpace(actualStdout.String()), "hello\nworld")) 87 88 container.Run(t, ctx, client, 89 container.WithImage(name), 90 container.WithCmd("/bin/sh", "-c", "[ ! -f /remove_me ]"), 91 ) 92 container.Run(t, ctx, client, 93 container.WithImage(name), 94 container.WithCmd("/bin/sh", "-c", `[ "$(echo $HELLO)" == "world" ]`), 95 ) 96 97 origHistory, err := client.ImageHistory(ctx, origID) 98 assert.NilError(t, err) 99 testHistory, err := client.ImageHistory(ctx, name) 100 assert.NilError(t, err) 101 102 inspect, _, err = client.ImageInspectWithRaw(ctx, name) 103 assert.NilError(t, err) 104 assert.Check(t, is.Len(testHistory, len(origHistory)+1)) 105 assert.Check(t, is.Len(inspect.RootFS.Layers, 2)) 106 }