github.com/boynux/docker@v1.11.0-rc4/integration-cli/docker_cli_import_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"compress/gzip"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"regexp"
    10  	"strings"
    11  
    12  	"github.com/docker/docker/pkg/integration/checker"
    13  	"github.com/go-check/check"
    14  )
    15  
    16  func (s *DockerSuite) TestImportDisplay(c *check.C) {
    17  	testRequires(c, DaemonIsLinux)
    18  	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
    19  	cleanedContainerID := strings.TrimSpace(out)
    20  
    21  	out, _, err := runCommandPipelineWithOutput(
    22  		exec.Command(dockerBinary, "export", cleanedContainerID),
    23  		exec.Command(dockerBinary, "import", "-"),
    24  	)
    25  	c.Assert(err, checker.IsNil)
    26  
    27  	c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
    28  
    29  	image := strings.TrimSpace(out)
    30  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
    31  	c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
    32  }
    33  
    34  func (s *DockerSuite) TestImportBadURL(c *check.C) {
    35  	testRequires(c, DaemonIsLinux)
    36  	out, _, err := dockerCmdWithError("import", "http://nourl/bad")
    37  	c.Assert(err, checker.NotNil, check.Commentf("import was supposed to fail but didn't"))
    38  	c.Assert(out, checker.Contains, "dial tcp", check.Commentf("expected an error msg but didn't get one"))
    39  }
    40  
    41  func (s *DockerSuite) TestImportFile(c *check.C) {
    42  	testRequires(c, DaemonIsLinux)
    43  	dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
    44  
    45  	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
    46  	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
    47  	defer os.Remove(temporaryFile.Name())
    48  
    49  	runCmd := exec.Command(dockerBinary, "export", "test-import")
    50  	runCmd.Stdout = bufio.NewWriter(temporaryFile)
    51  
    52  	_, err = runCommand(runCmd)
    53  	c.Assert(err, checker.IsNil, check.Commentf("failed to export a container"))
    54  
    55  	out, _ := dockerCmd(c, "import", temporaryFile.Name())
    56  	c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
    57  	image := strings.TrimSpace(out)
    58  
    59  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
    60  	c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
    61  }
    62  
    63  func (s *DockerSuite) TestImportGzipped(c *check.C) {
    64  	testRequires(c, DaemonIsLinux)
    65  	dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
    66  
    67  	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
    68  	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
    69  	defer os.Remove(temporaryFile.Name())
    70  
    71  	runCmd := exec.Command(dockerBinary, "export", "test-import")
    72  	w := gzip.NewWriter(temporaryFile)
    73  	runCmd.Stdout = w
    74  
    75  	_, err = runCommand(runCmd)
    76  	c.Assert(err, checker.IsNil, check.Commentf("failed to export a container"))
    77  	err = w.Close()
    78  	c.Assert(err, checker.IsNil, check.Commentf("failed to close gzip writer"))
    79  	temporaryFile.Close()
    80  	out, _ := dockerCmd(c, "import", temporaryFile.Name())
    81  	c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
    82  	image := strings.TrimSpace(out)
    83  
    84  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
    85  	c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
    86  }
    87  
    88  func (s *DockerSuite) TestImportFileWithMessage(c *check.C) {
    89  	testRequires(c, DaemonIsLinux)
    90  	dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
    91  
    92  	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
    93  	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
    94  	defer os.Remove(temporaryFile.Name())
    95  
    96  	runCmd := exec.Command(dockerBinary, "export", "test-import")
    97  	runCmd.Stdout = bufio.NewWriter(temporaryFile)
    98  
    99  	_, err = runCommand(runCmd)
   100  	c.Assert(err, checker.IsNil, check.Commentf("failed to export a container"))
   101  
   102  	message := "Testing commit message"
   103  	out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
   104  	c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
   105  	image := strings.TrimSpace(out)
   106  
   107  	out, _ = dockerCmd(c, "history", image)
   108  	split := strings.Split(out, "\n")
   109  
   110  	c.Assert(split, checker.HasLen, 3, check.Commentf("expected 3 lines from image history"))
   111  	r := regexp.MustCompile("[\\s]{2,}")
   112  	split = r.Split(split[1], -1)
   113  
   114  	c.Assert(message, checker.Equals, split[3], check.Commentf("didn't get expected value in commit message"))
   115  
   116  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
   117  	c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing"))
   118  }
   119  
   120  func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
   121  	_, _, err := dockerCmdWithError("import", "example.com/myImage.tar")
   122  	c.Assert(err, checker.NotNil, check.Commentf("import non-existing file must failed"))
   123  }