docker_cli_export_import_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. testRequires(c, DaemonIsLinux)
  11. containerID := "testexportcontainerandimportimage"
  12. dockerCmd(c, "run", "--name", containerID, "busybox", "true")
  13. out, _ := dockerCmd(c, "export", containerID)
  14. importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
  15. importCmd.Stdin = strings.NewReader(out)
  16. out, _, err := runCommandWithOutput(importCmd)
  17. if err != nil {
  18. c.Fatalf("failed to import image: %s, %v", out, err)
  19. }
  20. cleanedImageID := strings.TrimSpace(out)
  21. if cleanedImageID == "" {
  22. c.Fatalf("output should have been an image id, got: %s", out)
  23. }
  24. }
  25. // Used to test output flag in the export command
  26. func (s *DockerSuite) TestExportContainerWithOutputAndImportImage(c *check.C) {
  27. testRequires(c, DaemonIsLinux)
  28. containerID := "testexportcontainerwithoutputandimportimage"
  29. dockerCmd(c, "run", "--name", containerID, "busybox", "true")
  30. dockerCmd(c, "export", "--output=testexp.tar", containerID)
  31. defer os.Remove("testexp.tar")
  32. out, _, err := runCommandWithOutput(exec.Command("cat", "testexp.tar"))
  33. if err != nil {
  34. c.Fatal(out, err)
  35. }
  36. importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
  37. importCmd.Stdin = strings.NewReader(out)
  38. out, _, err = runCommandWithOutput(importCmd)
  39. if err != nil {
  40. c.Fatalf("failed to import image: %s, %v", out, err)
  41. }
  42. cleanedImageID := strings.TrimSpace(out)
  43. if cleanedImageID == "" {
  44. c.Fatalf("output should have been an image id, got: %s", out)
  45. }
  46. }