docker_cli_save_load_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/integration-cli/cli/build"
  11. "gotest.tools/v3/assert"
  12. "gotest.tools/v3/icmd"
  13. )
  14. type DockerCLISaveLoadSuite struct {
  15. ds *DockerSuite
  16. }
  17. func (s *DockerCLISaveLoadSuite) TearDownTest(ctx context.Context, c *testing.T) {
  18. s.ds.TearDownTest(ctx, c)
  19. }
  20. func (s *DockerCLISaveLoadSuite) OnTimeout(c *testing.T) {
  21. s.ds.OnTimeout(c)
  22. }
  23. // save a repo using gz compression and try to load it using stdout
  24. func (s *DockerCLISaveLoadSuite) TestSaveXzAndLoadRepoStdout(c *testing.T) {
  25. testRequires(c, DaemonIsLinux)
  26. name := "test-save-xz-and-load-repo-stdout"
  27. dockerCmd(c, "run", "--name", name, "busybox", "true")
  28. repoName := "foobar-save-load-test-xz-gz"
  29. out, _ := dockerCmd(c, "commit", name, repoName)
  30. dockerCmd(c, "inspect", repoName)
  31. repoTarball, err := RunCommandPipelineWithOutput(
  32. exec.Command(dockerBinary, "save", repoName),
  33. exec.Command("xz", "-c"),
  34. exec.Command("gzip", "-c"))
  35. assert.NilError(c, err, "failed to save repo: %v %v", out, err)
  36. deleteImages(repoName)
  37. icmd.RunCmd(icmd.Cmd{
  38. Command: []string{dockerBinary, "load"},
  39. Stdin: strings.NewReader(repoTarball),
  40. }).Assert(c, icmd.Expected{
  41. ExitCode: 1,
  42. })
  43. after, _, err := dockerCmdWithError("inspect", repoName)
  44. assert.ErrorContains(c, err, "", "the repo should not exist: %v", after)
  45. }
  46. // save a repo using xz+gz compression and try to load it using stdout
  47. func (s *DockerCLISaveLoadSuite) TestSaveXzGzAndLoadRepoStdout(c *testing.T) {
  48. testRequires(c, DaemonIsLinux)
  49. name := "test-save-xz-gz-and-load-repo-stdout"
  50. dockerCmd(c, "run", "--name", name, "busybox", "true")
  51. repoName := "foobar-save-load-test-xz-gz"
  52. dockerCmd(c, "commit", name, repoName)
  53. dockerCmd(c, "inspect", repoName)
  54. out, err := RunCommandPipelineWithOutput(
  55. exec.Command(dockerBinary, "save", repoName),
  56. exec.Command("xz", "-c"),
  57. exec.Command("gzip", "-c"))
  58. assert.NilError(c, err, "failed to save repo: %v %v", out, err)
  59. deleteImages(repoName)
  60. icmd.RunCmd(icmd.Cmd{
  61. Command: []string{dockerBinary, "load"},
  62. Stdin: strings.NewReader(out),
  63. }).Assert(c, icmd.Expected{
  64. ExitCode: 1,
  65. })
  66. after, _, err := dockerCmdWithError("inspect", repoName)
  67. assert.ErrorContains(c, err, "", "the repo should not exist: %v", after)
  68. }
  69. func (s *DockerCLISaveLoadSuite) TestSaveSingleTag(c *testing.T) {
  70. testRequires(c, DaemonIsLinux)
  71. repoName := "foobar-save-single-tag-test"
  72. dockerCmd(c, "tag", "busybox:latest", fmt.Sprintf("%v:latest", repoName))
  73. out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
  74. cleanedImageID := strings.TrimSpace(out)
  75. out, err := RunCommandPipelineWithOutput(
  76. exec.Command(dockerBinary, "save", fmt.Sprintf("%v:latest", repoName)),
  77. exec.Command("tar", "t"),
  78. exec.Command("grep", "-E", fmt.Sprintf("(^repositories$|%v)", cleanedImageID)))
  79. assert.NilError(c, err, "failed to save repo with image ID and 'repositories' file: %s, %v", out, err)
  80. }
  81. func (s *DockerCLISaveLoadSuite) TestSaveImageId(c *testing.T) {
  82. testRequires(c, DaemonIsLinux)
  83. repoName := "foobar-save-image-id-test"
  84. dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", repoName))
  85. out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
  86. cleanedLongImageID := strings.TrimPrefix(strings.TrimSpace(out), "sha256:")
  87. out, _ = dockerCmd(c, "images", "-q", repoName)
  88. cleanedShortImageID := strings.TrimSpace(out)
  89. // Make sure IDs are not empty
  90. assert.Assert(c, cleanedLongImageID != "", "Id should not be empty.")
  91. assert.Assert(c, cleanedShortImageID != "", "Id should not be empty.")
  92. saveCmd := exec.Command(dockerBinary, "save", cleanedShortImageID)
  93. tarCmd := exec.Command("tar", "t")
  94. var err error
  95. tarCmd.Stdin, err = saveCmd.StdoutPipe()
  96. assert.Assert(c, err == nil, "cannot set stdout pipe for tar: %v", err)
  97. grepCmd := exec.Command("grep", cleanedLongImageID)
  98. grepCmd.Stdin, err = tarCmd.StdoutPipe()
  99. assert.Assert(c, err == nil, "cannot set stdout pipe for grep: %v", err)
  100. assert.Assert(c, tarCmd.Start() == nil, "tar failed with error: %v", err)
  101. assert.Assert(c, saveCmd.Start() == nil, "docker save failed with error: %v", err)
  102. defer func() {
  103. saveCmd.Wait()
  104. tarCmd.Wait()
  105. dockerCmd(c, "rmi", repoName)
  106. }()
  107. out, _, err = runCommandWithOutput(grepCmd)
  108. assert.Assert(c, err == nil, "failed to save repo with image ID: %s, %v", out, err)
  109. }
  110. // save a repo and try to load it using flags
  111. func (s *DockerCLISaveLoadSuite) TestSaveAndLoadRepoFlags(c *testing.T) {
  112. testRequires(c, DaemonIsLinux)
  113. name := "test-save-and-load-repo-flags"
  114. dockerCmd(c, "run", "--name", name, "busybox", "true")
  115. repoName := "foobar-save-load-test"
  116. deleteImages(repoName)
  117. dockerCmd(c, "commit", name, repoName)
  118. before, _ := dockerCmd(c, "inspect", repoName)
  119. out, err := RunCommandPipelineWithOutput(
  120. exec.Command(dockerBinary, "save", repoName),
  121. exec.Command(dockerBinary, "load"))
  122. assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
  123. after, _ := dockerCmd(c, "inspect", repoName)
  124. assert.Equal(c, before, after, "inspect is not the same after a save / load")
  125. }
  126. func (s *DockerCLISaveLoadSuite) TestSaveWithNoExistImage(c *testing.T) {
  127. testRequires(c, DaemonIsLinux)
  128. imgName := "foobar-non-existing-image"
  129. out, _, err := dockerCmdWithError("save", "-o", "test-img.tar", imgName)
  130. assert.ErrorContains(c, err, "", "save image should fail for non-existing image")
  131. assert.Assert(c, strings.Contains(out, fmt.Sprintf("No such image: %s", imgName)))
  132. }
  133. func (s *DockerCLISaveLoadSuite) TestSaveMultipleNames(c *testing.T) {
  134. testRequires(c, DaemonIsLinux)
  135. repoName := "foobar-save-multi-name-test"
  136. // Make one image
  137. dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-one:latest", repoName))
  138. // Make two images
  139. dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-two:latest", repoName))
  140. out, err := RunCommandPipelineWithOutput(
  141. exec.Command(dockerBinary, "save", fmt.Sprintf("%v-one", repoName), fmt.Sprintf("%v-two:latest", repoName)),
  142. exec.Command("tar", "xO", "repositories"),
  143. exec.Command("grep", "-q", "-E", "(-one|-two)"),
  144. )
  145. assert.NilError(c, err, "failed to save multiple repos: %s, %v", out, err)
  146. }
  147. // Test loading a weird image where one of the layers is of zero size.
  148. // The layer.tar file is actually zero bytes, no padding or anything else.
  149. // See issue: 18170
  150. func (s *DockerCLISaveLoadSuite) TestLoadZeroSizeLayer(c *testing.T) {
  151. // this will definitely not work if using remote daemon
  152. // very weird test
  153. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  154. dockerCmd(c, "load", "-i", "testdata/emptyLayer.tar")
  155. }
  156. func (s *DockerCLISaveLoadSuite) TestSaveLoadParents(c *testing.T) {
  157. testRequires(c, DaemonIsLinux)
  158. skip.If(c, testEnv.UsingSnapshotter(), "Parent image property is not supported with containerd")
  159. makeImage := func(from string, addfile string) string {
  160. var out string
  161. out, _ = dockerCmd(c, "run", "-d", from, "touch", addfile)
  162. cleanedContainerID := strings.TrimSpace(out)
  163. out, _ = dockerCmd(c, "commit", cleanedContainerID)
  164. imageID := strings.TrimSpace(out)
  165. dockerCmd(c, "rm", "-f", cleanedContainerID)
  166. return imageID
  167. }
  168. idFoo := makeImage("busybox", "foo")
  169. idBar := makeImage(idFoo, "bar")
  170. tmpDir, err := os.MkdirTemp("", "save-load-parents")
  171. assert.NilError(c, err)
  172. defer os.RemoveAll(tmpDir)
  173. c.Log("tmpdir", tmpDir)
  174. outfile := filepath.Join(tmpDir, "out.tar")
  175. dockerCmd(c, "save", "-o", outfile, idBar, idFoo)
  176. dockerCmd(c, "rmi", idBar)
  177. dockerCmd(c, "load", "-i", outfile)
  178. inspectOut := inspectField(c, idBar, "Parent")
  179. assert.Equal(c, inspectOut, idFoo)
  180. inspectOut = inspectField(c, idFoo, "Parent")
  181. assert.Equal(c, inspectOut, "")
  182. }
  183. func (s *DockerCLISaveLoadSuite) TestSaveLoadNoTag(c *testing.T) {
  184. testRequires(c, DaemonIsLinux)
  185. name := "saveloadnotag"
  186. buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV foo=bar"))
  187. id := inspectField(c, name, "Id")
  188. // Test to make sure that save w/o name just shows imageID during load
  189. out, err := RunCommandPipelineWithOutput(
  190. exec.Command(dockerBinary, "save", id),
  191. exec.Command(dockerBinary, "load"))
  192. assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
  193. // Should not show 'name' but should show the image ID during the load
  194. assert.Assert(c, !strings.Contains(out, "Loaded image: "))
  195. assert.Assert(c, strings.Contains(out, "Loaded image ID:"))
  196. assert.Assert(c, strings.Contains(out, id))
  197. // Test to make sure that save by name shows that name during load
  198. out, err = RunCommandPipelineWithOutput(
  199. exec.Command(dockerBinary, "save", name),
  200. exec.Command(dockerBinary, "load"))
  201. assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
  202. assert.Assert(c, strings.Contains(out, "Loaded image: "+name+":latest"))
  203. assert.Assert(c, !strings.Contains(out, "Loaded image ID:"))
  204. }