github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/integration-cli/docker_cli_import_test.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "compress/gzip" 6 "io/ioutil" 7 "os" 8 "os/exec" 9 "regexp" 10 "strings" 11 12 "github.com/docker/docker/integration-cli/checker" 13 "github.com/docker/docker/pkg/testutil" 14 icmd "github.com/docker/docker/pkg/testutil/cmd" 15 "github.com/go-check/check" 16 ) 17 18 func (s *DockerSuite) TestImportDisplay(c *check.C) { 19 testRequires(c, DaemonIsLinux) 20 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") 21 cleanedContainerID := strings.TrimSpace(out) 22 23 out, _, err := testutil.RunCommandPipelineWithOutput( 24 exec.Command(dockerBinary, "export", cleanedContainerID), 25 exec.Command(dockerBinary, "import", "-"), 26 ) 27 c.Assert(err, checker.IsNil) 28 29 c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't")) 30 31 image := strings.TrimSpace(out) 32 out, _ = dockerCmd(c, "run", "--rm", image, "true") 33 c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing.")) 34 } 35 36 func (s *DockerSuite) TestImportBadURL(c *check.C) { 37 out, _, err := dockerCmdWithError("import", "http://nourl/bad") 38 c.Assert(err, checker.NotNil, check.Commentf("import was supposed to fail but didn't")) 39 // Depending on your system you can get either of these errors 40 if !strings.Contains(out, "dial tcp") && 41 !strings.Contains(out, "ApplyLayer exit status 1 stdout: stderr: archive/tar: invalid tar header") && 42 !strings.Contains(out, "Error processing tar file") { 43 c.Fatalf("expected an error msg but didn't get one.\nErr: %v\nOut: %v", err, out) 44 } 45 } 46 47 func (s *DockerSuite) TestImportFile(c *check.C) { 48 testRequires(c, DaemonIsLinux) 49 dockerCmd(c, "run", "--name", "test-import", "busybox", "true") 50 51 temporaryFile, err := ioutil.TempFile("", "exportImportTest") 52 c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file")) 53 defer os.Remove(temporaryFile.Name()) 54 55 icmd.RunCmd(icmd.Cmd{ 56 Command: []string{dockerBinary, "export", "test-import"}, 57 Stdout: bufio.NewWriter(temporaryFile), 58 }).Assert(c, icmd.Success) 59 60 out, _ := dockerCmd(c, "import", temporaryFile.Name()) 61 c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't")) 62 image := strings.TrimSpace(out) 63 64 out, _ = dockerCmd(c, "run", "--rm", image, "true") 65 c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing.")) 66 } 67 68 func (s *DockerSuite) TestImportGzipped(c *check.C) { 69 testRequires(c, DaemonIsLinux) 70 dockerCmd(c, "run", "--name", "test-import", "busybox", "true") 71 72 temporaryFile, err := ioutil.TempFile("", "exportImportTest") 73 c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file")) 74 defer os.Remove(temporaryFile.Name()) 75 76 w := gzip.NewWriter(temporaryFile) 77 icmd.RunCmd(icmd.Cmd{ 78 Command: []string{dockerBinary, "export", "test-import"}, 79 Stdout: w, 80 }).Assert(c, icmd.Success) 81 c.Assert(w.Close(), checker.IsNil, check.Commentf("failed to close gzip writer")) 82 temporaryFile.Close() 83 out, _ := dockerCmd(c, "import", temporaryFile.Name()) 84 c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't")) 85 image := strings.TrimSpace(out) 86 87 out, _ = dockerCmd(c, "run", "--rm", image, "true") 88 c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing.")) 89 } 90 91 func (s *DockerSuite) TestImportFileWithMessage(c *check.C) { 92 testRequires(c, DaemonIsLinux) 93 dockerCmd(c, "run", "--name", "test-import", "busybox", "true") 94 95 temporaryFile, err := ioutil.TempFile("", "exportImportTest") 96 c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file")) 97 defer os.Remove(temporaryFile.Name()) 98 99 icmd.RunCmd(icmd.Cmd{ 100 Command: []string{dockerBinary, "export", "test-import"}, 101 Stdout: bufio.NewWriter(temporaryFile), 102 }).Assert(c, icmd.Success) 103 104 message := "Testing commit message" 105 out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name()) 106 c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't")) 107 image := strings.TrimSpace(out) 108 109 out, _ = dockerCmd(c, "history", image) 110 split := strings.Split(out, "\n") 111 112 c.Assert(split, checker.HasLen, 3, check.Commentf("expected 3 lines from image history")) 113 r := regexp.MustCompile("[\\s]{2,}") 114 split = r.Split(split[1], -1) 115 116 c.Assert(message, checker.Equals, split[3], check.Commentf("didn't get expected value in commit message")) 117 118 out, _ = dockerCmd(c, "run", "--rm", image, "true") 119 c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing")) 120 } 121 122 func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) { 123 _, _, err := dockerCmdWithError("import", "example.com/myImage.tar") 124 c.Assert(err, checker.NotNil, check.Commentf("import non-existing file must failed")) 125 } 126 127 func (s *DockerSuite) TestImportWithQuotedChanges(c *check.C) { 128 testRequires(c, DaemonIsLinux) 129 dockerCmd(c, "run", "--name", "test-import", "busybox", "true") 130 131 temporaryFile, err := ioutil.TempFile("", "exportImportTest") 132 c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file")) 133 defer os.Remove(temporaryFile.Name()) 134 135 result := icmd.RunCmd(icmd.Cmd{ 136 Command: binaryWithArgs("export", "test-import"), 137 Stdout: bufio.NewWriter(temporaryFile), 138 }) 139 c.Assert(result, icmd.Matches, icmd.Success) 140 141 result = dockerCmdWithResult("import", "-c", `ENTRYPOINT ["/bin/sh", "-c"]`, temporaryFile.Name()) 142 c.Assert(result, icmd.Matches, icmd.Success) 143 image := strings.TrimSpace(result.Stdout()) 144 145 result = dockerCmdWithResult("run", "--rm", image, "true") 146 c.Assert(result, icmd.Matches, icmd.Expected{Out: icmd.None}) 147 }