|
| 1 | +package logger |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "go.uber.org/zap/zapcore" |
| 11 | +) |
| 12 | + |
| 13 | +func writeFile(t *testing.T, path, body string) { |
| 14 | + t.Helper() |
| 15 | + require.NoError(t, os.WriteFile(path, []byte(body), 0o600)) |
| 16 | +} |
| 17 | + |
| 18 | +func TestApplyConfigFile(t *testing.T) { |
| 19 | + t.Run("a level change reaches a live logger", func(t *testing.T) { |
| 20 | + conf := &Config{Level: "info"} |
| 21 | + l, err := NewZapLogger(conf) |
| 22 | + require.NoError(t, err) |
| 23 | + core := zapLoggerCore(l) |
| 24 | + require.False(t, core.Enabled(zapcore.DebugLevel)) |
| 25 | + |
| 26 | + path := filepath.Join(t.TempDir(), "logging.yaml") |
| 27 | + writeFile(t, path, "level: debug\n") |
| 28 | + |
| 29 | + applied, ok := applyConfigFile(conf, conf.snapshot(), path, nil) |
| 30 | + require.True(t, ok) |
| 31 | + require.NotEmpty(t, applied) |
| 32 | + require.True(t, zapLoggerCore(l).Enabled(zapcore.DebugLevel), |
| 33 | + "the atomic level behind the existing logger must move, not just Config.Level") |
| 34 | + }) |
| 35 | + |
| 36 | + t.Run("keys absent from the file keep their current values", func(t *testing.T) { |
| 37 | + // Update assigns every field, so applying a partial file over a zero Config would wipe |
| 38 | + // these. component_levels is where livekit-server lands pion_level. |
| 39 | + conf := &Config{ |
| 40 | + Level: "info", |
| 41 | + JSON: true, |
| 42 | + Sample: true, |
| 43 | + SampleInitial: 7, |
| 44 | + ComponentLevels: map[string]string{"pion": "error"}, |
| 45 | + } |
| 46 | + _, err := NewZapLogger(conf) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + path := filepath.Join(t.TempDir(), "logging.yaml") |
| 50 | + writeFile(t, path, "level: warn\n") |
| 51 | + |
| 52 | + _, ok := applyConfigFile(conf, conf.snapshot(), path, nil) |
| 53 | + require.True(t, ok) |
| 54 | + require.Equal(t, "warn", conf.Level) |
| 55 | + require.True(t, conf.JSON) |
| 56 | + require.True(t, conf.Sample) |
| 57 | + require.Equal(t, 7, conf.SampleInitial) |
| 58 | + require.Equal(t, map[string]string{"pion": "error"}, conf.ComponentLevels) |
| 59 | + }) |
| 60 | + |
| 61 | + t.Run("a component level in the file merges with the existing ones", func(t *testing.T) { |
| 62 | + conf := &Config{Level: "info", ComponentLevels: map[string]string{"pion": "error"}} |
| 63 | + l, err := NewZapLogger(conf) |
| 64 | + require.NoError(t, err) |
| 65 | + |
| 66 | + path := filepath.Join(t.TempDir(), "logging.yaml") |
| 67 | + writeFile(t, path, "component_levels:\n psrpc: debug\n") |
| 68 | + |
| 69 | + _, ok := applyConfigFile(conf, conf.snapshot(), path, nil) |
| 70 | + require.True(t, ok) |
| 71 | + require.Equal(t, "error", conf.ComponentLevels["pion"]) |
| 72 | + require.Equal(t, "debug", conf.ComponentLevels["psrpc"]) |
| 73 | + require.True(t, zapLoggerCore(l.WithComponent("psrpc")).Enabled(zapcore.DebugLevel)) |
| 74 | + }) |
| 75 | + |
| 76 | + t.Run("unchanged bytes are not reapplied", func(t *testing.T) { |
| 77 | + conf := &Config{Level: "info"} |
| 78 | + path := filepath.Join(t.TempDir(), "logging.yaml") |
| 79 | + writeFile(t, path, "level: debug\n") |
| 80 | + |
| 81 | + applied, ok := applyConfigFile(conf, conf.snapshot(), path, nil) |
| 82 | + require.True(t, ok) |
| 83 | + _, ok = applyConfigFile(conf, conf.snapshot(), path, applied) |
| 84 | + require.False(t, ok) |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("malformed yaml keeps the last good config", func(t *testing.T) { |
| 88 | + conf := &Config{Level: "info"} |
| 89 | + l, err := NewZapLogger(conf) |
| 90 | + require.NoError(t, err) |
| 91 | + |
| 92 | + path := filepath.Join(t.TempDir(), "logging.yaml") |
| 93 | + writeFile(t, path, "level: [not, a, string\n") |
| 94 | + |
| 95 | + _, ok := applyConfigFile(conf, conf.snapshot(), path, nil) |
| 96 | + require.False(t, ok) |
| 97 | + require.Equal(t, "info", conf.Level) |
| 98 | + require.False(t, zapLoggerCore(l).Enabled(zapcore.DebugLevel)) |
| 99 | + }) |
| 100 | + |
| 101 | + t.Run("a missing file is tolerated", func(t *testing.T) { |
| 102 | + conf := &Config{Level: "info"} |
| 103 | + _, ok := applyConfigFile(conf, conf.snapshot(), filepath.Join(t.TempDir(), "absent.yaml"), nil) |
| 104 | + require.False(t, ok) |
| 105 | + require.Equal(t, "info", conf.Level) |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +func TestWatchConfigFile(t *testing.T) { |
| 110 | + conf := &Config{Level: "info"} |
| 111 | + l, err := NewZapLogger(conf) |
| 112 | + require.NoError(t, err) |
| 113 | + |
| 114 | + path := filepath.Join(t.TempDir(), "logging.yaml") |
| 115 | + writeFile(t, path, "level: info\n") |
| 116 | + |
| 117 | + stop := WatchConfigFile(conf, path, 5*time.Millisecond) |
| 118 | + t.Cleanup(stop) |
| 119 | + |
| 120 | + writeFile(t, path, "level: debug\n") |
| 121 | + require.Eventually(t, func() bool { |
| 122 | + return zapLoggerCore(l).Enabled(zapcore.DebugLevel) |
| 123 | + }, 2*time.Second, 5*time.Millisecond, "watcher should pick up the rewritten file") |
| 124 | + |
| 125 | + stop() |
| 126 | + writeFile(t, path, "level: error\n") |
| 127 | + time.Sleep(50 * time.Millisecond) |
| 128 | + require.True(t, zapLoggerCore(l).Enabled(zapcore.DebugLevel), "stop must end the polling") |
| 129 | +} |
| 130 | + |
| 131 | +// Applying config while component levels are being resolved: sharedConfig.ComponentLevel reads |
| 132 | +// under its own mutex while Update writes the Config under a different one, so it must be reading |
| 133 | +// a copy it owns. Meaningful under -race. |
| 134 | +func TestApplyConfigFileWhileResolvingComponents(t *testing.T) { |
| 135 | + conf := &Config{Level: "info", ComponentLevels: map[string]string{"pion": "error"}} |
| 136 | + l, err := NewZapLogger(conf) |
| 137 | + require.NoError(t, err) |
| 138 | + |
| 139 | + dir := t.TempDir() |
| 140 | + path := filepath.Join(dir, "logging.yaml") |
| 141 | + |
| 142 | + done := make(chan struct{}) |
| 143 | + go func() { |
| 144 | + defer close(done) |
| 145 | + for i := 0; i < 500; i++ { |
| 146 | + _ = zapLoggerCore(l.WithComponent("psrpc").WithComponent("Egress")) |
| 147 | + } |
| 148 | + }() |
| 149 | + |
| 150 | + var last []byte |
| 151 | + for i, level := range []string{"debug", "warn", "info", "error"} { |
| 152 | + writeFile(t, path, "level: "+level+"\n") |
| 153 | + applied, ok := applyConfigFile(conf, conf.snapshot(), path, last) |
| 154 | + require.True(t, ok, "iteration %d", i) |
| 155 | + last = applied |
| 156 | + } |
| 157 | + <-done |
| 158 | + require.Equal(t, "error", conf.Level) |
| 159 | + require.Equal(t, "error", conf.ComponentLevels["pion"]) |
| 160 | +} |
| 161 | + |
| 162 | +// The reset path the chart documents: emptying the file must put the startup levels back, not |
| 163 | +// leave the last override in force. Applying each file over a baseline rather than over the |
| 164 | +// config currently in force is what makes this hold. |
| 165 | +func TestEmptyFileRestoresStartupConfig(t *testing.T) { |
| 166 | + conf := &Config{Level: "info", ComponentLevels: map[string]string{"pion": "error"}} |
| 167 | + l, err := NewZapLogger(conf) |
| 168 | + require.NoError(t, err) |
| 169 | + baseline := conf.snapshot() |
| 170 | + |
| 171 | + path := filepath.Join(t.TempDir(), "logging.yaml") |
| 172 | + writeFile(t, path, "level: debug\ncomponent_levels:\n psrpc: debug\n") |
| 173 | + applied, ok := applyConfigFile(conf, baseline, path, nil) |
| 174 | + require.True(t, ok) |
| 175 | + require.Equal(t, "debug", conf.Level) |
| 176 | + require.True(t, zapLoggerCore(l).Enabled(zapcore.DebugLevel)) |
| 177 | + |
| 178 | + writeFile(t, path, "{}\n") |
| 179 | + _, ok = applyConfigFile(conf, baseline, path, applied) |
| 180 | + require.True(t, ok) |
| 181 | + require.Equal(t, "info", conf.Level) |
| 182 | + require.Equal(t, map[string]string{"pion": "error"}, conf.ComponentLevels, |
| 183 | + "a component the file no longer names must stop applying") |
| 184 | + require.False(t, zapLoggerCore(l).Enabled(zapcore.DebugLevel)) |
| 185 | +} |
0 commit comments