docker_cli_save_load_unix_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // +build !windows
  2. package main
  3. import (
  4. "bytes"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "strings"
  9. "github.com/docker/docker/vendor/src/github.com/kr/pty"
  10. "github.com/go-check/check"
  11. )
  12. // save a repo and try to load it using stdout
  13. func (s *DockerSuite) TestSaveAndLoadRepoStdout(c *check.C) {
  14. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  15. out, _, err := runCommandWithOutput(runCmd)
  16. if err != nil {
  17. c.Fatalf("failed to create a container: %s, %v", out, err)
  18. }
  19. cleanedContainerID := strings.TrimSpace(out)
  20. repoName := "foobar-save-load-test"
  21. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  22. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  23. c.Fatalf("output should've been a container id: %s, %v", out, err)
  24. }
  25. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
  26. if out, _, err = runCommandWithOutput(commitCmd); err != nil {
  27. c.Fatalf("failed to commit container: %s, %v", out, err)
  28. }
  29. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  30. before, _, err := runCommandWithOutput(inspectCmd)
  31. if err != nil {
  32. c.Fatalf("the repo should exist before saving it: %s, %v", before, err)
  33. }
  34. tmpFile, err := ioutil.TempFile("", "foobar-save-load-test.tar")
  35. c.Assert(err, check.IsNil)
  36. defer os.Remove(tmpFile.Name())
  37. saveCmd := exec.Command(dockerBinary, "save", repoName)
  38. saveCmd.Stdout = tmpFile
  39. if _, err = runCommand(saveCmd); err != nil {
  40. c.Fatalf("failed to save repo: %v", err)
  41. }
  42. tmpFile, err = os.Open(tmpFile.Name())
  43. c.Assert(err, check.IsNil)
  44. deleteImages(repoName)
  45. loadCmd := exec.Command(dockerBinary, "load")
  46. loadCmd.Stdin = tmpFile
  47. if out, _, err = runCommandWithOutput(loadCmd); err != nil {
  48. c.Fatalf("failed to load repo: %s, %v", out, err)
  49. }
  50. inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
  51. after, _, err := runCommandWithOutput(inspectCmd)
  52. if err != nil {
  53. c.Fatalf("the repo should exist after loading it: %s %v", after, err)
  54. }
  55. if before != after {
  56. c.Fatalf("inspect is not the same after a save / load")
  57. }
  58. deleteContainer(cleanedContainerID)
  59. deleteImages(repoName)
  60. pty, tty, err := pty.Open()
  61. if err != nil {
  62. c.Fatalf("Could not open pty: %v", err)
  63. }
  64. cmd := exec.Command(dockerBinary, "save", repoName)
  65. cmd.Stdin = tty
  66. cmd.Stdout = tty
  67. cmd.Stderr = tty
  68. if err := cmd.Start(); err != nil {
  69. c.Fatalf("start err: %v", err)
  70. }
  71. if err := cmd.Wait(); err == nil {
  72. c.Fatal("did not break writing to a TTY")
  73. }
  74. buf := make([]byte, 1024)
  75. n, err := pty.Read(buf)
  76. if err != nil {
  77. c.Fatal("could not read tty output")
  78. }
  79. if !bytes.Contains(buf[:n], []byte("Cowardly refusing")) {
  80. c.Fatal("help output is not being yielded", out)
  81. }
  82. }