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