logger_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package management
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/rs/zerolog"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. // No listening sessions will not write to the channel
  10. func TestLoggerWrite_NoSessions(t *testing.T) {
  11. logger := NewLogger()
  12. zlog := zerolog.New(logger).With().Timestamp().Logger().Level(zerolog.InfoLevel)
  13. zlog.Info().Msg("hello")
  14. }
  15. // Validate that the session receives the event
  16. func TestLoggerWrite_OneSession(t *testing.T) {
  17. logger := NewLogger()
  18. zlog := zerolog.New(logger).With().Timestamp().Logger().Level(zerolog.InfoLevel)
  19. _, cancel := context.WithCancel(context.Background())
  20. defer cancel()
  21. session := newSession(logWindow, actor{ID: actorID}, cancel)
  22. logger.Listen(session)
  23. defer logger.Remove(session)
  24. assert.Equal(t, 1, logger.ActiveSessions())
  25. assert.Equal(t, session, logger.ActiveSession(actor{ID: actorID}))
  26. zlog.Info().Int(EventTypeKey, int(HTTP)).Msg("hello")
  27. select {
  28. case event := <-session.listener:
  29. assert.NotEmpty(t, event.Time)
  30. assert.Equal(t, "hello", event.Message)
  31. assert.Equal(t, Info, event.Level)
  32. assert.Equal(t, HTTP, event.Event)
  33. default:
  34. assert.Fail(t, "expected an event to be in the listener")
  35. }
  36. }
  37. // Validate all sessions receive the same event
  38. func TestLoggerWrite_MultipleSessions(t *testing.T) {
  39. logger := NewLogger()
  40. zlog := zerolog.New(logger).With().Timestamp().Logger().Level(zerolog.InfoLevel)
  41. _, cancel := context.WithCancel(context.Background())
  42. defer cancel()
  43. session1 := newSession(logWindow, actor{}, cancel)
  44. logger.Listen(session1)
  45. defer logger.Remove(session1)
  46. assert.Equal(t, 1, logger.ActiveSessions())
  47. session2 := newSession(logWindow, actor{}, cancel)
  48. logger.Listen(session2)
  49. assert.Equal(t, 2, logger.ActiveSessions())
  50. zlog.Info().Int(EventTypeKey, int(HTTP)).Msg("hello")
  51. for _, session := range []*session{session1, session2} {
  52. select {
  53. case event := <-session.listener:
  54. assert.NotEmpty(t, event.Time)
  55. assert.Equal(t, "hello", event.Message)
  56. assert.Equal(t, Info, event.Level)
  57. assert.Equal(t, HTTP, event.Event)
  58. default:
  59. assert.Fail(t, "expected an event to be in the listener")
  60. }
  61. }
  62. // Close session2 and make sure session1 still receives events
  63. logger.Remove(session2)
  64. zlog.Info().Int(EventTypeKey, int(HTTP)).Msg("hello2")
  65. select {
  66. case event := <-session1.listener:
  67. assert.NotEmpty(t, event.Time)
  68. assert.Equal(t, "hello2", event.Message)
  69. assert.Equal(t, Info, event.Level)
  70. assert.Equal(t, HTTP, event.Event)
  71. default:
  72. assert.Fail(t, "expected an event to be in the listener")
  73. }
  74. // Make sure a held reference to session2 doesn't receive events after being closed
  75. select {
  76. case <-session2.listener:
  77. assert.Fail(t, "An event was not expected to be in the session listener")
  78. default:
  79. // pass
  80. }
  81. }
  82. type mockWriter struct {
  83. event *Log
  84. err error
  85. }
  86. func (m *mockWriter) Write(p []byte) (int, error) {
  87. m.event, m.err = parseZerologEvent(p)
  88. return len(p), nil
  89. }
  90. // Validate all event types are set properly
  91. func TestParseZerologEvent_EventTypes(t *testing.T) {
  92. writer := mockWriter{}
  93. zlog := zerolog.New(&writer).With().Timestamp().Logger().Level(zerolog.InfoLevel)
  94. for _, test := range []LogEventType{
  95. Cloudflared,
  96. HTTP,
  97. TCP,
  98. UDP,
  99. } {
  100. t.Run(test.String(), func(t *testing.T) {
  101. defer func() { writer.err = nil }()
  102. zlog.Info().Int(EventTypeKey, int(test)).Msg("test")
  103. require.NoError(t, writer.err)
  104. require.Equal(t, test, writer.event.Event)
  105. })
  106. }
  107. // Invalid defaults to Cloudflared LogEventType
  108. t.Run("invalid", func(t *testing.T) {
  109. defer func() { writer.err = nil }()
  110. zlog.Info().Str(EventTypeKey, "unknown").Msg("test")
  111. require.NoError(t, writer.err)
  112. require.Equal(t, Cloudflared, writer.event.Event)
  113. })
  114. }
  115. // Validate top-level keys are removed from Fields
  116. func TestParseZerologEvent_Fields(t *testing.T) {
  117. writer := mockWriter{}
  118. zlog := zerolog.New(&writer).With().Timestamp().Logger().Level(zerolog.InfoLevel)
  119. zlog.Info().Int(EventTypeKey, int(Cloudflared)).Str("test", "test").Msg("test message")
  120. require.NoError(t, writer.err)
  121. event := writer.event
  122. require.NotEmpty(t, event.Time)
  123. require.Equal(t, Cloudflared, event.Event)
  124. require.Equal(t, Info, event.Level)
  125. require.Equal(t, "test message", event.Message)
  126. // Make sure Fields doesn't have other set keys used in the Log struct
  127. require.NotEmpty(t, event.Fields)
  128. require.Equal(t, "test", event.Fields["test"])
  129. require.NotContains(t, event.Fields, EventTypeKey)
  130. require.NotContains(t, event.Fields, LevelKey)
  131. require.NotContains(t, event.Fields, MessageKey)
  132. require.NotContains(t, event.Fields, TimeKey)
  133. }