docker_cli_save_load_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "reflect"
  9. "testing"
  10. )
  11. // save a repo and try to load it using stdout
  12. func TestSaveAndLoadRepoStdout(t *testing.T) {
  13. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  14. out, _, err := runCommandWithOutput(runCmd)
  15. errorOut(err, t, fmt.Sprintf("failed to create a container: %v %v", out, err))
  16. cleanedContainerID := stripTrailingCharacters(out)
  17. repoName := "foobar-save-load-test"
  18. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  19. out, _, err = runCommandWithOutput(inspectCmd)
  20. errorOut(err, t, fmt.Sprintf("output should've been a container id: %v %v", cleanedContainerID, err))
  21. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  22. out, _, err = runCommandWithOutput(commitCmd)
  23. errorOut(err, t, fmt.Sprintf("failed to commit container: %v %v", out, err))
  24. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  25. before, _, err := runCommandWithOutput(inspectCmd)
  26. errorOut(err, t, fmt.Sprintf("the repo should exist before saving it: %v %v", before, err))
  27. saveCmdTemplate := `%v save %v > /tmp/foobar-save-load-test.tar`
  28. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
  29. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  30. out, _, err = runCommandWithOutput(saveCmd)
  31. errorOut(err, t, fmt.Sprintf("failed to save repo: %v %v", out, err))
  32. deleteImages(repoName)
  33. loadCmdFinal := `cat /tmp/foobar-save-load-test.tar | docker load`
  34. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  35. out, _, err = runCommandWithOutput(loadCmd)
  36. errorOut(err, t, fmt.Sprintf("failed to load repo: %v %v", out, err))
  37. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  38. after, _, err := runCommandWithOutput(inspectCmd)
  39. errorOut(err, t, fmt.Sprintf("the repo should exist after loading it: %v %v", after, err))
  40. if before != after {
  41. t.Fatalf("inspect is not the same after a save / load")
  42. }
  43. deleteContainer(cleanedContainerID)
  44. deleteImages(repoName)
  45. os.Remove("/tmp/foobar-save-load-test.tar")
  46. logDone("save - save a repo using stdout")
  47. logDone("load - load a repo using stdout")
  48. }
  49. func TestSaveSingleTag(t *testing.T) {
  50. repoName := "foobar-save-single-tag-test"
  51. tagCmdFinal := fmt.Sprintf("%v tag busybox:latest %v:latest", dockerBinary, repoName)
  52. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  53. out, _, err := runCommandWithOutput(tagCmd)
  54. errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
  55. idCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
  56. idCmd := exec.Command("bash", "-c", idCmdFinal)
  57. out, _, err = runCommandWithOutput(idCmd)
  58. errorOut(err, t, fmt.Sprintf("failed to get repo ID: %v %v", out, err))
  59. cleanedImageID := stripTrailingCharacters(out)
  60. saveCmdFinal := fmt.Sprintf("%v save %v:latest | tar t | grep -E '(^repositories$|%v)'", dockerBinary, repoName, cleanedImageID)
  61. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  62. out, _, err = runCommandWithOutput(saveCmd)
  63. errorOut(err, t, fmt.Sprintf("failed to save repo with image ID and 'repositories' file: %v %v", out, err))
  64. deleteImages(repoName)
  65. logDone("save - save a specific image:tag")
  66. }
  67. func TestSaveImageId(t *testing.T) {
  68. repoName := "foobar-save-image-id-test"
  69. tagCmdFinal := fmt.Sprintf("%v tag scratch:latest %v:latest", dockerBinary, repoName)
  70. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  71. out, _, err := runCommandWithOutput(tagCmd)
  72. errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
  73. idLongCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
  74. idLongCmd := exec.Command("bash", "-c", idLongCmdFinal)
  75. out, _, err = runCommandWithOutput(idLongCmd)
  76. errorOut(err, t, fmt.Sprintf("failed to get repo ID: %v %v", out, err))
  77. cleanedLongImageID := stripTrailingCharacters(out)
  78. idShortCmdFinal := fmt.Sprintf("%v images -q %v", dockerBinary, repoName)
  79. idShortCmd := exec.Command("bash", "-c", idShortCmdFinal)
  80. out, _, err = runCommandWithOutput(idShortCmd)
  81. errorOut(err, t, fmt.Sprintf("failed to get repo short ID: %v %v", out, err))
  82. cleanedShortImageID := stripTrailingCharacters(out)
  83. saveCmdFinal := fmt.Sprintf("%v save %v | tar t | grep %v", dockerBinary, cleanedShortImageID, cleanedLongImageID)
  84. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  85. out, _, err = runCommandWithOutput(saveCmd)
  86. errorOut(err, t, fmt.Sprintf("failed to save repo with image ID: %v %v", out, err))
  87. deleteImages(repoName)
  88. logDone("save - save a image by ID")
  89. }
  90. // save a repo and try to load it using flags
  91. func TestSaveAndLoadRepoFlags(t *testing.T) {
  92. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  93. out, _, err := runCommandWithOutput(runCmd)
  94. errorOut(err, t, fmt.Sprintf("failed to create a container: %v %v", out, err))
  95. cleanedContainerID := stripTrailingCharacters(out)
  96. repoName := "foobar-save-load-test"
  97. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  98. out, _, err = runCommandWithOutput(inspectCmd)
  99. errorOut(err, t, fmt.Sprintf("output should've been a container id: %v %v", cleanedContainerID, err))
  100. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  101. out, _, err = runCommandWithOutput(commitCmd)
  102. errorOut(err, t, fmt.Sprintf("failed to commit container: %v %v", out, err))
  103. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  104. before, _, err := runCommandWithOutput(inspectCmd)
  105. errorOut(err, t, fmt.Sprintf("the repo should exist before saving it: %v %v", before, err))
  106. saveCmdTemplate := `%v save -o /tmp/foobar-save-load-test.tar %v`
  107. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
  108. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  109. out, _, err = runCommandWithOutput(saveCmd)
  110. errorOut(err, t, fmt.Sprintf("failed to save repo: %v %v", out, err))
  111. deleteImages(repoName)
  112. loadCmdFinal := `docker load -i /tmp/foobar-save-load-test.tar`
  113. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  114. out, _, err = runCommandWithOutput(loadCmd)
  115. errorOut(err, t, fmt.Sprintf("failed to load repo: %v %v", out, err))
  116. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  117. after, _, err := runCommandWithOutput(inspectCmd)
  118. errorOut(err, t, fmt.Sprintf("the repo should exist after loading it: %v %v", after, err))
  119. if before != after {
  120. t.Fatalf("inspect is not the same after a save / load")
  121. }
  122. deleteContainer(cleanedContainerID)
  123. deleteImages(repoName)
  124. os.Remove("/tmp/foobar-save-load-test.tar")
  125. logDone("save - save a repo using -o")
  126. logDone("load - load a repo using -i")
  127. }
  128. func TestSaveMultipleNames(t *testing.T) {
  129. repoName := "foobar-save-multi-name-test"
  130. // Make one image
  131. tagCmdFinal := fmt.Sprintf("%v tag scratch:latest %v-one:latest", dockerBinary, repoName)
  132. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  133. out, _, err := runCommandWithOutput(tagCmd)
  134. errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
  135. // Make two images
  136. tagCmdFinal = fmt.Sprintf("%v tag scratch:latest %v-two:latest", dockerBinary, repoName)
  137. tagCmd = exec.Command("bash", "-c", tagCmdFinal)
  138. out, _, err = runCommandWithOutput(tagCmd)
  139. errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
  140. saveCmdFinal := fmt.Sprintf("%v save %v-one %v-two:latest | tar xO repositories | grep -q -E '(-one|-two)'", dockerBinary, repoName, repoName)
  141. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  142. out, _, err = runCommandWithOutput(saveCmd)
  143. errorOut(err, t, fmt.Sprintf("failed to save multiple repos: %v %v", out, err))
  144. deleteImages(repoName)
  145. logDone("save - save by multiple names")
  146. }
  147. // Issue #6722 #5892 ensure directories are included in changes
  148. func TestSaveDirectoryPermissions(t *testing.T) {
  149. layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  150. name := "save-directory-permissions"
  151. tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
  152. extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
  153. os.Mkdir(extractionDirectory, 0777)
  154. if err != nil {
  155. t.Errorf("failed to create temporary directory: %s", err)
  156. }
  157. defer os.RemoveAll(tmpDir)
  158. defer deleteImages(name)
  159. _, err = buildImage(name,
  160. `FROM busybox
  161. RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a
  162. RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`,
  163. true)
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. saveCmdFinal := fmt.Sprintf("%s save %s | tar -xf - -C %s", dockerBinary, name, extractionDirectory)
  168. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  169. out, _, err := runCommandWithOutput(saveCmd)
  170. if err != nil {
  171. t.Errorf("failed to save and extract image: %s", out)
  172. }
  173. dirs, err := ioutil.ReadDir(extractionDirectory)
  174. if err != nil {
  175. t.Errorf("failed to get a listing of the layer directories: %s", err)
  176. }
  177. found := false
  178. for _, entry := range dirs {
  179. if entry.IsDir() {
  180. layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
  181. f, err := os.Open(layerPath)
  182. if err != nil {
  183. t.Fatalf("failed to open %s: %s", layerPath, err)
  184. }
  185. entries, err := ListTar(f)
  186. if err != nil {
  187. t.Fatalf("encountered error while listing tar entries: %s", err)
  188. }
  189. if reflect.DeepEqual(entries, layerEntries) {
  190. found = true
  191. }
  192. }
  193. }
  194. if !found {
  195. t.Fatalf("failed to find the layer with the right content listing")
  196. }
  197. logDone("save - ensure directories exist in exported layers")
  198. }