attach_test.go 1.4 KB

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