docker_cli_save_load_unix_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // +build !windows
  2. package main
  3. import (
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. "time"
  11. "github.com/docker/docker/integration-cli/checker"
  12. icmd "github.com/docker/docker/pkg/testutil/cmd"
  13. "github.com/go-check/check"
  14. "github.com/kr/pty"
  15. )
  16. // save a repo and try to load it using stdout
  17. func (s *DockerSuite) TestSaveAndLoadRepoStdout(c *check.C) {
  18. name := "test-save-and-load-repo-stdout"
  19. dockerCmd(c, "run", "--name", name, "busybox", "true")
  20. repoName := "foobar-save-load-test"
  21. before, _ := dockerCmd(c, "commit", name, repoName)
  22. before = strings.TrimRight(before, "\n")
  23. tmpFile, err := ioutil.TempFile("", "foobar-save-load-test.tar")
  24. c.Assert(err, check.IsNil)
  25. defer os.Remove(tmpFile.Name())
  26. icmd.RunCmd(icmd.Cmd{
  27. Command: []string{dockerBinary, "save", repoName},
  28. Stdout: tmpFile,
  29. }).Assert(c, icmd.Success)
  30. tmpFile, err = os.Open(tmpFile.Name())
  31. c.Assert(err, check.IsNil)
  32. defer tmpFile.Close()
  33. deleteImages(repoName)
  34. loadCmd := exec.Command(dockerBinary, "load")
  35. loadCmd.Stdin = tmpFile
  36. out, _, err := runCommandWithOutput(loadCmd)
  37. c.Assert(err, check.IsNil, check.Commentf(out))
  38. after := inspectField(c, repoName, "Id")
  39. after = strings.TrimRight(after, "\n")
  40. c.Assert(after, check.Equals, before) //inspect is not the same after a save / load
  41. deleteImages(repoName)
  42. pty, tty, err := pty.Open()
  43. c.Assert(err, check.IsNil)
  44. cmd := exec.Command(dockerBinary, "save", repoName)
  45. cmd.Stdin = tty
  46. cmd.Stdout = tty
  47. cmd.Stderr = tty
  48. c.Assert(cmd.Start(), check.IsNil)
  49. c.Assert(cmd.Wait(), check.NotNil) //did not break writing to a TTY
  50. buf := make([]byte, 1024)
  51. n, err := pty.Read(buf)
  52. c.Assert(err, check.IsNil) //could not read tty output
  53. c.Assert(string(buf[:n]), checker.Contains, "Cowardly refusing", check.Commentf("help output is not being yielded", out))
  54. }
  55. func (s *DockerSuite) TestSaveAndLoadWithProgressBar(c *check.C) {
  56. name := "test-load"
  57. buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
  58. RUN touch aa
  59. `))
  60. tmptar := name + ".tar"
  61. dockerCmd(c, "save", "-o", tmptar, name)
  62. defer os.Remove(tmptar)
  63. dockerCmd(c, "rmi", name)
  64. dockerCmd(c, "tag", "busybox", name)
  65. out, _ := dockerCmd(c, "load", "-i", tmptar)
  66. expected := fmt.Sprintf("The image %s:latest already exists, renaming the old one with ID", name)
  67. c.Assert(out, checker.Contains, expected)
  68. }
  69. // fail because load didn't receive data from stdin
  70. func (s *DockerSuite) TestLoadNoStdinFail(c *check.C) {
  71. pty, tty, err := pty.Open()
  72. c.Assert(err, check.IsNil)
  73. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  74. defer cancel()
  75. cmd := exec.CommandContext(ctx, dockerBinary, "load")
  76. cmd.Stdin = tty
  77. cmd.Stdout = tty
  78. cmd.Stderr = tty
  79. c.Assert(cmd.Run(), check.NotNil) // docker-load should fail
  80. buf := make([]byte, 1024)
  81. n, err := pty.Read(buf)
  82. c.Assert(err, check.IsNil) //could not read tty output
  83. c.Assert(string(buf[:n]), checker.Contains, "requested load from stdin, but stdin is empty")
  84. }