github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/integration-cli/docker_cli_export_import_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"strings"
     7  
     8  	"github.com/go-check/check"
     9  )
    10  
    11  // export an image and try to import it into a new one
    12  func (s *DockerSuite) TestExportContainerAndImportImage(c *check.C) {
    13  	containerID := "testexportcontainerandimportimage"
    14  
    15  	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", containerID, "busybox", "true")
    16  	out, _, err := runCommandWithOutput(runCmd)
    17  	if err != nil {
    18  		c.Fatal("failed to create a container", out, err)
    19  	}
    20  
    21  	inspectCmd := exec.Command(dockerBinary, "inspect", containerID)
    22  	out, _, err = runCommandWithOutput(inspectCmd)
    23  	if err != nil {
    24  		c.Fatalf("output should've been a container id: %s %s ", containerID, err)
    25  	}
    26  
    27  	exportCmd := exec.Command(dockerBinary, "export", containerID)
    28  	if out, _, err = runCommandWithOutput(exportCmd); err != nil {
    29  		c.Fatalf("failed to export container: %s, %v", out, err)
    30  	}
    31  
    32  	importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
    33  	importCmd.Stdin = strings.NewReader(out)
    34  	out, _, err = runCommandWithOutput(importCmd)
    35  	if err != nil {
    36  		c.Fatalf("failed to import image: %s, %v", out, err)
    37  	}
    38  
    39  	cleanedImageID := strings.TrimSpace(out)
    40  
    41  	inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
    42  	if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
    43  		c.Fatalf("output should've been an image id: %s, %v", out, err)
    44  	}
    45  
    46  }
    47  
    48  // Used to test output flag in the export command
    49  func (s *DockerSuite) TestExportContainerWithOutputAndImportImage(c *check.C) {
    50  	containerID := "testexportcontainerwithoutputandimportimage"
    51  
    52  	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", containerID, "busybox", "true")
    53  	out, _, err := runCommandWithOutput(runCmd)
    54  	if err != nil {
    55  		c.Fatal("failed to create a container", out, err)
    56  	}
    57  
    58  	inspectCmd := exec.Command(dockerBinary, "inspect", containerID)
    59  	out, _, err = runCommandWithOutput(inspectCmd)
    60  	if err != nil {
    61  		c.Fatalf("output should've been a container id: %s %s ", containerID, err)
    62  	}
    63  
    64  	defer os.Remove("testexp.tar")
    65  
    66  	exportCmd := exec.Command(dockerBinary, "export", "--output=testexp.tar", containerID)
    67  	if out, _, err = runCommandWithOutput(exportCmd); err != nil {
    68  		c.Fatalf("failed to export container: %s, %v", out, err)
    69  	}
    70  
    71  	out, _, err = runCommandWithOutput(exec.Command("cat", "testexp.tar"))
    72  	if err != nil {
    73  		c.Fatal(out, err)
    74  	}
    75  
    76  	importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
    77  	importCmd.Stdin = strings.NewReader(out)
    78  	out, _, err = runCommandWithOutput(importCmd)
    79  	if err != nil {
    80  		c.Fatalf("failed to import image: %s, %v", out, err)
    81  	}
    82  
    83  	cleanedImageID := strings.TrimSpace(out)
    84  
    85  	inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
    86  	if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
    87  		c.Fatalf("output should've been an image id: %s, %v", out, err)
    88  	}
    89  
    90  }