logging_linux_test.go 2.2 KB

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