docker_cli_import_test.go 2.9 KB

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