2014-03-31 19:31:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-03-29 17:42:11 +00:00
|
|
|
"bufio"
|
2016-02-16 19:19:23 +00:00
|
|
|
"compress/gzip"
|
2015-03-29 17:42:11 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-03-31 19:31:21 +00:00
|
|
|
"os/exec"
|
2015-08-20 04:01:50 +00:00
|
|
|
"regexp"
|
2014-03-31 19:31:21 +00:00
|
|
|
"strings"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2015-10-20 10:27:52 +00:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-04-18 16:46:47 +00:00
|
|
|
"github.com/go-check/check"
|
2014-03-31 19:31:21 +00:00
|
|
|
)
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestImportDisplay(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
|
2015-04-06 13:21:18 +00:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-02-14 10:27:06 +00:00
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _, err := runCommandPipelineWithOutput(
|
2015-02-14 10:27:06 +00:00
|
|
|
exec.Command(dockerBinary, "export", cleanedContainerID),
|
|
|
|
exec.Command(dockerBinary, "import", "-"),
|
|
|
|
)
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-03-31 19:31:21 +00:00
|
|
|
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
|
2014-03-31 19:31:21 +00:00
|
|
|
|
2015-10-20 10:27:52 +00:00
|
|
|
image := strings.TrimSpace(out)
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ = dockerCmd(c, "run", "--rm", image, "true")
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
|
2014-03-31 19:31:21 +00:00
|
|
|
}
|
2015-05-14 00:21:02 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestImportBadURL(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("import", "http://nourl/bad")
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("import was supposed to fail but didn't"))
|
2016-05-23 17:00:01 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2015-05-14 00:21:02 +00:00
|
|
|
}
|
2015-03-29 17:42:11 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestImportFile(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 06:55:40 +00:00
|
|
|
dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
|
2015-03-29 17:42:11 +00:00
|
|
|
|
|
|
|
temporaryFile, err := ioutil.TempFile("", "exportImportTest")
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
|
2015-03-29 17:42:11 +00:00
|
|
|
defer os.Remove(temporaryFile.Name())
|
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
runCmd := exec.Command(dockerBinary, "export", "test-import")
|
2015-03-29 17:42:11 +00:00
|
|
|
runCmd.Stdout = bufio.NewWriter(temporaryFile)
|
|
|
|
|
|
|
|
_, err = runCommand(runCmd)
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("failed to export a container"))
|
2015-03-29 17:42:11 +00:00
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ := dockerCmd(c, "import", temporaryFile.Name())
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
|
2015-03-29 17:42:11 +00:00
|
|
|
image := strings.TrimSpace(out)
|
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ = dockerCmd(c, "run", "--rm", image, "true")
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
|
2015-03-29 17:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 19:19:23 +00:00
|
|
|
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."))
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:01:50 +00:00
|
|
|
func (s *DockerSuite) TestImportFileWithMessage(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-08-20 04:01:50 +00:00
|
|
|
dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
|
|
|
|
|
|
|
|
temporaryFile, err := ioutil.TempFile("", "exportImportTest")
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
|
2015-08-20 04:01:50 +00:00
|
|
|
defer os.Remove(temporaryFile.Name())
|
|
|
|
|
|
|
|
runCmd := exec.Command(dockerBinary, "export", "test-import")
|
|
|
|
runCmd.Stdout = bufio.NewWriter(temporaryFile)
|
|
|
|
|
|
|
|
_, err = runCommand(runCmd)
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("failed to export a container"))
|
2015-08-20 04:01:50 +00:00
|
|
|
|
|
|
|
message := "Testing commit message"
|
|
|
|
out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
|
2015-08-20 04:01:50 +00:00
|
|
|
image := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "history", image)
|
|
|
|
split := strings.Split(out, "\n")
|
|
|
|
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(split, checker.HasLen, 3, check.Commentf("expected 3 lines from image history"))
|
2015-08-20 04:01:50 +00:00
|
|
|
r := regexp.MustCompile("[\\s]{2,}")
|
|
|
|
split = r.Split(split[1], -1)
|
|
|
|
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(message, checker.Equals, split[3], check.Commentf("didn't get expected value in commit message"))
|
2015-08-20 04:01:50 +00:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "run", "--rm", image, "true")
|
2015-10-20 10:27:52 +00:00
|
|
|
c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing"))
|
2015-08-20 04:01:50 +00:00
|
|
|
}
|
|
|
|
|
2015-03-29 17:42:11 +00:00
|
|
|
func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
|
2015-10-20 10:27:52 +00:00
|
|
|
_, _, err := dockerCmdWithError("import", "example.com/myImage.tar")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("import non-existing file must failed"))
|
2015-03-29 17:42:11 +00:00
|
|
|
}
|