summaryrefslogtreecommitdiff
path: root/integration/container/attach_test.go
blob: bc7a659c7280142599225a6ec1c75930e7a81723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package container // import "github.com/docker/docker/integration/container"

import (
	"context"
	"testing"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/network"
	"gotest.tools/v3/assert"
)

func TestAttachWithTTY(t *testing.T) {
	testAttach(t, true, types.MediaTypeRawStream)
}

func TestAttachWithoutTTy(t *testing.T) {
	testAttach(t, false, types.MediaTypeMultiplexedStream)
}

func testAttach(t *testing.T, tty bool, expected string) {
	defer setupTest(t)()
	client := testEnv.APIClient()

	resp, err := client.ContainerCreate(context.Background(),
		&container.Config{
			Image: "busybox",
			Cmd:   []string{"echo", "hello"},
			Tty:   tty,
		},
		&container.HostConfig{},
		&network.NetworkingConfig{},
		nil,
		"",
	)
	assert.NilError(t, err)
	container := resp.ID
	defer client.ContainerRemove(context.Background(), container, types.ContainerRemoveOptions{
		Force: true,
	})

	attach, err := client.ContainerAttach(context.Background(), container, types.ContainerAttachOptions{
		Stdout: true,
		Stderr: true,
	})
	assert.NilError(t, err)
	mediaType, ok := attach.MediaType()
	assert.Check(t, ok)
	assert.Check(t, mediaType == expected)
}