docker_cli_export_import_test.go 3.0 KB

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