docker_cli_save_load_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. is "gotest.tools/v3/assert/cmp"
  13. "gotest.tools/v3/icmd"
  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. filesFilter := fmt.Sprintf("(^manifest.json$|%v)", cleanedImageID)
  77. if testEnv.UsingSnapshotter() {
  78. filesFilter = fmt.Sprintf("(^index.json$|^manifest.json$|%v)", cleanedImageID)
  79. }
  80. out, err := RunCommandPipelineWithOutput(
  81. exec.Command(dockerBinary, "save", fmt.Sprintf("%v:latest", repoName)),
  82. exec.Command("tar", "t"),
  83. exec.Command("grep", "-E", filesFilter))
  84. assert.NilError(c, err, "failed to save repo with image ID and index files: %s, %v", out, err)
  85. }
  86. func (s *DockerCLISaveLoadSuite) TestSaveImageId(c *testing.T) {
  87. testRequires(c, DaemonIsLinux)
  88. repoName := "foobar-save-image-id-test"
  89. dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", repoName))
  90. out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
  91. cleanedLongImageID := strings.TrimPrefix(strings.TrimSpace(out), "sha256:")
  92. out, _ = dockerCmd(c, "images", "-q", repoName)
  93. cleanedShortImageID := strings.TrimSpace(out)
  94. // Make sure IDs are not empty
  95. assert.Assert(c, cleanedLongImageID != "", "Id should not be empty.")
  96. assert.Assert(c, cleanedShortImageID != "", "Id should not be empty.")
  97. saveCmd := exec.Command(dockerBinary, "save", cleanedShortImageID)
  98. tarCmd := exec.Command("tar", "t")
  99. var err error
  100. tarCmd.Stdin, err = saveCmd.StdoutPipe()
  101. assert.Assert(c, err == nil, "cannot set stdout pipe for tar: %v", err)
  102. grepCmd := exec.Command("grep", cleanedLongImageID)
  103. grepCmd.Stdin, err = tarCmd.StdoutPipe()
  104. assert.Assert(c, err == nil, "cannot set stdout pipe for grep: %v", err)
  105. assert.Assert(c, tarCmd.Start() == nil, "tar failed with error: %v", err)
  106. assert.Assert(c, saveCmd.Start() == nil, "docker save failed with error: %v", err)
  107. defer func() {
  108. saveCmd.Wait()
  109. tarCmd.Wait()
  110. dockerCmd(c, "rmi", repoName)
  111. }()
  112. out, _, err = runCommandWithOutput(grepCmd)
  113. assert.Assert(c, err == nil, "failed to save repo with image ID: %s, %v", out, err)
  114. }
  115. // save a repo and try to load it using flags
  116. func (s *DockerCLISaveLoadSuite) TestSaveAndLoadRepoFlags(c *testing.T) {
  117. testRequires(c, DaemonIsLinux)
  118. name := "test-save-and-load-repo-flags"
  119. dockerCmd(c, "run", "--name", name, "busybox", "true")
  120. repoName := "foobar-save-load-test"
  121. deleteImages(repoName)
  122. dockerCmd(c, "commit", name, repoName)
  123. before, _ := dockerCmd(c, "inspect", repoName)
  124. out, err := RunCommandPipelineWithOutput(
  125. exec.Command(dockerBinary, "save", repoName),
  126. exec.Command(dockerBinary, "load"))
  127. assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
  128. after, _ := dockerCmd(c, "inspect", repoName)
  129. assert.Equal(c, before, after, "inspect is not the same after a save / load")
  130. }
  131. func (s *DockerCLISaveLoadSuite) TestSaveWithNoExistImage(c *testing.T) {
  132. testRequires(c, DaemonIsLinux)
  133. imgName := "foobar-non-existing-image"
  134. out, _, err := dockerCmdWithError("save", "-o", "test-img.tar", imgName)
  135. assert.ErrorContains(c, err, "", "save image should fail for non-existing image")
  136. assert.Assert(c, strings.Contains(out, fmt.Sprintf("No such image: %s", imgName)))
  137. }
  138. func (s *DockerCLISaveLoadSuite) TestSaveMultipleNames(c *testing.T) {
  139. testRequires(c, DaemonIsLinux)
  140. repoName := "foobar-save-multi-name-test"
  141. oneTag := fmt.Sprintf("%v-one:latest", repoName)
  142. twoTag := fmt.Sprintf("%v-two:latest", repoName)
  143. dockerCmd(c, "tag", "emptyfs:latest", oneTag)
  144. dockerCmd(c, "tag", "emptyfs:latest", twoTag)
  145. out, err := RunCommandPipelineWithOutput(
  146. exec.Command(dockerBinary, "save", strings.TrimSuffix(oneTag, ":latest"), twoTag),
  147. exec.Command("tar", "xO", "index.json"),
  148. )
  149. assert.NilError(c, err, "failed to save multiple repos: %s, %v", out, err)
  150. assert.Check(c, is.Contains(out, oneTag))
  151. assert.Check(c, is.Contains(out, twoTag))
  152. }
  153. // Test loading a weird image where one of the layers is of zero size.
  154. // The layer.tar file is actually zero bytes, no padding or anything else.
  155. // See issue: 18170
  156. func (s *DockerCLISaveLoadSuite) TestLoadZeroSizeLayer(c *testing.T) {
  157. // this will definitely not work if using remote daemon
  158. // very weird test
  159. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  160. dockerCmd(c, "load", "-i", "testdata/emptyLayer.tar")
  161. }
  162. func (s *DockerCLISaveLoadSuite) TestSaveLoadParents(c *testing.T) {
  163. testRequires(c, DaemonIsLinux)
  164. makeImage := func(from string, addfile string) string {
  165. var out string
  166. out, _ = dockerCmd(c, "run", "-d", from, "touch", addfile)
  167. cleanedContainerID := strings.TrimSpace(out)
  168. out, _ = dockerCmd(c, "commit", cleanedContainerID)
  169. imageID := strings.TrimSpace(out)
  170. dockerCmd(c, "rm", "-f", cleanedContainerID)
  171. return imageID
  172. }
  173. idFoo := makeImage("busybox", "foo")
  174. idBar := makeImage(idFoo, "bar")
  175. tmpDir, err := os.MkdirTemp("", "save-load-parents")
  176. assert.NilError(c, err)
  177. defer os.RemoveAll(tmpDir)
  178. c.Log("tmpdir", tmpDir)
  179. outfile := filepath.Join(tmpDir, "out.tar")
  180. dockerCmd(c, "save", "-o", outfile, idBar, idFoo)
  181. dockerCmd(c, "rmi", idBar)
  182. dockerCmd(c, "load", "-i", outfile)
  183. inspectOut := inspectField(c, idBar, "Parent")
  184. assert.Equal(c, inspectOut, idFoo)
  185. inspectOut = inspectField(c, idFoo, "Parent")
  186. assert.Equal(c, inspectOut, "")
  187. }
  188. func (s *DockerCLISaveLoadSuite) TestSaveLoadNoTag(c *testing.T) {
  189. testRequires(c, DaemonIsLinux)
  190. name := "saveloadnotag"
  191. buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV foo=bar"))
  192. id := inspectField(c, name, "Id")
  193. // Test to make sure that save w/o name just shows imageID during load
  194. out, err := RunCommandPipelineWithOutput(
  195. exec.Command(dockerBinary, "save", id),
  196. exec.Command(dockerBinary, "load"))
  197. assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
  198. // Should not show 'name' but should show the image ID during the load
  199. assert.Assert(c, !strings.Contains(out, "Loaded image: "))
  200. assert.Assert(c, strings.Contains(out, "Loaded image ID:"))
  201. assert.Assert(c, strings.Contains(out, id))
  202. // Test to make sure that save by name shows that name during load
  203. out, err = RunCommandPipelineWithOutput(
  204. exec.Command(dockerBinary, "save", name),
  205. exec.Command(dockerBinary, "load"))
  206. assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
  207. assert.Assert(c, strings.Contains(out, "Loaded image: "+name+":latest"))
  208. assert.Assert(c, !strings.Contains(out, "Loaded image ID:"))
  209. }