docker_cli_save_load_test.go 9.9 KB

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