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"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
2014-03-31 19:31:21 +00:00
|
|
|
)
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestImportDisplay(c *testing.T) {
|
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
|
|
|
|
2017-08-22 21:07:52 +00:00
|
|
|
out, err := RunCommandPipelineWithOutput(
|
2015-02-14 10:27:06 +00:00
|
|
|
exec.Command(dockerBinary, "export", cleanedContainerID),
|
|
|
|
exec.Command(dockerBinary, "import", "-"),
|
|
|
|
)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2014-03-31 19:31:21 +00:00
|
|
|
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, strings.Count(out, "\n") == 1, "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")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, "", "command output should've been nothing.")
|
2014-03-31 19:31:21 +00:00
|
|
|
}
|
2015-05-14 00:21:02 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestImportBadURL(c *testing.T) {
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("import", "http://nourl/bad")
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, err != nil, "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") &&
|
2016-08-27 21:57:50 +00:00
|
|
|
!strings.Contains(out, "ApplyLayer exit status 1 stdout: stderr: archive/tar: invalid tar header") &&
|
2016-05-23 17:00:01 +00: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 00:21:02 +00:00
|
|
|
}
|
2015-03-29 17:42:11 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestImportFile(c *testing.T) {
|
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")
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, err == nil, "failed to create temporary file")
|
2015-03-29 17:42:11 +00:00
|
|
|
defer os.Remove(temporaryFile.Name())
|
|
|
|
|
2016-12-13 20:21:51 +00:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "export", "test-import"},
|
|
|
|
Stdout: bufio.NewWriter(temporaryFile),
|
|
|
|
}).Assert(c, icmd.Success)
|
2015-03-29 17:42:11 +00:00
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ := dockerCmd(c, "import", temporaryFile.Name())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, strings.Count(out, "\n") == 1, "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")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, "", "command output should've been nothing.")
|
2015-03-29 17:42:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestImportGzipped(c *testing.T) {
|
2016-02-16 19:19:23 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
|
|
|
|
|
|
|
|
temporaryFile, err := ioutil.TempFile("", "exportImportTest")
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, err == nil, "failed to create temporary file")
|
2016-02-16 19:19:23 +00:00
|
|
|
defer os.Remove(temporaryFile.Name())
|
|
|
|
|
|
|
|
w := gzip.NewWriter(temporaryFile)
|
2016-12-13 20:21:51 +00:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "export", "test-import"},
|
|
|
|
Stdout: w,
|
|
|
|
}).Assert(c, icmd.Success)
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, w.Close() == nil, "failed to close gzip writer")
|
2016-02-16 19:19:23 +00:00
|
|
|
temporaryFile.Close()
|
|
|
|
out, _ := dockerCmd(c, "import", temporaryFile.Name())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, strings.Count(out, "\n") == 1, "display is expected 1 '\\n' but didn't")
|
2016-02-16 19:19:23 +00:00
|
|
|
image := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "run", "--rm", image, "true")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, "", "command output should've been nothing.")
|
2016-02-16 19:19:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestImportFileWithMessage(c *testing.T) {
|
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")
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, err == nil, "failed to create temporary file")
|
2015-08-20 04:01:50 +00:00
|
|
|
defer os.Remove(temporaryFile.Name())
|
|
|
|
|
2016-12-13 20:21:51 +00:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "export", "test-import"},
|
|
|
|
Stdout: bufio.NewWriter(temporaryFile),
|
|
|
|
}).Assert(c, icmd.Success)
|
2015-08-20 04:01:50 +00:00
|
|
|
|
|
|
|
message := "Testing commit message"
|
|
|
|
out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, strings.Count(out, "\n") == 1, "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")
|
|
|
|
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, len(split), 3, "expected 3 lines from image history")
|
2019-08-05 14:53:46 +00:00
|
|
|
r := regexp.MustCompile(`[\s]{2,}`)
|
2015-08-20 04:01:50 +00:00
|
|
|
split = r.Split(split[1], -1)
|
|
|
|
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, message, split[3], "didn't get expected value in commit message")
|
2015-08-20 04:01:50 +00:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "run", "--rm", image, "true")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, "", "command output should've been nothing")
|
2015-08-20 04:01:50 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestImportFileNonExistentFile(c *testing.T) {
|
2015-10-20 10:27:52 +00:00
|
|
|
_, _, err := dockerCmdWithError("import", "example.com/myImage.tar")
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, err != nil, "import non-existing file must failed")
|
2015-03-29 17:42:11 +00:00
|
|
|
}
|
2016-08-31 22:48:53 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestImportWithQuotedChanges(c *testing.T) {
|
2016-08-31 22:48:53 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-03-27 15:12:48 +00:00
|
|
|
cli.DockerCmd(c, "run", "--name", "test-import", "busybox", "true")
|
2016-08-31 22:48:53 +00:00
|
|
|
|
|
|
|
temporaryFile, err := ioutil.TempFile("", "exportImportTest")
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, err == nil, "failed to create temporary file")
|
2016-08-31 22:48:53 +00:00
|
|
|
defer os.Remove(temporaryFile.Name())
|
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
cli.Docker(cli.Args("export", "test-import"), cli.WithStdout(bufio.NewWriter(temporaryFile))).Assert(c, icmd.Success)
|
2016-08-31 22:48:53 +00:00
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
result := cli.DockerCmd(c, "import", "-c", `ENTRYPOINT ["/bin/sh", "-c"]`, temporaryFile.Name())
|
2016-08-31 22:48:53 +00:00
|
|
|
image := strings.TrimSpace(result.Stdout())
|
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
result = cli.DockerCmd(c, "run", "--rm", image, "true")
|
2017-08-23 21:01:29 +00:00
|
|
|
result.Assert(c, icmd.Expected{Out: icmd.None})
|
2016-08-31 22:48:53 +00:00
|
|
|
}
|