docker_cli_export_import_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. out, _, err = runCommandWithOutput(exportCmd)
  25. errorOut(err, t, fmt.Sprintf("failed to export container: %v %v", out, err))
  26. importCmdFinal := `cat /tmp/testexp.tar | docker import - testexp`
  27. importCmd := exec.Command("bash", "-c", importCmdFinal)
  28. out, _, err = runCommandWithOutput(importCmd)
  29. errorOut(err, t, fmt.Sprintf("failed to import image: %v %v", out, err))
  30. cleanedImageID := stripTrailingCharacters(out)
  31. inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
  32. out, _, err = runCommandWithOutput(inspectCmd)
  33. errorOut(err, t, fmt.Sprintf("output should've been an image id: %v %v", out, err))
  34. deleteContainer(cleanedContainerID)
  35. deleteImages("testexp")
  36. os.Remove("/tmp/testexp.tar")
  37. logDone("export - export a container")
  38. logDone("import - import an image")
  39. }