docker_cli_save_load_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "reflect"
  10. "testing"
  11. "github.com/docker/docker/vendor/src/github.com/kr/pty"
  12. )
  13. // save a repo and try to load it using stdout
  14. func TestSaveAndLoadRepoStdout(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: %s, %v", out, err)
  19. }
  20. cleanedContainerID := stripTrailingCharacters(out)
  21. repoName := "foobar-save-load-test"
  22. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  23. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  24. t.Fatalf("output should've been a container id: %s, %v", out, err)
  25. }
  26. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  27. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  28. t.Fatalf("failed to commit container: %s, %v", out, err)
  29. }
  30. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  31. before, _, err := runCommandWithOutput(inspectCmd)
  32. if err != nil {
  33. t.Fatalf("the repo should exist before saving it: %s, %v", before, err)
  34. }
  35. saveCmdTemplate := `%v save %v > /tmp/foobar-save-load-test.tar`
  36. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
  37. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  38. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  39. t.Fatalf("failed to save repo: %s, %v", out, err)
  40. }
  41. deleteImages(repoName)
  42. loadCmdFinal := `cat /tmp/foobar-save-load-test.tar | docker load`
  43. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  44. if out, _, err = runCommandWithOutput(loadCmd); err != nil {
  45. t.Fatalf("failed to load repo: %s, %v", out, err)
  46. }
  47. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  48. after, _, err := runCommandWithOutput(inspectCmd)
  49. if err != nil {
  50. t.Fatalf("the repo should exist after loading it: %s %v", after, err)
  51. }
  52. if before != after {
  53. t.Fatalf("inspect is not the same after a save / load")
  54. }
  55. deleteContainer(cleanedContainerID)
  56. deleteImages(repoName)
  57. os.Remove("/tmp/foobar-save-load-test.tar")
  58. logDone("save - save/load a repo using stdout")
  59. pty, tty, err := pty.Open()
  60. if err != nil {
  61. t.Fatalf("Could not open pty: %v", err)
  62. }
  63. cmd := exec.Command(dockerBinary, "save", repoName)
  64. cmd.Stdin = tty
  65. cmd.Stdout = tty
  66. cmd.Stderr = tty
  67. if err := cmd.Start(); err != nil {
  68. t.Fatalf("start err: %v", err)
  69. }
  70. if err := cmd.Wait(); err == nil {
  71. t.Fatal("did not break writing to a TTY")
  72. }
  73. buf := make([]byte, 1024)
  74. n, err := pty.Read(buf)
  75. if err != nil {
  76. t.Fatal("could not read tty output")
  77. }
  78. if !bytes.Contains(buf[:n], []byte("Cowardly refusing")) {
  79. t.Fatal("help output is not being yielded", out)
  80. }
  81. logDone("save - do not save to a tty")
  82. }
  83. func TestSaveSingleTag(t *testing.T) {
  84. repoName := "foobar-save-single-tag-test"
  85. tagCmdFinal := fmt.Sprintf("%v tag busybox:latest %v:latest", dockerBinary, repoName)
  86. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  87. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  88. t.Fatalf("failed to tag repo: %s, %v", out, err)
  89. }
  90. idCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
  91. idCmd := exec.Command("bash", "-c", idCmdFinal)
  92. out, _, err := runCommandWithOutput(idCmd)
  93. if err != nil {
  94. t.Fatalf("failed to get repo ID: %s, %v", out, err)
  95. }
  96. cleanedImageID := stripTrailingCharacters(out)
  97. saveCmdFinal := fmt.Sprintf("%v save %v:latest | tar t | grep -E '(^repositories$|%v)'", dockerBinary, repoName, cleanedImageID)
  98. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  99. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  100. t.Fatalf("failed to save repo with image ID and 'repositories' file: %s, %v", out, err)
  101. }
  102. deleteImages(repoName)
  103. logDone("save - save a specific image:tag")
  104. }
  105. func TestSaveImageId(t *testing.T) {
  106. repoName := "foobar-save-image-id-test"
  107. tagCmdFinal := fmt.Sprintf("%v tag scratch:latest %v:latest", dockerBinary, repoName)
  108. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  109. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  110. t.Fatalf("failed to tag repo: %s, %v", out, err)
  111. }
  112. idLongCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
  113. idLongCmd := exec.Command("bash", "-c", idLongCmdFinal)
  114. out, _, err := runCommandWithOutput(idLongCmd)
  115. if err != nil {
  116. t.Fatalf("failed to get repo ID: %s, %v", out, err)
  117. }
  118. cleanedLongImageID := stripTrailingCharacters(out)
  119. idShortCmdFinal := fmt.Sprintf("%v images -q %v", dockerBinary, repoName)
  120. idShortCmd := exec.Command("bash", "-c", idShortCmdFinal)
  121. out, _, err = runCommandWithOutput(idShortCmd)
  122. if err != nil {
  123. t.Fatalf("failed to get repo short ID: %s, %v", out, err)
  124. }
  125. cleanedShortImageID := stripTrailingCharacters(out)
  126. saveCmdFinal := fmt.Sprintf("%v save %v | tar t | grep %v", dockerBinary, cleanedShortImageID, cleanedLongImageID)
  127. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  128. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  129. t.Fatalf("failed to save repo with image ID: %s, %v", out, err)
  130. }
  131. deleteImages(repoName)
  132. logDone("save - save a image by ID")
  133. }
  134. // save a repo and try to load it using flags
  135. func TestSaveAndLoadRepoFlags(t *testing.T) {
  136. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  137. out, _, err := runCommandWithOutput(runCmd)
  138. if err != nil {
  139. t.Fatalf("failed to create a container: %s, %v", out, err)
  140. }
  141. cleanedContainerID := stripTrailingCharacters(out)
  142. repoName := "foobar-save-load-test"
  143. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  144. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  145. t.Fatalf("output should've been a container id: %s, %v", out, err)
  146. }
  147. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  148. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  149. t.Fatalf("failed to commit container: %s, %v", out, err)
  150. }
  151. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  152. before, _, err := runCommandWithOutput(inspectCmd)
  153. if err != nil {
  154. t.Fatalf("the repo should exist before saving it: %s, %v", before, err)
  155. }
  156. saveCmdTemplate := `%v save -o /tmp/foobar-save-load-test.tar %v`
  157. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
  158. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  159. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  160. t.Fatalf("failed to save repo: %s, %v", out, err)
  161. }
  162. deleteImages(repoName)
  163. loadCmdFinal := `docker load -i /tmp/foobar-save-load-test.tar`
  164. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  165. if out, _, err = runCommandWithOutput(loadCmd); err != nil {
  166. t.Fatalf("failed to load repo: %s, %v", out, err)
  167. }
  168. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  169. after, _, err := runCommandWithOutput(inspectCmd)
  170. if err != nil {
  171. t.Fatalf("the repo should exist after loading it: %s, %v", after, err)
  172. }
  173. if before != after {
  174. t.Fatalf("inspect is not the same after a save / load")
  175. }
  176. deleteContainer(cleanedContainerID)
  177. deleteImages(repoName)
  178. os.Remove("/tmp/foobar-save-load-test.tar")
  179. logDone("save - save a repo using -o && load a repo using -i")
  180. }
  181. func TestSaveMultipleNames(t *testing.T) {
  182. repoName := "foobar-save-multi-name-test"
  183. // Make one image
  184. tagCmdFinal := fmt.Sprintf("%v tag scratch:latest %v-one:latest", dockerBinary, repoName)
  185. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  186. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  187. t.Fatalf("failed to tag repo: %s, %v", out, err)
  188. }
  189. // Make two images
  190. tagCmdFinal = fmt.Sprintf("%v tag scratch:latest %v-two:latest", dockerBinary, repoName)
  191. tagCmd = exec.Command("bash", "-c", tagCmdFinal)
  192. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  193. t.Fatalf("failed to tag repo: %s, %v", out, err)
  194. }
  195. saveCmdFinal := fmt.Sprintf("%v save %v-one %v-two:latest | tar xO repositories | grep -q -E '(-one|-two)'", dockerBinary, repoName, repoName)
  196. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  197. if out, _, err := runCommandWithOutput(saveCmd); err != nil {
  198. t.Fatalf("failed to save multiple repos: %s, %v", out, err)
  199. }
  200. deleteImages(repoName)
  201. logDone("save - save by multiple names")
  202. }
  203. // Issue #6722 #5892 ensure directories are included in changes
  204. func TestSaveDirectoryPermissions(t *testing.T) {
  205. layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  206. layerEntriesAUFS := []string{"./", ".wh..wh.aufs", ".wh..wh.orph/", ".wh..wh.plnk/", "opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  207. name := "save-directory-permissions"
  208. tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
  209. if err != nil {
  210. t.Errorf("failed to create temporary directory: %s", err)
  211. }
  212. extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
  213. os.Mkdir(extractionDirectory, 0777)
  214. defer os.RemoveAll(tmpDir)
  215. defer deleteImages(name)
  216. _, err = buildImage(name,
  217. `FROM busybox
  218. RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a
  219. RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`,
  220. true)
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. saveCmdFinal := fmt.Sprintf("%s save %s | tar -xf - -C %s", dockerBinary, name, extractionDirectory)
  225. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  226. if out, _, err := runCommandWithOutput(saveCmd); err != nil {
  227. t.Errorf("failed to save and extract image: %s", out)
  228. }
  229. dirs, err := ioutil.ReadDir(extractionDirectory)
  230. if err != nil {
  231. t.Errorf("failed to get a listing of the layer directories: %s", err)
  232. }
  233. found := false
  234. for _, entry := range dirs {
  235. if entry.IsDir() {
  236. layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
  237. f, err := os.Open(layerPath)
  238. if err != nil {
  239. t.Fatalf("failed to open %s: %s", layerPath, err)
  240. }
  241. entries, err := ListTar(f)
  242. if err != nil {
  243. t.Fatalf("encountered error while listing tar entries: %s", err)
  244. }
  245. if reflect.DeepEqual(entries, layerEntries) || reflect.DeepEqual(entries, layerEntriesAUFS) {
  246. found = true
  247. break
  248. }
  249. }
  250. }
  251. if !found {
  252. t.Fatalf("failed to find the layer with the right content listing")
  253. }
  254. logDone("save - ensure directories exist in exported layers")
  255. }