docker_cli_export_import_test.go 2.8 KB

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