blob: ff97a6d2f9822fdd364564907c2a0d1915359a76 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Lorenz Brunc7108352023-08-09 17:09:40 +02004//go:build linux
5// +build linux
6
7package logtree
8
9import (
10 "testing"
11 "time"
12
13 "github.com/google/go-cmp/cmp"
Serge Bazanski3c5d0632024-09-12 10:49:12 +000014
15 "source.monogon.dev/go/logging"
Lorenz Brunc7108352023-08-09 17:09:40 +020016)
17
18func TestParseKmsg(t *testing.T) {
19 now := time.Unix(1691593045, 128027944)
20 nowMonotonic := time.Duration(1501096434537722)
21
22 for i, te := range []struct {
23 line string
24 want *LeveledPayload
25 }{
26 // Empty line
27 {"", nil},
28 // Unknown format
29 {"Not a valid line", nil},
30 // Normal entry
31 {"6,30962,1501094342185,-;test\n", &LeveledPayload{
32 messages: []string{"test"},
33 timestamp: time.Date(2023, 8, 9, 14, 57, 23, 35675222, time.UTC),
Serge Bazanski3c5d0632024-09-12 10:49:12 +000034 severity: logging.INFO,
Lorenz Brunc7108352023-08-09 17:09:40 +020035 }},
36 // With metadata and different severity
37 {"4,30951,1486884175312,-;nvme nvme2: starting error recovery\n SUBSYSTEM=nvme\n DEVICE=c239:2\n", &LeveledPayload{
38 messages: []string{"nvme nvme2: starting error recovery"},
39 timestamp: time.Date(2023, 8, 9, 11, 00, 32, 868802222, time.UTC),
Serge Bazanski3c5d0632024-09-12 10:49:12 +000040 severity: logging.WARNING,
Lorenz Brunc7108352023-08-09 17:09:40 +020041 }},
42 } {
43 got := parseKmsg(now, nowMonotonic, []byte(te.line))
44 if diff := cmp.Diff(te.want, got, cmp.AllowUnexported(LeveledPayload{})); diff != "" {
45 t.Errorf("%d: mismatch (-want +got):\n%s", i, diff)
46 }
47 }
48}