attach_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "testing"
  4. "github.com/docker/docker/api/types"
  5. "github.com/docker/docker/api/types/container"
  6. "github.com/docker/docker/api/types/network"
  7. "github.com/docker/docker/testutil"
  8. "gotest.tools/v3/assert"
  9. is "gotest.tools/v3/assert/cmp"
  10. )
  11. func TestAttach(t *testing.T) {
  12. ctx := 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. ctx := testutil.StartSpan(ctx, t)
  34. resp, err := apiClient.ContainerCreate(ctx,
  35. &container.Config{
  36. Image: "busybox",
  37. Cmd: []string{"echo", "hello"},
  38. Tty: tc.tty,
  39. },
  40. &container.HostConfig{},
  41. &network.NetworkingConfig{},
  42. nil,
  43. "",
  44. )
  45. assert.NilError(t, err)
  46. attach, err := apiClient.ContainerAttach(ctx, resp.ID, container.AttachOptions{
  47. Stdout: true,
  48. Stderr: true,
  49. })
  50. assert.NilError(t, err)
  51. mediaType, ok := attach.MediaType()
  52. assert.Check(t, ok)
  53. assert.Check(t, is.Equal(mediaType, tc.expectedMediaType))
  54. })
  55. }
  56. }