docker_cli_save_load_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "reflect"
  10. "sort"
  11. "strings"
  12. "testing"
  13. "github.com/docker/docker/vendor/src/github.com/kr/pty"
  14. )
  15. // save a repo and try to load it using stdout
  16. func TestSaveAndLoadRepoStdout(t *testing.T) {
  17. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  18. out, _, err := runCommandWithOutput(runCmd)
  19. if err != nil {
  20. t.Fatalf("failed to create a container: %s, %v", out, err)
  21. }
  22. cleanedContainerID := stripTrailingCharacters(out)
  23. repoName := "foobar-save-load-test"
  24. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  25. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  26. t.Fatalf("output should've been a container id: %s, %v", out, err)
  27. }
  28. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  29. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  30. t.Fatalf("failed to commit container: %s, %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: %s, %v", before, err)
  36. }
  37. saveCmdTemplate := `%v save %v > /tmp/foobar-save-load-test.tar`
  38. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
  39. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  40. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  41. t.Fatalf("failed to save repo: %s, %v", out, err)
  42. }
  43. deleteImages(repoName)
  44. loadCmdFinal := `cat /tmp/foobar-save-load-test.tar | docker load`
  45. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  46. if out, _, err = runCommandWithOutput(loadCmd); err != nil {
  47. t.Fatalf("failed to load repo: %s, %v", out, err)
  48. }
  49. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  50. after, _, err := runCommandWithOutput(inspectCmd)
  51. if err != nil {
  52. t.Fatalf("the repo should exist after loading it: %s %v", after, err)
  53. }
  54. if before != after {
  55. t.Fatalf("inspect is not the same after a save / load")
  56. }
  57. deleteContainer(cleanedContainerID)
  58. deleteImages(repoName)
  59. os.Remove("/tmp/foobar-save-load-test.tar")
  60. logDone("save - save/load a repo using stdout")
  61. pty, tty, err := pty.Open()
  62. if err != nil {
  63. t.Fatalf("Could not open pty: %v", err)
  64. }
  65. cmd := exec.Command(dockerBinary, "save", repoName)
  66. cmd.Stdin = tty
  67. cmd.Stdout = tty
  68. cmd.Stderr = tty
  69. if err := cmd.Start(); err != nil {
  70. t.Fatalf("start err: %v", err)
  71. }
  72. if err := cmd.Wait(); err == nil {
  73. t.Fatal("did not break writing to a TTY")
  74. }
  75. buf := make([]byte, 1024)
  76. n, err := pty.Read(buf)
  77. if err != nil {
  78. t.Fatal("could not read tty output")
  79. }
  80. if !bytes.Contains(buf[:n], []byte("Cowardly refusing")) {
  81. t.Fatal("help output is not being yielded", out)
  82. }
  83. logDone("save - do not save to a tty")
  84. }
  85. // save a repo using gz compression and try to load it using stdout
  86. func TestSaveXzAndLoadRepoStdout(t *testing.T) {
  87. tempDir, err := ioutil.TempDir("", "test-save-xz-gz-load-repo-stdout")
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. defer os.RemoveAll(tempDir)
  92. tarballPath := filepath.Join(tempDir, "foobar-save-load-test.tar.xz.gz")
  93. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  94. out, _, err := runCommandWithOutput(runCmd)
  95. if err != nil {
  96. t.Fatalf("failed to create a container: %v %v", out, err)
  97. }
  98. cleanedContainerID := stripTrailingCharacters(out)
  99. repoName := "foobar-save-load-test-xz-gz"
  100. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  101. out, _, err = runCommandWithOutput(inspectCmd)
  102. if err != nil {
  103. t.Fatalf("output should've been a container id: %v %v", cleanedContainerID, err)
  104. }
  105. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  106. out, _, err = runCommandWithOutput(commitCmd)
  107. if err != nil {
  108. t.Fatalf("failed to commit container: %v %v", out, err)
  109. }
  110. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  111. before, _, err := runCommandWithOutput(inspectCmd)
  112. if err != nil {
  113. t.Fatalf("the repo should exist before saving it: %v %v", before, err)
  114. }
  115. saveCmdTemplate := `%v save %v | xz -c | gzip -c > %s`
  116. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName, tarballPath)
  117. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  118. out, _, err = runCommandWithOutput(saveCmd)
  119. if err != nil {
  120. t.Fatalf("failed to save repo: %v %v", out, err)
  121. }
  122. deleteImages(repoName)
  123. loadCmdFinal := fmt.Sprintf(`cat %s | docker load`, tarballPath)
  124. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  125. out, _, err = runCommandWithOutput(loadCmd)
  126. if err == nil {
  127. t.Fatalf("expected error, but succeeded with no error and output: %v", out)
  128. }
  129. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  130. after, _, err := runCommandWithOutput(inspectCmd)
  131. if err == nil {
  132. t.Fatalf("the repo should not exist: %v", after)
  133. }
  134. deleteContainer(cleanedContainerID)
  135. deleteImages(repoName)
  136. logDone("load - save a repo with xz compression & load it using stdout")
  137. }
  138. // save a repo using xz+gz compression and try to load it using stdout
  139. func TestSaveXzGzAndLoadRepoStdout(t *testing.T) {
  140. tempDir, err := ioutil.TempDir("", "test-save-xz-gz-load-repo-stdout")
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. defer os.RemoveAll(tempDir)
  145. tarballPath := filepath.Join(tempDir, "foobar-save-load-test.tar.xz.gz")
  146. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  147. out, _, err := runCommandWithOutput(runCmd)
  148. if err != nil {
  149. t.Fatalf("failed to create a container: %v %v", out, err)
  150. }
  151. cleanedContainerID := stripTrailingCharacters(out)
  152. repoName := "foobar-save-load-test-xz-gz"
  153. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  154. out, _, err = runCommandWithOutput(inspectCmd)
  155. if err != nil {
  156. t.Fatalf("output should've been a container id: %v %v", cleanedContainerID, err)
  157. }
  158. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  159. out, _, err = runCommandWithOutput(commitCmd)
  160. if err != nil {
  161. t.Fatalf("failed to commit container: %v %v", out, err)
  162. }
  163. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  164. before, _, err := runCommandWithOutput(inspectCmd)
  165. if err != nil {
  166. t.Fatalf("the repo should exist before saving it: %v %v", before, err)
  167. }
  168. saveCmdTemplate := `%v save %v | xz -c | gzip -c > %s`
  169. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName, tarballPath)
  170. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  171. out, _, err = runCommandWithOutput(saveCmd)
  172. if err != nil {
  173. t.Fatalf("failed to save repo: %v %v", out, err)
  174. }
  175. deleteImages(repoName)
  176. loadCmdFinal := fmt.Sprintf(`cat %s | docker load`, tarballPath)
  177. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  178. out, _, err = runCommandWithOutput(loadCmd)
  179. if err == nil {
  180. t.Fatalf("expected error, but succeeded with no error and output: %v", out)
  181. }
  182. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  183. after, _, err := runCommandWithOutput(inspectCmd)
  184. if err == nil {
  185. t.Fatalf("the repo should not exist: %v", after)
  186. }
  187. deleteContainer(cleanedContainerID)
  188. deleteImages(repoName)
  189. logDone("load - save a repo with xz+gz compression & load it using stdout")
  190. }
  191. func TestSaveSingleTag(t *testing.T) {
  192. repoName := "foobar-save-single-tag-test"
  193. tagCmdFinal := fmt.Sprintf("%v tag busybox:latest %v:latest", dockerBinary, repoName)
  194. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  195. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  196. t.Fatalf("failed to tag repo: %s, %v", out, err)
  197. }
  198. idCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
  199. idCmd := exec.Command("bash", "-c", idCmdFinal)
  200. out, _, err := runCommandWithOutput(idCmd)
  201. if err != nil {
  202. t.Fatalf("failed to get repo ID: %s, %v", out, err)
  203. }
  204. cleanedImageID := stripTrailingCharacters(out)
  205. saveCmdFinal := fmt.Sprintf("%v save %v:latest | tar t | grep -E '(^repositories$|%v)'", dockerBinary, repoName, cleanedImageID)
  206. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  207. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  208. t.Fatalf("failed to save repo with image ID and 'repositories' file: %s, %v", out, err)
  209. }
  210. deleteImages(repoName)
  211. logDone("save - save a specific image:tag")
  212. }
  213. func TestSaveImageId(t *testing.T) {
  214. repoName := "foobar-save-image-id-test"
  215. tagCmdFinal := fmt.Sprintf("%v tag emptyfs:latest %v:latest", dockerBinary, repoName)
  216. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  217. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  218. t.Fatalf("failed to tag repo: %s, %v", out, err)
  219. }
  220. idLongCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
  221. idLongCmd := exec.Command("bash", "-c", idLongCmdFinal)
  222. out, _, err := runCommandWithOutput(idLongCmd)
  223. if err != nil {
  224. t.Fatalf("failed to get repo ID: %s, %v", out, err)
  225. }
  226. cleanedLongImageID := stripTrailingCharacters(out)
  227. idShortCmdFinal := fmt.Sprintf("%v images -q %v", dockerBinary, repoName)
  228. idShortCmd := exec.Command("bash", "-c", idShortCmdFinal)
  229. out, _, err = runCommandWithOutput(idShortCmd)
  230. if err != nil {
  231. t.Fatalf("failed to get repo short ID: %s, %v", out, err)
  232. }
  233. cleanedShortImageID := stripTrailingCharacters(out)
  234. saveCmdFinal := fmt.Sprintf("%v save %v | tar t | grep %v", dockerBinary, cleanedShortImageID, cleanedLongImageID)
  235. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  236. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  237. t.Fatalf("failed to save repo with image ID: %s, %v", out, err)
  238. }
  239. deleteImages(repoName)
  240. logDone("save - save a image by ID")
  241. }
  242. // save a repo and try to load it using flags
  243. func TestSaveAndLoadRepoFlags(t *testing.T) {
  244. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  245. out, _, err := runCommandWithOutput(runCmd)
  246. if err != nil {
  247. t.Fatalf("failed to create a container: %s, %v", out, err)
  248. }
  249. cleanedContainerID := stripTrailingCharacters(out)
  250. repoName := "foobar-save-load-test"
  251. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  252. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  253. t.Fatalf("output should've been a container id: %s, %v", out, err)
  254. }
  255. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  256. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  257. t.Fatalf("failed to commit container: %s, %v", out, err)
  258. }
  259. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  260. before, _, err := runCommandWithOutput(inspectCmd)
  261. if err != nil {
  262. t.Fatalf("the repo should exist before saving it: %s, %v", before, err)
  263. }
  264. saveCmdTemplate := `%v save -o /tmp/foobar-save-load-test.tar %v`
  265. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
  266. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  267. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  268. t.Fatalf("failed to save repo: %s, %v", out, err)
  269. }
  270. deleteImages(repoName)
  271. loadCmdFinal := `docker load -i /tmp/foobar-save-load-test.tar`
  272. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  273. if out, _, err = runCommandWithOutput(loadCmd); err != nil {
  274. t.Fatalf("failed to load repo: %s, %v", out, err)
  275. }
  276. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  277. after, _, err := runCommandWithOutput(inspectCmd)
  278. if err != nil {
  279. t.Fatalf("the repo should exist after loading it: %s, %v", after, err)
  280. }
  281. if before != after {
  282. t.Fatalf("inspect is not the same after a save / load")
  283. }
  284. deleteContainer(cleanedContainerID)
  285. deleteImages(repoName)
  286. os.Remove("/tmp/foobar-save-load-test.tar")
  287. logDone("save - save a repo using -o && load a repo using -i")
  288. }
  289. func TestSaveMultipleNames(t *testing.T) {
  290. repoName := "foobar-save-multi-name-test"
  291. // Make one image
  292. tagCmdFinal := fmt.Sprintf("%v tag emptyfs:latest %v-one:latest", dockerBinary, repoName)
  293. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  294. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  295. t.Fatalf("failed to tag repo: %s, %v", out, err)
  296. }
  297. defer deleteImages(repoName + "-one")
  298. // Make two images
  299. tagCmdFinal = fmt.Sprintf("%v tag emptyfs:latest %v-two:latest", dockerBinary, repoName)
  300. tagCmd = exec.Command("bash", "-c", tagCmdFinal)
  301. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  302. t.Fatalf("failed to tag repo: %s, %v", out, err)
  303. }
  304. defer deleteImages(repoName + "-two")
  305. saveCmdFinal := fmt.Sprintf("%v save %v-one %v-two:latest | tar xO repositories | grep -q -E '(-one|-two)'", dockerBinary, repoName, repoName)
  306. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  307. if out, _, err := runCommandWithOutput(saveCmd); err != nil {
  308. t.Fatalf("failed to save multiple repos: %s, %v", out, err)
  309. }
  310. deleteImages(repoName)
  311. logDone("save - save by multiple names")
  312. }
  313. func TestSaveRepoWithMultipleImages(t *testing.T) {
  314. makeImage := func(from string, tag string) string {
  315. runCmd := exec.Command(dockerBinary, "run", "-d", from, "true")
  316. var (
  317. out string
  318. err error
  319. )
  320. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  321. t.Fatalf("failed to create a container: %v %v", out, err)
  322. }
  323. cleanedContainerID := stripTrailingCharacters(out)
  324. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, tag)
  325. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  326. t.Fatalf("failed to commit container: %v %v", out, err)
  327. }
  328. imageID := stripTrailingCharacters(out)
  329. deleteContainer(cleanedContainerID)
  330. return imageID
  331. }
  332. repoName := "foobar-save-multi-images-test"
  333. tagFoo := repoName + ":foo"
  334. tagBar := repoName + ":bar"
  335. idFoo := makeImage("busybox:latest", tagFoo)
  336. idBar := makeImage("busybox:latest", tagBar)
  337. deleteImages(repoName)
  338. // create the archive
  339. saveCmdFinal := fmt.Sprintf("%v save %v | tar t | grep 'VERSION' |cut -d / -f1", dockerBinary, repoName)
  340. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  341. out, _, err := runCommandWithOutput(saveCmd)
  342. if err != nil {
  343. t.Fatalf("failed to save multiple images: %s, %v", out, err)
  344. }
  345. actual := strings.Split(stripTrailingCharacters(out), "\n")
  346. // make the list of expected layers
  347. historyCmdFinal := fmt.Sprintf("%v history -q --no-trunc %v", dockerBinary, "busybox:latest")
  348. historyCmd := exec.Command("bash", "-c", historyCmdFinal)
  349. out, _, err = runCommandWithOutput(historyCmd)
  350. if err != nil {
  351. t.Fatalf("failed to get history: %s, %v", out, err)
  352. }
  353. expected := append(strings.Split(stripTrailingCharacters(out), "\n"), idFoo, idBar)
  354. sort.Strings(actual)
  355. sort.Strings(expected)
  356. if !reflect.DeepEqual(expected, actual) {
  357. t.Fatalf("achive does not contains the right layers: got %v, expected %v", actual, expected)
  358. }
  359. logDone("save - save repository with multiple images")
  360. }
  361. // Issue #6722 #5892 ensure directories are included in changes
  362. func TestSaveDirectoryPermissions(t *testing.T) {
  363. layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  364. layerEntriesAUFS := []string{"./", ".wh..wh.aufs", ".wh..wh.orph/", ".wh..wh.plnk/", "opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  365. name := "save-directory-permissions"
  366. tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
  367. if err != nil {
  368. t.Errorf("failed to create temporary directory: %s", err)
  369. }
  370. extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
  371. os.Mkdir(extractionDirectory, 0777)
  372. defer os.RemoveAll(tmpDir)
  373. defer deleteImages(name)
  374. _, err = buildImage(name,
  375. `FROM busybox
  376. RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a
  377. RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`,
  378. true)
  379. if err != nil {
  380. t.Fatal(err)
  381. }
  382. saveCmdFinal := fmt.Sprintf("%s save %s | tar -xf - -C %s", dockerBinary, name, extractionDirectory)
  383. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  384. if out, _, err := runCommandWithOutput(saveCmd); err != nil {
  385. t.Errorf("failed to save and extract image: %s", out)
  386. }
  387. dirs, err := ioutil.ReadDir(extractionDirectory)
  388. if err != nil {
  389. t.Errorf("failed to get a listing of the layer directories: %s", err)
  390. }
  391. found := false
  392. for _, entry := range dirs {
  393. if entry.IsDir() {
  394. layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
  395. f, err := os.Open(layerPath)
  396. if err != nil {
  397. t.Fatalf("failed to open %s: %s", layerPath, err)
  398. }
  399. entries, err := ListTar(f)
  400. if err != nil {
  401. t.Fatalf("encountered error while listing tar entries: %s", err)
  402. }
  403. if reflect.DeepEqual(entries, layerEntries) || reflect.DeepEqual(entries, layerEntriesAUFS) {
  404. found = true
  405. break
  406. }
  407. }
  408. }
  409. if !found {
  410. t.Fatalf("failed to find the layer with the right content listing")
  411. }
  412. logDone("save - ensure directories exist in exported layers")
  413. }