read_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package logging
  2. import (
  3. "bytes"
  4. "testing"
  5. "context"
  6. "time"
  7. "strings"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/pkg/stdcopy"
  11. "github.com/docker/docker/testutil/daemon"
  12. "gotest.tools/v3/assert"
  13. )
  14. // TestReadPluginNoRead tests that reads are supported even if the plugin isn't capable.
  15. func TestReadPluginNoRead(t *testing.T) {
  16. t.Parallel()
  17. d := daemon.New(t)
  18. d.StartWithBusybox(t, "--iptables=false")
  19. defer d.Stop(t)
  20. client, err := d.NewClient()
  21. assert.Assert(t, err)
  22. createPlugin(t, client, "test", "discard", asLogDriver)
  23. ctx := context.Background()
  24. defer func() {
  25. err = client.PluginRemove(ctx, "test", types.PluginRemoveOptions{Force: true})
  26. assert.Check(t, err)
  27. }()
  28. err = client.PluginEnable(ctx, "test", types.PluginEnableOptions{Timeout: 30})
  29. assert.Check(t, err)
  30. c, err := client.ContainerCreate(ctx,
  31. &container.Config{
  32. Image: "busybox",
  33. Cmd: []string{"/bin/echo", "hello world"},
  34. },
  35. &container.HostConfig{LogConfig: container.LogConfig{Type: "test"}},
  36. nil,
  37. "",
  38. )
  39. assert.Assert(t, err)
  40. err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
  41. assert.Assert(t, err)
  42. logs, err := client.ContainerLogs(ctx, c.ID, types.ContainerLogsOptions{ShowStdout: true})
  43. assert.Assert(t, err)
  44. defer logs.Close()
  45. buf := bytes.NewBuffer(nil)
  46. errCh := make(chan error)
  47. go func() {
  48. _, err := stdcopy.StdCopy(buf, buf, logs)
  49. errCh <- err
  50. }()
  51. select {
  52. case <-time.After(60 * time.Second):
  53. t.Fatal("timeout waiting for IO to complete")
  54. case err := <-errCh:
  55. assert.Assert(t, err)
  56. }
  57. assert.Assert(t, strings.TrimSpace(buf.String()) == "hello world", buf.Bytes())
  58. }