-
Notifications
You must be signed in to change notification settings - Fork 1
/
performance_data_test.go
290 lines (250 loc) · 7.97 KB
/
performance_data_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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package monitoringplugin
import (
"fmt"
"regexp"
"testing"
)
func TestPerformanceDataPointCreation(t *testing.T) {
metric := "testMetric"
var value float64 = 10
p := NewPerformanceDataPoint(metric, value)
if p.Metric != metric || p.Value != value {
t.Error("the created PerfomanceDataPoint NewPerformanceDataPoint")
}
unit := "%"
p.SetUnit(unit)
if p.Unit != unit {
t.Error("SetUnit failed")
}
label := "testLabel"
p.SetLabel(label)
if p.Label != label {
t.Error("SetLabel failed")
}
var min float64
p.SetMin(min)
if p.Min != min || p.Min == nil {
t.Error("SetMin failed")
}
var max float64 = 100
p.SetMax(max)
if p.Max != max || p.Max == nil {
t.Error("SetMax failed")
}
thresholds := Thresholds{
WarningMin: 0,
WarningMax: 10,
CriticalMin: 0,
CriticalMax: 20,
}
p.SetThresholds(thresholds)
if p.Thresholds != thresholds {
t.Error("SetThresholds failed")
}
return
}
func TestPerformanceDataPoint_validate(t *testing.T) {
p := NewPerformanceDataPoint("metric", 10).SetMin(0).SetMax(100)
if err := p.Validate(); err != nil {
t.Error("valid performance data point resulted in an error: " + err.Error())
}
//empty metric
p = NewPerformanceDataPoint("", 10)
if err := p.Validate(); err == nil {
t.Error("invalid performance data did not return an error (case: empty metric)")
}
//invalid metric
p = NewPerformanceDataPoint("metric=", 10)
if err := p.Validate(); err == nil {
t.Error("invalid performance data did not return an error (case: invalid metric, contains =)")
}
p = NewPerformanceDataPoint("'metric'", 10)
if err := p.Validate(); err == nil {
t.Error("invalid performance data did not return an error (case: invalid metric, contains single quotes)")
}
//invalid unit
p = NewPerformanceDataPoint("metric", 10).SetUnit("unit1")
if err := p.Validate(); err == nil {
t.Error("invalid performance data did not return an error (case: invalid unit, contains numbers)")
}
p = NewPerformanceDataPoint("metric", 10).SetUnit("unit;")
if err := p.Validate(); err == nil {
t.Error("invalid performance data did not return an error (case: invalid unit, contains semicolon)")
}
p = NewPerformanceDataPoint("metric", 10).SetUnit("unit'")
if err := p.Validate(); err == nil {
t.Error("invalid performance data did not return an error (case: invalid unit, contains single quotes)")
}
p = NewPerformanceDataPoint("metric", 10).SetUnit("unit\"")
if err := p.Validate(); err == nil {
t.Error("invalid performance data did not return an error (case: invalid unit, contains double quotes)")
}
//value < min
p = NewPerformanceDataPoint("metric", 10).SetMin(50)
if err := p.Validate(); err == nil {
t.Error("invalid performance data did not return an error (case: value < min)")
}
//value > max
p = NewPerformanceDataPoint("metric", 10).SetMax(5)
if err := p.Validate(); err == nil {
t.Error("invalid performance data did not return an error (case: value < min)")
}
//min > max
p = NewPerformanceDataPoint("metric", 10).SetMin(10).SetMax(5)
if err := p.Validate(); err == nil {
t.Error("invalid performance data did not return an error (case: max < min)")
}
}
func TestPerformanceDataPoint_output(t *testing.T) {
label := "metric"
value := 10.0
unit := "s"
warn := 40.0
crit := 50.0
min := 0.0
max := 60.0
p := NewPerformanceDataPoint(label, value)
regex := fmt.Sprintf("'%s'=%g", label, value)
match, err := regexp.Match(regex, p.output(false))
if err != nil {
t.Error(err.Error())
}
if !match {
t.Error("output string did not match regex")
}
p.SetUnit(unit)
regex = fmt.Sprintf("'%s'=%g%s", label, value, unit)
match, err = regexp.Match(regex, p.output(false))
if err != nil {
t.Error(err.Error())
}
if !match {
t.Error("output string did not match regex")
}
p.SetMax(max)
regex = fmt.Sprintf("'%s'=%g%s;;;;%g", label, value, unit, max)
match, err = regexp.Match(regex, p.output(false))
if err != nil {
t.Error(err.Error())
}
if !match {
t.Error("output string did not match regex")
}
p.SetThresholds(NewThresholds(nil, warn, nil, crit))
regex = fmt.Sprintf("'%s'=%g%s;~:%g;~:%g;;%g", label, value, unit, warn, crit, max)
match, err = regexp.Match(regex, p.output(false))
if err != nil {
t.Error(err.Error())
}
if !match {
t.Error("output string did not match regex")
}
p.SetThresholds(NewThresholds(0, nil, -10, nil))
regex = fmt.Sprintf("'%s'=%g%s;%d:;%d:;;%g", label, value, unit, 0, -10, max)
match, err = regexp.Match(regex, p.output(false))
if err != nil {
t.Error(err.Error())
}
if !match {
t.Error("output string did not match regex")
}
p.SetThresholds(NewThresholds(5, 10, 3, 11))
regex = fmt.Sprintf("'%s'=%g%s;%d:%d;%d:%d;;%g", label, value, unit, 5, 10, 3, 11, max)
match, err = regexp.Match(regex, p.output(false))
if err != nil {
t.Error(err.Error())
}
if !match {
t.Error("output string did not match regex")
}
p.SetThresholds(NewThresholds(0, warn, 0, crit))
regex = fmt.Sprintf("'%s'=%g%s;%g;%g;;%g", label, value, unit, warn, crit, max)
match, err = regexp.Match(regex, p.output(false))
if err != nil {
t.Error(err.Error())
}
if !match {
t.Error("output string did not match regex")
}
p.SetMin(min)
regex = fmt.Sprintf("'%s'=%g%s;%g;%g;%g;%g", label, value, unit, warn, crit, min, max)
match, err = regexp.Match(regex, p.output(false))
if err != nil {
t.Error(err.Error())
}
if !match {
t.Error("output string did not match regex")
}
regex = fmt.Sprintf(`'{"metric":"%s"}'=%g%s;%g;%g;%g;%g`, label, value, unit, warn, crit, min, max)
match, err = regexp.Match(regex, p.output(true))
if err != nil {
t.Error(err.Error())
}
if !match {
t.Error("output string did not match regex")
}
tag := "tag"
p.SetLabel(tag)
regex = fmt.Sprintf(`'{"metric":"%s","label":"%s"}'=%g%s;%g;%g;%g;%g`, label, tag, value, unit, warn, crit, min, max)
match, err = regexp.Match(regex, p.output(true))
if err != nil {
t.Error(err.Error())
}
if !match {
t.Error("output string did not match regex")
}
regex = fmt.Sprintf(`'%s_%s'=%g%s;%g;%g;%g;%g`, label, tag, value, unit, warn, crit, min, max)
match, err = regexp.Match(regex, p.output(false))
if err != nil {
t.Error(err.Error())
}
if !match {
t.Error("output string did not match regex")
}
}
func TestPerformanceData_add(t *testing.T) {
perfData := make(performanceData)
//valid perfdata point
err := perfData.add(NewPerformanceDataPoint("metric", 10))
if err != nil {
t.Error("adding a valid performance data point resulted in an error")
return
}
if _, ok := perfData[performanceDataPointKey{"metric", ""}]; !ok {
t.Error("performance data point was not added to the map of performance data points")
}
err = perfData.add(NewPerformanceDataPoint("metric", 10))
if err == nil {
t.Error("there was no error when adding a performance data point with a metric, that already exists in performance data")
}
err = perfData.add(NewPerformanceDataPoint("metric", 10).SetLabel("tag1"))
if err != nil {
t.Error("adding a valid performance data point resulted in an error")
return
}
err = perfData.add(NewPerformanceDataPoint("metric", 10).SetLabel("tag2"))
if err != nil {
t.Error("adding a valid performance data point resulted in an error")
return
}
err = perfData.add(NewPerformanceDataPoint("metric", 10).SetLabel("tag1"))
if err == nil {
t.Error("there was no error when adding a performance data point with a metric and tag, that already exists in performance data")
}
}
func TestResponse_SetPerformanceDataJsonLabel(t *testing.T) {
perfData := make(performanceData)
//valid perfdata point
err := perfData.add(NewPerformanceDataPoint("metric", 10))
if err != nil {
t.Error("adding a valid performance data point resulted in an error")
return
}
if _, ok := perfData[performanceDataPointKey{"metric", ""}]; !ok {
t.Error("performance data point was not added to the map of performance data points")
}
err = perfData.add(NewPerformanceDataPoint("metric", 10))
if err == nil {
t.Error("there was no error when adding a performance data point with a metric, that already exists in performance data")
}
}