1
0
mirror of https://github.com/moby/moby.git synced 2025-04-18 20:44:11 +03:00
moby/integration-cli/docker_cli_attach_test.go
Aleksa Sarai 0553d3d994
tests: migrate away from assert.Assert(err == nil)
Unfortunately, gofmt doesn't know about types so it was necessary to
find all of the err == nil statements through trial and error. Note that
there is no is.NilError, so for assert.Check(t, err == nil) we need to
switch to just doing assert.Check(t, err). If err is an error type, this
is equivalent (and there isn't another trivial way of doing it). Here
are the full set of rules used:

Generic "err == nil":

  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, err == nil) -> assert.NilError(t, err)"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, err == nil) -> assert.Check(t, err)"

Generic, but with a different variable name:

  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, sr.err == nil) -> assert.NilError(t, sr.err)"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, sr.err == nil) -> assert.Check(t, sr.err)"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, err2 == nil) -> assert.NilError(t, err2)"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, err2 == nil) -> assert.Check(t, err2)"

JSON-related error assertions:

  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, json.Unmarshal(a, b) == nil) -> assert.NilError(t, json.Unmarshal(a, b))"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, json.Unmarshal(a, b) == nil) -> assert.Check(t, json.Unmarshal(a, b))"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, json.NewDecoder(a).Decode(b) == nil) -> assert.NilError(t, json.NewDecoder(a).Decode(b))"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, json.NewDecoder(a).Decode(b) == nil) -> assert.Check(t, json.NewDecoder(a).Decode(b))"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, json.NewEncoder(a).Encode(b) == nil) -> assert.NilError(t, json.NewEncoder(a).Encode(b))"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, json.NewEncoder(a).Encode(b) == nil) -> assert.Check(t, json.NewEncoder(a).Encode(b))"

Process-related error assertions:

  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, a.Start() == nil) -> assert.NilError(t, a.Start())"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, a.Start() == nil) -> assert.Check(t, a.Start())"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, a.Kill() == nil) -> assert.NilError(t, a.Kill())"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, a.Kill() == nil) -> assert.Check(t, a.Kill())"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, a.Signal(b) == nil) -> assert.NilError(t, a.Signal(b))"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, a.Signal(b) == nil) -> assert.Check(t, a.Signal(b))"

waitInspect:

  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, waitInspect(a, b, c, d) == nil) -> assert.NilError(t, waitInspect(a, b, c, d))"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, waitInspect(a, b, c, d) == nil) -> assert.Check(t, waitInspect(a, b, c, d))"

File closing error assertions:

  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, a.Close() == nil) -> assert.NilError(t, a.Close())"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, a.Close() == nil) -> assert.Check(t, a.Close())"

mount.MakeRShared:

  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, mount.MakeRShared(a) == nil) -> assert.NilError(t, mount.MakeRShared(a))"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, mount.MakeRShared(a) == nil) -> assert.Check(t, mount.MakeRShared(a))"

daemon.SwarmLeave:

  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, d.SwarmLeave(a, b, c) == nil) -> assert.NilError(t, d.SwarmLeave(a, b, c))"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, d.SwarmLeave(a, b, c) == nil) -> assert.Check(t, d.SwarmLeave(a, b, c))"

os.MkdirAll:

  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Assert(t, os.MkdirAll(a, b) == nil) -> assert.NilError(t, os.MkdirAll(a, b))"
  find . -type f -name "*_test.go" | \
    xargs gofmt -w -r "assert.Check(t, os.MkdirAll(a, b) == nil) -> assert.Check(t, os.MkdirAll(a, b))"

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2024-11-22 23:59:20 +11:00

200 lines
4.7 KiB
Go

