docker_cli_export_import_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "strings"
  6. "github.com/go-check/check"
  7. )
  8. // export an image and try to import it into a new one
  9. func (s *DockerSuite) TestExportContainerAndImportImage(c *check.C) {
  10. containerID := "testexportcontainerandimportimage"
  11. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", containerID, "busybox", "true")
  12. out, _, err := runCommandWithOutput(runCmd)
  13. if err != nil {
  14. c.Fatal("failed to create a container", out, err)
  15. }
  16. inspectCmd := exec.Command(dockerBinary, "inspect", containerID)
  17. out, _, err = runCommandWithOutput(inspectCmd)
  18. if err != nil {
  19. c.Fatalf("output should've been a container id: %s %s ", containerID, err)
  20. }
  21. exportCmd := exec.Command(dockerBinary, "export", containerID)
  22. if out, _, err = runCommandWithOutput(exportCmd); err != nil {
  23. c.Fatalf("failed to export container: %s, %v", out, err)
  24. }
  25. importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
  26. importCmd.Stdin = strings.NewReader(out)
  27. out, _, err = runCommandWithOutput(importCmd)
  28. if err != nil {
  29. c.Fatalf("failed to import image: %s, %v", out, err)
  30. }
  31. cleanedImageID := strings.TrimSpace(out)
  32. inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
  33. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  34. c.Fatalf("output should've been an image id: %s, %v", out, err)
  35. }
  36. }
  37. // Used to test output flag in the export command
  38. func (s *DockerSuite) TestExportContainerWithOutputAndImportImage(c *check.C) {
  39. containerID := "testexportcontainerwithoutputandimportimage"
  40. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", containerID, "busybox", "true")
  41. out, _, err := runCommandWithOutput(runCmd)
  42. if err != nil {
  43. c.Fatal("failed to create a container", out, err)
  44. }
  45. inspectCmd := exec.Command(dockerBinary, "inspect", containerID)
  46. out, _, err = runCommandWithOutput(inspectCmd)
  47. if err != nil {
  48. c.Fatalf("output should've been a container id: %s %s ", containerID, err)
  49. }
  50. defer os.Remove("testexp.tar")
  51. exportCmd := exec.Command(dockerBinary, "export", "--output=testexp.tar", containerID)
  52. if out, _, err = runCommandWithOutput(exportCmd); err != nil {
  53. c.Fatalf("failed to export container: %s, %v", out, err)
  54. }
  55. out, _, err = runCommandWithOutput(exec.Command("cat", "testexp.tar"))
  56. if err != nil {
  57. c.Fatal(out, err)
  58. }
  59. importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
  60. importCmd.Stdin = strings.NewReader(out)
  61. out, _, err = runCommandWithOutput(importCmd)
  62. if err != nil {
  63. c.Fatalf("failed to import image: %s, %v", out, err)
  64. }
  65. cleanedImageID := strings.TrimSpace(out)
  66. inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
  67. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  68. c.Fatalf("output should've been an image id: %s, %v", out, err)
  69. }
  70. }