2021-08-23 13:14:53 +00:00
|
|
|
//go:build !windows
|
2014-11-20 05:19:16 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-10-31 12:12:10 +00:00
|
|
|
"context"
|
2016-04-13 02:45:42 +00:00
|
|
|
"fmt"
|
2014-11-20 05:19:16 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-01-28 14:19:25 +00:00
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2016-10-31 12:12:10 +00:00
|
|
|
"time"
|
2014-11-20 05:19:16 +00:00
|
|
|
|
2019-07-29 23:59:08 +00:00
|
|
|
"github.com/creack/pty"
|
2023-07-27 17:56:27 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2017-03-23 17:35:22 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/testutil"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
2023-09-22 11:17:31 +00:00
|
|
|
"gotest.tools/v3/skip"
|
2014-11-20 05:19:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// save a repo and try to load it using stdout
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLISaveLoadSuite) TestSaveAndLoadRepoStdout(c *testing.T) {
|
2015-05-16 14:59:53 +00:00
|
|
|
name := "test-save-and-load-repo-stdout"
|
2023-07-27 17:56:27 +00:00
|
|
|
cli.DockerCmd(c, "run", "--name", name, "busybox", "true")
|
2014-11-20 05:19:16 +00:00
|
|
|
|
2023-07-27 17:56:27 +00:00
|
|
|
imgRepoName := "foobar-save-load-test"
|
|
|
|
before := cli.DockerCmd(c, "commit", name, imgRepoName).Stdout()
|
2016-01-28 14:19:25 +00:00
|
|
|
before = strings.TrimRight(before, "\n")
|
2014-11-20 05:19:16 +00:00
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
tmpFile, err := os.CreateTemp("", "foobar-save-load-test.tar")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-05-16 18:04:25 +00:00
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
|
2016-12-13 20:21:51 +00:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
2023-07-27 17:56:27 +00:00
|
|
|
Command: []string{dockerBinary, "save", imgRepoName},
|
2016-12-13 20:21:51 +00:00
|
|
|
Stdout: tmpFile,
|
|
|
|
}).Assert(c, icmd.Success)
|
2014-11-20 05:19:16 +00:00
|
|
|
|
2015-05-16 18:04:25 +00:00
|
|
|
tmpFile, err = os.Open(tmpFile.Name())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-06-25 03:57:21 +00:00
|
|
|
defer tmpFile.Close()
|
2015-05-16 18:04:25 +00:00
|
|
|
|
2023-07-27 17:56:27 +00:00
|
|
|
deleteImages(imgRepoName)
|
2014-11-20 05:19:16 +00:00
|
|
|
|
2017-01-16 15:39:12 +00:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "load"},
|
|
|
|
Stdin: tmpFile,
|
|
|
|
}).Assert(c, icmd.Success)
|
2014-11-20 05:19:16 +00:00
|
|
|
|
2023-07-27 17:56:27 +00:00
|
|
|
after := inspectField(c, imgRepoName, "Id")
|
2016-01-28 14:19:25 +00:00
|
|
|
after = strings.TrimRight(after, "\n")
|
2014-11-20 05:19:16 +00:00
|
|
|
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, after, before, "inspect is not the same after a save / load")
|
2014-11-20 05:19:16 +00:00
|
|
|
|
2023-07-27 17:56:27 +00:00
|
|
|
deleteImages(imgRepoName)
|
2014-11-20 05:19:16 +00:00
|
|
|
|
2023-07-27 17:56:27 +00:00
|
|
|
p, tty, err := pty.Open()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2023-07-27 17:56:27 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "save", imgRepoName)
|
2014-11-20 05:19:16 +00:00
|
|
|
cmd.Stdin = tty
|
|
|
|
cmd.Stdout = tty
|
|
|
|
cmd.Stderr = tty
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, cmd.Start())
|
|
|
|
assert.ErrorContains(c, cmd.Wait(), "", "did not break writing to a TTY")
|
2014-11-20 05:19:16 +00:00
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
2023-07-27 17:56:27 +00:00
|
|
|
n, err := p.Read(buf)
|
2019-11-27 14:36:45 +00:00
|
|
|
assert.NilError(c, err, "could not read tty output")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, strings.Contains(string(buf[:n]), "cowardly refusing"), "help output is not being yielded")
|
2014-11-20 05:19:16 +00:00
|
|
|
}
|
2016-04-13 02:45:42 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLISaveLoadSuite) TestSaveAndLoadWithProgressBar(c *testing.T) {
|
2023-09-22 11:17:31 +00:00
|
|
|
// TODO(vvoland): https://github.com/moby/moby/issues/43910
|
|
|
|
skip.If(c, testEnv.UsingSnapshotter(), "TODO: Not implemented yet")
|
|
|
|
|
2016-04-13 02:45:42 +00:00
|
|
|
name := "test-load"
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, name, build.WithDockerfile(`FROM busybox
|
2016-04-13 02:45:42 +00:00
|
|
|
RUN touch aa
|
2017-01-16 10:30:14 +00:00
|
|
|
`))
|
2016-04-13 02:45:42 +00:00
|
|
|
|
|
|
|
tmptar := name + ".tar"
|
2023-07-27 17:56:27 +00:00
|
|
|
cli.DockerCmd(c, "save", "-o", tmptar, name)
|
2016-04-13 02:45:42 +00:00
|
|
|
defer os.Remove(tmptar)
|
|
|
|
|
2023-07-27 17:56:27 +00:00
|
|
|
cli.DockerCmd(c, "rmi", name)
|
|
|
|
cli.DockerCmd(c, "tag", "busybox", name)
|
|
|
|
out := cli.DockerCmd(c, "load", "-i", tmptar).Combined()
|
2016-04-13 02:45:42 +00:00
|
|
|
expected := fmt.Sprintf("The image %s:latest already exists, renaming the old one with ID", name)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, expected))
|
2016-04-13 02:45:42 +00:00
|
|
|
}
|
2016-10-31 12:12:10 +00:00
|
|
|
|
|
|
|
// fail because load didn't receive data from stdin
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLISaveLoadSuite) TestLoadNoStdinFail(c *testing.T) {
|
2023-07-27 17:56:27 +00:00
|
|
|
p, tty, err := pty.Open()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx, cancel := context.WithTimeout(testutil.GetContext(c), 5*time.Second)
|
2016-10-31 12:12:10 +00:00
|
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(ctx, dockerBinary, "load")
|
|
|
|
cmd.Stdin = tty
|
|
|
|
cmd.Stdout = tty
|
|
|
|
cmd.Stderr = tty
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.ErrorContains(c, cmd.Run(), "", "docker-load should fail")
|
2016-10-31 12:12:10 +00:00
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
2023-07-27 17:56:27 +00:00
|
|
|
n, err := p.Read(buf)
|
2022-01-20 12:43:42 +00:00
|
|
|
assert.NilError(c, err) // could not read tty output
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, strings.Contains(string(buf[:n]), "requested load from stdin, but stdin is empty"))
|
2016-10-31 12:12:10 +00:00
|
|
|
}
|