2014-03-31 15:31:21 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-03-29 13:42:11 -04:00
|
|
|
"bufio"
|
2016-02-16 14:19:23 -05:00
|
|
|
"compress/gzip"
|
2015-03-29 13:42:11 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-03-31 15:31:21 -04:00
|
|
|
"os/exec"
|
2015-08-20 00:01:50 -04:00
|
|
|
"regexp"
|
2014-03-31 15:31:21 -04:00
|
|
|
"strings"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2017-03-27 11:12:48 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/pkg/testutil"
|
|
|
|
icmd "github.com/docker/docker/pkg/testutil/cmd"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2014-03-31 15:31:21 -04:00
|
|
|
)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestImportDisplay(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-02-14 05:27:06 -05:00
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
out, _, err := testutil.RunCommandPipelineWithOutput(
|
2015-02-14 05:27:06 -05:00
|
|
|
exec.Command(dockerBinary, "export", cleanedContainerID),
|
|
|
|
exec.Command(dockerBinary, "import", "-"),
|
|
|
|
)
|
2015-10-20 06:27:52 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-03-31 15:31:21 -04:00
|
|
|
|
2015-10-20 06:27:52 -04:00
|
|
|
c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
|
2014-03-31 15:31:21 -04:00
|
|
|
|
2015-10-20 06:27:52 -04:00
|
|
|
image := strings.TrimSpace(out)
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ = dockerCmd(c, "run", "--rm", image, "true")
|
2015-10-20 06:27:52 -04:00
|
|
|
c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
|
2014-03-31 15:31:21 -04:00
|
|
|
}
|
2015-05-13 20:21:02 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestImportBadURL(c *check.C) {
|
2015-07-27 14:13:25 -04:00
|
|
|
out, _, err := dockerCmdWithError("import", "http://nourl/bad")
|
2015-10-20 06:27:52 -04:00
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("import was supposed to fail but didn't"))
|
2016-05-23 13:00:01 -04:00
|
|
|
// Depending on your system you can get either of these errors
|
|
|
|
if !strings.Contains(out, "dial tcp") &&
|
2016-08-27 17:57:50 -04:00
|
|
|
!strings.Contains(out, "ApplyLayer exit status 1 stdout: stderr: archive/tar: invalid tar header") &&
|
2016-05-23 13:00:01 -04:00
|
|
|
!strings.Contains(out, "Error processing tar file") {
|
|
|
|
c.Fatalf("expected an error msg but didn't get one.\nErr: %v\nOut: %v", err, out)
|
|
|
|
}
|
2015-05-13 20:21:02 -04:00
|
|
|
}
|
2015-03-29 13:42:11 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestImportFile(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 02:55:40 -04:00
|
|
|
dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
|
2015-03-29 13:42:11 -04:00
|
|
|
|
|
|
|
temporaryFile, err := ioutil.TempFile("", "exportImportTest")
|
2015-10-20 06:27:52 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
|
2015-03-29 13:42:11 -04:00
|
|
|
defer os.Remove(temporaryFile.Name())
|
|
|
|
|
2016-12-13 15:21:51 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "export", "test-import"},
|
|
|
|
Stdout: bufio.NewWriter(temporaryFile),
|
|
|
|
}).Assert(c, icmd.Success)
|
2015-03-29 13:42:11 -04:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "import", temporaryFile.Name())
|
2015-10-20 06:27:52 -04:00
|
|
|
c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
|
2015-03-29 13:42:11 -04:00
|
|
|
image := strings.TrimSpace(out)
|
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ = dockerCmd(c, "run", "--rm", image, "true")
|
2015-10-20 06:27:52 -04:00
|
|
|
c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
|
2015-03-29 13:42:11 -04:00
|
|
|
}
|
|
|
|
|
2016-02-16 14:19:23 -05:00
|
|
|
func (s *DockerSuite) TestImportGzipped(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
|
|
|
|
|
|
|
|
temporaryFile, err := ioutil.TempFile("", "exportImportTest")
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
|
|
|
|
defer os.Remove(temporaryFile.Name())
|
|
|
|
|
|
|
|
w := gzip.NewWriter(temporaryFile)
|
2016-12-13 15:21:51 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "export", "test-import"},
|
|
|
|
Stdout: w,
|
|
|
|
}).Assert(c, icmd.Success)
|
|
|
|
c.Assert(w.Close(), checker.IsNil, check.Commentf("failed to close gzip writer"))
|
2016-02-16 14:19:23 -05:00
|
|
|
temporaryFile.Close()
|
|
|
|
out, _ := dockerCmd(c, "import", temporaryFile.Name())
|
|
|
|
c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
|
|
|
|
image := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "run", "--rm", image, "true")
|
|
|
|
c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
|
|
|
|
}
|
|
|
|
|
2015-08-20 00:01:50 -04:00
|
|
|
func (s *DockerSuite) TestImportFileWithMessage(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-08-20 00:01:50 -04:00
|
|
|
dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
|
|
|
|
|
|
|
|
temporaryFile, err := ioutil.TempFile("", "exportImportTest")
|
2015-10-20 06:27:52 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
|
2015-08-20 00:01:50 -04:00
|
|
|
defer os.Remove(temporaryFile.Name())
|
|
|
|
|
2016-12-13 15:21:51 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "export", "test-import"},
|
|
|
|
Stdout: bufio.NewWriter(temporaryFile),
|
|
|
|
}).Assert(c, icmd.Success)
|
2015-08-20 00:01:50 -04:00
|
|
|
|
|
|
|
message := "Testing commit message"
|
|
|
|
out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
|
2015-10-20 06:27:52 -04:00
|
|
|
c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
|
2015-08-20 00:01:50 -04:00
|
|
|
image := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "history", image)
|
|
|
|
split := strings.Split(out, "\n")
|
|
|
|
|
2015-10-20 06:27:52 -04:00
|
|
|
c.Assert(split, checker.HasLen, 3, check.Commentf("expected 3 lines from image history"))
|
2015-08-20 00:01:50 -04:00
|
|
|
r := regexp.MustCompile("[\\s]{2,}")
|
|
|
|
split = r.Split(split[1], -1)
|
|
|
|
|
2015-10-20 06:27:52 -04:00
|
|
|
c.Assert(message, checker.Equals, split[3], check.Commentf("didn't get expected value in commit message"))
|
2015-08-20 00:01:50 -04:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "run", "--rm", image, "true")
|
2015-10-20 06:27:52 -04:00
|
|
|
c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing"))
|
2015-08-20 00:01:50 -04:00
|
|
|
}
|
|
|
|
|
2015-03-29 13:42:11 -04:00
|
|
|
func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
|
2015-10-20 06:27:52 -04:00
|
|
|
_, _, err := dockerCmdWithError("import", "example.com/myImage.tar")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("import non-existing file must failed"))
|
2015-03-29 13:42:11 -04:00
|
|
|
}
|
2016-08-31 18:48:53 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestImportWithQuotedChanges(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-03-27 11:12:48 -04:00
|
|
|
cli.DockerCmd(c, "run", "--name", "test-import", "busybox", "true")
|
2016-08-31 18:48:53 -04:00
|
|
|
|
|
|
|
temporaryFile, err := ioutil.TempFile("", "exportImportTest")
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
|
|
|
|
defer os.Remove(temporaryFile.Name())
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
cli.Docker(cli.Args("export", "test-import"), cli.WithStdout(bufio.NewWriter(temporaryFile))).Assert(c, icmd.Success)
|
2016-08-31 18:48:53 -04:00
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
result := cli.DockerCmd(c, "import", "-c", `ENTRYPOINT ["/bin/sh", "-c"]`, temporaryFile.Name())
|
2016-08-31 18:48:53 -04:00
|
|
|
image := strings.TrimSpace(result.Stdout())
|
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
result = cli.DockerCmd(c, "run", "--rm", image, "true")
|
2016-08-31 18:48:53 -04:00
|
|
|
c.Assert(result, icmd.Matches, icmd.Expected{Out: icmd.None})
|
|
|
|
}
|