docker_cli_import_test.go 5.3 KB

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