docker_cli_save_load_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "reflect"
  9. "sort"
  10. "strings"
  11. "testing"
  12. )
  13. // save a repo using gz compression and try to load it using stdout
  14. func TestSaveXzAndLoadRepoStdout(t *testing.T) {
  15. tempDir, err := ioutil.TempDir("", "test-save-xz-gz-load-repo-stdout")
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. defer os.RemoveAll(tempDir)
  20. tarballPath := filepath.Join(tempDir, "foobar-save-load-test.tar.xz.gz")
  21. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  22. out, _, err := runCommandWithOutput(runCmd)
  23. if err != nil {
  24. t.Fatalf("failed to create a container: %v %v", out, err)
  25. }
  26. cleanedContainerID := stripTrailingCharacters(out)
  27. repoName := "foobar-save-load-test-xz-gz"
  28. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  29. out, _, err = runCommandWithOutput(inspectCmd)
  30. if err != nil {
  31. t.Fatalf("output should've been a container id: %v %v", cleanedContainerID, err)
  32. }
  33. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  34. out, _, err = runCommandWithOutput(commitCmd)
  35. if err != nil {
  36. t.Fatalf("failed to commit container: %v %v", out, err)
  37. }
  38. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  39. before, _, err := runCommandWithOutput(inspectCmd)
  40. if err != nil {
  41. t.Fatalf("the repo should exist before saving it: %v %v", before, err)
  42. }
  43. saveCmdTemplate := `%v save %v | xz -c | gzip -c > %s`
  44. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName, tarballPath)
  45. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  46. out, _, err = runCommandWithOutput(saveCmd)
  47. if err != nil {
  48. t.Fatalf("failed to save repo: %v %v", out, err)
  49. }
  50. deleteImages(repoName)
  51. loadCmdFinal := fmt.Sprintf(`cat %s | docker load`, tarballPath)
  52. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  53. out, _, err = runCommandWithOutput(loadCmd)
  54. if err == nil {
  55. t.Fatalf("expected error, but succeeded with no error and output: %v", out)
  56. }
  57. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  58. after, _, err := runCommandWithOutput(inspectCmd)
  59. if err == nil {
  60. t.Fatalf("the repo should not exist: %v", after)
  61. }
  62. deleteContainer(cleanedContainerID)
  63. deleteImages(repoName)
  64. logDone("load - save a repo with xz compression & load it using stdout")
  65. }
  66. // save a repo using xz+gz compression and try to load it using stdout
  67. func TestSaveXzGzAndLoadRepoStdout(t *testing.T) {
  68. tempDir, err := ioutil.TempDir("", "test-save-xz-gz-load-repo-stdout")
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. defer os.RemoveAll(tempDir)
  73. tarballPath := filepath.Join(tempDir, "foobar-save-load-test.tar.xz.gz")
  74. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  75. out, _, err := runCommandWithOutput(runCmd)
  76. if err != nil {
  77. t.Fatalf("failed to create a container: %v %v", out, err)
  78. }
  79. cleanedContainerID := stripTrailingCharacters(out)
  80. repoName := "foobar-save-load-test-xz-gz"
  81. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  82. out, _, err = runCommandWithOutput(inspectCmd)
  83. if err != nil {
  84. t.Fatalf("output should've been a container id: %v %v", cleanedContainerID, err)
  85. }
  86. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  87. out, _, err = runCommandWithOutput(commitCmd)
  88. if err != nil {
  89. t.Fatalf("failed to commit container: %v %v", out, err)
  90. }
  91. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  92. before, _, err := runCommandWithOutput(inspectCmd)
  93. if err != nil {
  94. t.Fatalf("the repo should exist before saving it: %v %v", before, err)
  95. }
  96. saveCmdTemplate := `%v save %v | xz -c | gzip -c > %s`
  97. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName, tarballPath)
  98. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  99. out, _, err = runCommandWithOutput(saveCmd)
  100. if err != nil {
  101. t.Fatalf("failed to save repo: %v %v", out, err)
  102. }
  103. deleteImages(repoName)
  104. loadCmdFinal := fmt.Sprintf(`cat %s | docker load`, tarballPath)
  105. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  106. out, _, err = runCommandWithOutput(loadCmd)
  107. if err == nil {
  108. t.Fatalf("expected error, but succeeded with no error and output: %v", out)
  109. }
  110. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  111. after, _, err := runCommandWithOutput(inspectCmd)
  112. if err == nil {
  113. t.Fatalf("the repo should not exist: %v", after)
  114. }
  115. deleteContainer(cleanedContainerID)
  116. deleteImages(repoName)
  117. logDone("load - save a repo with xz+gz compression & load it using stdout")
  118. }
  119. func TestSaveSingleTag(t *testing.T) {
  120. repoName := "foobar-save-single-tag-test"
  121. tagCmdFinal := fmt.Sprintf("%v tag busybox:latest %v:latest", dockerBinary, repoName)
  122. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  123. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  124. t.Fatalf("failed to tag repo: %s, %v", out, err)
  125. }
  126. idCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
  127. idCmd := exec.Command("bash", "-c", idCmdFinal)
  128. out, _, err := runCommandWithOutput(idCmd)
  129. if err != nil {
  130. t.Fatalf("failed to get repo ID: %s, %v", out, err)
  131. }
  132. cleanedImageID := stripTrailingCharacters(out)
  133. saveCmdFinal := fmt.Sprintf("%v save %v:latest | tar t | grep -E '(^repositories$|%v)'", dockerBinary, repoName, cleanedImageID)
  134. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  135. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  136. t.Fatalf("failed to save repo with image ID and 'repositories' file: %s, %v", out, err)
  137. }
  138. deleteImages(repoName)
  139. logDone("save - save a specific image:tag")
  140. }
  141. func TestSaveImageId(t *testing.T) {
  142. repoName := "foobar-save-image-id-test"
  143. tagCmdFinal := fmt.Sprintf("%v tag emptyfs:latest %v:latest", dockerBinary, repoName)
  144. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  145. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  146. t.Fatalf("failed to tag repo: %s, %v", out, err)
  147. }
  148. idLongCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
  149. idLongCmd := exec.Command("bash", "-c", idLongCmdFinal)
  150. out, _, err := runCommandWithOutput(idLongCmd)
  151. if err != nil {
  152. t.Fatalf("failed to get repo ID: %s, %v", out, err)
  153. }
  154. cleanedLongImageID := stripTrailingCharacters(out)
  155. idShortCmdFinal := fmt.Sprintf("%v images -q %v", dockerBinary, repoName)
  156. idShortCmd := exec.Command("bash", "-c", idShortCmdFinal)
  157. out, _, err = runCommandWithOutput(idShortCmd)
  158. if err != nil {
  159. t.Fatalf("failed to get repo short ID: %s, %v", out, err)
  160. }
  161. cleanedShortImageID := stripTrailingCharacters(out)
  162. saveCmdFinal := fmt.Sprintf("%v save %v | tar t | grep %v", dockerBinary, cleanedShortImageID, cleanedLongImageID)
  163. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  164. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  165. t.Fatalf("failed to save repo with image ID: %s, %v", out, err)
  166. }
  167. deleteImages(repoName)
  168. logDone("save - save a image by ID")
  169. }
  170. // save a repo and try to load it using flags
  171. func TestSaveAndLoadRepoFlags(t *testing.T) {
  172. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  173. out, _, err := runCommandWithOutput(runCmd)
  174. if err != nil {
  175. t.Fatalf("failed to create a container: %s, %v", out, err)
  176. }
  177. cleanedContainerID := stripTrailingCharacters(out)
  178. repoName := "foobar-save-load-test"
  179. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  180. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  181. t.Fatalf("output should've been a container id: %s, %v", out, err)
  182. }
  183. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  184. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  185. t.Fatalf("failed to commit container: %s, %v", out, err)
  186. }
  187. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  188. before, _, err := runCommandWithOutput(inspectCmd)
  189. if err != nil {
  190. t.Fatalf("the repo should exist before saving it: %s, %v", before, err)
  191. }
  192. saveCmdTemplate := `%v save -o /tmp/foobar-save-load-test.tar %v`
  193. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
  194. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  195. if out, _, err = runCommandWithOutput(saveCmd); err != nil {
  196. t.Fatalf("failed to save repo: %s, %v", out, err)
  197. }
  198. deleteImages(repoName)
  199. loadCmdFinal := `docker load -i /tmp/foobar-save-load-test.tar`
  200. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  201. if out, _, err = runCommandWithOutput(loadCmd); err != nil {
  202. t.Fatalf("failed to load repo: %s, %v", out, err)
  203. }
  204. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  205. after, _, err := runCommandWithOutput(inspectCmd)
  206. if err != nil {
  207. t.Fatalf("the repo should exist after loading it: %s, %v", after, err)
  208. }
  209. if before != after {
  210. t.Fatalf("inspect is not the same after a save / load")
  211. }
  212. deleteContainer(cleanedContainerID)
  213. deleteImages(repoName)
  214. os.Remove("/tmp/foobar-save-load-test.tar")
  215. logDone("save - save a repo using -o && load a repo using -i")
  216. }
  217. func TestSaveMultipleNames(t *testing.T) {
  218. repoName := "foobar-save-multi-name-test"
  219. // Make one image
  220. tagCmdFinal := fmt.Sprintf("%v tag emptyfs:latest %v-one:latest", dockerBinary, repoName)
  221. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  222. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  223. t.Fatalf("failed to tag repo: %s, %v", out, err)
  224. }
  225. defer deleteImages(repoName + "-one")
  226. // Make two images
  227. tagCmdFinal = fmt.Sprintf("%v tag emptyfs:latest %v-two:latest", dockerBinary, repoName)
  228. tagCmd = exec.Command("bash", "-c", tagCmdFinal)
  229. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  230. t.Fatalf("failed to tag repo: %s, %v", out, err)
  231. }
  232. defer deleteImages(repoName + "-two")
  233. saveCmdFinal := fmt.Sprintf("%v save %v-one %v-two:latest | tar xO repositories | grep -q -E '(-one|-two)'", dockerBinary, repoName, repoName)
  234. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  235. if out, _, err := runCommandWithOutput(saveCmd); err != nil {
  236. t.Fatalf("failed to save multiple repos: %s, %v", out, err)
  237. }
  238. deleteImages(repoName)
  239. logDone("save - save by multiple names")
  240. }
  241. func TestSaveRepoWithMultipleImages(t *testing.T) {
  242. makeImage := func(from string, tag string) string {
  243. runCmd := exec.Command(dockerBinary, "run", "-d", from, "true")
  244. var (
  245. out string
  246. err error
  247. )
  248. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  249. t.Fatalf("failed to create a container: %v %v", out, err)
  250. }
  251. cleanedContainerID := stripTrailingCharacters(out)
  252. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, tag)
  253. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  254. t.Fatalf("failed to commit container: %v %v", out, err)
  255. }
  256. imageID := stripTrailingCharacters(out)
  257. deleteContainer(cleanedContainerID)
  258. return imageID
  259. }
  260. repoName := "foobar-save-multi-images-test"
  261. tagFoo := repoName + ":foo"
  262. tagBar := repoName + ":bar"
  263. idFoo := makeImage("busybox:latest", tagFoo)
  264. idBar := makeImage("busybox:latest", tagBar)
  265. deleteImages(repoName)
  266. // create the archive
  267. saveCmdFinal := fmt.Sprintf("%v save %v | tar t | grep 'VERSION' |cut -d / -f1", dockerBinary, repoName)
  268. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  269. out, _, err := runCommandWithOutput(saveCmd)
  270. if err != nil {
  271. t.Fatalf("failed to save multiple images: %s, %v", out, err)
  272. }
  273. actual := strings.Split(stripTrailingCharacters(out), "\n")
  274. // make the list of expected layers
  275. historyCmdFinal := fmt.Sprintf("%v history -q --no-trunc %v", dockerBinary, "busybox:latest")
  276. historyCmd := exec.Command("bash", "-c", historyCmdFinal)
  277. out, _, err = runCommandWithOutput(historyCmd)
  278. if err != nil {
  279. t.Fatalf("failed to get history: %s, %v", out, err)
  280. }
  281. expected := append(strings.Split(stripTrailingCharacters(out), "\n"), idFoo, idBar)
  282. sort.Strings(actual)
  283. sort.Strings(expected)
  284. if !reflect.DeepEqual(expected, actual) {
  285. t.Fatalf("achive does not contains the right layers: got %v, expected %v", actual, expected)
  286. }
  287. logDone("save - save repository with multiple images")
  288. }
  289. // Issue #6722 #5892 ensure directories are included in changes
  290. func TestSaveDirectoryPermissions(t *testing.T) {
  291. layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  292. layerEntriesAUFS := []string{"./", ".wh..wh.aufs", ".wh..wh.orph/", ".wh..wh.plnk/", "opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
  293. name := "save-directory-permissions"
  294. tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
  295. if err != nil {
  296. t.Errorf("failed to create temporary directory: %s", err)
  297. }
  298. extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
  299. os.Mkdir(extractionDirectory, 0777)
  300. defer os.RemoveAll(tmpDir)
  301. defer deleteImages(name)
  302. _, err = buildImage(name,
  303. `FROM busybox
  304. RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a
  305. RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`,
  306. true)
  307. if err != nil {
  308. t.Fatal(err)
  309. }
  310. saveCmdFinal := fmt.Sprintf("%s save %s | tar -xf - -C %s", dockerBinary, name, extractionDirectory)
  311. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  312. if out, _, err := runCommandWithOutput(saveCmd); err != nil {
  313. t.Errorf("failed to save and extract image: %s", out)
  314. }
  315. dirs, err := ioutil.ReadDir(extractionDirectory)
  316. if err != nil {
  317. t.Errorf("failed to get a listing of the layer directories: %s", err)
  318. }
  319. found := false
  320. for _, entry := range dirs {
  321. if entry.IsDir() {
  322. layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
  323. f, err := os.Open(layerPath)
  324. if err != nil {
  325. t.Fatalf("failed to open %s: %s", layerPath, err)
  326. }
  327. entries, err := ListTar(f)
  328. if err != nil {
  329. t.Fatalf("encountered error while listing tar entries: %s", err)
  330. }
  331. if reflect.DeepEqual(entries, layerEntries) || reflect.DeepEqual(entries, layerEntriesAUFS) {
  332. found = true
  333. break
  334. }
  335. }
  336. }
  337. if !found {
  338. t.Fatalf("failed to find the layer with the right content listing")
  339. }
  340. logDone("save - ensure directories exist in exported layers")
  341. }