github.com/afein/docker@v1.8.2/integration-cli/docker_cli_import_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/exec"
     8  	"strings"
     9  
    10  	"github.com/go-check/check"
    11  )
    12  
    13  func (s *DockerSuite) TestImportDisplay(c *check.C) {
    14  	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
    15  	cleanedContainerID := strings.TrimSpace(out)
    16  
    17  	out, _, err := runCommandPipelineWithOutput(
    18  		exec.Command(dockerBinary, "export", cleanedContainerID),
    19  		exec.Command(dockerBinary, "import", "-"),
    20  	)
    21  	if err != nil {
    22  		c.Errorf("import failed with errors: %v, output: %q", err, out)
    23  	}
    24  
    25  	if n := strings.Count(out, "\n"); n != 1 {
    26  		c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out)
    27  	}
    28  	image := strings.TrimSpace(out)
    29  
    30  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
    31  	if out != "" {
    32  		c.Fatalf("command output should've been nothing, was %q", out)
    33  	}
    34  }
    35  
    36  func (s *DockerSuite) TestImportBadURL(c *check.C) {
    37  	out, _, err := dockerCmdWithError(c, "import", "http://nourl/bad")
    38  	if err == nil {
    39  		c.Fatal("import was supposed to fail but didn't")
    40  	}
    41  	if !strings.Contains(out, "dial tcp") {
    42  		c.Fatalf("expected an error msg but didn't get one:\n%s", out)
    43  	}
    44  }
    45  
    46  func (s *DockerSuite) TestImportFile(c *check.C) {
    47  	dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
    48  
    49  	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
    50  	if err != nil {
    51  		c.Fatal("failed to create temporary file", "", err)
    52  	}
    53  	defer os.Remove(temporaryFile.Name())
    54  
    55  	runCmd := exec.Command(dockerBinary, "export", "test-import")
    56  	runCmd.Stdout = bufio.NewWriter(temporaryFile)
    57  
    58  	_, err = runCommand(runCmd)
    59  	if err != nil {
    60  		c.Fatal("failed to export a container", err)
    61  	}
    62  
    63  	out, _ := dockerCmd(c, "import", temporaryFile.Name())
    64  	if n := strings.Count(out, "\n"); n != 1 {
    65  		c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out)
    66  	}
    67  	image := strings.TrimSpace(out)
    68  
    69  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
    70  	if out != "" {
    71  		c.Fatalf("command output should've been nothing, was %q", out)
    72  	}
    73  }
    74  
    75  func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
    76  	_, exitCode, err := dockerCmdWithError(c, "import", "example.com/myImage.tar")
    77  	if exitCode == 0 || err == nil {
    78  		c.Fatalf("import non-existing file must failed")
    79  	}
    80  }