docker_cli_save_load_test.go 12 KB

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