github.com/damirazo/docker@v1.9.0/integration-cli/docker_cli_import_test.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "io/ioutil" 6 "os" 7 "os/exec" 8 "regexp" 9 "strings" 10 11 "github.com/go-check/check" 12 ) 13 14 func (s *DockerSuite) TestImportDisplay(c *check.C) { 15 testRequires(c, DaemonIsLinux) 16 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") 17 cleanedContainerID := strings.TrimSpace(out) 18 19 out, _, err := runCommandPipelineWithOutput( 20 exec.Command(dockerBinary, "export", cleanedContainerID), 21 exec.Command(dockerBinary, "import", "-"), 22 ) 23 if err != nil { 24 c.Errorf("import failed with errors: %v, output: %q", err, out) 25 } 26 27 if n := strings.Count(out, "\n"); n != 1 { 28 c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out) 29 } 30 image := strings.TrimSpace(out) 31 32 out, _ = dockerCmd(c, "run", "--rm", image, "true") 33 if out != "" { 34 c.Fatalf("command output should've been nothing, was %q", out) 35 } 36 } 37 38 func (s *DockerSuite) TestImportBadURL(c *check.C) { 39 testRequires(c, DaemonIsLinux) 40 out, _, err := dockerCmdWithError("import", "http://nourl/bad") 41 if err == nil { 42 c.Fatal("import was supposed to fail but didn't") 43 } 44 if !strings.Contains(out, "dial tcp") { 45 c.Fatalf("expected an error msg but didn't get one:\n%s", out) 46 } 47 } 48 49 func (s *DockerSuite) TestImportFile(c *check.C) { 50 testRequires(c, DaemonIsLinux) 51 dockerCmd(c, "run", "--name", "test-import", "busybox", "true") 52 53 temporaryFile, err := ioutil.TempFile("", "exportImportTest") 54 if err != nil { 55 c.Fatal("failed to create temporary file", "", err) 56 } 57 defer os.Remove(temporaryFile.Name()) 58 59 runCmd := exec.Command(dockerBinary, "export", "test-import") 60 runCmd.Stdout = bufio.NewWriter(temporaryFile) 61 62 _, err = runCommand(runCmd) 63 if err != nil { 64 c.Fatal("failed to export a container", err) 65 } 66 67 out, _ := dockerCmd(c, "import", temporaryFile.Name()) 68 if n := strings.Count(out, "\n"); n != 1 { 69 c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out) 70 } 71 image := strings.TrimSpace(out) 72 73 out, _ = dockerCmd(c, "run", "--rm", image, "true") 74 if out != "" { 75 c.Fatalf("command output should've been nothing, was %q", out) 76 } 77 } 78 79 func (s *DockerSuite) TestImportFileWithMessage(c *check.C) { 80 testRequires(c, DaemonIsLinux) 81 dockerCmd(c, "run", "--name", "test-import", "busybox", "true") 82 83 temporaryFile, err := ioutil.TempFile("", "exportImportTest") 84 if err != nil { 85 c.Fatal("failed to create temporary file", "", err) 86 } 87 defer os.Remove(temporaryFile.Name()) 88 89 runCmd := exec.Command(dockerBinary, "export", "test-import") 90 runCmd.Stdout = bufio.NewWriter(temporaryFile) 91 92 _, err = runCommand(runCmd) 93 if err != nil { 94 c.Fatal("failed to export a container", err) 95 } 96 97 message := "Testing commit message" 98 out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name()) 99 if n := strings.Count(out, "\n"); n != 1 { 100 c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out) 101 } 102 image := strings.TrimSpace(out) 103 104 out, _ = dockerCmd(c, "history", image) 105 split := strings.Split(out, "\n") 106 107 if len(split) != 3 { 108 c.Fatalf("expected 3 lines from image history, got %d", len(split)) 109 } 110 r := regexp.MustCompile("[\\s]{2,}") 111 split = r.Split(split[1], -1) 112 113 if message != split[3] { 114 c.Fatalf("expected %s in commit message, got %s", message, split[3]) 115 } 116 117 out, _ = dockerCmd(c, "run", "--rm", image, "true") 118 if out != "" { 119 c.Fatalf("command output should've been nothing, was %q", out) 120 } 121 } 122 123 func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) { 124 _, exitCode, err := dockerCmdWithError("import", "example.com/myImage.tar") 125 if exitCode == 0 || err == nil { 126 c.Fatalf("import non-existing file must failed") 127 } 128 }