read_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package logging
  2. import (
  3. "bytes"
  4. "context"
  5. "runtime"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/api/types/container"
  11. "github.com/docker/docker/pkg/stdcopy"
  12. "github.com/docker/docker/testutil/daemon"
  13. "gotest.tools/v3/assert"
  14. )
  15. // TestReadPluginNoRead tests that reads are supported even if the plugin isn't capable.
  16. func TestReadPluginNoRead(t *testing.T) {
  17. if runtime.GOOS == "windows" {
  18. t.Skip("no unix domain sockets on Windows")
  19. }
  20. t.Parallel()
  21. d := daemon.New(t)
  22. d.StartWithBusybox(t, "--iptables=false")
  23. defer d.Stop(t)
  24. client, err := d.NewClient()
  25. assert.Assert(t, err)
  26. createPlugin(t, client, "test", "discard", asLogDriver)
  27. ctx := context.Background()
  28. err = client.PluginEnable(ctx, "test", types.PluginEnableOptions{Timeout: 30})
  29. assert.Check(t, err)
  30. d.Stop(t)
  31. cfg := &container.Config{
  32. Image: "busybox",
  33. Cmd: []string{"/bin/echo", "hello world"},
  34. }
  35. for desc, test := range map[string]struct {
  36. dOpts []string
  37. logsSupported bool
  38. }{
  39. "default": {logsSupported: true},
  40. "disabled caching": {[]string{"--log-opt=cache-disabled=true"}, false},
  41. "explicitly enabled caching": {[]string{"--log-opt=cache-disabled=false"}, true},
  42. } {
  43. t.Run(desc, func(t *testing.T) {
  44. d.Start(t, append([]string{"--iptables=false"}, test.dOpts...)...)
  45. defer d.Stop(t)
  46. c, err := client.ContainerCreate(ctx,
  47. cfg,
  48. &container.HostConfig{LogConfig: container.LogConfig{Type: "test"}},
  49. nil,
  50. nil,
  51. "",
  52. )
  53. assert.Assert(t, err)
  54. defer client.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{Force: true})
  55. err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
  56. assert.Assert(t, err)
  57. logs, err := client.ContainerLogs(ctx, c.ID, types.ContainerLogsOptions{ShowStdout: true})
  58. if !test.logsSupported {
  59. assert.Assert(t, err != nil)
  60. return
  61. }
  62. assert.Assert(t, err)
  63. defer logs.Close()
  64. buf := bytes.NewBuffer(nil)
  65. errCh := make(chan error, 1)
  66. go func() {
  67. _, err := stdcopy.StdCopy(buf, buf, logs)
  68. errCh <- err
  69. }()
  70. select {
  71. case <-time.After(60 * time.Second):
  72. t.Fatal("timeout waiting for IO to complete")
  73. case err := <-errCh:
  74. assert.Assert(t, err)
  75. }
  76. assert.Assert(t, strings.TrimSpace(buf.String()) == "hello world", buf.Bytes())
  77. })
  78. }
  79. }