github.com/nguyentm83/docker@v1.5.0/integration-cli/docker_cli_export_import_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "testing" 8 ) 9 10 // export an image and try to import it into a new one 11 func TestExportContainerAndImportImage(t *testing.T) { 12 runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true") 13 out, _, err := runCommandWithOutput(runCmd) 14 if err != nil { 15 t.Fatal("failed to create a container", out, err) 16 } 17 18 cleanedContainerID := stripTrailingCharacters(out) 19 20 inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID) 21 out, _, err = runCommandWithOutput(inspectCmd) 22 if err != nil { 23 t.Fatalf("output should've been a container id: %s %s ", cleanedContainerID, err) 24 } 25 26 exportCmdTemplate := `%v export %v > /tmp/testexp.tar` 27 exportCmdFinal := fmt.Sprintf(exportCmdTemplate, dockerBinary, cleanedContainerID) 28 exportCmd := exec.Command("bash", "-c", exportCmdFinal) 29 if out, _, err = runCommandWithOutput(exportCmd); err != nil { 30 t.Fatalf("failed to export container: %s, %v", out, err) 31 } 32 33 importCmdFinal := `cat /tmp/testexp.tar | docker import - repo/testexp:v1` 34 importCmd := exec.Command("bash", "-c", importCmdFinal) 35 out, _, err = runCommandWithOutput(importCmd) 36 if err != nil { 37 t.Fatalf("failed to import image: %s, %v", out, err) 38 } 39 40 cleanedImageID := stripTrailingCharacters(out) 41 42 inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID) 43 if out, _, err = runCommandWithOutput(inspectCmd); err != nil { 44 t.Fatalf("output should've been an image id: %s, %v", out, err) 45 } 46 47 deleteContainer(cleanedContainerID) 48 deleteImages("repo/testexp:v1") 49 50 os.Remove("/tmp/testexp.tar") 51 52 logDone("export - export a container") 53 logDone("import - import an image") 54 }