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