docker_cli_import_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package main
  2. import (
  3. "bufio"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "regexp"
  8. "strings"
  9. "github.com/go-check/check"
  10. )
  11. func (s *DockerSuite) TestImportDisplay(c *check.C) {
  12. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  13. cleanedContainerID := strings.TrimSpace(out)
  14. out, _, err := runCommandPipelineWithOutput(
  15. exec.Command(dockerBinary, "export", cleanedContainerID),
  16. exec.Command(dockerBinary, "import", "-"),
  17. )
  18. if err != nil {
  19. c.Errorf("import failed with errors: %v, output: %q", err, out)
  20. }
  21. if n := strings.Count(out, "\n"); n != 1 {
  22. c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out)
  23. }
  24. image := strings.TrimSpace(out)
  25. out, _ = dockerCmd(c, "run", "--rm", image, "true")
  26. if out != "" {
  27. c.Fatalf("command output should've been nothing, was %q", out)
  28. }
  29. }
  30. func (s *DockerSuite) TestImportBadURL(c *check.C) {
  31. out, _, err := dockerCmdWithError("import", "http://nourl/bad")
  32. if err == nil {
  33. c.Fatal("import was supposed to fail but didn't")
  34. }
  35. if !strings.Contains(out, "dial tcp") {
  36. c.Fatalf("expected an error msg but didn't get one:\n%s", out)
  37. }
  38. }
  39. func (s *DockerSuite) TestImportFile(c *check.C) {
  40. dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
  41. temporaryFile, err := ioutil.TempFile("", "exportImportTest")
  42. if err != nil {
  43. c.Fatal("failed to create temporary file", "", err)
  44. }
  45. defer os.Remove(temporaryFile.Name())
  46. runCmd := exec.Command(dockerBinary, "export", "test-import")
  47. runCmd.Stdout = bufio.NewWriter(temporaryFile)
  48. _, err = runCommand(runCmd)
  49. if err != nil {
  50. c.Fatal("failed to export a container", err)
  51. }
  52. out, _ := dockerCmd(c, "import", temporaryFile.Name())
  53. if n := strings.Count(out, "\n"); n != 1 {
  54. c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out)
  55. }
  56. image := strings.TrimSpace(out)
  57. out, _ = dockerCmd(c, "run", "--rm", image, "true")
  58. if out != "" {
  59. c.Fatalf("command output should've been nothing, was %q", out)
  60. }
  61. }
  62. func (s *DockerSuite) TestImportFileWithMessage(c *check.C) {
  63. dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
  64. temporaryFile, err := ioutil.TempFile("", "exportImportTest")
  65. if err != nil {
  66. c.Fatal("failed to create temporary file", "", err)
  67. }
  68. defer os.Remove(temporaryFile.Name())
  69. runCmd := exec.Command(dockerBinary, "export", "test-import")
  70. runCmd.Stdout = bufio.NewWriter(temporaryFile)
  71. _, err = runCommand(runCmd)
  72. if err != nil {
  73. c.Fatal("failed to export a container", err)
  74. }
  75. message := "Testing commit message"
  76. out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
  77. if n := strings.Count(out, "\n"); n != 1 {
  78. c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out)
  79. }
  80. image := strings.TrimSpace(out)
  81. out, _ = dockerCmd(c, "history", image)
  82. split := strings.Split(out, "\n")
  83. if len(split) != 3 {
  84. c.Fatalf("expected 3 lines from image history, got %d", len(split))
  85. }
  86. r := regexp.MustCompile("[\\s]{2,}")
  87. split = r.Split(split[1], -1)
  88. if message != split[3] {
  89. c.Fatalf("expected %s in commit message, got %s", message, split[3])
  90. }
  91. out, _ = dockerCmd(c, "run", "--rm", image, "true")
  92. if out != "" {
  93. c.Fatalf("command output should've been nothing, was %q", out)
  94. }
  95. }
  96. func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
  97. _, exitCode, err := dockerCmdWithError("import", "example.com/myImage.tar")
  98. if exitCode == 0 || err == nil {
  99. c.Fatalf("import non-existing file must failed")
  100. }
  101. }