docker_cli_save_load_test.go 10 KB

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