-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_test.go
197 lines (157 loc) · 3.62 KB
/
parser_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package phonelab
import (
"bufio"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
type testStruct struct {
F1 int32 `logcat:"field1"`
F2 int64 `logcat:"field2"`
F3 uint32
F4 uint64
F5 string `logcat:"stringField5"`
F6 float32
F7 float64
F8 int `logcat:"-"`
}
func TestUnpackLogcat(t *testing.T) {
t.Parallel()
assert := assert.New(t)
ts := testStruct{}
ts.F8 = 1000
values := map[string]string{
"field1": "1",
"field2": "2",
"f3": "3",
"f4": "4",
"stringField5": "five",
"f6": "6.1111",
"f7": "7.50",
"f8": "1234", // Shouldn't change
}
err := UnpackLogcatEntry(&ts, values)
assert.Nil(err)
t.Log(ts)
assert.Equal(int32(1), ts.F1)
assert.Equal(int64(2), ts.F2)
assert.Equal(uint32(3), ts.F3)
assert.Equal(uint64(4), ts.F4)
assert.Equal("five", ts.F5)
assert.Equal(float32(6.1111), ts.F6)
assert.Equal(float64(7.50), ts.F7)
assert.Equal(int(1000), ts.F8)
}
func TestUnpackLogcatErrors(t *testing.T) {
t.Parallel()
assert := assert.New(t)
ts := testStruct{}
values := map[string]string{}
// Must be a pointer
err := UnpackLogcatEntry(ts, values)
assert.NotNil(err)
// Parse errors
values = map[string]string{
"field1": "9999999999999999999999999999999999999999999999999999999",
}
err = UnpackLogcatEntry(&ts, values)
assert.NotNil(err)
values = map[string]string{
"f6": "foo",
}
err = UnpackLogcatEntry(&ts, values)
assert.NotNil(err)
}
func TestLoglineParser(t *testing.T) {
t.Parallel()
assert := assert.New(t)
parser := NewLoglineParser()
parser.AddKnownTags()
file, err := os.Open("./test/test.10000.log")
assert.NotNil(file)
assert.Nil(err)
if err != nil {
t.FailNow()
}
printk, power, thermal, cpu_freq, unknown := 0, 0, 0, 0, 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
obj, err := parser.Parse(line)
// Everyting in the testfile is well formed. Also,
// all subparsers are not set to error on unknown lines.
assert.NotNil(obj)
assert.Nil(err)
if err != nil {
t.FailNow()
}
log := obj.(*Logline)
switch log.Payload.(type) {
case *PrintkLog:
printk += 1
case *PLPowerBatteryLog:
power += 1
case *ThermalTemp:
thermal += 1
case *CpuFrequency:
cpu_freq += 1
case string:
unknown += 1
default:
t.Fatalf("Unexpected type: %T", log)
}
}
assert.True(printk > 0)
assert.True(power > 0)
assert.True(thermal > 0)
assert.True(cpu_freq > 0)
assert.True(unknown > 0)
t.Log(printk)
t.Log(power)
t.Log(thermal)
t.Log(cpu_freq)
t.Log(unknown)
}
func TestLoglineParserSingleTag(t *testing.T) {
t.Parallel()
assert := assert.New(t)
parser := NewLoglineParser()
// We'll only add a printk parser that doesn't know how to
// parse any payloads, but doesn't error.
pkParser := NewPrintkParser()
pkParser.ErrOnUnknownTag = false
pkParser.Subparsers = make([]*PrintkSubparser, 0)
parser.SetParser("KernelPrintk", pkParser)
file, err := os.Open("./test/test.10000.log")
assert.NotNil(file)
assert.Nil(err)
if err != nil {
t.FailNow()
}
printk, unknown := 0, 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
obj, err := parser.Parse(line)
// Everyting in the testfile is well formed. Also,
// all subparsers are not set to error on unknown lines.
assert.NotNil(obj)
assert.Nil(err)
if err != nil {
t.FailNow()
}
log := obj.(*Logline)
switch log.Payload.(type) {
case *PrintkLog:
printk += 1
case string:
unknown += 1
default:
t.Fatalf("Unexpected type: %T", log)
}
}
assert.True(printk > 0)
assert.True(unknown > 0)
t.Log(printk)
t.Log(unknown)
}