blob: 24f2acf2375dd91bcace84172ef31e40445b8b6c [file] [log] [blame]
Lorenz Brunc7108352023-08-09 17:09:40 +02001//go:build linux
2// +build linux
3
4package logtree
5
6import (
7 "testing"
8 "time"
9
10 "github.com/google/go-cmp/cmp"
Serge Bazanski3c5d0632024-09-12 10:49:12 +000011
12 "source.monogon.dev/go/logging"
Lorenz Brunc7108352023-08-09 17:09:40 +020013)
14
15func TestParseKmsg(t *testing.T) {
16 now := time.Unix(1691593045, 128027944)
17 nowMonotonic := time.Duration(1501096434537722)
18
19 for i, te := range []struct {
20 line string
21 want *LeveledPayload
22 }{
23 // Empty line
24 {"", nil},
25 // Unknown format
26 {"Not a valid line", nil},
27 // Normal entry
28 {"6,30962,1501094342185,-;test\n", &LeveledPayload{
29 messages: []string{"test"},
30 timestamp: time.Date(2023, 8, 9, 14, 57, 23, 35675222, time.UTC),
Serge Bazanski3c5d0632024-09-12 10:49:12 +000031 severity: logging.INFO,
Lorenz Brunc7108352023-08-09 17:09:40 +020032 }},
33 // With metadata and different severity
34 {"4,30951,1486884175312,-;nvme nvme2: starting error recovery\n SUBSYSTEM=nvme\n DEVICE=c239:2\n", &LeveledPayload{
35 messages: []string{"nvme nvme2: starting error recovery"},
36 timestamp: time.Date(2023, 8, 9, 11, 00, 32, 868802222, time.UTC),
Serge Bazanski3c5d0632024-09-12 10:49:12 +000037 severity: logging.WARNING,
Lorenz Brunc7108352023-08-09 17:09:40 +020038 }},
39 } {
40 got := parseKmsg(now, nowMonotonic, []byte(te.line))
41 if diff := cmp.Diff(te.want, got, cmp.AllowUnexported(LeveledPayload{})); diff != "" {
42 t.Errorf("%d: mismatch (-want +got):\n%s", i, diff)
43 }
44 }
45}