docker_cli_save_load_test.go 8.6 KB

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