docker_cli_save_load_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "github.com/docker/docker/integration-cli/cli/build"
  10. "gotest.tools/v3/assert"
  11. "gotest.tools/v3/icmd"
  12. )
  13. type DockerCLISaveLoadSuite struct {
  14. ds *DockerSuite
  15. }
  16. func (s *DockerCLISaveLoadSuite) TearDownTest(c *testing.T) {
  17. s.ds.TearDownTest(c)
  18. }
  19. func (s *DockerCLISaveLoadSuite) OnTimeout(c *testing.T) {
  20. s.ds.OnTimeout(c)
  21. }
  22. // save a repo using gz compression and try to load it using stdout
  23. func (s *DockerCLISaveLoadSuite) TestSaveXzAndLoadRepoStdout(c *testing.T) {
  24. testRequires(c, DaemonIsLinux)
  25. name := "test-save-xz-and-load-repo-stdout"
  26. dockerCmd(c, "run", "--name", name, "busybox", "true")
  27. repoName := "foobar-save-load-test-xz-gz"
  28. out, _ := dockerCmd(c, "commit", name, repoName)
  29. dockerCmd(c, "inspect", repoName)
  30. repoTarball, err := RunCommandPipelineWithOutput(
  31. exec.Command(dockerBinary, "save", repoName),
  32. exec.Command("xz", "-c"),
  33. exec.Command("gzip", "-c"))
  34. assert.NilError(c, err, "failed to save repo: %v %v", out, err)
  35. deleteImages(repoName)
  36. icmd.RunCmd(icmd.Cmd{
  37. Command: []string{dockerBinary, "load"},
  38. Stdin: strings.NewReader(repoTarball),
  39. }).Assert(c, icmd.Expected{
  40. ExitCode: 1,
  41. })
  42. after, _, err := dockerCmdWithError("inspect", repoName)
  43. assert.ErrorContains(c, err, "", "the repo should not exist: %v", after)
  44. }
  45. // save a repo using xz+gz compression and try to load it using stdout
  46. func (s *DockerCLISaveLoadSuite) TestSaveXzGzAndLoadRepoStdout(c *testing.T) {
  47. testRequires(c, DaemonIsLinux)
  48. name := "test-save-xz-gz-and-load-repo-stdout"
  49. dockerCmd(c, "run", "--name", name, "busybox", "true")
  50. repoName := "foobar-save-load-test-xz-gz"
  51. dockerCmd(c, "commit", name, repoName)
  52. dockerCmd(c, "inspect", repoName)
  53. out, err := RunCommandPipelineWithOutput(
  54. exec.Command(dockerBinary, "save", repoName),
  55. exec.Command("xz", "-c"),
  56. exec.Command("gzip", "-c"))
  57. assert.NilError(c, err, "failed to save repo: %v %v", out, err)
  58. deleteImages(repoName)
  59. icmd.RunCmd(icmd.Cmd{
  60. Command: []string{dockerBinary, "load"},
  61. Stdin: strings.NewReader(out),
  62. }).Assert(c, icmd.Expected{
  63. ExitCode: 1,
  64. })
  65. after, _, err := dockerCmdWithError("inspect", repoName)
  66. assert.ErrorContains(c, err, "", "the repo should not exist: %v", after)
  67. }
  68. func (s *DockerCLISaveLoadSuite) TestSaveSingleTag(c *testing.T) {
  69. testRequires(c, DaemonIsLinux)
  70. repoName := "foobar-save-single-tag-test"
  71. dockerCmd(c, "tag", "busybox:latest", fmt.Sprintf("%v:latest", repoName))
  72. out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
  73. cleanedImageID := strings.TrimSpace(out)
  74. out, err := RunCommandPipelineWithOutput(
  75. exec.Command(dockerBinary, "save", fmt.Sprintf("%v:latest", repoName)),
  76. exec.Command("tar", "t"),
  77. exec.Command("grep", "-E", fmt.Sprintf("(^repositories$|%v)", cleanedImageID)))
  78. assert.NilError(c, err, "failed to save repo with image ID and 'repositories' file: %s, %v", out, err)
  79. }
  80. func (s *DockerCLISaveLoadSuite) TestSaveImageId(c *testing.T) {
  81. testRequires(c, DaemonIsLinux)
  82. repoName := "foobar-save-image-id-test"
  83. dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", repoName))
  84. out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
  85. cleanedLongImageID := strings.TrimPrefix(strings.TrimSpace(out), "sha256:")
  86. out, _ = dockerCmd(c, "images", "-q", repoName)
  87. cleanedShortImageID := strings.TrimSpace(out)
  88. // Make sure IDs are not empty
  89. assert.Assert(c, cleanedLongImageID != "", "Id should not be empty.")
  90. assert.Assert(c, cleanedShortImageID != "", "Id should not be empty.")
  91. saveCmd := exec.Command(dockerBinary, "save", cleanedShortImageID)
  92. tarCmd := exec.Command("tar", "t")
  93. var err error
  94. tarCmd.Stdin, err = saveCmd.StdoutPipe()
  95. assert.Assert(c, err == nil, "cannot set stdout pipe for tar: %v", err)
  96. grepCmd := exec.Command("grep", cleanedLongImageID)
  97. grepCmd.Stdin, err = tarCmd.StdoutPipe()
  98. assert.Assert(c, err == nil, "cannot set stdout pipe for grep: %v", err)
  99. assert.Assert(c, tarCmd.Start() == nil, "tar failed with error: %v", err)
  100. assert.Assert(c, saveCmd.Start() == nil, "docker save failed with error: %v", err)
  101. defer func() {
  102. saveCmd.Wait()
  103. tarCmd.Wait()
  104. dockerCmd(c, "rmi", repoName)
  105. }()
  106. out, _, err = runCommandWithOutput(grepCmd)
  107. assert.Assert(c, err == nil, "failed to save repo with image ID: %s, %v", out, err)
  108. }
  109. // save a repo and try to load it using flags
  110. func (s *DockerCLISaveLoadSuite) TestSaveAndLoadRepoFlags(c *testing.T) {
  111. testRequires(c, DaemonIsLinux)
  112. name := "test-save-and-load-repo-flags"
  113. dockerCmd(c, "run", "--name", name, "busybox", "true")
  114. repoName := "foobar-save-load-test"
  115. deleteImages(repoName)
  116. dockerCmd(c, "commit", name, repoName)
  117. before, _ := dockerCmd(c, "inspect", repoName)
  118. out, err := RunCommandPipelineWithOutput(
  119. exec.Command(dockerBinary, "save", repoName),
  120. exec.Command(dockerBinary, "load"))
  121. assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
  122. after, _ := dockerCmd(c, "inspect", repoName)
  123. assert.Equal(c, before, after, "inspect is not the same after a save / load")
  124. }
  125. func (s *DockerCLISaveLoadSuite) TestSaveWithNoExistImage(c *testing.T) {
  126. testRequires(c, DaemonIsLinux)
  127. imgName := "foobar-non-existing-image"
  128. out, _, err := dockerCmdWithError("save", "-o", "test-img.tar", imgName)
  129. assert.ErrorContains(c, err, "", "save image should fail for non-existing image")
  130. assert.Assert(c, strings.Contains(out, fmt.Sprintf("No such image: %s", imgName)))
  131. }
  132. func (s *DockerCLISaveLoadSuite) TestSaveMultipleNames(c *testing.T) {
  133. testRequires(c, DaemonIsLinux)
  134. repoName := "foobar-save-multi-name-test"
  135. // Make one image
  136. dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-one:latest", repoName))
  137. // Make two images
  138. dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-two:latest", repoName))
  139. out, err := RunCommandPipelineWithOutput(
  140. exec.Command(dockerBinary, "save", fmt.Sprintf("%v-one", repoName), fmt.Sprintf("%v-two:latest", repoName)),
  141. exec.Command("tar", "xO", "repositories"),
  142. exec.Command("grep", "-q", "-E", "(-one|-two)"),
  143. )
  144. assert.NilError(c, err, "failed to save multiple repos: %s, %v", out, err)
  145. }
  146. // Test loading a weird image where one of the layers is of zero size.
  147. // The layer.tar file is actually zero bytes, no padding or anything else.
  148. // See issue: 18170
  149. func (s *DockerCLISaveLoadSuite) TestLoadZeroSizeLayer(c *testing.T) {
  150. // this will definitely not work if using remote daemon
  151. // very weird test
  152. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  153. dockerCmd(c, "load", "-i", "testdata/emptyLayer.tar")
  154. }
  155. func (s *DockerCLISaveLoadSuite) TestSaveLoadParents(c *testing.T) {
  156. testRequires(c, DaemonIsLinux)
  157. makeImage := func(from string, addfile string) string {
  158. var (
  159. out string
  160. )
  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. }