docker_cli_import_test.go 3.5 KB

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