docker_cli_export_import_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. )
  8. // export an image and try to import it into a new one
  9. func TestExportContainerAndImportImage(t *testing.T) {
  10. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  11. out, _, err := runCommandWithOutput(runCmd)
  12. if err != nil {
  13. t.Fatal("failed to create a container", out, err)
  14. }
  15. cleanedContainerID := stripTrailingCharacters(out)
  16. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  17. out, _, err = runCommandWithOutput(inspectCmd)
  18. if err != nil {
  19. t.Fatalf("output should've been a container id: %s %s ", cleanedContainerID, err)
  20. }
  21. exportCmd := exec.Command(dockerBinary, "export", cleanedContainerID)
  22. if out, _, err = runCommandWithOutput(exportCmd); err != nil {
  23. t.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. t.Fatalf("failed to import image: %s, %v", out, err)
  30. }
  31. cleanedImageID := stripTrailingCharacters(out)
  32. inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
  33. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  34. t.Fatalf("output should've been an image id: %s, %v", out, err)
  35. }
  36. deleteContainer(cleanedContainerID)
  37. deleteImages("repo/testexp:v1")
  38. logDone("export - export a container")
  39. logDone("import - import an image")
  40. }
  41. // Used to test output flag in the export command
  42. func TestExportContainerWithOutputAndImportImage(t *testing.T) {
  43. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  44. out, _, err := runCommandWithOutput(runCmd)
  45. if err != nil {
  46. t.Fatal("failed to create a container", out, err)
  47. }
  48. cleanedContainerID := stripTrailingCharacters(out)
  49. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  50. out, _, err = runCommandWithOutput(inspectCmd)
  51. if err != nil {
  52. t.Fatalf("output should've been a container id: %s %s ", cleanedContainerID, err)
  53. }
  54. exportCmd := exec.Command(dockerBinary, "export", "--output=testexp.tar", cleanedContainerID)
  55. if out, _, err = runCommandWithOutput(exportCmd); err != nil {
  56. t.Fatalf("failed to export container: %s, %v", out, err)
  57. }
  58. out, _, err = runCommandWithOutput(exec.Command("cat", "testexp.tar"))
  59. if err != nil {
  60. t.Fatal(out, err)
  61. }
  62. importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
  63. importCmd.Stdin = strings.NewReader(out)
  64. out, _, err = runCommandWithOutput(importCmd)
  65. if err != nil {
  66. t.Fatalf("failed to import image: %s, %v", out, err)
  67. }
  68. cleanedImageID := stripTrailingCharacters(out)
  69. inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
  70. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  71. t.Fatalf("output should've been an image id: %s, %v", out, err)
  72. }
  73. deleteContainer(cleanedContainerID)
  74. deleteImages("repo/testexp:v1")
  75. os.Remove("/tmp/testexp.tar")
  76. logDone("export - export a container with output flag")
  77. logDone("import - import an image with output flag")
  78. }