Skip to content

Commit 8a31ff9

Browse files
test(metrics): add regression test for invalid file path
Added TestZerologMetricsInvalidFileDoesNotPanic to ensure that if the metrics log file cannot be opened, it gracefully degrades instead of causing a nil pointer dereference. Signed-off-by: abhaygoudannavar <abhaysgoudnvr@gmail.com>
1 parent 85995ee commit 8a31ff9

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

internal/metrics/metrics_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
"github.com/rs/zerolog"
23+
"github.com/stretchr/testify/require"
2324
)
2425

2526
func TestZerologMetricsMetadata(t *testing.T) {
@@ -52,3 +53,18 @@ func TestZerologMetricsMetadata(t *testing.T) {
5253
t.Errorf("timestampOrder = %v, want 0", got)
5354
}
5455
}
56+
57+
func TestZerologMetricsInvalidFileDoesNotPanic(t *testing.T) {
58+
// Provide a path to a directory that definitely does not exist
59+
invalidPath := "/does/not/exist/timestamps.log"
60+
61+
// Create the metrics writer with timestamps enabled but an invalid path
62+
writer := NewZerologMetrics(true, invalidPath, "container-test")
63+
64+
// Before the fix, writer would be nil here.
65+
require.NotNil(t, writer, "Expected NewZerologMetrics to return a fallback writer, got nil")
66+
67+
// This call would panic with a nil pointer dereference without the fix.
68+
// With the fix, it will safely execute the mockWriter's no-op Capture().
69+
writer.Capture(TS00)
70+
}

0 commit comments

Comments
 (0)