integration-cli: remove startContainerGetOutput, runCommandWithOutput

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-09-17 12:01:02 +02:00
parent 2b4f5592d2
commit 5028ff1f40
6 changed files with 49 additions and 57 deletions

View File

@@ -5,7 +5,6 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@@ -14,6 +13,7 @@ import (
"github.com/moby/moby/v2/integration-cli/cli" "github.com/moby/moby/v2/integration-cli/cli"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
) )
func (s *DockerCLICpSuite) TestCpToContainerWithPermissions(c *testing.T) { func (s *DockerCLICpSuite) TestCpToContainerWithPermissions(c *testing.T) {
@@ -33,11 +33,12 @@ func (s *DockerCLICpSuite) TestCpToContainerWithPermissions(c *testing.T) {
srcPath := cpPath(tmpDir, "permdirtest") srcPath := cpPath(tmpDir, "permdirtest")
dstPath := containerCpPath(containerName, "/") dstPath := containerCpPath(containerName, "/")
args := []string{"cp", "-a", srcPath, dstPath} res := icmd.RunCommand(dockerBinary, "cp", "-a", srcPath, dstPath)
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, args...)) out, err := res.Combined(), res.Error
assert.NilError(c, err, "output: %v", out) assert.NilError(c, err, "output: %v", out)
out, err = startContainerGetOutput(c, containerName) res = icmd.RunCommand(dockerBinary, "start", "-a", containerName)
out, err = res.Combined(), res.Error
assert.NilError(c, err, "output: %v", out) assert.NilError(c, err, "output: %v", out)
assert.Equal(c, strings.TrimSpace(out), "2 2 700\n65534 65534 400", "output: %v", out) assert.Equal(c, strings.TrimSpace(out), "2 2 700\n65534 65534 400", "output: %v", out)
} }

View File

