docker_cli_save_load_test.go 10 KB

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