docker_cli_save_load_test.go 12 KB

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