| 1 |
package nebula |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"log/slog" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/slackhq/nebula/config" |
| 10 |
"github.com/slackhq/nebula/test" |
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
) |
| 14 |
|
| 15 |
func TestNewPunchyFromConfig(t *testing.T) { |
| 16 |
l := test.NewLogger() |
| 17 |
c := config.NewC(l) |
| 18 |
|
| 19 |
// Test defaults |
| 20 |
p := NewPunchyFromConfig(test.NewLogger(), c, nil) |
| 21 |
assert.False(t, p.punch.Load()) |
| 22 |
assert.False(t, p.respond.Load()) |
| 23 |
assert.Equal(t, time.Second, time.Duration(p.delay.Load())) |
| 24 |
assert.Equal(t, 5*time.Second, time.Duration(p.respondDelay.Load())) |
| 25 |
|
| 26 |
// punchy deprecation |
| 27 |
c.Settings["punchy"] = true |
| 28 |
p = NewPunchyFromConfig(test.NewLogger(), c, nil) |
| 29 |
assert.True(t, p.punch.Load()) |
| 30 |
|
| 31 |
// punchy.punch |
| 32 |
c.Settings["punchy"] = map[string]any{"punch": true} |
| 33 |
p = NewPunchyFromConfig(test.NewLogger(), c, nil) |
| 34 |
assert.True(t, p.punch.Load()) |
| 35 |
|
| 36 |
// punch_back deprecation |
| 37 |
c.Settings["punch_back"] = true |
| 38 |
p = NewPunchyFromConfig(test.NewLogger(), c, nil) |
| 39 |
assert.True(t, p.respond.Load()) |
| 40 |
|
| 41 |
// punchy.respond |
| 42 |
c.Settings["punchy"] = map[string]any{"respond": true} |
| 43 |
c.Settings["punch_back"] = false |
| 44 |
p = NewPunchyFromConfig(test.NewLogger(), c, nil) |
| 45 |
assert.True(t, p.respond.Load()) |
| 46 |
|
| 47 |
// punchy.delay |
| 48 |
c.Settings["punchy"] = map[string]any{"delay": "1m"} |
| 49 |
p = NewPunchyFromConfig(test.NewLogger(), c, nil) |
| 50 |
assert.Equal(t, time.Minute, time.Duration(p.delay.Load())) |
| 51 |
|
| 52 |
// punchy.respond_delay |
| 53 |
c.Settings["punchy"] = map[string]any{"respond_delay": "1m"} |
| 54 |
p = NewPunchyFromConfig(test.NewLogger(), c, nil) |
| 55 |
assert.Equal(t, time.Minute, time.Duration(p.respondDelay.Load())) |
| 56 |
} |
| 57 |
|
| 58 |
func TestPunchy_reload(t *testing.T) { |
| 59 |
l := test.NewLogger() |
| 60 |
c := config.NewC(l) |
| 61 |
delay, _ := time.ParseDuration("1m") |
| 62 |
require.NoError(t, c.LoadString(` |
| 63 |
punchy: |
| 64 |
punch: false |
| 65 |
delay: 1m |
| 66 |
respond: false |
| 67 |
`)) |
| 68 |
p := NewPunchyFromConfig(test.NewLogger(), c, nil) |
| 69 |
assert.False(t, p.punch.Load()) |
| 70 |
assert.Equal(t, delay, time.Duration(p.delay.Load())) |
| 71 |
assert.False(t, p.respond.Load()) |
| 72 |
|
| 73 |
newDelay, _ := time.ParseDuration("10m") |
| 74 |
require.NoError(t, c.ReloadConfigString(` |
| 75 |
punchy: |
| 76 |
punch: true |
| 77 |
delay: 10m |
| 78 |
respond: true |
| 79 |
`)) |
| 80 |
p.reload(c, false) |
| 81 |
assert.True(t, p.punch.Load()) |
| 82 |
assert.Equal(t, newDelay, time.Duration(p.delay.Load())) |
| 83 |
assert.True(t, p.respond.Load()) |
| 84 |
} |
| 85 |
|
| 86 |
// The tests below pin the shape of each log line Punchy produces so changes |
| 87 |
// cannot silently break whatever operators are grepping for. The assertions |
| 88 |
// are on the structured message + attrs (e.g. "punchy.respond changed" with |
| 89 |
// a respond=true field) rather than a formatted string. Tests filter by |
| 90 |
// message rather than asserting total entry counts so unrelated info lines |
| 91 |
// are tolerated without being locked into the format. |
| 92 |
|
| 93 |
type capturedEntry struct { |
| 94 |
Level slog.Level |
| 95 |
Msg string |
| 96 |
Attrs map[string]any |
| 97 |
} |
| 98 |
|
| 99 |
// capturingHandler is a slog.Handler that records each Record it receives so |
| 100 |
// tests can assert on the level, message, and attribute map of individual log |
| 101 |
// lines without coupling to any specific text format. |
| 102 |
type capturingHandler struct { |
| 103 |
entries []capturedEntry |
| 104 |
} |
| 105 |
|
| 106 |
func (h *capturingHandler) Enabled(_ context.Context, _ slog.Level) bool { return true } |
| 107 |
|
| 108 |
func (h *capturingHandler) Handle(_ context.Context, r slog.Record) error { |
| 109 |
e := capturedEntry{ |
| 110 |
Level: r.Level, |
| 111 |
Msg: r.Message, |
| 112 |
Attrs: make(map[string]any), |
| 113 |
} |
| 114 |
r.Attrs(func(a slog.Attr) bool { |
| 115 |
e.Attrs[a.Key] = a.Value.Resolve().Any() |
| 116 |
return true |
| 117 |
}) |
| 118 |
h.entries = append(h.entries, e) |
| 119 |
return nil |
| 120 |
} |
| 121 |
|
| 122 |
func (h *capturingHandler) WithAttrs(_ []slog.Attr) slog.Handler { return h } |
| 123 |
func (h *capturingHandler) WithGroup(_ string) slog.Handler { return h } |
| 124 |
|
| 125 |
func newCapturingPunchyLogger(t *testing.T) (*slog.Logger, *capturingHandler) { |
| 126 |
t.Helper() |
| 127 |
hook := &capturingHandler{} |
| 128 |
return slog.New(hook), hook |
| 129 |
} |
| 130 |
|
| 131 |
func findEntry(t *testing.T, entries []capturedEntry, msg string) capturedEntry { |
| 132 |
t.Helper() |
| 133 |
for _, e := range entries { |
| 134 |
if e.Msg == msg { |
| 135 |
return e |
| 136 |
} |
| 137 |
} |
| 138 |
t.Fatalf("no entry with message %q among %d entries", msg, len(entries)) |
| 139 |
return capturedEntry{} |
| 140 |
} |
| 141 |
|
| 142 |
func TestPunchy_LogFormat_InitialEnabled(t *testing.T) { |
| 143 |
l, hook := newCapturingPunchyLogger(t) |
| 144 |
c := config.NewC(test.NewLogger()) |
| 145 |
require.NoError(t, c.LoadString(`punchy: {punch: true}`)) |
| 146 |
|
| 147 |
NewPunchyFromConfig(l, c, nil) |
| 148 |
|
| 149 |
entry := findEntry(t, hook.entries, "punchy enabled") |
| 150 |
assert.Equal(t, slog.LevelInfo, entry.Level) |
| 151 |
assert.Empty(t, entry.Attrs) |
| 152 |
} |
| 153 |
|
| 154 |
func TestPunchy_LogFormat_InitialDisabled(t *testing.T) { |
| 155 |
l, hook := newCapturingPunchyLogger(t) |
| 156 |
c := config.NewC(test.NewLogger()) |
| 157 |
require.NoError(t, c.LoadString(`punchy: {punch: false}`)) |
| 158 |
|
| 159 |
NewPunchyFromConfig(l, c, nil) |
| 160 |
|
| 161 |
entry := findEntry(t, hook.entries, "punchy disabled") |
| 162 |
assert.Equal(t, slog.LevelInfo, entry.Level) |
| 163 |
assert.Empty(t, entry.Attrs) |
| 164 |
} |
| 165 |
|
| 166 |
func TestPunchy_LogFormat_ReloadPunch(t *testing.T) { |
| 167 |
l, hook := newCapturingPunchyLogger(t) |
| 168 |
c := config.NewC(test.NewLogger()) |
| 169 |
require.NoError(t, c.LoadString(`punchy: {punch: false}`)) |
| 170 |
NewPunchyFromConfig(l, c, nil) |
| 171 |
hook.entries = nil |
| 172 |
|
| 173 |
require.NoError(t, c.ReloadConfigString(`punchy: {punch: true}`)) |
| 174 |
|
| 175 |
entry := findEntry(t, hook.entries, "punchy.punch changed") |
| 176 |
assert.Equal(t, slog.LevelInfo, entry.Level) |
| 177 |
assert.Equal(t, map[string]any{"punch": true}, entry.Attrs) |
| 178 |
} |
| 179 |
|
| 180 |
func TestPunchy_LogFormat_ReloadRespond(t *testing.T) { |
| 181 |
l, hook := newCapturingPunchyLogger(t) |
| 182 |
c := config.NewC(test.NewLogger()) |
| 183 |
require.NoError(t, c.LoadString(`punchy: {respond: false}`)) |
| 184 |
NewPunchyFromConfig(l, c, nil) |
| 185 |
hook.entries = nil |
| 186 |
|
| 187 |
require.NoError(t, c.ReloadConfigString(`punchy: {respond: true}`)) |
| 188 |
|
| 189 |
entry := findEntry(t, hook.entries, "punchy.respond changed") |
| 190 |
assert.Equal(t, slog.LevelInfo, entry.Level) |
| 191 |
assert.Equal(t, map[string]any{"respond": true}, entry.Attrs) |
| 192 |
} |
| 193 |
|
| 194 |
func TestPunchy_LogFormat_ReloadDelay(t *testing.T) { |
| 195 |
l, hook := newCapturingPunchyLogger(t) |
| 196 |
c := config.NewC(test.NewLogger()) |
| 197 |
require.NoError(t, c.LoadString(`punchy: {delay: 1s}`)) |
| 198 |
NewPunchyFromConfig(l, c, nil) |
| 199 |
hook.entries = nil |
| 200 |
|
| 201 |
require.NoError(t, c.ReloadConfigString(`punchy: {delay: 10s}`)) |
| 202 |
|
| 203 |
entry := findEntry(t, hook.entries, "punchy.delay changed") |
| 204 |
assert.Equal(t, slog.LevelInfo, entry.Level) |
| 205 |
assert.Equal(t, map[string]any{"delay": 10 * time.Second}, entry.Attrs) |
| 206 |
} |
| 207 |
|
| 208 |
func TestPunchy_LogFormat_ReloadTargetAllRemotes(t *testing.T) { |
| 209 |
l, hook := newCapturingPunchyLogger(t) |
| 210 |
c := config.NewC(test.NewLogger()) |
| 211 |
require.NoError(t, c.LoadString(`punchy: {target_all_remotes: false}`)) |
| 212 |
NewPunchyFromConfig(l, c, nil) |
| 213 |
hook.entries = nil |
| 214 |
|
| 215 |
require.NoError(t, c.ReloadConfigString(`punchy: {target_all_remotes: true}`)) |
| 216 |
|
| 217 |
entry := findEntry(t, hook.entries, "punchy.target_all_remotes changed") |
| 218 |
assert.Equal(t, slog.LevelInfo, entry.Level) |
| 219 |
assert.Equal(t, map[string]any{"target_all_remotes": true}, entry.Attrs) |
| 220 |
} |
| 221 |
|
| 222 |
func TestPunchy_LogFormat_ReloadRespondDelay(t *testing.T) { |
| 223 |
l, hook := newCapturingPunchyLogger(t) |
| 224 |
c := config.NewC(test.NewLogger()) |
| 225 |
require.NoError(t, c.LoadString(`punchy: {respond_delay: 5s}`)) |
| 226 |
NewPunchyFromConfig(l, c, nil) |
| 227 |
hook.entries = nil |
| 228 |
|
| 229 |
require.NoError(t, c.ReloadConfigString(`punchy: {respond_delay: 15s}`)) |
| 230 |
|
| 231 |
entry := findEntry(t, hook.entries, "punchy.respond_delay changed") |
| 232 |
assert.Equal(t, slog.LevelInfo, entry.Level) |
| 233 |
assert.Equal(t, map[string]any{"respond_delay": 15 * time.Second}, entry.Attrs) |
| 234 |
} |