github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/integration/container/attach_test.go (about)

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/docker/api/types"
     7  	"github.com/docker/docker/api/types/container"
     8  	"github.com/docker/docker/api/types/network"
     9  	"github.com/docker/docker/testutil"
    10  	"gotest.tools/v3/assert"
    11  	is "gotest.tools/v3/assert/cmp"
    12  )
    13  
    14  func TestAttach(t *testing.T) {
    15  	ctx := setupTest(t)
    16  	apiClient := testEnv.APIClient()
    17  
    18  	tests := []struct {
    19  		doc               string
    20  		tty               bool
    21  		expectedMediaType string
    22  	}{
    23  		{
    24  			doc:               "without TTY",
    25  			expectedMediaType: types.MediaTypeMultiplexedStream,
    26  		},
    27  		{
    28  			doc:               "with TTY",
    29  			tty:               true,
    30  			expectedMediaType: types.MediaTypeRawStream,
    31  		},
    32  	}
    33  	for _, tc := range tests {
    34  		tc := tc
    35  		t.Run(tc.doc, func(t *testing.T) {
    36  			t.Parallel()
    37  
    38  			ctx := testutil.StartSpan(ctx, t)
    39  			resp, err := apiClient.ContainerCreate(ctx,
    40  				&container.Config{
    41  					Image: "busybox",
    42  					Cmd:   []string{"echo", "hello"},
    43  					Tty:   tc.tty,
    44  				},
    45  				&container.HostConfig{},
    46  				&network.NetworkingConfig{},
    47  				nil,
    48  				"",
    49  			)
    50  			assert.NilError(t, err)
    51  			attach, err := apiClient.ContainerAttach(ctx, resp.ID, container.AttachOptions{
    52  				Stdout: true,
    53  				Stderr: true,
    54  			})
    55  			assert.NilError(t, err)
    56  			mediaType, ok := attach.MediaType()
    57  			assert.Check(t, ok)
    58  			assert.Check(t, is.Equal(mediaType, tc.expectedMediaType))
    59  		})
    60  	}
    61  }