github.com/michael-k/docker@v1.7.0-rc2/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", "--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  	exportCmd := exec.Command(dockerBinary, "export", containerID)
    22  	if out, _, err = runCommandWithOutput(exportCmd); err != nil {
    23  		c.Fatalf("failed to export container: %s, %v", out, err)
    24  	}
    25  
    26  	importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
    27  	importCmd.Stdin = strings.NewReader(out)
    28  	out, _, err = runCommandWithOutput(importCmd)
    29  	if err != nil {
    30  		c.Fatalf("failed to import image: %s, %v", out, err)
    31  	}
    32  
    33  	cleanedImageID := strings.TrimSpace(out)
    34  	if cleanedImageID == "" {
    35  		c.Fatalf("output should have been an image id, got: %s", out)
    36  	}
    37  }
    38  
    39  // Used to test output flag in the export command
    40  func (s *DockerSuite) TestExportContainerWithOutputAndImportImage(c *check.C) {
    41  	containerID := "testexportcontainerwithoutputandimportimage"
    42  
    43  	runCmd := exec.Command(dockerBinary, "run", "--name", containerID, "busybox", "true")
    44  	out, _, err := runCommandWithOutput(runCmd)
    45  	if err != nil {
    46  		c.Fatal("failed to create a container", out, err)
    47  	}
    48  
    49  	defer os.Remove("testexp.tar")
    50  
    51  	exportCmd := exec.Command(dockerBinary, "export", "--output=testexp.tar", containerID)
    52  	if out, _, err = runCommandWithOutput(exportCmd); err != nil {
    53  		c.Fatalf("failed to export container: %s, %v", out, err)
    54  	}
    55  
    56  	out, _, err = runCommandWithOutput(exec.Command("cat", "testexp.tar"))
    57  	if err != nil {
    58  		c.Fatal(out, err)
    59  	}
    60  
    61  	importCmd := exec.Command(dockerBinary, "import", "-", "repo/testexp:v1")
    62  	importCmd.Stdin = strings.NewReader(out)
    63  	out, _, err = runCommandWithOutput(importCmd)
    64  	if err != nil {
    65  		c.Fatalf("failed to import image: %s, %v", out, err)
    66  	}
    67  
    68  	cleanedImageID := strings.TrimSpace(out)
    69  	if cleanedImageID == "" {
    70  		c.Fatalf("output should have been an image id, got: %s", out)
    71  	}
    72  }