docker_cli_save_load_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. filesFilter := fmt.Sprintf("(^manifest.json$|%v)", cleanedImageID)
  80. if testEnv.UsingSnapshotter() {
  81. filesFilter = fmt.Sprintf("(^index.json$|^manifest.json$|%v)", cleanedImageID)
  82. }
  83. out, err := RunCommandPipelineWithOutput(
  84. exec.Command(dockerBinary, "save", fmt.Sprintf("%v:latest", repoName)),
  85. exec.Command("tar", "t"),
  86. exec.Command("grep", "-E", filesFilter))
  87. assert.NilError(c, err, "failed to save repo with image ID and index files: %s, %v", out, err)
  88. }
  89. func (s *DockerCLISaveLoadSuite) TestSaveImageId(c *testing.T) {
  90. testRequires(c, DaemonIsLinux)
  91. repoName := "foobar-save-image-id-test"
  92. dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", repoName))
  93. out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
  94. cleanedLongImageID := strings.TrimPrefix(strings.TrimSpace(out), "sha256:")
  95. out, _ = dockerCmd(c, "images", "-q", repoName)
  96. cleanedShortImageID := strings.TrimSpace(out)
  97. // Make sure IDs are not empty
  98. assert.Assert(c, cleanedLongImageID != "", "Id should not be empty.")
  99. assert.Assert(c, cleanedShortImageID != "", "Id should not be empty.")
  100. saveCmd := exec.Command(dockerBinary, "save", cleanedShortImageID)
  101. tarCmd := exec.Command("tar", "t")
  102. var err error
  103. tarCmd.Stdin, err = saveCmd.StdoutPipe()
  104. assert.Assert(c, err == nil, "cannot set stdout pipe for tar: %v", err)
  105. grepCmd := exec.Command("grep", cleanedLongImageID)
  106. grepCmd.Stdin, err = tarCmd.StdoutPipe()
  107. assert.Assert(c, err == nil, "cannot set stdout pipe for grep: %v", err)
  108. assert.Assert(c, tarCmd.Start() == nil, "tar failed with error: %v", err)
  109. assert.Assert(c, saveCmd.Start() == nil, "docker save failed with error: %v", err)
  110. defer func() {
  111. saveCmd.Wait()
  112. tarCmd.Wait()
  113. dockerCmd(c, "rmi", repoName)
  114. }()
  115. out, _, err = runCommandWithOutput(grepCmd)
  116. assert.Assert(c, err == nil, "failed to save repo with image ID: %s, %v", out, err)
  117. }
  118. // save a repo and try to load it using flags
  119. func (s *DockerCLISaveLoadSuite) TestSaveAndLoadRepoFlags(c *testing.T) {
  120. testRequires(c, DaemonIsLinux)
  121. name := "test-save-and-load-repo-flags"
  122. dockerCmd(c, "run", "--name", name, "busybox", "true")
  123. repoName := "foobar-save-load-test"
  124. deleteImages(repoName)
  125. dockerCmd(c, "commit", name, repoName)
  126. beforeStr, _, err := dockerCmdWithError("inspect", repoName)
  127. assert.NilError(c, err, "failed to inspect before save")
  128. out, err := RunCommandPipelineWithOutput(
  129. exec.Command(dockerBinary, "save", repoName),
  130. exec.Command(dockerBinary, "load"))
  131. assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
  132. afterStr, _, err := dockerCmdWithError("inspect", repoName)
  133. assert.NilError(c, err, "failed to inspect after load")
  134. var before, after []types.ImageInspect
  135. err = json.Unmarshal([]byte(beforeStr), &before)
  136. assert.NilError(c, err, "failed to parse inspect 'before' output")
  137. err = json.Unmarshal([]byte(afterStr), &after)
  138. assert.NilError(c, err, "failed to parse inspect 'after' output")
  139. assert.Assert(c, is.Len(before, 1))
  140. assert.Assert(c, is.Len(after, 1))
  141. if testEnv.UsingSnapshotter() {
  142. // Ignore LastTagTime difference with c8d.
  143. // It is not stored in the image archive, but in the imageStore
  144. // which is a graphdrivers implementation detail.
  145. //
  146. // It works because we load the image into the same daemon which saved
  147. // the image. It would still fail with the graphdrivers if the image
  148. // was loaded into a different daemon (which should be the case in a
  149. // real-world scenario).
  150. before[0].Metadata.LastTagTime = after[0].Metadata.LastTagTime
  151. }
  152. assert.Check(c, is.DeepEqual(before, after), "inspect is not the same after a save / load")
  153. }
  154. func (s *DockerCLISaveLoadSuite) TestSaveWithNoExistImage(c *testing.T) {
  155. testRequires(c, DaemonIsLinux)
  156. imgName := "foobar-non-existing-image"
  157. out, _, err := dockerCmdWithError("save", "-o", "test-img.tar", imgName)
  158. assert.ErrorContains(c, err, "", "save image should fail for non-existing image")
  159. assert.Assert(c, strings.Contains(out, fmt.Sprintf("No such image: %s", imgName)))
  160. }
  161. func (s *DockerCLISaveLoadSuite) TestSaveMultipleNames(c *testing.T) {
  162. testRequires(c, DaemonIsLinux)
  163. repoName := "foobar-save-multi-name-test"
  164. oneTag := fmt.Sprintf("%v-one:latest", repoName)
  165. twoTag := fmt.Sprintf("%v-two:latest", repoName)
  166. dockerCmd(c, "tag", "emptyfs:latest", oneTag)
  167. dockerCmd(c, "tag", "emptyfs:latest", twoTag)
  168. out, err := RunCommandPipelineWithOutput(
  169. exec.Command(dockerBinary, "save", strings.TrimSuffix(oneTag, ":latest"), twoTag),
  170. exec.Command("tar", "xO", "index.json"),
  171. )
  172. assert.NilError(c, err, "failed to save multiple repos: %s, %v", out, err)
  173. assert.Check(c, is.Contains(out, oneTag))
  174. assert.Check(c, is.Contains(out, twoTag))
  175. }
  176. // Test loading a weird image where one of the layers is of zero size.
  177. // The layer.tar file is actually zero bytes, no padding or anything else.
  178. // See issue: 18170
  179. func (s *DockerCLISaveLoadSuite) TestLoadZeroSizeLayer(c *testing.T) {
  180. // TODO(vvoland): Create an OCI image with 0 bytes layer.
  181. skip.If(c, testEnv.UsingSnapshotter(), "input archive is not OCI compatible")
  182. // this will definitely not work if using remote daemon
  183. // very weird test
  184. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  185. dockerCmd(c, "load", "-i", "testdata/emptyLayer.tar")
  186. }
  187. func (s *DockerCLISaveLoadSuite) TestSaveLoadParents(c *testing.T) {
  188. testRequires(c, DaemonIsLinux)
  189. skip.If(c, testEnv.UsingSnapshotter(), "Parent image property is not supported with containerd")
  190. makeImage := func(from string, addfile string) string {
  191. var out string
  192. out, _ = dockerCmd(c, "run", "-d", from, "touch", addfile)
  193. cleanedContainerID := strings.TrimSpace(out)
  194. out, _ = dockerCmd(c, "commit", cleanedContainerID)
  195. imageID := strings.TrimSpace(out)
  196. dockerCmd(c, "rm", "-f", cleanedContainerID)
  197. return imageID
  198. }
  199. idFoo := makeImage("busybox", "foo")
  200. idBar := makeImage(idFoo, "bar")
  201. tmpDir, err := os.MkdirTemp("", "save-load-parents")
  202. assert.NilError(c, err)
  203. defer os.RemoveAll(tmpDir)
  204. c.Log("tmpdir", tmpDir)
  205. outfile := filepath.Join(tmpDir, "out.tar")
  206. dockerCmd(c, "save", "-o", outfile, idBar, idFoo)
  207. dockerCmd(c, "rmi", idBar)
  208. dockerCmd(c, "load", "-i", outfile)
  209. inspectOut := inspectField(c, idBar, "Parent")
  210. assert.Equal(c, inspectOut, idFoo)
  211. inspectOut = inspectField(c, idFoo, "Parent")
  212. assert.Equal(c, inspectOut, "")
  213. }
  214. func (s *DockerCLISaveLoadSuite) TestSaveLoadNoTag(c *testing.T) {
  215. testRequires(c, DaemonIsLinux)
  216. name := "saveloadnotag"
  217. buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV foo=bar"))
  218. id := inspectField(c, name, "Id")
  219. // Test to make sure that save w/o name just shows imageID during load
  220. out, err := RunCommandPipelineWithOutput(
  221. exec.Command(dockerBinary, "save", id),
  222. exec.Command(dockerBinary, "load"))
  223. assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
  224. // Should not show 'name' but should show the image ID during the load
  225. assert.Assert(c, !strings.Contains(out, "Loaded image: "))
  226. assert.Assert(c, strings.Contains(out, "Loaded image ID:"))
  227. assert.Assert(c, strings.Contains(out, id))
  228. // Test to make sure that save by name shows that name during load
  229. out, err = RunCommandPipelineWithOutput(
  230. exec.Command(dockerBinary, "save", name),
  231. exec.Command(dockerBinary, "load"))
  232. assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err)
  233. assert.Assert(c, strings.Contains(out, "Loaded image: "+name+":latest"))
  234. assert.Assert(c, !strings.Contains(out, "Loaded image ID:"))
  235. }