docker_cli_save_load_unix_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //go:build !windows
  2. package main
  3. import (
  4. "context"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/creack/pty"
  12. "github.com/docker/docker/integration-cli/cli/build"
  13. "gotest.tools/v3/assert"
  14. "gotest.tools/v3/icmd"
  15. )
  16. // save a repo and try to load it using stdout
  17. func (s *DockerCLISaveLoadSuite) TestSaveAndLoadRepoStdout(c *testing.T) {
  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 := os.CreateTemp("", "foobar-save-load-test.tar")
  24. assert.NilError(c, err)
  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. assert.NilError(c, err)
  32. defer tmpFile.Close()
  33. deleteImages(repoName)
  34. icmd.RunCmd(icmd.Cmd{
  35. Command: []string{dockerBinary, "load"},
  36. Stdin: tmpFile,
  37. }).Assert(c, icmd.Success)
  38. after := inspectField(c, repoName, "Id")
  39. after = strings.TrimRight(after, "\n")
  40. assert.Equal(c, after, before, "inspect is not the same after a save / load")
  41. deleteImages(repoName)
  42. pty, tty, err := pty.Open()
  43. assert.NilError(c, err)
  44. cmd := exec.Command(dockerBinary, "save", repoName)
  45. cmd.Stdin = tty
  46. cmd.Stdout = tty
  47. cmd.Stderr = tty
  48. assert.NilError(c, cmd.Start())
  49. assert.ErrorContains(c, cmd.Wait(), "", "did not break writing to a TTY")
  50. buf := make([]byte, 1024)
  51. n, err := pty.Read(buf)
  52. assert.NilError(c, err, "could not read tty output")
  53. assert.Assert(c, strings.Contains(string(buf[:n]), "cowardly refusing"), "help output is not being yielded")
  54. }
  55. func (s *DockerCLISaveLoadSuite) TestSaveAndLoadWithProgressBar(c *testing.T) {
  56. name := "test-load"
  57. buildImageSuccessfully(c, name, build.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. assert.Assert(c, strings.Contains(out, expected))
  68. }
  69. // fail because load didn't receive data from stdin
  70. func (s *DockerCLISaveLoadSuite) TestLoadNoStdinFail(c *testing.T) {
  71. pty, tty, err := pty.Open()
  72. assert.NilError(c, err)
  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. assert.ErrorContains(c, cmd.Run(), "", "docker-load should fail")
  80. buf := make([]byte, 1024)
  81. n, err := pty.Read(buf)
  82. assert.NilError(c, err) // could not read tty output
  83. assert.Assert(c, strings.Contains(string(buf[:n]), "requested load from stdin, but stdin is empty"))
  84. }