integration-cli/docker_cli_import_test.go
b430f4f4
 package main
 
 import (
3f64b76d
 	"bufio"
 	"io/ioutil"
 	"os"
b430f4f4
 	"os/exec"
936b2c6a
 	"regexp"
b430f4f4
 	"strings"
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", "-"),
 	)
3ec564bf
 	if err != nil {
dc944ea7
 		c.Errorf("import failed with errors: %v, output: %q", err, out)
3ec564bf
 	}
b430f4f4
 
c7bec928
 	if n := strings.Count(out, "\n"); n != 1 {
dc944ea7
 		c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out)
b430f4f4
 	}
c7bec928
 	image := strings.TrimSpace(out)
b430f4f4
 
668e2369
 	out, _ = dockerCmd(c, "run", "--rm", image, "true")
c7bec928
 	if out != "" {
dc944ea7
 		c.Fatalf("command output should've been nothing, was %q", out)
c7bec928
 	}
b430f4f4
 }
3f4926e4
 
 func (s *DockerSuite) TestImportBadURL(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	out, _, err := dockerCmdWithError("import", "http://nourl/bad")
3f4926e4
 	if err == nil {
 		c.Fatal("import was supposed to fail but didn't")
 	}
 	if !strings.Contains(out, "dial tcp") {
 		c.Fatalf("expected an error msg but didn't get one:\n%s", out)
 	}
 }
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")
 	if err != nil {
 		c.Fatal("failed to create temporary file", "", err)
 	}
 	defer os.Remove(temporaryFile.Name())
 
668e2369
 	runCmd := exec.Command(dockerBinary, "export", "test-import")
3f64b76d
 	runCmd.Stdout = bufio.NewWriter(temporaryFile)
 
 	_, err = runCommand(runCmd)
 	if err != nil {
668e2369
 		c.Fatal("failed to export a container", err)
3f64b76d
 	}
 
668e2369
 	out, _ := dockerCmd(c, "import", temporaryFile.Name())
3f64b76d
 	if n := strings.Count(out, "\n"); n != 1 {
 		c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out)
 	}
 	image := strings.TrimSpace(out)
 
668e2369
 	out, _ = dockerCmd(c, "run", "--rm", image, "true")
3f64b76d
 	if out != "" {
 		c.Fatalf("command output should've been nothing, was %q", out)
 	}
 }
 
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")
 	if err != nil {
 		c.Fatal("failed to create temporary file", "", err)
 	}
 	defer os.Remove(temporaryFile.Name())
 
 	runCmd := exec.Command(dockerBinary, "export", "test-import")
 	runCmd.Stdout = bufio.NewWriter(temporaryFile)
 
 	_, err = runCommand(runCmd)
 	if err != nil {
 		c.Fatal("failed to export a container", err)
 	}
 
 	message := "Testing commit message"
 	out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
 	if n := strings.Count(out, "\n"); n != 1 {
 		c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out)
 	}
 	image := strings.TrimSpace(out)
 
 	out, _ = dockerCmd(c, "history", image)
 	split := strings.Split(out, "\n")
 
 	if len(split) != 3 {
 		c.Fatalf("expected 3 lines from image history, got %d", len(split))
 	}
 	r := regexp.MustCompile("[\\s]{2,}")
 	split = r.Split(split[1], -1)
 
 	if message != split[3] {
 		c.Fatalf("expected %s in commit message, got %s", message, split[3])
 	}
 
 	out, _ = dockerCmd(c, "run", "--rm", image, "true")
 	if out != "" {
 		c.Fatalf("command output should've been nothing, was %q", out)
 	}
 }
 
3f64b76d
 func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
693ba98c
 	_, exitCode, err := dockerCmdWithError("import", "example.com/myImage.tar")
3f64b76d
 	if exitCode == 0 || err == nil {
 		c.Fatalf("import non-existing file must failed")
 	}
 }