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