docker_cli_export_import_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/import a container/image")
  39. }
  40. // Used to test output flag in the export command
  41. func TestExportContainerWithOutputAndImportImage(t *testing.T) {
  42. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  43. out, _, err := runCommandWithOutput(runCmd)
  44. if err != nil {
  45. t.Fatal("failed to create a container", out, err)
  46. }
  47. cleanedContainerID := stripTrailingCharacters(out)
  48. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  49. out, _, err = runCommandWithOutput(inspectCmd)
  50. if err != nil {
  51. t.Fatalf("output should've been a container id: %s %s ", cleanedContainerID, err)
  52. }
  53. exportCmd := exec.Command(dockerBinary, "export", "--output=testexp.tar", cleanedContainerID)
  54. if out, _, err = runCommandWithOutput(exportCmd); err != nil {
  55. t.Fatalf("failed to export container: %s, %v", out, err)
  56. }
  57. out, _, err = runCommandWithOutput(exec.Command("cat", "testexp.tar"))
  58. if err != nil {
  59. t.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. t.Fatalf("failed to import image: %s, %v", out, err)
  66. }
  67. cleanedImageID := stripTrailingCharacters(out)
  68. inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
  69. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  70. t.Fatalf("output should've been an image id: %s, %v", out, err)
  71. }
  72. deleteContainer(cleanedContainerID)
  73. deleteImages("repo/testexp:v1")
  74. os.Remove("/tmp/testexp.tar")
  75. logDone("export - export/import a container/image with output flag")
  76. }