docker_cli_save_load_test.go 11 KB

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