@@ -10,7 +10,6 @@ import (
1010 "log/slog"
1111 "os"
1212 "os/exec"
13- "regexp"
1413 "strings"
1514 "testing"
1615 "time"
@@ -22,6 +21,7 @@ import (
2221 "k8s.io/klog/v2"
2322
2423 "github.com/jetstack/preflight/pkg/logs"
24+ "github.com/jetstack/preflight/pkg/testutil"
2525)
2626
2727// TestLogs demonstrates how the logging flags affect the logging output.
@@ -307,67 +307,8 @@ E0000 00:00:00.000000 00000 logs_test.go:000] "Contextual error" err="fake-err
307307 test .expectStdout = strings .TrimPrefix (test .expectStdout , "\n " )
308308 test .expectStderr = strings .TrimPrefix (test .expectStderr , "\n " )
309309
310- require .Equal (t , test .expectStdout , replaceWithStaticTimestamps (stdoutStr ), "stdout doesn't match" )
311- require .Equal (t , test .expectStderr , replaceWithStaticTimestamps (stderrStr ), "stderr doesn't match" )
312- })
313- }
314- }
315-
316- var (
317- timestampRegexpStdLog = regexp .MustCompile (`\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}` )
318- timestampRegexpKlog = regexp .MustCompile (`\d{4} \d{2}:\d{2}:\d{2}\.\d{6} +\d+` )
319- timestampRegexpJSON = regexp .MustCompile (`"ts":\d+\.?\d*` )
320- fileAndLineRegexpJSON = regexp .MustCompile (`"caller":"([^"]+).go:\d+"` )
321- fileAndLineRegexpKlog = regexp .MustCompile (` ([^:]+).go:\d+` )
322- )
323-
324- // Replaces the klog and JSON timestamps with a static timestamp to make it
325- // easier to assert the logs. It also replaces the line number with 000 as it
326- // often changes.
327- //
328- // I1018 15:12:57.953433 22183 logs.go:000] log
329- // {"ts":1729258473588.828,"caller":"log/log.go:000","msg":"log Print","v":0}
330- // 2024/10/18 15:40:50 log Print
331- //
332- // to the fixed:
333- //
334- // I0000 00:00:00.000000 00000 logs.go:000] log
335- // {"ts":0000000000000.000,"caller":"log/log.go:000","msg":"log Print","v":0}
336- // 0000/00/00 00:00:00 log Print
337- func replaceWithStaticTimestamps (input string ) string {
338- input = timestampRegexpKlog .ReplaceAllString (input , "0000 00:00:00.000000 00000" )
339- input = timestampRegexpJSON .ReplaceAllString (input , `"ts":0000000000000.000` )
340- input = timestampRegexpStdLog .ReplaceAllString (input , "0000/00/00 00:00:00" )
341- input = fileAndLineRegexpJSON .ReplaceAllString (input , `"caller":"$1.go:000"` )
342- input = fileAndLineRegexpKlog .ReplaceAllString (input , " $1.go:000" )
343- return input
344- }
345-
346- func Test_replaceWithStaticTimestamps (t * testing.T ) {
347- tests := []struct {
348- name string
349- input string
350- expected string
351- }{
352- {
353- name : "klog" ,
354- input : `I1018 15:20:42.861239 2386 logs_test.go:13] "Contextual Info Level 3" logger="foo" key="value"` ,
355- expected : `I0000 00:00:00.000000 00000 logs_test.go:000] "Contextual Info Level 3" logger="foo" key="value"` ,
356- },
357- {
358- name : "json-with-nanoseconds" ,
359- input : `{"ts":1729270111728.125,"caller":"logs/logs_test.go:000","msg":"slog Warn","v":0}` ,
360- expected : `{"ts":0000000000000.000,"caller":"logs/logs_test.go:000","msg":"slog Warn","v":0}` ,
361- },
362- {
363- name : "json-might-not-have-nanoseconds" ,
364- input : `{"ts":1729270111728,"caller":"logs/logs_test.go:000","msg":"slog Info","v":0}` ,
365- expected : `{"ts":0000000000000.000,"caller":"logs/logs_test.go:000","msg":"slog Info","v":0}` ,
366- },
367- }
368- for _ , test := range tests {
369- t .Run (test .name , func (t * testing.T ) {
370- assert .Equal (t , test .expected , replaceWithStaticTimestamps (test .input ))
310+ require .Equal (t , test .expectStdout , testutil .ReplaceWithStaticTimestamps (stdoutStr ), "stdout doesn't match" )
311+ require .Equal (t , test .expectStderr , testutil .ReplaceWithStaticTimestamps (stderrStr ), "stderr doesn't match" )
371312 })
372313 }
373314}
0 commit comments