docker_cli_import_test.go 4.6 KB

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