docker_cli_save_load_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. func TestSaveSingleTag(t *testing.T) {
  86. repoName := "foobar-save-single-tag-test"
  87. tagCmdFinal := fmt.Sprintf("%v tag busybox:latest %v:latest", dockerBinary, repoName)
  88. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  89. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  90. t.Fatalf("failed to tag repo: %s, %v", out, err)
  91. }
  92. idCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
  93. idCmd := exec.Command("bash", "-c", idCmdFinal)
  94. out, _, err := runCommandWithOutput(idCmd)
  95. if err != nil {
  96. t.Fatalf("failed to get repo ID: %s, %v", out, err)
  97. }
  98. cleanedImageID := stripTrailingCharacters(out)
  99. saveCmdFinal := fmt.Sprintf("%v save %v:latest | tar t | grep -E '(^repositories$|%v)'", dockerBinary, repoName, cleanedImageID)
  100. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  101. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  102. t.Fatalf("failed to save repo with image ID and 'repositories' file: %s, %v", out, err)
  103. }
  104. deleteImages(repoName)
  105. logDone("save - save a specific image:tag")
  106. }
  107. func TestSaveImageId(t *testing.T) {
  108. repoName := "foobar-save-image-id-test"
  109. tagCmdFinal := fmt.Sprintf("%v tag scratch:latest %v:latest", dockerBinary, repoName)
  110. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  111. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  112. t.Fatalf("failed to tag repo: %s, %v", out, err)
  113. }
  114. idLongCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
  115. idLongCmd := exec.Command("bash", "-c", idLongCmdFinal)
  116. out, _, err := runCommandWithOutput(idLongCmd)
  117. if err != nil {
  118. t.Fatalf("failed to get repo ID: %s, %v", out, err)
  119. }
  120. cleanedLongImageID := stripTrailingCharacters(out)
  121. idShortCmdFinal := fmt.Sprintf("%v images -q %v", dockerBinary, repoName)
  122. idShortCmd := exec.Command("bash", "-c", idShortCmdFinal)
  123. out, _, err = runCommandWithOutput(idShortCmd)
  124. if err != nil {
  125. t.Fatalf("failed to get repo short ID: %s, %v", out, err)
  126. }
  127. cleanedShortImageID := stripTrailingCharacters(out)
  128. saveCmdFinal := fmt.Sprintf("%v save %v | tar t | grep %v", dockerBinary, cleanedShortImageID, cleanedLongImageID)
  129. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  130. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  131. t.Fatalf("failed to save repo with image ID: %s, %v", out, err)
  132. }
  133. deleteImages(repoName)
  134. logDone("save - save a image by ID")
  135. }
  136. // save a repo and try to load it using flags
  137. func TestSaveAndLoadRepoFlags(t *testing.T) {
  138. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  139. out, _, err := runCommandWithOutput(runCmd)
  140. if err != nil {
  141. t.Fatalf("failed to create a container: %s, %v", out, err)
  142. }
  143. cleanedContainerID := stripTrailingCharacters(out)
  144. repoName := "foobar-save-load-test"
  145. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  146. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  147. t.Fatalf("output should've been a container id: %s, %v", out, err)
  148. }
  149. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  150. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  151. t.Fatalf("failed to commit container: %s, %v", out, err)
  152. }
  153. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  154. before, _, err := runCommandWithOutput(inspectCmd)
  155. if err != nil {
  156. t.Fatalf("the repo should exist before saving it: %s, %v", before, err)
  157. }
  158. saveCmdTemplate := `%v save -o /tmp/foobar-save-load-test.tar %v`
  159. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
  160. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  161. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  162. t.Fatalf("failed to save repo: %s, %v", out, err)
  163. }
  164. deleteImages(repoName)
  165. loadCmdFinal := `docker load -i /tmp/foobar-save-load-test.tar`
  166. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  167. if out, _, err = runCommandWithOutput(loadCmd); err != nil {
  168. t.Fatalf("failed to load repo: %s, %v", out, err)
  169. }
  170. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  171. after, _, err := runCommandWithOutput(inspectCmd)
  172. if err != nil {
  173. t.Fatalf("the repo should exist after loading it: %s, %v", after, err)
  174. }
  175. if before != after {
  176. t.Fatalf("inspect is not the same after a save / load")
  177. }
  178. deleteContainer(cleanedContainerID)
  179. deleteImages(repoName)
  180. os.Remove("/tmp/foobar-save-load-test.tar")
  181. logDone("save - save a repo using -o && 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. defer deleteImages(repoName + "-one")
  192. // Make two images
  193. tagCmdFinal = fmt.Sprintf("%v tag scratch:latest %v-two: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. defer deleteImages(repoName + "-two")
  199. saveCmdFinal := fmt.Sprintf("%v save %v-one %v-two:latest | tar xO repositories | grep -q -E '(-one|-two)'", dockerBinary, repoName, repoName)
  200. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  201. if out, _, err := runCommandWithOutput(saveCmd); err != nil {
  202. t.Fatalf("failed to save multiple repos: %s, %v", out, err)
  203. }
  204. deleteImages(repoName)
  205. logDone("save - save by multiple names")
  206. }
  207. func TestSaveRepoWithMultipleImages(t *testing.T) {
  208. makeImage := func(from string, tag string) string {
  209. runCmd := exec.Command(dockerBinary, "run", "-d", from, "true")
  210. var (
  211. out string
  212. err error
  213. )
  214. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  215. t.Fatalf("failed to create a container: %v %v", out, err)
  216. }
  217. cleanedContainerID := stripTrailingCharacters(out)
  218. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, tag)
  219. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  220. t.Fatalf("failed to commit container: %v %v", out, err)
  221. }
  222. imageID := stripTrailingCharacters(out)
  223. deleteContainer(cleanedContainerID)
  224. return imageID
  225. }
  226. repoName := "foobar-save-multi-images-test"
  227. tagFoo := repoName + ":foo"
  228. tagBar := repoName + ":bar"
  229. idFoo := makeImage("busybox:latest", tagFoo)
  230. idBar := makeImage("busybox:latest", tagBar)
  231. deleteImages(repoName)
  232. // create the archive
  233. saveCmdFinal := fmt.Sprintf("%v save %v | tar t | grep 'VERSION' |cut -d / -f1", dockerBinary, repoName)
  234. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  235. out, _, err := runCommandWithOutput(saveCmd)
  236. if err != nil {
  237. t.Fatalf("failed to save multiple images: %s, %v", out, err)
  238. }
  239. actual := strings.Split(stripTrailingCharacters(out), "\n")
  240. // make the list of expected layers
  241. historyCmdFinal := fmt.Sprintf("%v history -q --no-trunc %v", dockerBinary, "busybox:latest")
  242. historyCmd := exec.Command("bash", "-c", historyCmdFinal)
  243. out, _, err = runCommandWithOutput(historyCmd)
  244. if err != nil {
  245. t.Fatalf("failed to get history: %s, %v", out, err)
  246. }
  247. expected := append(strings.Split(stripTrailingCharacters(out), "\n"), idFoo, idBar)
  248. sort.Strings(actual)
  249. sort.Strings(expected)
  250. if !reflect.DeepEqual(expected, actual) {
  251. t.Fatalf("achive does not contains the right layers: got %v, expected %v", actual, expected)
  252. }
  253. logDone("save - save repository with multiple images")
  254. }
  255. // Issue #6722 #5892 ensure directories are included in changes
  256. func TestSaveDirectoryPermissions(t *testing.T) {
  257. layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  258. layerEntriesAUFS := []string{"./", ".wh..wh.aufs", ".wh..wh.orph/", ".wh..wh.plnk/", "opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  259. name := "save-directory-permissions"
  260. tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
  261. if err != nil {
  262. t.Errorf("failed to create temporary directory: %s", err)
  263. }
  264. extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
  265. os.Mkdir(extractionDirectory, 0777)
  266. defer os.RemoveAll(tmpDir)
  267. defer deleteImages(name)
  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. t.Fatal(err)
  275. }
  276. saveCmdFinal := fmt.Sprintf("%s save %s | tar -xf - -C %s", dockerBinary, name, extractionDirectory)
  277. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  278. if out, _, err := runCommandWithOutput(saveCmd); err != nil {
  279. t.Errorf("failed to save and extract image: %s", out)
  280. }
  281. dirs, err := ioutil.ReadDir(extractionDirectory)
  282. if err != nil {
  283. t.Errorf("failed to get a listing of the layer directories: %s", err)
  284. }
  285. found := false
  286. for _, entry := range dirs {
  287. if entry.IsDir() {
  288. layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
  289. f, err := os.Open(layerPath)
  290. if err != nil {
  291. t.Fatalf("failed to open %s: %s", layerPath, err)
  292. }
  293. entries, err := ListTar(f)
  294. if err != nil {
  295. t.Fatalf("encountered error while listing tar entries: %s", err)
  296. }
  297. if reflect.DeepEqual(entries, layerEntries) || reflect.DeepEqual(entries, layerEntriesAUFS) {
  298. found = true
  299. break
  300. }
  301. }
  302. }
  303. if !found {
  304. t.Fatalf("failed to find the layer with the right content listing")
  305. }
  306. logDone("save - ensure directories exist in exported layers")
  307. }