docker_cli_import_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "bufio"
  4. "compress/gzip"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "regexp"
  9. "strings"
  10. "github.com/docker/docker/integration-cli/checker"
  11. "github.com/docker/docker/integration-cli/cli"
  12. "github.com/docker/docker/pkg/testutil"
  13. icmd "github.com/docker/docker/pkg/testutil/cmd"
  14. "github.com/go-check/check"
  15. )
  16. func (s *DockerSuite) TestImportDisplay(c *check.C) {
  17. testRequires(c, DaemonIsLinux)
  18. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  19. cleanedContainerID := strings.TrimSpace(out)
  20. out, _, err := testutil.RunCommandPipelineWithOutput(
  21. exec.Command(dockerBinary, "export", cleanedContainerID),
  22. exec.Command(dockerBinary, "import", "-"),
  23. )
  24. c.Assert(err, checker.IsNil)
  25. c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
  26. image := strings.TrimSpace(out)
  27. out, _ = dockerCmd(c, "run", "--rm", image, "true")
  28. c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
  29. }
  30. func (s *DockerSuite) TestImportBadURL(c *check.C) {
  31. out, _, err := dockerCmdWithError("import", "http://nourl/bad")
  32. c.Assert(err, checker.NotNil, check.Commentf("import was supposed to fail but didn't"))
  33. // Depending on your system you can get either of these errors
  34. if !strings.Contains(out, "dial tcp") &&
  35. !strings.Contains(out, "ApplyLayer exit status 1 stdout: stderr: archive/tar: invalid tar header") &&
  36. !strings.Contains(out, "Error processing tar file") {
  37. c.Fatalf("expected an error msg but didn't get one.\nErr: %v\nOut: %v", err, out)
  38. }
  39. }
  40. func (s *DockerSuite) TestImportFile(c *check.C) {
  41. testRequires(c, DaemonIsLinux)
  42. dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
  43. temporaryFile, err := ioutil.TempFile("", "exportImportTest")
  44. c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
  45. defer os.Remove(temporaryFile.Name())
  46. icmd.RunCmd(icmd.Cmd{
  47. Command: []string{dockerBinary, "export", "test-import"},
  48. Stdout: bufio.NewWriter(temporaryFile),
  49. }).Assert(c, icmd.Success)
  50. out, _ := dockerCmd(c, "import", temporaryFile.Name())
  51. c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
  52. image := strings.TrimSpace(out)
  53. out, _ = dockerCmd(c, "run", "--rm", image, "true")
  54. c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
  55. }
  56. func (s *DockerSuite) TestImportGzipped(c *check.C) {
  57. testRequires(c, DaemonIsLinux)
  58. dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
  59. temporaryFile, err := ioutil.TempFile("", "exportImportTest")
  60. c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
  61. defer os.Remove(temporaryFile.Name())
  62. w := gzip.NewWriter(temporaryFile)
  63. icmd.RunCmd(icmd.Cmd{
  64. Command: []string{dockerBinary, "export", "test-import"},
  65. Stdout: w,
  66. }).Assert(c, icmd.Success)
  67. c.Assert(w.Close(), checker.IsNil, check.Commentf("failed to close gzip writer"))
  68. temporaryFile.Close()
  69. out, _ := dockerCmd(c, "import", temporaryFile.Name())
  70. c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
  71. image := strings.TrimSpace(out)
  72. out, _ = dockerCmd(c, "run", "--rm", image, "true")
  73. c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing."))
  74. }
  75. func (s *DockerSuite) TestImportFileWithMessage(c *check.C) {
  76. testRequires(c, DaemonIsLinux)
  77. dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
  78. temporaryFile, err := ioutil.TempFile("", "exportImportTest")
  79. c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
  80. defer os.Remove(temporaryFile.Name())
  81. icmd.RunCmd(icmd.Cmd{
  82. Command: []string{dockerBinary, "export", "test-import"},
  83. Stdout: bufio.NewWriter(temporaryFile),
  84. }).Assert(c, icmd.Success)
  85. message := "Testing commit message"
  86. out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
  87. c.Assert(out, checker.Count, "\n", 1, check.Commentf("display is expected 1 '\\n' but didn't"))
  88. image := strings.TrimSpace(out)
  89. out, _ = dockerCmd(c, "history", image)
  90. split := strings.Split(out, "\n")
  91. c.Assert(split, checker.HasLen, 3, check.Commentf("expected 3 lines from image history"))
  92. r := regexp.MustCompile("[\\s]{2,}")
  93. split = r.Split(split[1], -1)
  94. c.Assert(message, checker.Equals, split[3], check.Commentf("didn't get expected value in commit message"))
  95. out, _ = dockerCmd(c, "run", "--rm", image, "true")
  96. c.Assert(out, checker.Equals, "", check.Commentf("command output should've been nothing"))
  97. }
  98. func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
  99. _, _, err := dockerCmdWithError("import", "example.com/myImage.tar")
  100. c.Assert(err, checker.NotNil, check.Commentf("import non-existing file must failed"))
  101. }
  102. func (s *DockerSuite) TestImportWithQuotedChanges(c *check.C) {
  103. testRequires(c, DaemonIsLinux)
  104. cli.DockerCmd(c, "run", "--name", "test-import", "busybox", "true")
  105. temporaryFile, err := ioutil.TempFile("", "exportImportTest")
  106. c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
  107. defer os.Remove(temporaryFile.Name())
  108. cli.Docker(cli.Args("export", "test-import"), cli.WithStdout(bufio.NewWriter(temporaryFile))).Assert(c, icmd.Success)
  109. result := cli.DockerCmd(c, "import", "-c", `ENTRYPOINT ["/bin/sh", "-c"]`, temporaryFile.Name())
  110. image := strings.TrimSpace(result.Stdout())
  111. result = cli.DockerCmd(c, "run", "--rm", image, "true")
  112. c.Assert(result, icmd.Matches, icmd.Expected{Out: icmd.None})
  113. }