-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_test.go
99 lines (90 loc) · 1.89 KB
/
log_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package netconsole_test
import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/mdlayher/netconsole"
)
func TestParseLog(t *testing.T) {
tests := []struct {
name string
s string
ll netconsole.Log
ok bool
}{
{
name: "empty",
s: "",
},
{
name: "no brackets",
s: " 22.671488 foo",
},
{
name: "bad whole number",
s: "[ xx.671488] foo",
},
{
name: "bad decimal",
s: "[ 22.xx] foo",
},
{
name: "no message",
s: "[ 22.671488] ",
},
{
name: "OK",
s: "[ 22.671488] raid6: using algorithm avx2x4 gen() 21138 MB/s",
ll: netconsole.Log{
Elapsed: 22*time.Second + 671488*time.Microsecond,
Message: "raid6: using algorithm avx2x4 gen() 21138 MB/s",
},
ok: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ll, err := netconsole.ParseLog(tt.s)
if tt.ok && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !tt.ok && err == nil {
t.Fatalf("expected an error, but none occurred: %v", err)
}
if !tt.ok {
// Don't bother doing comparison if log is invalid.
t.Logf("OK error: %v", err)
return
}
if diff := cmp.Diff(tt.ll, ll); diff != "" {
t.Fatalf("unexpected Log (-want +got):\n%s", diff)
}
})
}
}
func BenchmarkParseLog(b *testing.B) {
tests := []struct {
name string
s string
}{
{
name: "invalid",
s: "foo",
},
{
name: "short",
s: "[ 0.097798] x86: Booted up 1 node, 4 CPUs",
},
{
name: "long",
s: "[ 82.742346] systemd[1]: systemd 229 running in system mode. (+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN)",
},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = netconsole.ParseLog(tt.s)
}
})
}
}