docker_cli_save_load_unix_test.go 3.1 KB

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