docker_cli_import_test.go 4.6 KB

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