integration-cli: DockerCLIAttachSuite: replace dockerCmd and waitRun

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-07-27 13:16:15 +02:00
parent 1baec48367
commit cf95278122
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 25 additions and 19 deletions

View file

@ -110,10 +110,9 @@ func (s *DockerCLIAttachSuite) TestAttachTTYWithoutStdin(c *testing.T) {
// will just fail and `MISS` all the other tests. For now, disabling it. Will
// open an issue to track re-enabling this and root-causing the problem.
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
out := cli.DockerCmd(c, "run", "-d", "-ti", "busybox").Stdout()
id := strings.TrimSpace(out)
assert.NilError(c, waitRun(id))
cli.WaitRun(c, id)
done := make(chan error, 1)
go func() {
@ -129,10 +128,17 @@ func (s *DockerCLIAttachSuite) TestAttachTTYWithoutStdin(c *testing.T) {
if runtime.GOOS == "windows" {
expected += ". If you are using mintty, try prefixing the command with 'winpty'"
}
if out, _, err := runCommandWithOutput(cmd); err == nil {
result := icmd.RunCmd(icmd.Cmd{
Command: cmd.Args,
Env: cmd.Env,
Dir: cmd.Dir,
Stdin: cmd.Stdin,
Stdout: cmd.Stdout,
})
if result.Error == nil {
done <- fmt.Errorf("attach should have failed")
return
} else if !strings.Contains(out, expected) {
} else if !strings.Contains(result.Combined(), expected) {
done <- fmt.Errorf("attach failed with error %q: expected %q", out, expected)
return
}
@ -148,7 +154,7 @@ func (s *DockerCLIAttachSuite) TestAttachTTYWithoutStdin(c *testing.T) {
func (s *DockerCLIAttachSuite) TestAttachDisconnect(c *testing.T) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-di", "busybox", "/bin/cat")
out := cli.DockerCmd(c, "run", "-di", "busybox", "/bin/cat").Stdout()
id := strings.TrimSpace(out)
cmd := exec.Command(dockerBinary, "attach", id)
@ -182,9 +188,9 @@ func (s *DockerCLIAttachSuite) TestAttachDisconnect(c *testing.T) {
func (s *DockerCLIAttachSuite) TestAttachPausedContainer(c *testing.T) {
testRequires(c, IsPausable)
runSleepingContainer(c, "-d", "--name=test")
dockerCmd(c, "pause", "test")
cli.DockerCmd(c, "pause", "test")
result := dockerCmdWithResult("attach", "test")
result := cli.Docker(cli.Args("attach", "test"))
result.Assert(c, icmd.Expected{
Error: "exit status 1",
ExitCode: 1,

View file

@ -11,6 +11,7 @@ import (
"time"
"github.com/creack/pty"
"github.com/docker/docker/integration-cli/cli"
"gotest.tools/v3/assert"
)
@ -18,12 +19,11 @@ import (
func (s *DockerCLIAttachSuite) TestAttachClosedOnContainerStop(c *testing.T) {
testRequires(c, testEnv.IsLocalDaemon)
out, _ := dockerCmd(c, "run", "-dti", "busybox", "/bin/sh", "-c", `trap 'exit 0' SIGTERM; while true; do sleep 1; done`)
out := cli.DockerCmd(c, "run", "-dti", "busybox", "/bin/sh", "-c", `trap 'exit 0' SIGTERM; while true; do sleep 1; done`).Stdout()
id := strings.TrimSpace(out)
assert.NilError(c, waitRun(id))
cli.WaitRun(c, id)
pty, tty, err := pty.Open()
pt, tty, err := pty.Open()
assert.NilError(c, err)
attachCmd := exec.Command(dockerBinary, "attach", id)
@ -38,19 +38,19 @@ func (s *DockerCLIAttachSuite) TestAttachClosedOnContainerStop(c *testing.T) {
time.Sleep(300 * time.Millisecond)
defer close(errChan)
// Container is waiting for us to signal it to stop
dockerCmd(c, "stop", id)
cli.DockerCmd(c, "stop", id)
// And wait for the attach command to end
errChan <- attachCmd.Wait()
}()
// Wait for the docker to end (should be done by the
// stop command in the go routine)
dockerCmd(c, "wait", id)
cli.DockerCmd(c, "wait", id)
select {
case err := <-errChan:
tty.Close()
out, _ := io.ReadAll(pty)
out, _ := io.ReadAll(pt)
assert.Assert(c, err == nil, "out: %v", string(out))
case <-time.After(attachWait):
c.Fatal("timed out without attach returning")
@ -73,7 +73,7 @@ func (s *DockerCLIAttachSuite) TestAttachAfterDetach(c *testing.T) {
close(cmdExit)
}()
assert.Assert(c, waitRun(name) == nil)
cli.WaitRun(c, name)
cpty.Write([]byte{16})
time.Sleep(100 * time.Millisecond)
@ -123,9 +123,9 @@ func (s *DockerCLIAttachSuite) TestAttachAfterDetach(c *testing.T) {
// TestAttachDetach checks that attach in tty mode can be detached using the long container ID
func (s *DockerCLIAttachSuite) TestAttachDetach(c *testing.T) {
out, _ := dockerCmd(c, "run", "-itd", "busybox", "cat")
out := cli.DockerCmd(c, "run", "-itd", "busybox", "cat").Stdout()
id := strings.TrimSpace(out)
assert.NilError(c, waitRun(id))
cli.WaitRun(c, id)
cpty, tty, err := pty.Open()
assert.NilError(c, err)
@ -138,7 +138,7 @@ func (s *DockerCLIAttachSuite) TestAttachDetach(c *testing.T) {
defer stdout.Close()
err = cmd.Start()
assert.NilError(c, err)
assert.NilError(c, waitRun(id))
cli.WaitRun(c, id)
_, err = cpty.Write([]byte("hello\n"))
assert.NilError(c, err)