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