docker_cli_save_load_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. defer deleteContainer(cleanedContainerID)
  177. repoName := "foobar-save-load-test"
  178. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  179. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  180. c.Fatalf("output should've been a container id: %s, %v", out, err)
  181. }
  182. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  183. deleteImages(repoName)
  184. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  185. c.Fatalf("failed to commit container: %s, %v", out, err)
  186. }
  187. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  188. before, _, err := runCommandWithOutput(inspectCmd)
  189. if err != nil {
  190. c.Fatalf("the repo should exist before saving it: %s, %v", before, err)
  191. }
  192. out, _, err = runCommandPipelineWithOutput(
  193. exec.Command(dockerBinary, "save", repoName),
  194. exec.Command(dockerBinary, "load"))
  195. if err != nil {
  196. c.Fatalf("failed to save and load repo: %s, %v", out, err)
  197. }
  198. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  199. after, _, err := runCommandWithOutput(inspectCmd)
  200. if err != nil {
  201. c.Fatalf("the repo should exist after loading it: %s, %v", after, err)
  202. }
  203. if before != after {
  204. c.Fatalf("inspect is not the same after a save / load")
  205. }
  206. }
  207. func (s *DockerSuite) TestSaveMultipleNames(c *check.C) {
  208. repoName := "foobar-save-multi-name-test"
  209. // Make one image
  210. tagCmd := exec.Command(dockerBinary, "tag", "emptyfs:latest", fmt.Sprintf("%v-one:latest", repoName))
  211. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  212. c.Fatalf("failed to tag repo: %s, %v", out, err)
  213. }
  214. defer deleteImages(repoName + "-one")
  215. // Make two images
  216. tagCmd = exec.Command(dockerBinary, "tag", "emptyfs:latest", fmt.Sprintf("%v-two:latest", repoName))
  217. out, _, err := runCommandWithOutput(tagCmd)
  218. if err != nil {
  219. c.Fatalf("failed to tag repo: %s, %v", out, err)
  220. }
  221. defer deleteImages(repoName + "-two")
  222. out, _, err = runCommandPipelineWithOutput(
  223. exec.Command(dockerBinary, "save", fmt.Sprintf("%v-one", repoName), fmt.Sprintf("%v-two:latest", repoName)),
  224. exec.Command("tar", "xO", "repositories"),
  225. exec.Command("grep", "-q", "-E", "(-one|-two)"),
  226. )
  227. if err != nil {
  228. c.Fatalf("failed to save multiple repos: %s, %v", out, err)
  229. }
  230. }
  231. func (s *DockerSuite) TestSaveRepoWithMultipleImages(c *check.C) {
  232. makeImage := func(from string, tag string) string {
  233. runCmd := exec.Command(dockerBinary, "run", "-d", from, "true")
  234. var (
  235. out string
  236. err error
  237. )
  238. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  239. c.Fatalf("failed to create a container: %v %v", out, err)
  240. }
  241. cleanedContainerID := strings.TrimSpace(out)
  242. defer deleteContainer(cleanedContainerID)
  243. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, tag)
  244. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  245. c.Fatalf("failed to commit container: %v %v", out, err)
  246. }
  247. imageID := strings.TrimSpace(out)
  248. return imageID
  249. }
  250. repoName := "foobar-save-multi-images-test"
  251. tagFoo := repoName + ":foo"
  252. tagBar := repoName + ":bar"
  253. idFoo := makeImage("busybox:latest", tagFoo)
  254. defer deleteImages(idFoo)
  255. idBar := makeImage("busybox:latest", tagBar)
  256. defer deleteImages(idBar)
  257. deleteImages(repoName)
  258. // create the archive
  259. out, _, err := runCommandPipelineWithOutput(
  260. exec.Command(dockerBinary, "save", repoName),
  261. exec.Command("tar", "t"),
  262. exec.Command("grep", "VERSION"),
  263. exec.Command("cut", "-d", "/", "-f1"))
  264. if err != nil {
  265. c.Fatalf("failed to save multiple images: %s, %v", out, err)
  266. }
  267. actual := strings.Split(strings.TrimSpace(out), "\n")
  268. // make the list of expected layers
  269. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "history", "-q", "--no-trunc", "busybox:latest"))
  270. if err != nil {
  271. c.Fatalf("failed to get history: %s, %v", out, err)
  272. }
  273. expected := append(strings.Split(strings.TrimSpace(out), "\n"), idFoo, idBar)
  274. sort.Strings(actual)
  275. sort.Strings(expected)
  276. if !reflect.DeepEqual(expected, actual) {
  277. c.Fatalf("achive does not contains the right layers: got %v, expected %v", actual, expected)
  278. }
  279. }
  280. // Issue #6722 #5892 ensure directories are included in changes
  281. func (s *DockerSuite) TestSaveDirectoryPermissions(c *check.C) {
  282. layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  283. layerEntriesAUFS := []string{"./", ".wh..wh.aufs", ".wh..wh.orph/", ".wh..wh.plnk/", "opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  284. name := "save-directory-permissions"
  285. tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
  286. if err != nil {
  287. c.Errorf("failed to create temporary directory: %s", err)
  288. }
  289. extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
  290. os.Mkdir(extractionDirectory, 0777)
  291. defer os.RemoveAll(tmpDir)
  292. defer deleteImages(name)
  293. _, err = buildImage(name,
  294. `FROM busybox
  295. RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a
  296. RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`,
  297. true)
  298. if err != nil {
  299. c.Fatal(err)
  300. }
  301. if out, _, err := runCommandPipelineWithOutput(
  302. exec.Command(dockerBinary, "save", name),
  303. exec.Command("tar", "-xf", "-", "-C", extractionDirectory),
  304. ); err != nil {
  305. c.Errorf("failed to save and extract image: %s", out)
  306. }
  307. dirs, err := ioutil.ReadDir(extractionDirectory)
  308. if err != nil {
  309. c.Errorf("failed to get a listing of the layer directories: %s", err)
  310. }
  311. found := false
  312. for _, entry := range dirs {
  313. var entriesSansDev []string
  314. if entry.IsDir() {
  315. layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
  316. f, err := os.Open(layerPath)
  317. if err != nil {
  318. c.Fatalf("failed to open %s: %s", layerPath, err)
  319. }
  320. entries, err := ListTar(f)
  321. for _, e := range entries {
  322. if !strings.Contains(e, "dev/") {
  323. entriesSansDev = append(entriesSansDev, e)
  324. }
  325. }
  326. if err != nil {
  327. c.Fatalf("encountered error while listing tar entries: %s", err)
  328. }
  329. if reflect.DeepEqual(entriesSansDev, layerEntries) || reflect.DeepEqual(entriesSansDev, layerEntriesAUFS) {
  330. found = true
  331. break
  332. }
  333. }
  334. }
  335. if !found {
  336. c.Fatalf("failed to find the layer with the right content listing")
  337. }
  338. }