docker_cli_export_import_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  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. exportCmdTemplate := `%v export %v > /tmp/testexp.tar`
  22. exportCmdFinal := fmt.Sprintf(exportCmdTemplate, dockerBinary, cleanedContainerID)
  23. exportCmd := exec.Command("bash", "-c", exportCmdFinal)
  24. if out, _, err = runCommandWithOutput(exportCmd); err != nil {
  25. t.Fatalf("failed to export container: %s, %v", out, err)
  26. }
  27. importCmdFinal := `cat /tmp/testexp.tar | docker import - repo/testexp:v1`
  28. importCmd := exec.Command("bash", "-c", importCmdFinal)
  29. out, _, err = runCommandWithOutput(importCmd)
  30. if err != nil {
  31. t.Fatalf("failed to import image: %s, %v", out, err)
  32. }
  33. cleanedImageID := stripTrailingCharacters(out)
  34. inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
  35. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  36. t.Fatalf("output should've been an image id: %s, %v", out, err)
  37. }
  38. deleteContainer(cleanedContainerID)
  39. deleteImages("repo/testexp:v1")
  40. os.Remove("/tmp/testexp.tar")
  41. logDone("export - export a container")
  42. logDone("import - import an image")
  43. }