@@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@@ -15,6 +14,7 @@ import (
"github.com/moby/moby/v2/integration-cli/cli" "github.com/moby/moby/v2/integration-cli/cli"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp" is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/icmd"
) )
type fileType uint32 type fileType uint32
@@ -195,26 +195,14 @@ func containerCpPathTrailingSep(containerID string, pathElements ...string) stri
func runDockerCp(t *testing.T, src, dst string) error { func runDockerCp(t *testing.T, src, dst string) error {
t.Helper() t.Helper()
args := []string{"cp", src, dst} res := icmd.RunCommand(dockerBinary, "cp", src, dst)
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, args...)); err != nil { out, err := res.Combined(), res.Error
if err != nil {
return fmt.Errorf("error executing `docker cp` command: %s: %s", err, out) return fmt.Errorf("error executing `docker cp` command: %s: %s", err, out)
} }
return nil return nil
} }
func startContainerGetOutput(t *testing.T, containerID string) (string, error) {
t.Helper()
args := []string{"start", "-a", containerID}
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, args...))
if err != nil {
return "", fmt.Errorf("error executing `docker start` command: %s: %s", err, out)
}
return out, nil
}
func getTestDir(t *testing.T, label string) (tmpDir string) { func getTestDir(t *testing.T, label string) (tmpDir string) {
t.Helper() t.Helper()
var err error var err error
@@ -269,18 +257,18 @@ func symlinkTargetEquals(t *testing.T, symlink, expectedTarget string) error {
return nil return nil
} }
// TODO(thaJeztah): deprecate and replace uses with [icmd.RunCommand.Assert(icmd.Expected)]
func containerStartOutputEquals(t *testing.T, containerID, contents string) error { func containerStartOutputEquals(t *testing.T, containerID, contents string) error {
t.Helper() t.Helper()
out, err := startContainerGetOutput(t, containerID) res := icmd.RunCommand(dockerBinary, "start", "-a", containerID)
out, err := res.Combined(), res.Error
if err != nil { if err != nil {
return err return err
} }
if out != contents { if out != contents {
return fmt.Errorf("output contents not equal - expected %q, got %q", contents, out) return fmt.Errorf("output contents not equal - expected %q, got %q", contents, out)
} }
return nil return nil
} }

View File

@@ -8,7 +8,6 @@ import (
"os" "os"
"os/exec" "os/exec"
"reflect" "reflect"
"runtime"
"sort" "sort"
"strings" "strings"
"sync" "sync"
@@ -165,10 +164,18 @@ func (s *DockerCLIExecSuite) TestExecTTYCloseStdin(c *testing.T) {
stdinRw, err := cmd.StdinPipe() stdinRw, err := cmd.StdinPipe()
assert.NilError(c, err) assert.NilError(c, err)
stdinRw.Write([]byte("test")) _, err = stdinRw.Write([]byte("test"))
stdinRw.Close() assert.Check(c, err)
_ = stdinRw.Close()
out, _, err := runCommandWithOutput(cmd) res := icmd.RunCmd(icmd.Cmd{
Command: cmd.Args,
Env: cmd.Env,
Dir: cmd.Dir,
Stdin: cmd.Stdin,
Stdout: cmd.Stdout,
})
out, err := res.Combined(), res.Error
assert.NilError(c, err, out) assert.NilError(c, err, out)
out = cli.DockerCmd(c, "top", "exec_tty_stdin").Combined() out = cli.DockerCmd(c, "top", "exec_tty_stdin").Combined()
@@ -193,10 +200,16 @@ func (s *DockerCLIExecSuite) TestExecTTYWithoutStdin(c *testing.T) {
} }
expected := "the input device is not a TTY" expected := "the input device is not a TTY"
if runtime.GOOS == "windows" {
expected += ". If you are using mintty, try prefixing the command with 'winpty'" res := icmd.RunCmd(icmd.Cmd{
} Command: cmd.Args,
if out, _, err := runCommandWithOutput(cmd); err == nil { Env: cmd.Env,
Dir: cmd.Dir,
Stdin: cmd.Stdin,
Stdout: cmd.Stdout,
})
out, err := res.Combined(), res.Error
if err == nil {
errChan <- errors.New("exec should have failed") errChan <- errors.New("exec should have failed")
return return
} else if !strings.Contains(out, expected) { } else if !strings.Contains(out, expected) {

View File

@@ -15,7 +15,6 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"regexp" "regexp"
"runtime"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@@ -2407,10 +2406,15 @@ func (s *DockerCLIRunSuite) TestRunTTYWithPipe(c *testing.T) {
} }
expected := "the input device is not a TTY" expected := "the input device is not a TTY"
if runtime.GOOS == "windows" { res := icmd.RunCmd(icmd.Cmd{
expected += ". If you are using mintty, try prefixing the command with 'winpty'" Command: cmd.Args,
} Env: cmd.Env,
if out, _, err := runCommandWithOutput(cmd); err == nil { Dir: cmd.Dir,
Stdin: cmd.Stdin,
Stdout: cmd.Stdout,
})
out, err := res.Combined(), res.Error
if err == nil {
errChan <- errors.New("run should have failed") errChan <- errors.New("run should have failed")
return return
} else if !strings.Contains(out, expected) { } else if !strings.Contains(out, expected) {

View File

@@ -128,15 +128,15 @@ func (s *DockerCLISaveLoadSuite) TestSaveImageId(c *testing.T) {
assert.Assert(c, cleanedLongImageID != "", "Id should not be empty.") assert.Assert(c, cleanedLongImageID != "", "Id should not be empty.")
assert.Assert(c, cleanedShortImageID != "", "Id should not be empty.") assert.Assert(c, cleanedShortImageID != "", "Id should not be empty.")
// TODO(thaJeztah): this fails with full image ID
saveCmd := exec.Command(dockerBinary, "save", cleanedShortImageID) saveCmd := exec.Command(dockerBinary, "save", cleanedShortImageID)
tarCmd := exec.Command("tar", "t") tarCmd := exec.Command("tar", "t")
var err error var err error
tarCmd.Stdin, err = saveCmd.StdoutPipe() tarCmd.Stdin, err = saveCmd.StdoutPipe()
assert.Assert(c, err == nil, "cannot set stdout pipe for tar: %v", err) assert.NilError(c, err, "cannot set stdin pipe for tar: %v", err)
grepCmd := exec.Command("grep", cleanedLongImageID) tarStream, err := tarCmd.StdoutPipe()
grepCmd.Stdin, err = tarCmd.StdoutPipe() assert.NilError(c, err, "get tar output pipe: %v", err)
assert.Assert(c, err == nil, "cannot set stdout pipe for grep: %v", err)
assert.Assert(c, tarCmd.Start() == nil, "tar failed with error: %v", err) assert.Assert(c, tarCmd.Start() == nil, "tar failed with error: %v", err)
assert.Assert(c, saveCmd.Start() == nil, "docker save failed with error: %v", err) assert.Assert(c, saveCmd.Start() == nil, "docker save failed with error: %v", err)
@@ -146,9 +146,9 @@ func (s *DockerCLISaveLoadSuite) TestSaveImageId(c *testing.T) {
cli.DockerCmd(c, "rmi", imgRepoName) cli.DockerCmd(c, "rmi", imgRepoName)
}() }()
out, _, err = runCommandWithOutput(grepCmd) res := icmd.RunCmd(icmd.Command("grep", "blobs/sha256/"+cleanedLongImageID), icmd.WithStdin(tarStream))
out, err = res.Combined(), res.Error
assert.Assert(c, err == nil, "failed to save repo with image ID: %s, %v", out, err) assert.NilError(c, err, "failed to save repo with image ID: %s, %v", out, err)
} }
// save a repo and try to load it using flags // save a repo and try to load it using flags

View File

@@ -11,7 +11,6 @@ import (
"github.com/moby/moby/v2/integration-cli/cli" "github.com/moby/moby/v2/integration-cli/cli"
"github.com/moby/moby/v2/internal/testutil" "github.com/moby/moby/v2/internal/testutil"
"github.com/pkg/errors" "github.com/pkg/errors"
"gotest.tools/v3/icmd"
) )
func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) { func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
@@ -21,19 +20,6 @@ func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
return "", "/" return "", "/"
} }
// TODO: update code to call cmd.RunCmd directly, and remove this function
// Deprecated: use gotest.tools/icmd
func runCommandWithOutput(execCmd *exec.Cmd) (string, int, error) {
result := icmd.RunCmd(icmd.Cmd{
Command: execCmd.Args,
Env: execCmd.Env,
Dir: execCmd.Dir,
Stdin: execCmd.Stdin,
Stdout: execCmd.Stdout,
})
return result.Combined(), result.ExitCode, result.Error
}
// ParseCgroupPaths parses 'procCgroupData', which is output of '/proc/<pid>/cgroup', and returns // ParseCgroupPaths parses 'procCgroupData', which is output of '/proc/<pid>/cgroup', and returns
// a map which cgroup name as key and path as value. // a map which cgroup name as key and path as value.
func ParseCgroupPaths(procCgroupData string) map[string]string { func ParseCgroupPaths(procCgroupData string) map[string]string {