docker_cli_save_load_test.go 8.8 KB

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