forked from aws/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pf_test.go
273 lines (258 loc) · 9.98 KB
/
pf_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package pf
import (
"log"
"reflect"
"strconv"
"testing"
"github.com/influxdata/telegraf/testutil"
)
type measurementResult struct {
tags map[string]string
fields map[string]interface{}
}
func TestPfctlInvocation(t *testing.T) {
type pfctlInvocationTestCase struct {
config PF
cmd string
args []string
}
var testCases = []pfctlInvocationTestCase{
// 0: no sudo
{
config: PF{UseSudo: false},
cmd: "fakepfctl",
args: []string{"-s", "info"},
},
// 1: with sudo
{
config: PF{UseSudo: true},
cmd: "fakesudo",
args: []string{"fakepfctl", "-s", "info"},
},
}
for i, tt := range testCases {
execLookPath = func(cmd string) (string, error) { return "fake" + cmd, nil }
t.Run(strconv.Itoa(i), func(t *testing.T) {
log.Printf("running #%d\n", i)
cmd, args, err := tt.config.buildPfctlCmd()
if err != nil {
t.Fatalf("error when running buildPfctlCmd: %s", err)
}
if tt.cmd != cmd || !reflect.DeepEqual(tt.args, args) {
t.Errorf("%d: expected %s - %#v got %s - %#v", i, tt.cmd, tt.args, cmd, args)
}
})
}
}
func TestPfMeasurements(t *testing.T) {
type pfTestCase struct {
TestInput string
err error
measurements []measurementResult
}
testCases := []pfTestCase{
// 0: nil input should raise an error
{TestInput: "", err: errParseHeader},
// 1: changes to pfctl output should raise an error
{TestInput: `Status: Enabled for 161 days 21:24:45 Debug: Urgent
Interface Stats for re1 IPv4 IPv6
Bytes In 2585823744614 1059233657221
Bytes Out 1227266932673 3274698578875
Packets In
Passed 2289953086 1945437219
Blocked 392835739 48609
Packets Out
Passed 1649146326 2605569054
Blocked 107 0
State Table Total Rate
Current Entrys 649
searches 18421725761 1317.0/s
inserts 156762508 11.2/s
removals 156761859 11.2/s
Counters
match 473002784 33.8/s
bad-offset 0 0.0/s
fragment 2729 0.0/s
short 107 0.0/s
normalize 1685 0.0/s
memory 101 0.0/s
bad-timestamp 0 0.0/s
congestion 0 0.0/s
ip-option 152301 0.0/s
proto-cksum 108 0.0/s
state-mismatch 24393 0.0/s
state-insert 92 0.0/s
state-limit 0 0.0/s
src-limit 0 0.0/s
synproxy 0 0.0/s
`,
err: errMissingData("current entries"),
},
// 2: bad numbers should raise an error
{TestInput: `Status: Enabled for 0 days 00:26:05 Debug: Urgent
State Table Total Rate
current entries -23
searches 11325 7.2/s
inserts 5 0.0/s
removals 3 0.0/s
Counters
match 11226 7.2/s
bad-offset 0 0.0/s
fragment 0 0.0/s
short 0 0.0/s
normalize 0 0.0/s
memory 0 0.0/s
bad-timestamp 0 0.0/s
congestion 0 0.0/s
ip-option 0 0.0/s
proto-cksum 0 0.0/s
state-mismatch 0 0.0/s
state-insert 0 0.0/s
state-limit 0 0.0/s
src-limit 0 0.0/s
synproxy 0 0.0/s
`,
err: errMissingData("current entries"),
},
{TestInput: `Status: Enabled for 0 days 00:26:05 Debug: Urgent
State Table Total Rate
current entries 2
searches 11325 7.2/s
inserts 5 0.0/s
removals 3 0.0/s
Counters
match 11226 7.2/s
bad-offset 0 0.0/s
fragment 0 0.0/s
short 0 0.0/s
normalize 0 0.0/s
memory 0 0.0/s
bad-timestamp 0 0.0/s
congestion 0 0.0/s
ip-option 0 0.0/s
proto-cksum 0 0.0/s
state-mismatch 0 0.0/s
state-insert 0 0.0/s
state-limit 0 0.0/s
src-limit 0 0.0/s
synproxy 0 0.0/s
`,
measurements: []measurementResult{
{
fields: map[string]interface{}{
"entries": int64(2),
"searches": int64(11325),
"inserts": int64(5),
"removals": int64(3),
"match": int64(11226),
"bad-offset": int64(0),
"fragment": int64(0),
"short": int64(0),
"normalize": int64(0),
"memory": int64(0),
"bad-timestamp": int64(0),
"congestion": int64(0),
"ip-option": int64(0),
"proto-cksum": int64(0),
"state-mismatch": int64(0),
"state-insert": int64(0),
"state-limit": int64(0),
"src-limit": int64(0),
"synproxy": int64(0)},
tags: map[string]string{},
},
},
},
{TestInput: `Status: Enabled for 161 days 21:24:45 Debug: Urgent
Interface Stats for re1 IPv4 IPv6
Bytes In 2585823744614 1059233657221
Bytes Out 1227266932673 3274698578875
Packets In
Passed 2289953086 1945437219
Blocked 392835739 48609
Packets Out
Passed 1649146326 2605569054
Blocked 107 0
State Table Total Rate
current entries 649
searches 18421725761 1317.0/s
inserts 156762508 11.2/s
removals 156761859 11.2/s
Counters
match 473002784 33.8/s
bad-offset 0 0.0/s
fragment 2729 0.0/s
short 107 0.0/s
normalize 1685 0.0/s
memory 101 0.0/s
bad-timestamp 0 0.0/s
congestion 0 0.0/s
ip-option 152301 0.0/s
proto-cksum 108 0.0/s
state-mismatch 24393 0.0/s
state-insert 92 0.0/s
state-limit 0 0.0/s
src-limit 0 0.0/s
synproxy 0 0.0/s
`,
measurements: []measurementResult{
{
fields: map[string]interface{}{
"entries": int64(649),
"searches": int64(18421725761),
"inserts": int64(156762508),
"removals": int64(156761859),
"match": int64(473002784),
"bad-offset": int64(0),
"fragment": int64(2729),
"short": int64(107),
"normalize": int64(1685),
"memory": int64(101),
"bad-timestamp": int64(0),
"congestion": int64(0),
"ip-option": int64(152301),
"proto-cksum": int64(108),
"state-mismatch": int64(24393),
"state-insert": int64(92),
"state-limit": int64(0),
"src-limit": int64(0),
"synproxy": int64(0)},
tags: map[string]string{},
},
},
},
}
for i, tt := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
log.Printf("running #%d\n", i)
pf := &PF{
infoFunc: func() (string, error) {
return tt.TestInput, nil
},
}
acc := new(testutil.Accumulator)
err := acc.GatherError(pf.Gather)
if !reflect.DeepEqual(tt.err, err) {
t.Errorf("%d: expected error '%#v' got '%#v'", i, tt.err, err)
}
n := 0
for j, v := range tt.measurements {
if len(acc.Metrics) < n+1 {
t.Errorf("%d: expected at least %d values got %d", i, n+1, len(acc.Metrics))
break
}
m := acc.Metrics[n]
if !reflect.DeepEqual(m.Measurement, measurement) {
t.Errorf("%d %d: expected measurement '%#v' got '%#v'\n", i, j, measurement, m.Measurement)
}
if !reflect.DeepEqual(m.Tags, v.tags) {
t.Errorf("%d %d: expected tags\n%#v got\n%#v\n", i, j, v.tags, m.Tags)
}
if !reflect.DeepEqual(m.Fields, v.fields) {
t.Errorf("%d %d: expected fields\n%#v got\n%#v\n", i, j, v.fields, m.Fields)
}
n++
}
})
}
}