@@ -15,6 +15,7 @@ import (
15
15
16
16
_ "github.com/Venafi/vcert/v5"
17
17
"github.com/spf13/pflag"
18
+ "github.com/stretchr/testify/assert"
18
19
"github.com/stretchr/testify/require"
19
20
"k8s.io/klog/v2"
20
21
@@ -316,3 +317,76 @@ func replaceWithStaticTimestamps(input string) string {
316
317
input = fileAndLineRegexpKlog .ReplaceAllString (input , " $1.go:000" )
317
318
return input
318
319
}
320
+
321
+ func TestLogToSlogWriter (t * testing.T ) {
322
+ // This test makes sure that all the agent's Log.Fatalf calls are correctly
323
+ // translated to slog.Error calls.
324
+ //
325
+ // This list was generated using:
326
+ // grep -r "Log\.Fatalf" ./cmd ./pkg
327
+ given := strings .TrimPrefix (`
328
+ Failed to load config file for agent from
329
+ Failed to read config file
330
+ Failed to parse config file
331
+ While evaluating configuration
332
+ failed to run pprof profiler
333
+ failed to run the health check server
334
+ failed to start a controller-runtime component
335
+ failed to wait for controller-runtime component to stop
336
+ running data gatherer %s of type %s as Local, data-path override present
337
+ failed to instantiate %q data gatherer
338
+ failed to read local data file
339
+ failed to unmarshal local data file
340
+ failed to output to local file
341
+ Exiting due to fatal error uploading
342
+ halting datagathering in strict mode due to error
343
+ Cannot marshal readings
344
+ Failed to read config file
345
+ Failed to parse config file
346
+ Failed to validate data gatherers
347
+ this is a happy log that should show as INFO` , "\n " )
348
+ expect := strings .TrimPrefix (`
349
+ level=ERROR msg="Failed to load config file for agent from" source=agent
350
+ level=ERROR msg="Failed to read config file" source=agent
351
+ level=ERROR msg="Failed to parse config file" source=agent
352
+ level=ERROR msg="While evaluating configuration" source=agent
353
+ level=ERROR msg="failed to run pprof profiler" source=agent
354
+ level=ERROR msg="failed to run the health check server" source=agent
355
+ level=ERROR msg="failed to start a controller-runtime component" source=agent
356
+ level=ERROR msg="failed to wait for controller-runtime component to stop" source=agent
357
+ level=ERROR msg="running data gatherer %!s(MISSING) of type %!s(MISSING) as Local, data-path override present" source=agent
358
+ level=ERROR msg="failed to instantiate %!q(MISSING) data gatherer" source=agent
359
+ level=ERROR msg="failed to read local data file" source=agent
360
+ level=ERROR msg="failed to unmarshal local data file" source=agent
361
+ level=ERROR msg="failed to output to local file" source=agent
362
+ level=ERROR msg="Exiting due to fatal error uploading" source=agent
363
+ level=ERROR msg="halting datagathering in strict mode due to error" source=agent
364
+ level=ERROR msg="Cannot marshal readings" source=agent
365
+ level=ERROR msg="Failed to read config file" source=agent
366
+ level=ERROR msg="Failed to parse config file" source=agent
367
+ level=ERROR msg="Failed to validate data gatherers" source=agent
368
+ level=INFO msg="this is a happy log that should show as INFO" source=agent
369
+ ` , "\n " )
370
+
371
+ gotBuf := & bytes.Buffer {}
372
+ slogHandler := slog .NewTextHandler (gotBuf , & slog.HandlerOptions {
373
+ // Remove the timestamp from the logs so that we can compare them.
374
+ ReplaceAttr : func (groups []string , a slog.Attr ) slog.Attr {
375
+ if a .Key == "time" {
376
+ return slog.Attr {}
377
+ }
378
+ return a
379
+ },
380
+ })
381
+ slogLogger := slog .New (slogHandler )
382
+
383
+ logger := log .New (& bytes.Buffer {}, "" , 0 )
384
+ logger .SetOutput (logs.LogToSlogWriter {Slog : slogLogger , Source : "agent" })
385
+
386
+ for _ , line := range strings .Split (given , "\n " ) {
387
+ // Simulate the current agent's logs.
388
+ logger .Printf (line )
389
+ }
390
+
391
+ assert .Equal (t , expect , gotBuf .String ())
392
+ }
0 commit comments