package main
import (
"bufio"
"context"
"fmt"
"io"
"os/exec"
"runtime"
"strings"
"sync"
"testing"
"time"
"github.com/docker/docker/integration-cli/cli"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
)
const attachWait = 5 * time.Second
type DockerCLIAttachSuite struct {
ds *DockerSuite
}
func (s *DockerCLIAttachSuite) TearDownTest(ctx context.Context, c *testing.T) {
s.ds.TearDownTest(ctx, c)
}
func (s *DockerCLIAttachSuite) OnTimeout(c *testing.T) {
s.ds.OnTimeout(c)
}
func (s *DockerCLIAttachSuite) TestAttachMultipleAndRestart(c *testing.T) {
endGroup := &sync.WaitGroup{}
startGroup := &sync.WaitGroup{}
endGroup.Add(3)
startGroup.Add(3)
cli.DockerCmd(c, "run", "--name", "attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done")
cli.WaitRun(c, "attacher")
startDone := make(chan struct{})
endDone := make(chan struct{})
go func() {
endGroup.Wait()
close(endDone)
}()
go func() {
startGroup.Wait()
close(startDone)
}()
for i := 0; i < 3; i++ {
go func() {
cmd := exec.Command(dockerBinary, "attach", "attacher")
defer func() {
cmd.Wait()
endGroup.Done()
}()
out, err := cmd.StdoutPipe()
if err != nil {
c.Error(err)
}
defer out.Close()
if err := cmd.Start(); err != nil {
c.Error(err)
}
buf := make([]byte, 1024)
if _, err := out.Read(buf); err != nil && err != io.EOF {
c.Error(err)
}
startGroup.Done()
if !strings.Contains(string(buf), "hello") {
c.Errorf("unexpected output %s expected hello\n", string(buf))
}
}()
}
select {
case <-startDone:
case <-time.After(attachWait):
c.Fatalf("Attaches did not initialize properly")
}
cli.DockerCmd(c, "kill", "attacher")
select {
case <-endDone:
case <-time.After(attachWait):
c.Fatalf("Attaches did not finish properly")
}
}
func (s *DockerCLIAttachSuite) TestAttachTTYWithoutStdin(c *testing.T) {
// TODO: Figure out how to get this running again reliable on Windows.
// It works by accident at the moment. Sometimes. I've gone back to v1.13.0 and see the same.
// On Windows, docker run -d -ti busybox causes the container to exit immediately.
// Obviously a year back when I updated the test, that was not the case. However,
// with this, and the test racing with the tear-down which panic's, sometimes CI
// 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 := cli.DockerCmd(c, "run", "-d", "-ti", "busybox").Stdout()
id := strings.TrimSpace(out)
cli.WaitRun(c, id)
done := make(chan error, 1)
go func() {
defer close(done)
cmd := exec.Command(dockerBinary, "attach", id)
if _, err := cmd.StdinPipe(); err != nil {
done <- err
return
}
expected := "the input device is not a TTY"
if runtime.GOOS == "windows" {
expected += ". If you are using mintty, try prefixing the command with 'winpty'"
}
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(result.Combined(), expected) {
done <- fmt.Errorf("attach failed with error %q: expected %q", out, expected)
return
}
}()
select {
case err := <-done:
assert.NilError(c, err)
case <-time.After(attachWait):
c.Fatal("attach is running but should have failed")
}
}
func (s *DockerCLIAttachSuite) TestAttachDisconnect(c *testing.T) {
testRequires(c, DaemonIsLinux)
out := cli.DockerCmd(c, "run", "-di", "busybox", "/bin/cat").Stdout()
id := strings.TrimSpace(out)
cmd := exec.Command(dockerBinary, "attach", id)
stdin, err := cmd.StdinPipe()
if err != nil {
c.Fatal(err)
}
defer stdin.Close()
stdout, err := cmd.StdoutPipe()
assert.NilError(c, err)
defer stdout.Close()
assert.NilError(c, cmd.Start())
defer func() {
cmd.Process.Kill()
cmd.Wait()
}()
_, err = stdin.Write([]byte("hello\n"))
assert.NilError(c, err)
out, err = bufio.NewReader(stdout).ReadString('\n')
assert.NilError(c, err)
assert.Equal(c, strings.TrimSpace(out), "hello")
assert.NilError(c, stdin.Close())
// Expect container to still be running after stdin is closed
running := inspectField(c, id, "State.Running")
assert.Equal(c, running, "true")
}
func (s *DockerCLIAttachSuite) TestAttachPausedContainer(c *testing.T) {
testRequires(c, IsPausable)
runSleepingContainer(c, "-d", "--name=test")
cli.DockerCmd(c, "pause", "test")
result := cli.Docker(cli.Args("attach", "test"))
result.Assert(c, icmd.Expected{
Error: "exit status 1",
ExitCode: 1,
Err: "You cannot attach to a paused container, unpause it first",
})
}