diff --git a/integration-cli/docker_cli_save_load_test.go b/integration-cli/docker_cli_save_load_test.go index 033cdeaada..492b1da330 100644 --- a/integration-cli/docker_cli_save_load_test.go +++ b/integration-cli/docker_cli_save_load_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/docker/docker/api/types" + "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" @@ -34,19 +35,19 @@ func (s *DockerCLISaveLoadSuite) OnTimeout(c *testing.T) { func (s *DockerCLISaveLoadSuite) TestSaveXzAndLoadRepoStdout(c *testing.T) { testRequires(c, DaemonIsLinux) name := "test-save-xz-and-load-repo-stdout" - dockerCmd(c, "run", "--name", name, "busybox", "true") + cli.DockerCmd(c, "run", "--name", name, "busybox", "true") - repoName := "foobar-save-load-test-xz-gz" - out, _ := dockerCmd(c, "commit", name, repoName) + imgRepoName := "foobar-save-load-test-xz-gz" + out := cli.DockerCmd(c, "commit", name, imgRepoName).Combined() - dockerCmd(c, "inspect", repoName) + cli.DockerCmd(c, "inspect", imgRepoName) repoTarball, err := RunCommandPipelineWithOutput( - exec.Command(dockerBinary, "save", repoName), + exec.Command(dockerBinary, "save", imgRepoName), exec.Command("xz", "-c"), exec.Command("gzip", "-c")) assert.NilError(c, err, "failed to save repo: %v %v", out, err) - deleteImages(repoName) + deleteImages(imgRepoName) icmd.RunCmd(icmd.Cmd{ Command: []string{dockerBinary, "load"}, @@ -55,7 +56,7 @@ func (s *DockerCLISaveLoadSuite) TestSaveXzAndLoadRepoStdout(c *testing.T) { ExitCode: 1, }) - after, _, err := dockerCmdWithError("inspect", repoName) + after, _, err := dockerCmdWithError("inspect", imgRepoName) assert.ErrorContains(c, err, "", "the repo should not exist: %v", after) } @@ -63,12 +64,12 @@ func (s *DockerCLISaveLoadSuite) TestSaveXzAndLoadRepoStdout(c *testing.T) { func (s *DockerCLISaveLoadSuite) TestSaveXzGzAndLoadRepoStdout(c *testing.T) { testRequires(c, DaemonIsLinux) name := "test-save-xz-gz-and-load-repo-stdout" - dockerCmd(c, "run", "--name", name, "busybox", "true") + cli.DockerCmd(c, "run", "--name", name, "busybox", "true") repoName := "foobar-save-load-test-xz-gz" - dockerCmd(c, "commit", name, repoName) + cli.DockerCmd(c, "commit", name, repoName) - dockerCmd(c, "inspect", repoName) + cli.DockerCmd(c, "inspect", repoName) out, err := RunCommandPipelineWithOutput( exec.Command(dockerBinary, "save", repoName), @@ -91,10 +92,10 @@ func (s *DockerCLISaveLoadSuite) TestSaveXzGzAndLoadRepoStdout(c *testing.T) { func (s *DockerCLISaveLoadSuite) TestSaveSingleTag(c *testing.T) { testRequires(c, DaemonIsLinux) - repoName := "foobar-save-single-tag-test" - dockerCmd(c, "tag", "busybox:latest", fmt.Sprintf("%v:latest", repoName)) + imgRepoName := "foobar-save-single-tag-test" + cli.DockerCmd(c, "tag", "busybox:latest", fmt.Sprintf("%v:latest", imgRepoName)) - out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName) + out := cli.DockerCmd(c, "images", "-q", "--no-trunc", imgRepoName).Stdout() cleanedImageID := strings.TrimSpace(out) filesFilter := fmt.Sprintf("(^manifest.json$|%v)", cleanedImageID) @@ -102,7 +103,7 @@ func (s *DockerCLISaveLoadSuite) TestSaveSingleTag(c *testing.T) { filesFilter = fmt.Sprintf("(^index.json$|^manifest.json$|%v)", cleanedImageID) } out, err := RunCommandPipelineWithOutput( - exec.Command(dockerBinary, "save", fmt.Sprintf("%v:latest", repoName)), + exec.Command(dockerBinary, "save", fmt.Sprintf("%v:latest", imgRepoName)), exec.Command("tar", "t"), exec.Command("grep", "-E", filesFilter)) assert.NilError(c, err, "failed to save repo with image ID and index files: %s, %v", out, err) @@ -110,13 +111,13 @@ func (s *DockerCLISaveLoadSuite) TestSaveSingleTag(c *testing.T) { func (s *DockerCLISaveLoadSuite) TestSaveImageId(c *testing.T) { testRequires(c, DaemonIsLinux) - repoName := "foobar-save-image-id-test" - dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", repoName)) + imgRepoName := "foobar-save-image-id-test" + cli.DockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", imgRepoName)) - out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName) + out := cli.DockerCmd(c, "images", "-q", "--no-trunc", imgRepoName).Stdout() cleanedLongImageID := strings.TrimPrefix(strings.TrimSpace(out), "sha256:") - out, _ = dockerCmd(c, "images", "-q", repoName) + out = cli.DockerCmd(c, "images", "-q", imgRepoName).Stdout() cleanedShortImageID := strings.TrimSpace(out) // Make sure IDs are not empty @@ -138,7 +139,7 @@ func (s *DockerCLISaveLoadSuite) TestSaveImageId(c *testing.T) { defer func() { saveCmd.Wait() tarCmd.Wait() - dockerCmd(c, "rmi", repoName) + cli.DockerCmd(c, "rmi", imgRepoName) }() out, _, err = runCommandWithOutput(grepCmd) @@ -149,24 +150,22 @@ func (s *DockerCLISaveLoadSuite) TestSaveImageId(c *testing.T) { // save a repo and try to load it using flags func (s *DockerCLISaveLoadSuite) TestSaveAndLoadRepoFlags(c *testing.T) { testRequires(c, DaemonIsLinux) - name := "test-save-and-load-repo-flags" - dockerCmd(c, "run", "--name", name, "busybox", "true") + const name = "test-save-and-load-repo-flags" + cli.DockerCmd(c, "run", "--name", name, "busybox", "true") - repoName := "foobar-save-load-test" + const imgRepoName = "foobar-save-load-test" - deleteImages(repoName) - dockerCmd(c, "commit", name, repoName) + deleteImages(imgRepoName) + cli.DockerCmd(c, "commit", name, imgRepoName) - beforeStr, _, err := dockerCmdWithError("inspect", repoName) - assert.NilError(c, err, "failed to inspect before save") + beforeStr := cli.DockerCmd(c, "inspect", imgRepoName).Stdout() out, err := RunCommandPipelineWithOutput( - exec.Command(dockerBinary, "save", repoName), + exec.Command(dockerBinary, "save", imgRepoName), exec.Command(dockerBinary, "load")) assert.NilError(c, err, "failed to save and load repo: %s, %v", out, err) - afterStr, _, err := dockerCmdWithError("inspect", repoName) - assert.NilError(c, err, "failed to inspect after load") + afterStr := cli.DockerCmd(c, "inspect", imgRepoName).Stdout() var before, after []types.ImageInspect err = json.Unmarshal([]byte(beforeStr), &before) @@ -204,13 +203,13 @@ func (s *DockerCLISaveLoadSuite) TestSaveWithNoExistImage(c *testing.T) { func (s *DockerCLISaveLoadSuite) TestSaveMultipleNames(c *testing.T) { testRequires(c, DaemonIsLinux) - repoName := "foobar-save-multi-name-test" + const imgRepoName = "foobar-save-multi-name-test" - oneTag := fmt.Sprintf("%v-one:latest", repoName) - twoTag := fmt.Sprintf("%v-two:latest", repoName) + oneTag := fmt.Sprintf("%v-one:latest", imgRepoName) + twoTag := fmt.Sprintf("%v-two:latest", imgRepoName) - dockerCmd(c, "tag", "emptyfs:latest", oneTag) - dockerCmd(c, "tag", "emptyfs:latest", twoTag) + cli.DockerCmd(c, "tag", "emptyfs:latest", oneTag) + cli.DockerCmd(c, "tag", "emptyfs:latest", twoTag) out, err := RunCommandPipelineWithOutput( exec.Command(dockerBinary, "save", strings.TrimSuffix(oneTag, ":latest"), twoTag), @@ -233,7 +232,7 @@ func (s *DockerCLISaveLoadSuite) TestLoadZeroSizeLayer(c *testing.T) { // very weird test testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon) - dockerCmd(c, "load", "-i", "testdata/emptyLayer.tar") + cli.DockerCmd(c, "load", "-i", "testdata/emptyLayer.tar") } func (s *DockerCLISaveLoadSuite) TestSaveLoadParents(c *testing.T) { @@ -241,14 +240,13 @@ func (s *DockerCLISaveLoadSuite) TestSaveLoadParents(c *testing.T) { skip.If(c, testEnv.UsingSnapshotter(), "Parent image property is not supported with containerd") makeImage := func(from string, addfile string) string { - var out string - out, _ = dockerCmd(c, "run", "-d", from, "touch", addfile) - cleanedContainerID := strings.TrimSpace(out) + id := cli.DockerCmd(c, "run", "-d", from, "touch", addfile).Stdout() + id = strings.TrimSpace(id) - out, _ = dockerCmd(c, "commit", cleanedContainerID) - imageID := strings.TrimSpace(out) + imageID := cli.DockerCmd(c, "commit", id).Stdout() + imageID = strings.TrimSpace(imageID) - dockerCmd(c, "rm", "-f", cleanedContainerID) + cli.DockerCmd(c, "rm", "-f", id) return imageID } @@ -263,9 +261,9 @@ func (s *DockerCLISaveLoadSuite) TestSaveLoadParents(c *testing.T) { outfile := filepath.Join(tmpDir, "out.tar") - dockerCmd(c, "save", "-o", outfile, idBar, idFoo) - dockerCmd(c, "rmi", idBar) - dockerCmd(c, "load", "-i", outfile) + cli.DockerCmd(c, "save", "-o", outfile, idBar, idFoo) + cli.DockerCmd(c, "rmi", idBar) + cli.DockerCmd(c, "load", "-i", outfile) inspectOut := inspectField(c, idBar, "Parent") assert.Equal(c, inspectOut, idFoo) diff --git a/integration-cli/docker_cli_save_load_unix_test.go b/integration-cli/docker_cli_save_load_unix_test.go index 13e585d3f6..91f3e61706 100644 --- a/integration-cli/docker_cli_save_load_unix_test.go +++ b/integration-cli/docker_cli_save_load_unix_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/creack/pty" + "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/testutil" "gotest.tools/v3/assert" @@ -22,10 +23,10 @@ import ( // save a repo and try to load it using stdout func (s *DockerCLISaveLoadSuite) TestSaveAndLoadRepoStdout(c *testing.T) { name := "test-save-and-load-repo-stdout" - dockerCmd(c, "run", "--name", name, "busybox", "true") + cli.DockerCmd(c, "run", "--name", name, "busybox", "true") - repoName := "foobar-save-load-test" - before, _ := dockerCmd(c, "commit", name, repoName) + imgRepoName := "foobar-save-load-test" + before := cli.DockerCmd(c, "commit", name, imgRepoName).Stdout() before = strings.TrimRight(before, "\n") tmpFile, err := os.CreateTemp("", "foobar-save-load-test.tar") @@ -33,7 +34,7 @@ func (s *DockerCLISaveLoadSuite) TestSaveAndLoadRepoStdout(c *testing.T) { defer os.Remove(tmpFile.Name()) icmd.RunCmd(icmd.Cmd{ - Command: []string{dockerBinary, "save", repoName}, + Command: []string{dockerBinary, "save", imgRepoName}, Stdout: tmpFile, }).Assert(c, icmd.Success) @@ -41,23 +42,23 @@ func (s *DockerCLISaveLoadSuite) TestSaveAndLoadRepoStdout(c *testing.T) { assert.NilError(c, err) defer tmpFile.Close() - deleteImages(repoName) + deleteImages(imgRepoName) icmd.RunCmd(icmd.Cmd{ Command: []string{dockerBinary, "load"}, Stdin: tmpFile, }).Assert(c, icmd.Success) - after := inspectField(c, repoName, "Id") + after := inspectField(c, imgRepoName, "Id") after = strings.TrimRight(after, "\n") assert.Equal(c, after, before, "inspect is not the same after a save / load") - deleteImages(repoName) + deleteImages(imgRepoName) - pty, tty, err := pty.Open() + p, tty, err := pty.Open() assert.NilError(c, err) - cmd := exec.Command(dockerBinary, "save", repoName) + cmd := exec.Command(dockerBinary, "save", imgRepoName) cmd.Stdin = tty cmd.Stdout = tty cmd.Stderr = tty @@ -66,7 +67,7 @@ func (s *DockerCLISaveLoadSuite) TestSaveAndLoadRepoStdout(c *testing.T) { buf := make([]byte, 1024) - n, err := pty.Read(buf) + n, err := p.Read(buf) assert.NilError(c, err, "could not read tty output") assert.Assert(c, strings.Contains(string(buf[:n]), "cowardly refusing"), "help output is not being yielded") } @@ -81,19 +82,19 @@ func (s *DockerCLISaveLoadSuite) TestSaveAndLoadWithProgressBar(c *testing.T) { `)) tmptar := name + ".tar" - dockerCmd(c, "save", "-o", tmptar, name) + cli.DockerCmd(c, "save", "-o", tmptar, name) defer os.Remove(tmptar) - dockerCmd(c, "rmi", name) - dockerCmd(c, "tag", "busybox", name) - out, _ := dockerCmd(c, "load", "-i", tmptar) + cli.DockerCmd(c, "rmi", name) + cli.DockerCmd(c, "tag", "busybox", name) + out := cli.DockerCmd(c, "load", "-i", tmptar).Combined() expected := fmt.Sprintf("The image %s:latest already exists, renaming the old one with ID", name) assert.Assert(c, strings.Contains(out, expected)) } // fail because load didn't receive data from stdin func (s *DockerCLISaveLoadSuite) TestLoadNoStdinFail(c *testing.T) { - pty, tty, err := pty.Open() + p, tty, err := pty.Open() assert.NilError(c, err) ctx, cancel := context.WithTimeout(testutil.GetContext(c), 5*time.Second) defer cancel() @@ -105,7 +106,7 @@ func (s *DockerCLISaveLoadSuite) TestLoadNoStdinFail(c *testing.T) { buf := make([]byte, 1024) - n, err := pty.Read(buf) + n, err := p.Read(buf) assert.NilError(c, err) // could not read tty output assert.Assert(c, strings.Contains(string(buf[:n]), "requested load from stdin, but stdin is empty")) }