logging_test.go 2.4 KB

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