github.com/go/docker@v1.12.0-rc2/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  	// Depending on your system you can get either of these errors
    39  	if !strings.Contains(out, "dial tcp") &&
    40  		!strings.Contains(out, "Error processing tar file") {
    41  		c.Fatalf("expected an error msg but didn't get one.\nErr: %v\nOut: %v", err, out)
    42  	}
    43  }
    44  
    45  func (s *DockerSuite) TestImportFile(c *check.C) {
    46  	testRequires(c, DaemonIsLinux)
    47  	dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
    48  
    49  	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
    50  	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
    51  	defer os.Remove(temporaryFile.Name())
    52  
    53  	runCmd := exec.Command(dockerBinary, "export", "test-import")
    54  	runCmd.Stdout = bufio.NewWriter(temporaryFile)
    55  
    56  	_, err = runCommand(runCmd)
    57  	c.Assert(err, checker.IsNil, check.Commentf("failed to export a container"))
    58  
    59  	out, _ := dockerCmd(c, "import", temporaryFile.Name())
    60  	c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
    61  	image := strings.TrimSpace(out)
    62  
    63  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
    64  	c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
    65  }
    66  
    67  func (s *DockerSuite) TestImportGzipped(c *check.C) {
    68  	testRequires(c, DaemonIsLinux)
    69  	dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
    70  
    71  	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
    72  	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
    73  	defer os.Remove(temporaryFile.Name())
    74  
    75  	runCmd := exec.Command(dockerBinary, "export", "test-import")
    76  	w := gzip.NewWriter(temporaryFile)
    77  	runCmd.Stdout = w
    78  
    79  	_, err = runCommand(runCmd)
    80  	c.Assert(err, checker.IsNil, check.Commentf("failed to export a container"))
    81  	err = w.Close()
    82  	c.Assert(err, checker.IsNil, check.Commentf("failed to close gzip writer"))
    83  	temporaryFile.Close()
    84  	out, _ := dockerCmd(c, "import", temporaryFile.Name())
    85  	c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
    86  	image := strings.TrimSpace(out)
    87  
    88  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
    89  	c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
    90  }
    91  
    92  func (s *DockerSuite) TestImportFileWithMessage(c *check.C) {
    93  	testRequires(c, DaemonIsLinux)
    94  	dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
    95  
    96  	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
    97  	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
    98  	defer os.Remove(temporaryFile.Name())
    99  
   100  	runCmd := exec.Command(dockerBinary, "export", "test-import")
   101  	runCmd.Stdout = bufio.NewWriter(temporaryFile)
   102  
   103  	_, err = runCommand(runCmd)
   104  	c.Assert(err, checker.IsNil, check.Commentf("failed to export a container"))
   105  
   106  	message := "Testing commit message"
   107  	out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
   108  	c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
   109  	image := strings.TrimSpace(out)
   110  
   111  	out, _ = dockerCmd(c, "history", image)
   112  	split := strings.Split(out, "\n")
   113  
   114  	c.Assert(split, checker.HasLen, 3, check.Commentf("expected 3 lines from image history"))
   115  	r := regexp.MustCompile("[\\s]{2,}")
   116  	split = r.Split(split[1], -1)
   117  
   118  	c.Assert(message, checker.Equals, split[3], check.Commentf("didn't get expected value in commit message"))
   119  
   120  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
   121  	c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing"))
   122  }
   123  
   124  func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
   125  	_, _, err := dockerCmdWithError("import", "example.com/myImage.tar")
   126  	c.Assert(err, checker.NotNil, check.Commentf("import non-existing file must failed"))
   127  }