build_squash_test.go 2.7 KB

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