docker_cli_export_import_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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", "--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. exportCmd := exec.Command(dockerBinary, "export", containerID)
  17. if out, _, err = runCommandWithOutput(exportCmd); err != nil {
  18. c.Fatalf("failed to export container: %s, %v", out, err)
  19. }
  20. importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
  21. importCmd.Stdin = strings.NewReader(out)
  22. out, _, err = runCommandWithOutput(importCmd)
  23. if err != nil {
  24. c.Fatalf("failed to import image: %s, %v", out, err)
  25. }
  26. cleanedImageID := strings.TrimSpace(out)
  27. if cleanedImageID == "" {
  28. c.Fatalf("output should have been an image id, got: %s", out)
  29. }
  30. }
  31. // Used to test output flag in the export command
  32. func (s *DockerSuite) TestExportContainerWithOutputAndImportImage(c *check.C) {
  33. containerID := "testexportcontainerwithoutputandimportimage"
  34. runCmd := exec.Command(dockerBinary, "run", "--name", containerID, "busybox", "true")
  35. out, _, err := runCommandWithOutput(runCmd)
  36. if err != nil {
  37. c.Fatal("failed to create a container", out, err)
  38. }
  39. defer os.Remove("testexp.tar")
  40. exportCmd := exec.Command(dockerBinary, "export", "--output=testexp.tar", containerID)
  41. if out, _, err = runCommandWithOutput(exportCmd); err != nil {
  42. c.Fatalf("failed to export container: %s, %v", out, err)
  43. }
  44. out, _, err = runCommandWithOutput(exec.Command("cat", "testexp.tar"))
  45. if err != nil {
  46. c.Fatal(out, err)
  47. }
  48. importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
  49. importCmd.Stdin = strings.NewReader(out)
  50. out, _, err = runCommandWithOutput(importCmd)
  51. if err != nil {
  52. c.Fatalf("failed to import image: %s, %v", out, err)
  53. }
  54. cleanedImageID := strings.TrimSpace(out)
  55. if cleanedImageID == "" {
  56. c.Fatalf("output should have been an image id, got: %s", out)
  57. }
  58. }