12 "github.com/docker/docker/integration-cli/checker"
13 "github.com/docker/docker/integration-cli/cli"
14 "github.com/docker/docker/pkg/testutil"
15 icmd "github.com/docker/docker/pkg/testutil/cmd"
16 "github.com/go-check/check"
19 func (s *DockerSuite) TestImportDisplay(c *check.C) {
20 testRequires(c, DaemonIsLinux)
21 out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
22 cleanedContainerID := strings.TrimSpace(out)
24 out, _, err := testutil.RunCommandPipelineWithOutput(
25 exec.Command(dockerBinary, "export", cleanedContainerID),
26 exec.Command(dockerBinary, "import", "-"),
28 c.Assert(err, checker.IsNil)
30 c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
32 image := strings.TrimSpace(out)
33 out, _ = dockerCmd(c, "run", "--rm", image, "true")
34 c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
37 func (s *DockerSuite) TestImportBadURL(c *check.C) {
38 out, _, err := dockerCmdWithError("import", "http://nourl/bad")
39 c.Assert(err, checker.NotNil, check.Commentf("import was supposed to fail but didn't"))
40 // Depending on your system you can get either of these errors
41 if !strings.Contains(out, "dial tcp") &&
42 !strings.Contains(out, "ApplyLayer exit status 1 stdout: stderr: archive/tar: invalid tar header") &&
43 !strings.Contains(out, "Error processing tar file") {
44 c.Fatalf("expected an error msg but didn't get one.\nErr: %v\nOut: %v", err, out)
48 func (s *DockerSuite) TestImportFile(c *check.C) {
49 testRequires(c, DaemonIsLinux)
50 dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
52 temporaryFile, err := ioutil.TempFile("", "exportImportTest")
53 c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
54 defer os.Remove(temporaryFile.Name())
57 Command: []string{dockerBinary, "export", "test-import"},
58 Stdout: bufio.NewWriter(temporaryFile),
59 }).Assert(c, icmd.Success)
61 out, _ := dockerCmd(c, "import", temporaryFile.Name())
62 c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
63 image := strings.TrimSpace(out)
65 out, _ = dockerCmd(c, "run", "--rm", image, "true")
66 c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
69 func (s *DockerSuite) TestImportGzipped(c *check.C) {
70 testRequires(c, DaemonIsLinux)
71 dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
73 temporaryFile, err := ioutil.TempFile("", "exportImportTest")
74 c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
75 defer os.Remove(temporaryFile.Name())
77 w := gzip.NewWriter(temporaryFile)
79 Command: []string{dockerBinary, "export", "test-import"},
81 }).Assert(c, icmd.Success)
82 c.Assert(w.Close(), checker.IsNil, check.Commentf("failed to close gzip writer"))
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)
88 out, _ = dockerCmd(c, "run", "--rm", image, "true")
89 c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
92 func (s *DockerSuite) TestImportFileWithMessage(c *check.C) {
93 testRequires(c, DaemonIsLinux)
94 dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
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())
100 icmd.RunCmd(icmd.Cmd{
101 Command: []string{dockerBinary, "export", "test-import"},
102 Stdout: bufio.NewWriter(temporaryFile),
103 }).Assert(c, icmd.Success)
105 message := "Testing commit message"
106 out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
107 c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
108 image := strings.TrimSpace(out)
110 out, _ = dockerCmd(c, "history", image)
111 split := strings.Split(out, "\n")
113 c.Assert(split, checker.HasLen, 3, check.Commentf("expected 3 lines from image history"))
114 r := regexp.MustCompile("[\\s]{2,}")
115 split = r.Split(split[1], -1)
117 c.Assert(message, checker.Equals, split[3], check.Commentf("didn't get expected value in commit message"))
119 out, _ = dockerCmd(c, "run", "--rm", image, "true")
120 c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing"))
123 func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
124 _, _, err := dockerCmdWithError("import", "example.com/myImage.tar")
125 c.Assert(err, checker.NotNil, check.Commentf("import non-existing file must failed"))
128 func (s *DockerSuite) TestImportWithQuotedChanges(c *check.C) {
129 testRequires(c, DaemonIsLinux)
130 cli.DockerCmd(c, "run", "--name", "test-import", "busybox", "true")
132 temporaryFile, err := ioutil.TempFile("", "exportImportTest")
133 c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
134 defer os.Remove(temporaryFile.Name())
136 cli.Docker(cli.Args("export", "test-import"), cli.WithStdout(bufio.NewWriter(temporaryFile))).Assert(c, icmd.Success)
138 result := cli.DockerCmd(c, "import", "-c", `ENTRYPOINT ["/bin/sh", "-c"]`, temporaryFile.Name())
139 image := strings.TrimSpace(result.Stdout())
141 result = cli.DockerCmd(c, "run", "--rm", image, "true")
142 c.Assert(result, icmd.Matches, icmd.Expected{Out: icmd.None})