attach_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. )
  10. func TestAttachWithTTY(t *testing.T) {
  11. testAttach(t, true, types.MediaTypeRawStream)
  12. }
  13. func TestAttachWithoutTTy(t *testing.T) {
  14. testAttach(t, false, types.MediaTypeMultiplexedStream)
  15. }
  16. func testAttach(t *testing.T, tty bool, expected string) {
  17. defer setupTest(t)()
  18. client := testEnv.APIClient()
  19. resp, err := client.ContainerCreate(context.Background(),
  20. &container.Config{
  21. Image: "busybox",
  22. Cmd: []string{"echo", "hello"},
  23. Tty: tty,
  24. },
  25. &container.HostConfig{},
  26. &network.NetworkingConfig{},
  27. nil,
  28. "",
  29. )
  30. assert.NilError(t, err)
  31. container := resp.ID
  32. defer client.ContainerRemove(context.Background(), container, types.ContainerRemoveOptions{
  33. Force: true,
  34. })
  35. attach, err := client.ContainerAttach(context.Background(), container, types.ContainerAttachOptions{
  36. Stdout: true,
  37. Stderr: true,
  38. })
  39. assert.NilError(t, err)
  40. mediaType, ok := attach.MediaType()
  41. assert.Check(t, ok)
  42. assert.Check(t, mediaType == expected)
  43. }