docker_cli_save_load_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "testing"
  7. )
  8. // save a repo and try to load it using stdout
  9. func TestSaveAndLoadRepoStdout(t *testing.T) {
  10. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  11. out, _, err := runCommandWithOutput(runCmd)
  12. errorOut(err, t, fmt.Sprintf("failed to create a container: %v %v", out, err))
  13. cleanedContainerID := stripTrailingCharacters(out)
  14. repoName := "foobar-save-load-test"
  15. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  16. out, _, err = runCommandWithOutput(inspectCmd)
  17. errorOut(err, t, fmt.Sprintf("output should've been a container id: %v %v", cleanedContainerID, err))
  18. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  19. out, _, err = runCommandWithOutput(commitCmd)
  20. errorOut(err, t, fmt.Sprintf("failed to commit container: %v %v", out, err))
  21. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  22. before, _, err := runCommandWithOutput(inspectCmd)
  23. errorOut(err, t, fmt.Sprintf("the repo should exist before saving it: %v %v", before, err))
  24. saveCmdTemplate := `%v save %v > /tmp/foobar-save-load-test.tar`
  25. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
  26. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  27. out, _, err = runCommandWithOutput(saveCmd)
  28. errorOut(err, t, fmt.Sprintf("failed to save repo: %v %v", out, err))
  29. deleteImages(repoName)
  30. loadCmdFinal := `cat /tmp/foobar-save-load-test.tar | docker load`
  31. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  32. out, _, err = runCommandWithOutput(loadCmd)
  33. errorOut(err, t, fmt.Sprintf("failed to load repo: %v %v", out, err))
  34. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  35. after, _, err := runCommandWithOutput(inspectCmd)
  36. errorOut(err, t, fmt.Sprintf("the repo should exist after loading it: %v %v", after, err))
  37. if before != after {
  38. t.Fatalf("inspect is not the same after a save / load")
  39. }
  40. deleteContainer(cleanedContainerID)
  41. deleteImages(repoName)
  42. os.Remove("/tmp/foobar-save-load-test.tar")
  43. logDone("save - save a repo using stdout")
  44. logDone("load - load a repo using stdout")
  45. }
  46. func TestSaveSingleTag(t *testing.T) {
  47. repoName := "foobar-save-single-tag-test"
  48. tagCmdFinal := fmt.Sprintf("%v tag busybox:latest %v:latest", dockerBinary, repoName)
  49. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  50. out, _, err := runCommandWithOutput(tagCmd)
  51. errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
  52. idCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
  53. idCmd := exec.Command("bash", "-c", idCmdFinal)
  54. out, _, err = runCommandWithOutput(idCmd)
  55. errorOut(err, t, fmt.Sprintf("failed to get repo ID: %v %v", out, err))
  56. cleanedImageID := stripTrailingCharacters(out)
  57. saveCmdFinal := fmt.Sprintf("%v save %v:latest | tar t | grep -E '(^repositories$|%v)'", dockerBinary, repoName, cleanedImageID)
  58. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  59. out, _, err = runCommandWithOutput(saveCmd)
  60. errorOut(err, t, fmt.Sprintf("failed to save repo with image ID and 'repositories' file: %v %v", out, err))
  61. deleteImages(repoName)
  62. logDone("save - save a specific image:tag")
  63. }
  64. func TestSaveImageId(t *testing.T) {
  65. repoName := "foobar-save-image-id-test"
  66. tagCmdFinal := fmt.Sprintf("%v tag scratch:latest %v:latest", dockerBinary, repoName)
  67. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  68. out, _, err := runCommandWithOutput(tagCmd)
  69. errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
  70. idLongCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
  71. idLongCmd := exec.Command("bash", "-c", idLongCmdFinal)
  72. out, _, err = runCommandWithOutput(idLongCmd)
  73. errorOut(err, t, fmt.Sprintf("failed to get repo ID: %v %v", out, err))
  74. cleanedLongImageID := stripTrailingCharacters(out)
  75. idShortCmdFinal := fmt.Sprintf("%v images -q %v", dockerBinary, repoName)
  76. idShortCmd := exec.Command("bash", "-c", idShortCmdFinal)
  77. out, _, err = runCommandWithOutput(idShortCmd)
  78. errorOut(err, t, fmt.Sprintf("failed to get repo short ID: %v %v", out, err))
  79. cleanedShortImageID := stripTrailingCharacters(out)
  80. saveCmdFinal := fmt.Sprintf("%v save %v | tar t | grep %v", dockerBinary, cleanedShortImageID, cleanedLongImageID)
  81. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  82. out, _, err = runCommandWithOutput(saveCmd)
  83. errorOut(err, t, fmt.Sprintf("failed to save repo with image ID: %v %v", out, err))
  84. deleteImages(repoName)
  85. logDone("save - save a image by ID")
  86. }
  87. // save a repo and try to load it using flags
  88. func TestSaveAndLoadRepoFlags(t *testing.T) {
  89. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  90. out, _, err := runCommandWithOutput(runCmd)
  91. errorOut(err, t, fmt.Sprintf("failed to create a container: %v %v", out, err))
  92. cleanedContainerID := stripTrailingCharacters(out)
  93. repoName := "foobar-save-load-test"
  94. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  95. out, _, err = runCommandWithOutput(inspectCmd)
  96. errorOut(err, t, fmt.Sprintf("output should've been a container id: %v %v", cleanedContainerID, err))
  97. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  98. out, _, err = runCommandWithOutput(commitCmd)
  99. errorOut(err, t, fmt.Sprintf("failed to commit container: %v %v", out, err))
  100. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  101. before, _, err := runCommandWithOutput(inspectCmd)
  102. errorOut(err, t, fmt.Sprintf("the repo should exist before saving it: %v %v", before, err))
  103. saveCmdTemplate := `%v save -o /tmp/foobar-save-load-test.tar %v`
  104. saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
  105. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  106. out, _, err = runCommandWithOutput(saveCmd)
  107. errorOut(err, t, fmt.Sprintf("failed to save repo: %v %v", out, err))
  108. deleteImages(repoName)
  109. loadCmdFinal := `docker load -i /tmp/foobar-save-load-test.tar`
  110. loadCmd := exec.Command("bash", "-c", loadCmdFinal)
  111. out, _, err = runCommandWithOutput(loadCmd)
  112. errorOut(err, t, fmt.Sprintf("failed to load repo: %v %v", out, err))
  113. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  114. after, _, err := runCommandWithOutput(inspectCmd)
  115. errorOut(err, t, fmt.Sprintf("the repo should exist after loading it: %v %v", after, err))
  116. if before != after {
  117. t.Fatalf("inspect is not the same after a save / load")
  118. }
  119. deleteContainer(cleanedContainerID)
  120. deleteImages(repoName)
  121. os.Remove("/tmp/foobar-save-load-test.tar")
  122. logDone("save - save a repo using -o")
  123. logDone("load - load a repo using -i")
  124. }
  125. func TestSaveMultipleNames(t *testing.T) {
  126. repoName := "foobar-save-multi-name-test"
  127. // Make one image
  128. tagCmdFinal := fmt.Sprintf("%v tag scratch:latest %v-one:latest", dockerBinary, repoName)
  129. tagCmd := exec.Command("bash", "-c", tagCmdFinal)
  130. out, _, err := runCommandWithOutput(tagCmd)
  131. errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
  132. // Make two images
  133. tagCmdFinal = fmt.Sprintf("%v tag scratch:latest %v-two:latest", dockerBinary, repoName)
  134. tagCmd = exec.Command("bash", "-c", tagCmdFinal)
  135. out, _, err = runCommandWithOutput(tagCmd)
  136. errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
  137. saveCmdFinal := fmt.Sprintf("%v save %v-one %v-two:latest | tar xO repositories | grep -q -E '(-one|-two)'", dockerBinary, repoName, repoName)
  138. saveCmd := exec.Command("bash", "-c", saveCmdFinal)
  139. out, _, err = runCommandWithOutput(saveCmd)
  140. errorOut(err, t, fmt.Sprintf("failed to save multiple repos: %v %v", out, err))
  141. deleteImages(repoName)
  142. logDone("save - save by multiple names")
  143. }