docker_cli_save_load_test.go 9.4 KB

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