docker_cli_export_import_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. defer deleteImages("repo/testexp:v1")
  12. defer deleteContainer(containerID)
  13. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", containerID, "busybox", "true")
  14. out, _, err := runCommandWithOutput(runCmd)
  15. if err != nil {
  16. c.Fatal("failed to create a container", out, err)
  17. }
  18. inspectCmd := exec.Command(dockerBinary, "inspect", containerID)
  19. out, _, err = runCommandWithOutput(inspectCmd)
  20. if err != nil {
  21. c.Fatalf("output should've been a container id: %s %s ", containerID, err)
  22. }
  23. exportCmd := exec.Command(dockerBinary, "export", containerID)
  24. if out, _, err = runCommandWithOutput(exportCmd); err != nil {
  25. c.Fatalf("failed to export container: %s, %v", out, err)
  26. }
  27. importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
  28. importCmd.Stdin = strings.NewReader(out)
  29. out, _, err = runCommandWithOutput(importCmd)
  30. if err != nil {
  31. c.Fatalf("failed to import image: %s, %v", out, err)
  32. }
  33. cleanedImageID := strings.TrimSpace(out)
  34. inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
  35. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  36. c.Fatalf("output should've been an image id: %s, %v", out, err)
  37. }
  38. }
  39. // Used to test output flag in the export command
  40. func (s *DockerSuite) TestExportContainerWithOutputAndImportImage(c *check.C) {
  41. containerID := "testexportcontainerwithoutputandimportimage"
  42. defer deleteImages("repo/testexp:v1")
  43. defer deleteContainer(containerID)
  44. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", containerID, "busybox", "true")
  45. out, _, err := runCommandWithOutput(runCmd)
  46. if err != nil {
  47. c.Fatal("failed to create a container", out, err)
  48. }
  49. inspectCmd := exec.Command(dockerBinary, "inspect", containerID)
  50. out, _, err = runCommandWithOutput(inspectCmd)
  51. if err != nil {
  52. c.Fatalf("output should've been a container id: %s %s ", containerID, err)
  53. }
  54. defer os.Remove("testexp.tar")
  55. exportCmd := exec.Command(dockerBinary, "export", "--output=testexp.tar", containerID)
  56. if out, _, err = runCommandWithOutput(exportCmd); err != nil {
  57. c.Fatalf("failed to export container: %s, %v", out, err)
  58. }
  59. out, _, err = runCommandWithOutput(exec.Command("cat", "testexp.tar"))
  60. if err != nil {
  61. c.Fatal(out, err)
  62. }
  63. importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
  64. importCmd.Stdin = strings.NewReader(out)
  65. out, _, err = runCommandWithOutput(importCmd)
  66. if err != nil {
  67. c.Fatalf("failed to import image: %s, %v", out, err)
  68. }
  69. cleanedImageID := strings.TrimSpace(out)
  70. inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
  71. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  72. c.Fatalf("output should've been an image id: %s, %v", out, err)
  73. }
  74. }