docker_cli_save_load_test.go 9.2 KB

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