docker_cli_save_load_test.go 11 KB

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