logging_linux_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package logging
  2. import (
  3. "bufio"
  4. "context"
  5. "os"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/docker/docker/api/types"
  10. containertypes "github.com/docker/docker/api/types/container"
  11. "github.com/docker/docker/integration/internal/container"
  12. "github.com/docker/docker/testutil"
  13. "github.com/docker/docker/testutil/daemon"
  14. "gotest.tools/v3/assert"
  15. "gotest.tools/v3/skip"
  16. )
  17. func TestContinueAfterPluginCrash(t *testing.T) {
  18. skip.If(t, testEnv.IsRemoteDaemon, "test requires daemon on the same host")
  19. t.Parallel()
  20. ctx := testutil.StartSpan(baseContext, t)
  21. d := daemon.New(t)
  22. d.StartWithBusybox(ctx, t, "--iptables=false", "--init")
  23. defer d.Stop(t)
  24. client := d.NewClientT(t)
  25. createPlugin(ctx, t, client, "test", "close_on_start", asLogDriver)
  26. ctxT, cancel := context.WithTimeout(ctx, 60*time.Second)
  27. defer cancel()
  28. assert.Assert(t, client.PluginEnable(ctxT, "test", types.PluginEnableOptions{Timeout: 30}))
  29. cancel()
  30. defer client.PluginRemove(ctx, "test", types.PluginRemoveOptions{Force: true})
  31. ctxT, cancel = context.WithTimeout(ctx, 60*time.Second)
  32. defer cancel()
  33. id := container.Run(ctxT, t, client,
  34. container.WithAutoRemove,
  35. container.WithLogDriver("test"),
  36. container.WithCmd(
  37. "/bin/sh", "-c", "while true; do sleep 1; echo hello; done",
  38. ),
  39. )
  40. cancel()
  41. defer client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{Force: true})
  42. // Attach to the container to make sure it's written a few times to stdout
  43. attach, err := client.ContainerAttach(ctx, id, containertypes.AttachOptions{Stream: true, Stdout: true})
  44. assert.NilError(t, err)
  45. chErr := make(chan error, 1)
  46. go func() {
  47. defer close(chErr)
  48. rdr := bufio.NewReader(attach.Reader)
  49. for i := 0; i < 5; i++ {
  50. _, _, err := rdr.ReadLine()
  51. if err != nil {
  52. chErr <- err
  53. return
  54. }
  55. }
  56. }()
  57. select {
  58. case err := <-chErr:
  59. assert.NilError(t, err)
  60. case <-time.After(60 * time.Second):
  61. t.Fatal("timeout waiting for container i/o")
  62. }
  63. // check daemon logs for "broken pipe"
  64. // TODO(@cpuguy83): This is horribly hacky but is the only way to really test this case right now.
  65. // It would be nice if there was a way to know that a broken pipe has occurred without looking through the logs.
  66. log, err := os.Open(d.LogFileName())
  67. assert.NilError(t, err)
  68. scanner := bufio.NewScanner(log)
  69. for scanner.Scan() {
  70. assert.Assert(t, !strings.Contains(scanner.Text(), "broken pipe"))
  71. }
  72. }