Files
moby/integration/container/attach_test.go
Sebastiaan van Stijn 53d78d73e6 integration/container: remove redundant capturing of loop vars (copyloopvar)
integration/container/attach_test.go:39:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/container_test.go:28:3: The copy of the 'for' variable "ep" can be deleted (Go 1.22+) (copyloopvar)
            ep := ep
            ^
    integration/container/create_test.go:57:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/create_test.go:120:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/create_test.go:406:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/create_test.go:583:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/exec_test.go:218:4: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
                tc := tc
                ^
    integration/container/kill_test.go:70:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/kill_test.go:110:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/logs_test.go:130:3: The copy of the 'for' variable "tC" can be deleted (Go 1.22+) (copyloopvar)
            tC := tC
            ^
    integration/container/overlayfs_linux_test.go:59:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/resize_test.go:107:4: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
                tc := tc
                ^
    integration/container/restart_test.go:78:5: The copy of the 'for' variable "stopDaemon" can be deleted (Go 1.22+) (copyloopvar)
                    stopDaemon := stopDaemon
                    ^
    integration/container/restart_test.go:188:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/run_linux_test.go:341:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/stop_linux_test.go:58:3: The copy of the 'for' variable "d" can be deleted (Go 1.22+) (copyloopvar)
            d := d
            ^
    integration/container/wait_test.go:40:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/wait_test.go:83:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/wait_test.go:133:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^
    integration/container/wait_test.go:205:3: The copy of the 'for' variable "tc" can be deleted (Go 1.22+) (copyloopvar)
            tc := tc
            ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-12 14:02:13 +01:00

121 lines
3.0 KiB
Go

package container // import "github.com/docker/docker/integration/container"
import (
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
systemutil "github.com/docker/docker/integration/internal/system"
"github.com/docker/docker/testutil"
"github.com/docker/docker/testutil/daemon"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/poll"
"gotest.tools/v3/skip"
)
func TestAttach(t *testing.T) {
ctx := setupTest(t)
apiClient := testEnv.APIClient()
tests := []struct {
doc string
tty bool
expectedMediaType string
}{
{
doc: "without TTY",
expectedMediaType: types.MediaTypeMultiplexedStream,
},
{
doc: "with TTY",
tty: true,
expectedMediaType: types.MediaTypeRawStream,
},
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
t.Parallel()
ctx := testutil.StartSpan(ctx, t)
resp, err := apiClient.ContainerCreate(ctx,
&container.Config{
Image: "busybox",
Cmd: []string{"echo", "hello"},
Tty: tc.tty,
},
&container.HostConfig{},
&network.NetworkingConfig{},
nil,
"",
)
assert.NilError(t, err)
attach, err := apiClient.ContainerAttach(ctx, resp.ID, container.AttachOptions{
Stdout: true,
Stderr: true,
})
assert.NilError(t, err)
mediaType, ok := attach.MediaType()
assert.Check(t, ok)
assert.Check(t, is.Equal(mediaType, tc.expectedMediaType))
})
}
}
// Regression test for #37182
func TestAttachDisconnectLeak(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType != "linux", "Bug still exists on Windows")
t.Parallel()
ctx := testutil.StartSpan(baseContext, t)
// Use a new daemon to make sure stuff from other tests isn't affecting the
// goroutine count.
d := daemon.New(t)
defer d.Cleanup(t)
d.StartWithBusybox(ctx, t, "--iptables=false", "--ip6tables=false")
client := d.NewClientT(t)
resp, err := client.ContainerCreate(ctx,
&container.Config{
Image: "busybox",
Cmd: []string{"/bin/sh", "-c", "while true; usleep 100000; done"},
},
&container.HostConfig{},
&network.NetworkingConfig{},
nil,
"",
)
assert.NilError(t, err)
cID := resp.ID
defer client.ContainerRemove(ctx, cID, container.RemoveOptions{
Force: true,
})
nGoroutines := systemutil.WaitForStableGoroutineCount(ctx, t, client)
attach, err := client.ContainerAttach(ctx, cID, container.AttachOptions{
Stdout: true,
})
assert.NilError(t, err)
defer attach.Close()
poll.WaitOn(t, func(_ poll.LogT) poll.Result {
count := systemutil.WaitForStableGoroutineCount(ctx, t, client)
if count > nGoroutines {
return poll.Success()
}
return poll.Continue("waiting for goroutines to increase from %d, current: %d", nGoroutines, count)
},
poll.WithTimeout(time.Minute),
)
attach.Close()
poll.WaitOn(t, systemutil.CheckGoroutineCount(ctx, client, nGoroutines), poll.WithTimeout(time.Minute))
}