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