github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/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  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
    15  	out, _, err := runCommandWithOutput(runCmd)
    16  	if err != nil {
    17  		c.Fatal("failed to create a container", out, err)
    18  	}
    19  	cleanedContainerID := strings.TrimSpace(out)
    20  
    21  	out, _, err = runCommandPipelineWithOutput(
    22  		exec.Command(dockerBinary, "export", cleanedContainerID),
    23  		exec.Command(dockerBinary, "import", "-"),
    24  	)
    25  	if err != nil {
    26  		c.Errorf("import failed with errors: %v, output: %q", err, out)
    27  	}
    28  
    29  	if n := strings.Count(out, "\n"); n != 1 {
    30  		c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out)
    31  	}
    32  	image := strings.TrimSpace(out)
    33  
    34  	runCmd = exec.Command(dockerBinary, "run", "--rm", image, "true")
    35  	out, _, err = runCommandWithOutput(runCmd)
    36  	if err != nil {
    37  		c.Fatal("failed to create a container", out, err)
    38  	}
    39  
    40  	if out != "" {
    41  		c.Fatalf("command output should've been nothing, was %q", out)
    42  	}
    43  
    44  }
    45  
    46  func (s *DockerSuite) TestImportBadURL(c *check.C) {
    47  	runCmd := exec.Command(dockerBinary, "import", "http://nourl/bad")
    48  	out, _, err := runCommandWithOutput(runCmd)
    49  	if err == nil {
    50  		c.Fatal("import was supposed to fail but didn't")
    51  	}
    52  	if !strings.Contains(out, "dial tcp") {
    53  		c.Fatalf("expected an error msg but didn't get one:\n%s", out)
    54  	}
    55  }
    56  
    57  func (s *DockerSuite) TestImportFile(c *check.C) {
    58  	runCmd := exec.Command(dockerBinary, "run", "--name", "test-import", "busybox", "true")
    59  	out, _, err := runCommandWithOutput(runCmd)
    60  	if err != nil {
    61  		c.Fatal("failed to create a container", out, err)
    62  	}
    63  
    64  	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
    65  	if err != nil {
    66  		c.Fatal("failed to create temporary file", "", err)
    67  	}
    68  	defer os.Remove(temporaryFile.Name())
    69  
    70  	runCmd = exec.Command(dockerBinary, "export", "test-import")
    71  	runCmd.Stdout = bufio.NewWriter(temporaryFile)
    72  
    73  	_, err = runCommand(runCmd)
    74  	if err != nil {
    75  		c.Fatal("failed to export a container", out, err)
    76  	}
    77  
    78  	runCmd = exec.Command(dockerBinary, "import", temporaryFile.Name())
    79  	out, _, err = runCommandWithOutput(runCmd)
    80  	if err != nil {
    81  		c.Fatal("failed to import a container", out, err)
    82  	}
    83  
    84  	if n := strings.Count(out, "\n"); n != 1 {
    85  		c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out)
    86  	}
    87  	image := strings.TrimSpace(out)
    88  
    89  	runCmd = exec.Command(dockerBinary, "run", "--rm", image, "true")
    90  	out, _, err = runCommandWithOutput(runCmd)
    91  	if err != nil {
    92  		c.Fatal("failed to create a container", out, err)
    93  	}
    94  
    95  	if out != "" {
    96  		c.Fatalf("command output should've been nothing, was %q", out)
    97  	}
    98  
    99  }
   100  
   101  func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
   102  	runCmd := exec.Command(dockerBinary, "import", "example.com/myImage.tar")
   103  	_, exitCode, err := runCommandWithOutput(runCmd)
   104  	if exitCode == 0 || err == nil {
   105  		c.Fatalf("import non-existing file must failed")
   106  	}
   107  
   108  }