docker_cli_save_load_test.go 10 KB

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