Skip to content

Commit f5f86b3

Browse files
committed
TestNoneCache now checks the output of the logs
1 parent e8c5eed commit f5f86b3

File tree

4 files changed

+95
-63
lines changed

4 files changed

+95
-63
lines changed

pkg/datagatherer/k8s/cache_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88
"github.com/d4l3k/messagediff"
99
"github.com/go-logr/logr"
1010
"github.com/pmylund/go-cache"
11+
"github.com/stretchr/testify/assert"
1112
"k8s.io/apimachinery/pkg/runtime"
1213
"k8s.io/klog/v2/ktesting"
1314

1415
"github.com/jetstack/preflight/api"
16+
"github.com/jetstack/preflight/pkg/testutil"
1517
)
1618

1719
func makeGatheredResource(obj runtime.Object, deletedAt api.Time) *api.GatheredResource {
@@ -143,10 +145,17 @@ func TestOnAddCache(t *testing.T) {
143145
// TestNoneCache demonstrates that the cache helpers do not crash if passed a
144146
// non-cachable object, but log an error with a reference to the object type.
145147
func TestNoneCache(t *testing.T) {
146-
log := ktesting.NewLogger(t, ktesting.NewConfig(ktesting.Verbosity(10)))
148+
var buffer ktesting.BufferTL
149+
log := ktesting.NewLogger(&buffer, ktesting.NewConfig(ktesting.Verbosity(10)))
147150

148151
type notCachable struct{}
149152
onAdd(log, &notCachable{}, nil)
150153
onUpdate(log, &notCachable{}, nil, nil)
151154
onDelete(log, &notCachable{}, nil)
155+
156+
assert.Equal(t, testutil.Undent(`
157+
E0000 00:00:00.000000] Cache update failure err="not a cacheResource type: *k8s.notCachable missing metadata/uid field" operation="add"
158+
E0000 00:00:00.000000] Cache update failure err="not a cacheResource type: *k8s.notCachable missing metadata/uid field" operation="update"
159+
E0000 00:00:00.000000] Cache update failure err="not a cacheResource type: *k8s.notCachable missing metadata/uid field" operation="delete"
160+
`), testutil.ReplaceWithStaticTimestamps(buffer.String()))
152161
}

pkg/logs/logs_test.go

+3-62
Original file line numberDiff line numberDiff line change
@@ -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
}

pkg/testutil/logs.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package testutil
2+
3+
import (
4+
"regexp"
5+
)
6+
7+
// Replaces the klog and JSON timestamps with a static timestamp to make it
8+
// easier to assert the logs. It also replaces the line number with 000 as it
9+
// often changes.
10+
//
11+
// From: I1018 15:12:57.953433 22183 logs.go:000] log
12+
// To: I0000 00:00:00.000000 00000 logs.go:000] log
13+
//
14+
// From: I1018 15:12:57.953433] log
15+
// To: I0000 00:00:00.000000] log
16+
//
17+
// From: {"ts":1729258473588.828,"caller":"log/log.go:000","msg":"log Print","v":0}
18+
// To: {"ts":0000000000000.000,"caller":"log/log.go:000","msg":"log Print","v":0}
19+
//
20+
// From: 2024/10/18 15:40:50 log Print
21+
// To: 0000/00/00 00:00:00 log Print
22+
func ReplaceWithStaticTimestamps(input string) string {
23+
input = timestampRegexpKlog.ReplaceAllString(input, "0000 00:00:00.000000 00000")
24+
input = timestampRegexpKlogAlt.ReplaceAllString(input, "0000 00:00:00.000000")
25+
input = timestampRegexpJSON.ReplaceAllString(input, `"ts":0000000000000.000`)
26+
input = timestampRegexpStdLog.ReplaceAllString(input, "0000/00/00 00:00:00")
27+
input = fileAndLineRegexpJSON.ReplaceAllString(input, `"caller":"$1.go:000"`)
28+
input = fileAndLineRegexpKlog.ReplaceAllString(input, " $1.go:000")
29+
return input
30+
}
31+
32+
var (
33+
timestampRegexpStdLog = regexp.MustCompile(`\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}`)
34+
35+
timestampRegexpKlog = regexp.MustCompile(`\d{4} \d{2}:\d{2}:\d{2}\.\d{6} +\d+`)
36+
timestampRegexpKlogAlt = regexp.MustCompile(`\d{4} \d{2}:\d{2}:\d{2}\.\d{6}`)
37+
fileAndLineRegexpKlog = regexp.MustCompile(` ([^:]+).go:\d+`)
38+
39+
timestampRegexpJSON = regexp.MustCompile(`"ts":\d+\.?\d*`)
40+
fileAndLineRegexpJSON = regexp.MustCompile(`"caller":"([^"]+).go:\d+"`)
41+
)

pkg/testutil/logs_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package testutil
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestReplaceWithStaticTimestamps(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
input string
13+
expected string
14+
}{
15+
{
16+
name: "klog",
17+
input: `I1018 15:20:42.861239 2386 logs_test.go:13] "Contextual Info Level 3" logger="foo" key="value"`,
18+
expected: `I0000 00:00:00.000000 00000 logs_test.go:000] "Contextual Info Level 3" logger="foo" key="value"`,
19+
},
20+
{
21+
name: "klog without process ID and without file name",
22+
input: `E1114 11:15:39.455086] Cache update failure err="not a cacheResource type: *k8s.notCachable missing metadata/uid field" operation="add"`,
23+
expected: `E0000 00:00:00.000000] Cache update failure err="not a cacheResource type: *k8s.notCachable missing metadata/uid field" operation="add"`,
24+
},
25+
{
26+
name: "json-with-nanoseconds",
27+
input: `{"ts":1729270111728.125,"caller":"logs/logs_test.go:000","msg":"slog Warn","v":0}`,
28+
expected: `{"ts":0000000000000.000,"caller":"logs/logs_test.go:000","msg":"slog Warn","v":0}`,
29+
},
30+
{
31+
name: "json-might-not-have-nanoseconds",
32+
input: `{"ts":1729270111728,"caller":"logs/logs_test.go:000","msg":"slog Info","v":0}`,
33+
expected: `{"ts":0000000000000.000,"caller":"logs/logs_test.go:000","msg":"slog Info","v":0}`,
34+
},
35+
}
36+
for _, test := range tests {
37+
t.Run(test.name, func(t *testing.T) {
38+
assert.Equal(t, test.expected, ReplaceWithStaticTimestamps(test.input))
39+
})
40+
}
41+
}

0 commit comments

Comments
 (0)