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
 
288214e5
 	"github.com/docker/docker/pkg/integration/checker"
dc944ea7
 	"github.com/go-check/check"
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
 
668e2369
 	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) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
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") &&
 		!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())
 
668e2369
 	runCmd := exec.Command(dockerBinary, "export", "test-import")
3f64b76d
 	runCmd.Stdout = bufio.NewWriter(temporaryFile)
 
 	_, err = runCommand(runCmd)
288214e5
 	c.Assert(err, checker.IsNil, check.Commentf("failed to export a container"))
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())
 
 	runCmd := exec.Command(dockerBinary, "export", "test-import")
 	w := gzip.NewWriter(temporaryFile)
 	runCmd.Stdout = w
 
 	_, err = runCommand(runCmd)
 	c.Assert(err, checker.IsNil, check.Commentf("failed to export a container"))
 	err = w.Close()
 	c.Assert(err, checker.IsNil, check.Commentf("failed to close gzip writer"))
 	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())
 
 	runCmd := exec.Command(dockerBinary, "export", "test-import")
 	runCmd.Stdout = bufio.NewWriter(temporaryFile)
 
 	_, err = runCommand(runCmd)
288214e5
 	c.Assert(err, checker.IsNil, check.Commentf("failed to export a container"))
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
 }