github.com/alok87/docker@v1.6.0/integration-cli/docker_cli_export_import_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "os/exec" 6 "strings" 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 exportCmd := exec.Command(dockerBinary, "export", cleanedContainerID) 27 if out, _, err = runCommandWithOutput(exportCmd); err != nil { 28 t.Fatalf("failed to export container: %s, %v", out, err) 29 } 30 31 importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1") 32 importCmd.Stdin = strings.NewReader(out) 33 out, _, err = runCommandWithOutput(importCmd) 34 if err != nil { 35 t.Fatalf("failed to import image: %s, %v", out, err) 36 } 37 38 cleanedImageID := stripTrailingCharacters(out) 39 40 inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID) 41 if out, _, err = runCommandWithOutput(inspectCmd); err != nil { 42 t.Fatalf("output should've been an image id: %s, %v", out, err) 43 } 44 45 deleteContainer(cleanedContainerID) 46 deleteImages("repo/testexp:v1") 47 48 logDone("export - export a container") 49 logDone("import - import an image") 50 } 51 52 // Used to test output flag in the export command 53 func TestExportContainerWithOutputAndImportImage(t *testing.T) { 54 runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true") 55 out, _, err := runCommandWithOutput(runCmd) 56 if err != nil { 57 t.Fatal("failed to create a container", out, err) 58 } 59 60 cleanedContainerID := stripTrailingCharacters(out) 61 62 inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID) 63 out, _, err = runCommandWithOutput(inspectCmd) 64 if err != nil { 65 t.Fatalf("output should've been a container id: %s %s ", cleanedContainerID, err) 66 } 67 68 exportCmd := exec.Command(dockerBinary, "export", "--output=testexp.tar", cleanedContainerID) 69 if out, _, err = runCommandWithOutput(exportCmd); err != nil { 70 t.Fatalf("failed to export container: %s, %v", out, err) 71 } 72 73 out, _, err = runCommandWithOutput(exec.Command("cat", "testexp.tar")) 74 if err != nil { 75 t.Fatal(out, err) 76 } 77 78 importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1") 79 importCmd.Stdin = strings.NewReader(out) 80 out, _, err = runCommandWithOutput(importCmd) 81 if err != nil { 82 t.Fatalf("failed to import image: %s, %v", out, err) 83 } 84 85 cleanedImageID := stripTrailingCharacters(out) 86 87 inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID) 88 if out, _, err = runCommandWithOutput(inspectCmd); err != nil { 89 t.Fatalf("output should've been an image id: %s, %v", out, err) 90 } 91 92 deleteContainer(cleanedContainerID) 93 deleteImages("repo/testexp:v1") 94 95 os.Remove("/tmp/testexp.tar") 96 97 logDone("export - export a container with output flag") 98 logDone("import - import an image with output flag") 99 }