docker_cli_save_load_test.go 13 KB

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