docker_cli_save_load_test.go 9.7 KB

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