docker_cli_save_load_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. // 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. func TestSaveRepoWithMultipleImages(t *testing.T) {
  206. makeImage := func(from string, tag string) string {
  207. runCmd := exec.Command(dockerBinary, "run", "-d", from, "true")
  208. var (
  209. out string
  210. err error
  211. )
  212. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  213. t.Fatalf("failed to create a container: %v %v", out, err)
  214. }
  215. cleanedContainerID := stripTrailingCharacters(out)
  216. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, tag)
  217. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  218. t.Fatalf("failed to commit container: %v %v", out, err)
  219. }
  220. imageID := stripTrailingCharacters(out)
  221. deleteContainer(cleanedContainerID)
  222. return imageID
  223. }
  224. repoName := "foobar-save-multi-images-test"
  225. tagFoo := repoName + ":foo"
  226. tagBar := repoName + ":bar"
  227. idFoo := makeImage("busybox:latest", tagFoo)
  228. idBar := makeImage("busybox:latest", tagBar)
  229. deleteImages(repoName)
  230. // create the archive
  231. saveCmdFinal := fmt.Sprintf("%v save %v | tar t | grep 'VERSION' |cut -d / -f1", dockerBinary, repoName)
  232. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  233. out, _, err := runCommandWithOutput(saveCmd)
  234. if err != nil {
  235. t.Fatalf("failed to save multiple images: %s, %v", out, err)
  236. }
  237. actual := strings.Split(stripTrailingCharacters(out), "\n")
  238. // make the list of expected layers
  239. historyCmdFinal := fmt.Sprintf("%v history -q --no-trunc %v", dockerBinary, "busybox:latest")
  240. historyCmd := exec.Command("bash", "-c", historyCmdFinal)
  241. out, _, err = runCommandWithOutput(historyCmd)
  242. if err != nil {
  243. t.Fatalf("failed to get history: %s, %v", out, err)
  244. }
  245. expected := append(strings.Split(stripTrailingCharacters(out), "\n"), idFoo, idBar)
  246. sort.Strings(actual)
  247. sort.Strings(expected)
  248. if !reflect.DeepEqual(expected, actual) {
  249. t.Fatalf("achive does not contains the right layers: got %v, expected %v", actual, expected)
  250. }
  251. logDone("save - save repository with multiple images")
  252. }
  253. // Issue #6722 #5892 ensure directories are included in changes
  254. func TestSaveDirectoryPermissions(t *testing.T) {
  255. layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  256. layerEntriesAUFS := []string{"./", ".wh..wh.aufs", ".wh..wh.orph/", ".wh..wh.plnk/", "opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  257. name := "save-directory-permissions"
  258. tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
  259. if err != nil {
  260. t.Errorf("failed to create temporary directory: %s", err)
  261. }
  262. extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
  263. os.Mkdir(extractionDirectory, 0777)
  264. defer os.RemoveAll(tmpDir)
  265. defer deleteImages(name)
  266. _, err = buildImage(name,
  267. `FROM busybox
  268. RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a
  269. RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`,
  270. true)
  271. if err != nil {
  272. t.Fatal(err)
  273. }
  274. saveCmdFinal := fmt.Sprintf("%s save %s | tar -xf - -C %s", dockerBinary, name, extractionDirectory)
  275. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  276. if out, _, err := runCommandWithOutput(saveCmd); err != nil {
  277. t.Errorf("failed to save and extract image: %s", out)
  278. }
  279. dirs, err := ioutil.ReadDir(extractionDirectory)
  280. if err != nil {
  281. t.Errorf("failed to get a listing of the layer directories: %s", err)
  282. }
  283. found := false
  284. for _, entry := range dirs {
  285. if entry.IsDir() {
  286. layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
  287. f, err := os.Open(layerPath)
  288. if err != nil {
  289. t.Fatalf("failed to open %s: %s", layerPath, err)
  290. }
  291. entries, err := ListTar(f)
  292. if err != nil {
  293. t.Fatalf("encountered error while listing tar entries: %s", err)
  294. }
  295. if reflect.DeepEqual(entries, layerEntries) || reflect.DeepEqual(entries, layerEntriesAUFS) {
  296. found = true
  297. break
  298. }
  299. }
  300. }
  301. if !found {
  302. t.Fatalf("failed to find the layer with the right content listing")
  303. }
  304. logDone("save - ensure directories exist in exported layers")
  305. }