integration-cli/docker_cli_import_test.go
b430f4f4
 package main
 
 import (
3f64b76d
 	"bufio"
e1c2eb0d
 	"compress/gzip"
3f64b76d
 	"io/ioutil"
 	"os"
b430f4f4
 	"os/exec"
936b2c6a
 	"regexp"
b430f4f4
 	"strings"
dc944ea7
 
33968e6c
 	"github.com/docker/docker/integration-cli/checker"
eeaa6c96
 	"github.com/docker/docker/integration-cli/cli"
dc944ea7
 	"github.com/go-check/check"
92427b3a
 	"github.com/gotestyourself/gotestyourself/icmd"
b430f4f4
 )
 
dc944ea7
 func (s *DockerSuite) TestImportDisplay(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
c7bec928
 
e885af2a
 	out, err := RunCommandPipelineWithOutput(
c7bec928
 		exec.Command(dockerBinary, "export", cleanedContainerID),
 		exec.Command(dockerBinary, "import", "-"),
 	)
288214e5
 	c.Assert(err, checker.IsNil)
b430f4f4
 
288214e5
 	c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
b430f4f4
 
288214e5
 	image := strings.TrimSpace(out)
668e2369
 	out, _ = dockerCmd(c, "run", "--rm", image, "true")
288214e5
 	c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
b430f4f4
 }
3f4926e4
 
 func (s *DockerSuite) TestImportBadURL(c *check.C) {
693ba98c
 	out, _, err := dockerCmdWithError("import", "http://nourl/bad")
288214e5
 	c.Assert(err, checker.NotNil, check.Commentf("import was supposed to fail but didn't"))
ac043c7d
 	// Depending on your system you can get either of these errors
 	if !strings.Contains(out, "dial tcp") &&
e2be3fee
 		!strings.Contains(out, "ApplyLayer exit status 1 stdout:  stderr: archive/tar: invalid tar header") &&
ac043c7d
 		!strings.Contains(out, "Error processing tar file") {
 		c.Fatalf("expected an error msg but didn't get one.\nErr: %v\nOut: %v", err, out)
 	}
3f4926e4
 }
3f64b76d
 
 func (s *DockerSuite) TestImportFile(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
3f64b76d
 
 	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
288214e5
 	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
3f64b76d
 	defer os.Remove(temporaryFile.Name())
 
def13fa2
 	icmd.RunCmd(icmd.Cmd{
 		Command: []string{dockerBinary, "export", "test-import"},
 		Stdout:  bufio.NewWriter(temporaryFile),
 	}).Assert(c, icmd.Success)
3f64b76d
 
668e2369
 	out, _ := dockerCmd(c, "import", temporaryFile.Name())
288214e5
 	c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
3f64b76d
 	image := strings.TrimSpace(out)
 
668e2369
 	out, _ = dockerCmd(c, "run", "--rm", image, "true")
288214e5
 	c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
3f64b76d
 }
 
e1c2eb0d
 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)
def13fa2
 	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"))
e1c2eb0d
 	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."))
 }
 
936b2c6a
 func (s *DockerSuite) TestImportFileWithMessage(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
936b2c6a
 	dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
 
 	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
288214e5
 	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
936b2c6a
 	defer os.Remove(temporaryFile.Name())
 
def13fa2
 	icmd.RunCmd(icmd.Cmd{
 		Command: []string{dockerBinary, "export", "test-import"},
 		Stdout:  bufio.NewWriter(temporaryFile),
 	}).Assert(c, icmd.Success)
936b2c6a
 
 	message := "Testing commit message"
 	out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
288214e5
 	c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
936b2c6a
 	image := strings.TrimSpace(out)
 
 	out, _ = dockerCmd(c, "history", image)
 	split := strings.Split(out, "\n")
 
288214e5
 	c.Assert(split, checker.HasLen, 3, check.Commentf("expected 3 lines from image history"))
936b2c6a
 	r := regexp.MustCompile("[\\s]{2,}")
 	split = r.Split(split[1], -1)
 
288214e5
 	c.Assert(message, checker.Equals, split[3], check.Commentf("didn't get expected value in commit message"))
936b2c6a
 
 	out, _ = dockerCmd(c, "run", "--rm", image, "true")
288214e5
 	c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing"))
936b2c6a
 }
 
3f64b76d
 func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
288214e5
 	_, _, err := dockerCmdWithError("import", "example.com/myImage.tar")
 	c.Assert(err, checker.NotNil, check.Commentf("import non-existing file must failed"))
3f64b76d
 }
a79a2741
 
 func (s *DockerSuite) TestImportWithQuotedChanges(c *check.C) {
 	testRequires(c, DaemonIsLinux)
eeaa6c96
 	cli.DockerCmd(c, "run", "--name", "test-import", "busybox", "true")
a79a2741
 
 	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
 	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
 	defer os.Remove(temporaryFile.Name())
 
eeaa6c96
 	cli.Docker(cli.Args("export", "test-import"), cli.WithStdout(bufio.NewWriter(temporaryFile))).Assert(c, icmd.Success)
a79a2741
 
eeaa6c96
 	result := cli.DockerCmd(c, "import", "-c", `ENTRYPOINT ["/bin/sh", "-c"]`, temporaryFile.Name())
a79a2741
 	image := strings.TrimSpace(result.Stdout())
 
eeaa6c96
 	result = cli.DockerCmd(c, "run", "--rm", image, "true")
92427b3a
 	result.Assert(c, icmd.Expected{Out: icmd.None})
a79a2741
 }