docker_cli_save_load_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "reflect"
  9. "sort"
  10. "strings"
  11. "github.com/go-check/check"
  12. )
  13. // save a repo using gz compression and try to load it using stdout
  14. func (s *DockerSuite) TestSaveXzAndLoadRepoStdout(c *check.C) {
  15. name := "test-save-xz-and-load-repo-stdout"
  16. dockerCmd(c, "run", "--name", name, "busybox", "true")
  17. repoName := "foobar-save-load-test-xz-gz"
  18. out, _ := dockerCmd(c, "commit", name, repoName)
  19. dockerCmd(c, "inspect", repoName)
  20. repoTarball, _, err := runCommandPipelineWithOutput(
  21. exec.Command(dockerBinary, "save", repoName),
  22. exec.Command("xz", "-c"),
  23. exec.Command("gzip", "-c"))
  24. if err != nil {
  25. c.Fatalf("failed to save repo: %v %v", out, err)
  26. }
  27. deleteImages(repoName)
  28. loadCmd := exec.Command(dockerBinary, "load")
  29. loadCmd.Stdin = strings.NewReader(repoTarball)
  30. out, _, err = runCommandWithOutput(loadCmd)
  31. if err == nil {
  32. c.Fatalf("expected error, but succeeded with no error and output: %v", out)
  33. }
  34. after, _, err := dockerCmdWithError(c, "inspect", repoName)
  35. if err == nil {
  36. c.Fatalf("the repo should not exist: %v", after)
  37. }
  38. }
  39. // save a repo using xz+gz compression and try to load it using stdout
  40. func (s *DockerSuite) TestSaveXzGzAndLoadRepoStdout(c *check.C) {
  41. name := "test-save-xz-gz-and-load-repo-stdout"
  42. dockerCmd(c, "run", "--name", name, "busybox", "true")
  43. repoName := "foobar-save-load-test-xz-gz"
  44. dockerCmd(c, "commit", name, repoName)
  45. dockerCmd(c, "inspect", repoName)
  46. out, _, err := runCommandPipelineWithOutput(
  47. exec.Command(dockerBinary, "save", repoName),
  48. exec.Command("xz", "-c"),
  49. exec.Command("gzip", "-c"))
  50. if err != nil {
  51. c.Fatalf("failed to save repo: %v %v", out, err)
  52. }
  53. deleteImages(repoName)
  54. loadCmd := exec.Command(dockerBinary, "load")
  55. loadCmd.Stdin = strings.NewReader(out)
  56. out, _, err = runCommandWithOutput(loadCmd)
  57. if err == nil {
  58. c.Fatalf("expected error, but succeeded with no error and output: %v", out)
  59. }
  60. after, _, err := dockerCmdWithError(c, "inspect", repoName)
  61. if err == nil {
  62. c.Fatalf("the repo should not exist: %v", after)
  63. }
  64. }
  65. func (s *DockerSuite) TestSaveSingleTag(c *check.C) {
  66. repoName := "foobar-save-single-tag-test"
  67. dockerCmd(c, "tag", "busybox:latest", fmt.Sprintf("%v:latest", repoName))
  68. out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
  69. cleanedImageID := strings.TrimSpace(out)
  70. out, _, err := runCommandPipelineWithOutput(
  71. exec.Command(dockerBinary, "save", fmt.Sprintf("%v:latest", repoName)),
  72. exec.Command("tar", "t"),
  73. exec.Command("grep", "-E", fmt.Sprintf("(^repositories$|%v)", cleanedImageID)))
  74. if err != nil {
  75. c.Fatalf("failed to save repo with image ID and 'repositories' file: %s, %v", out, err)
  76. }
  77. }
  78. func (s *DockerSuite) TestSaveImageId(c *check.C) {
  79. repoName := "foobar-save-image-id-test"
  80. dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", repoName))
  81. out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
  82. cleanedLongImageID := strings.TrimSpace(out)
  83. out, _ = dockerCmd(c, "images", "-q", repoName)
  84. cleanedShortImageID := strings.TrimSpace(out)
  85. saveCmd := exec.Command(dockerBinary, "save", cleanedShortImageID)
  86. tarCmd := exec.Command("tar", "t")
  87. var err error
  88. tarCmd.Stdin, err = saveCmd.StdoutPipe()
  89. if err != nil {
  90. c.Fatalf("cannot set stdout pipe for tar: %v", err)
  91. }
  92. grepCmd := exec.Command("grep", cleanedLongImageID)
  93. grepCmd.Stdin, err = tarCmd.StdoutPipe()
  94. if err != nil {
  95. c.Fatalf("cannot set stdout pipe for grep: %v", err)
  96. }
  97. if err = tarCmd.Start(); err != nil {
  98. c.Fatalf("tar failed with error: %v", err)
  99. }
  100. if err = saveCmd.Start(); err != nil {
  101. c.Fatalf("docker save failed with error: %v", err)
  102. }
  103. defer saveCmd.Wait()
  104. defer tarCmd.Wait()
  105. out, _, err = runCommandWithOutput(grepCmd)
  106. if err != nil {
  107. c.Fatalf("failed to save repo with image ID: %s, %v", out, err)
  108. }
  109. }
  110. // save a repo and try to load it using flags
  111. func (s *DockerSuite) TestSaveAndLoadRepoFlags(c *check.C) {
  112. name := "test-save-and-load-repo-flags"
  113. dockerCmd(c, "run", "--name", name, "busybox", "true")
  114. repoName := "foobar-save-load-test"
  115. deleteImages(repoName)
  116. dockerCmd(c, "commit", name, repoName)
  117. before, _ := dockerCmd(c, "inspect", repoName)
  118. out, _, err := runCommandPipelineWithOutput(
  119. exec.Command(dockerBinary, "save", repoName),
  120. exec.Command(dockerBinary, "load"))
  121. if err != nil {
  122. c.Fatalf("failed to save and load repo: %s, %v", out, err)
  123. }
  124. after, _ := dockerCmd(c, "inspect", repoName)
  125. if before != after {
  126. c.Fatalf("inspect is not the same after a save / load")
  127. }
  128. }
  129. func (s *DockerSuite) TestSaveMultipleNames(c *check.C) {
  130. repoName := "foobar-save-multi-name-test"
  131. // Make one image
  132. dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-one:latest", repoName))
  133. // Make two images
  134. dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-two:latest", repoName))
  135. out, _, err := runCommandPipelineWithOutput(
  136. exec.Command(dockerBinary, "save", fmt.Sprintf("%v-one", repoName), fmt.Sprintf("%v-two:latest", repoName)),
  137. exec.Command("tar", "xO", "repositories"),
  138. exec.Command("grep", "-q", "-E", "(-one|-two)"),
  139. )
  140. if err != nil {
  141. c.Fatalf("failed to save multiple repos: %s, %v", out, err)
  142. }
  143. }
  144. func (s *DockerSuite) TestSaveRepoWithMultipleImages(c *check.C) {
  145. makeImage := func(from string, tag string) string {
  146. var (
  147. out string
  148. )
  149. out, _ = dockerCmd(c, "run", "-d", from, "true")
  150. cleanedContainerID := strings.TrimSpace(out)
  151. out, _ = dockerCmd(c, "commit", cleanedContainerID, tag)
  152. imageID := strings.TrimSpace(out)
  153. return imageID
  154. }
  155. repoName := "foobar-save-multi-images-test"
  156. tagFoo := repoName + ":foo"
  157. tagBar := repoName + ":bar"
  158. idFoo := makeImage("busybox:latest", tagFoo)
  159. idBar := makeImage("busybox:latest", tagBar)
  160. deleteImages(repoName)
  161. // create the archive
  162. out, _, err := runCommandPipelineWithOutput(
  163. exec.Command(dockerBinary, "save", repoName),
  164. exec.Command("tar", "t"),
  165. exec.Command("grep", "VERSION"),
  166. exec.Command("cut", "-d", "/", "-f1"))
  167. if err != nil {
  168. c.Fatalf("failed to save multiple images: %s, %v", out, err)
  169. }
  170. actual := strings.Split(strings.TrimSpace(out), "\n")
  171. // make the list of expected layers
  172. out, _ = dockerCmd(c, "history", "-q", "--no-trunc", "busybox:latest")
  173. expected := append(strings.Split(strings.TrimSpace(out), "\n"), idFoo, idBar)
  174. sort.Strings(actual)
  175. sort.Strings(expected)
  176. if !reflect.DeepEqual(expected, actual) {
  177. c.Fatalf("archive does not contains the right layers: got %v, expected %v", actual, expected)
  178. }
  179. }
  180. // Issue #6722 #5892 ensure directories are included in changes
  181. func (s *DockerSuite) TestSaveDirectoryPermissions(c *check.C) {
  182. layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  183. layerEntriesAUFS := []string{"./", ".wh..wh.aufs", ".wh..wh.orph/", ".wh..wh.plnk/", "opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  184. name := "save-directory-permissions"
  185. tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
  186. if err != nil {
  187. c.Errorf("failed to create temporary directory: %s", err)
  188. }
  189. extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
  190. os.Mkdir(extractionDirectory, 0777)
  191. defer os.RemoveAll(tmpDir)
  192. _, err = buildImage(name,
  193. `FROM busybox
  194. RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a
  195. RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`,
  196. true)
  197. if err != nil {
  198. c.Fatal(err)
  199. }
  200. if out, _, err := runCommandPipelineWithOutput(
  201. exec.Command(dockerBinary, "save", name),
  202. exec.Command("tar", "-xf", "-", "-C", extractionDirectory),
  203. ); err != nil {
  204. c.Errorf("failed to save and extract image: %s", out)
  205. }
  206. dirs, err := ioutil.ReadDir(extractionDirectory)
  207. if err != nil {
  208. c.Errorf("failed to get a listing of the layer directories: %s", err)
  209. }
  210. found := false
  211. for _, entry := range dirs {
  212. var entriesSansDev []string
  213. if entry.IsDir() {
  214. layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
  215. f, err := os.Open(layerPath)
  216. if err != nil {
  217. c.Fatalf("failed to open %s: %s", layerPath, err)
  218. }
  219. entries, err := ListTar(f)
  220. for _, e := range entries {
  221. if !strings.Contains(e, "dev/") {
  222. entriesSansDev = append(entriesSansDev, e)
  223. }
  224. }
  225. if err != nil {
  226. c.Fatalf("encountered error while listing tar entries: %s", err)
  227. }
  228. if reflect.DeepEqual(entriesSansDev, layerEntries) || reflect.DeepEqual(entriesSansDev, layerEntriesAUFS) {
  229. found = true
  230. break
  231. }
  232. }
  233. }
  234. if !found {
  235. c.Fatalf("failed to find the layer with the right content listing")
  236. }
  237. }