-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TestNoneCache now checks the output of the logs
- Loading branch information
Showing
4 changed files
with
95 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package testutil | ||
|
||
import ( | ||
"regexp" | ||
) | ||
|
||
// Replaces the klog and JSON timestamps with a static timestamp to make it | ||
// easier to assert the logs. It also replaces the line number with 000 as it | ||
// often changes. | ||
// | ||
// From: I1018 15:12:57.953433 22183 logs.go:000] log | ||
// To: I0000 00:00:00.000000 00000 logs.go:000] log | ||
// | ||
// From: I1018 15:12:57.953433] log | ||
// To: I0000 00:00:00.000000] log | ||
// | ||
// From: {"ts":1729258473588.828,"caller":"log/log.go:000","msg":"log Print","v":0} | ||
// To: {"ts":0000000000000.000,"caller":"log/log.go:000","msg":"log Print","v":0} | ||
// | ||
// From: 2024/10/18 15:40:50 log Print | ||
// To: 0000/00/00 00:00:00 log Print | ||
func ReplaceWithStaticTimestamps(input string) string { | ||
input = timestampRegexpKlog.ReplaceAllString(input, "0000 00:00:00.000000 00000") | ||
input = timestampRegexpKlogAlt.ReplaceAllString(input, "0000 00:00:00.000000") | ||
input = timestampRegexpJSON.ReplaceAllString(input, `"ts":0000000000000.000`) | ||
input = timestampRegexpStdLog.ReplaceAllString(input, "0000/00/00 00:00:00") | ||
input = fileAndLineRegexpJSON.ReplaceAllString(input, `"caller":"$1.go:000"`) | ||
input = fileAndLineRegexpKlog.ReplaceAllString(input, " $1.go:000") | ||
return input | ||
} | ||
|
||
var ( | ||
timestampRegexpStdLog = regexp.MustCompile(`\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}`) | ||
|
||
timestampRegexpKlog = regexp.MustCompile(`\d{4} \d{2}:\d{2}:\d{2}\.\d{6} +\d+`) | ||
timestampRegexpKlogAlt = regexp.MustCompile(`\d{4} \d{2}:\d{2}:\d{2}\.\d{6}`) | ||
fileAndLineRegexpKlog = regexp.MustCompile(` ([^:]+).go:\d+`) | ||
|
||
timestampRegexpJSON = regexp.MustCompile(`"ts":\d+\.?\d*`) | ||
fileAndLineRegexpJSON = regexp.MustCompile(`"caller":"([^"]+).go:\d+"`) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package testutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReplaceWithStaticTimestamps(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
name: "klog", | ||
input: `I1018 15:20:42.861239 2386 logs_test.go:13] "Contextual Info Level 3" logger="foo" key="value"`, | ||
expected: `I0000 00:00:00.000000 00000 logs_test.go:000] "Contextual Info Level 3" logger="foo" key="value"`, | ||
}, | ||
{ | ||
name: "klog without process ID and without file name", | ||
input: `E1114 11:15:39.455086] Cache update failure err="not a cacheResource type: *k8s.notCachable missing metadata/uid field" operation="add"`, | ||
expected: `E0000 00:00:00.000000] Cache update failure err="not a cacheResource type: *k8s.notCachable missing metadata/uid field" operation="add"`, | ||
}, | ||
{ | ||
name: "json-with-nanoseconds", | ||
input: `{"ts":1729270111728.125,"caller":"logs/logs_test.go:000","msg":"slog Warn","v":0}`, | ||
expected: `{"ts":0000000000000.000,"caller":"logs/logs_test.go:000","msg":"slog Warn","v":0}`, | ||
}, | ||
{ | ||
name: "json-might-not-have-nanoseconds", | ||
input: `{"ts":1729270111728,"caller":"logs/logs_test.go:000","msg":"slog Info","v":0}`, | ||
expected: `{"ts":0000000000000.000,"caller":"logs/logs_test.go:000","msg":"slog Info","v":0}`, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
assert.Equal(t, test.expected, ReplaceWithStaticTimestamps(test.input)) | ||
}) | ||
} | ||
} |