read_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package logging
  2. import (
  3. "bytes"
  4. "runtime"
  5. "strings"
  6. "testing"
  7. "time"
  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"
  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. ctx := testutil.StartSpan(baseContext, t)
  22. d := daemon.New(t)
  23. d.StartWithBusybox(ctx, t, "--iptables=false")
  24. defer d.Stop(t)
  25. client, err := d.NewClient()
  26. assert.Assert(t, err)
  27. createPlugin(ctx, t, client, "test", "discard", asLogDriver)
  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. ctx := testutil.StartSpan(ctx, t)
  45. d.Start(t, append([]string{"--iptables=false"}, test.dOpts...)...)
  46. defer d.Stop(t)
  47. c, err := client.ContainerCreate(ctx,
  48. cfg,
  49. &container.HostConfig{LogConfig: container.LogConfig{Type: "test"}},
  50. nil,
  51. nil,
  52. "",
  53. )
  54. assert.Assert(t, err)
  55. defer client.ContainerRemove(ctx, c.ID, container.RemoveOptions{Force: true})
  56. err = client.ContainerStart(ctx, c.ID, container.StartOptions{})
  57. assert.Assert(t, err)
  58. logs, err := client.ContainerLogs(ctx, c.ID, container.LogsOptions{ShowStdout: true})
  59. if !test.logsSupported {
  60. assert.Assert(t, err != nil)
  61. return
  62. }
  63. assert.Assert(t, err)
  64. defer logs.Close()
  65. buf := bytes.NewBuffer(nil)
  66. errCh := make(chan error, 1)
  67. go func() {
  68. _, err := stdcopy.StdCopy(buf, buf, logs)
  69. errCh <- err
  70. }()
  71. select {
  72. case <-time.After(60 * time.Second):
  73. t.Fatal("timeout waiting for IO to complete")
  74. case err := <-errCh:
  75. assert.Assert(t, err)
  76. }
  77. assert.Assert(t, strings.TrimSpace(buf.String()) == "hello world", buf.Bytes())
  78. })
  79. }
  80. }