Debian Codegraph @

performance_data_test.go

raw
1 package monitoringplugin
2
3 import (
4 "fmt"
5 "regexp"
6 "testing"
7 )
8
9 func TestPerformanceDataPointCreation(t *testing.T) {
10 metric := "testMetric"
11 var value float64 = 10
12 p := NewPerformanceDataPoint(metric, value)
13
14 if p.Metric != metric || p.Value != value {
15 t.Error("the created PerfomanceDataPoint NewPerformanceDataPoint")
16 }
17
18 unit := "%"
19 p.SetUnit(unit)
20 if p.Unit != unit {
21 t.Error("SetUnit failed")
22 }
23
24 label := "testLabel"
25 p.SetLabel(label)
26 if p.Label != label {
27 t.Error("SetLabel failed")
28 }
29
30 var min float64
31 p.SetMin(min)
32 if p.Min != min || p.Min == nil {
33 t.Error("SetMin failed")
34 }
35
36 var max float64 = 100
37 p.SetMax(max)
38 if p.Max != max || p.Max == nil {
39 t.Error("SetMax failed")
40 }
41
42 thresholds := Thresholds{
43 WarningMin: 0,
44 WarningMax: 10,
45 CriticalMin: 0,
46 CriticalMax: 20,
47 }
48 p.SetThresholds(thresholds)
49 if p.Thresholds != thresholds {
50 t.Error("SetThresholds failed")
51 }
52 return
53 }
54
55 func TestPerformanceDataPoint_validate(t *testing.T) {
56 p := NewPerformanceDataPoint("metric", 10).SetMin(0).SetMax(100)
57 if err := p.Validate(); err != nil {
58 t.Error("valid performance data point resulted in an error: " + err.Error())
59 }
60
61 //empty metric
62 p = NewPerformanceDataPoint("", 10)
63 if err := p.Validate(); err == nil {
64 t.Error("invalid performance data did not return an error (case: empty metric)")
65 }
66
67 //invalid metric
68 p = NewPerformanceDataPoint("metric=", 10)
69 if err := p.Validate(); err == nil {
70 t.Error("invalid performance data did not return an error (case: invalid metric, contains =)")
71 }
72 p = NewPerformanceDataPoint("'metric'", 10)
73
74 if err := p.Validate(); err == nil {
75 t.Error("invalid performance data did not return an error (case: invalid metric, contains single quotes)")
76 }
77
78 //invalid unit
79 p = NewPerformanceDataPoint("metric", 10).SetUnit("unit1")
80 if err := p.Validate(); err == nil {
81 t.Error("invalid performance data did not return an error (case: invalid unit, contains numbers)")
82 }
83 p = NewPerformanceDataPoint("metric", 10).SetUnit("unit;")
84 if err := p.Validate(); err == nil {
85 t.Error("invalid performance data did not return an error (case: invalid unit, contains semicolon)")
86 }
87 p = NewPerformanceDataPoint("metric", 10).SetUnit("unit'")
88 if err := p.Validate(); err == nil {
89 t.Error("invalid performance data did not return an error (case: invalid unit, contains single quotes)")
90 }
91 p = NewPerformanceDataPoint("metric", 10).SetUnit("unit\"")
92 if err := p.Validate(); err == nil {
93 t.Error("invalid performance data did not return an error (case: invalid unit, contains double quotes)")
94 }
95
96 //value < min
97 p = NewPerformanceDataPoint("metric", 10).SetMin(50)
98 if err := p.Validate(); err == nil {
99 t.Error("invalid performance data did not return an error (case: value < min)")
100 }
101
102 //value > max
103 p = NewPerformanceDataPoint("metric", 10).SetMax(5)
104 if err := p.Validate(); err == nil {
105 t.Error("invalid performance data did not return an error (case: value < min)")
106 }
107
108 //min > max
109 p = NewPerformanceDataPoint("metric", 10).SetMin(10).SetMax(5)
110 if err := p.Validate(); err == nil {
111 t.Error("invalid performance data did not return an error (case: max < min)")
112 }
113 }
114
115 func TestPerformanceDataPoint_output(t *testing.T) {
116 label := "metric"
117 value := 10.0
118 unit := "s"
119 warn := 40.0
120 crit := 50.0
121 min := 0.0
122 max := 60.0
123
124 p := NewPerformanceDataPoint(label, value)
125 regex := fmt.Sprintf("'%s'=%g", label, value)
126 match, err := regexp.Match(regex, p.output(false))
127 if err != nil {
128 t.Error(err.Error())
129 }
130 if !match {
131 t.Error("output string did not match regex")
132 }
133
134 p.SetUnit(unit)
135 regex = fmt.Sprintf("'%s'=%g%s", label, value, unit)
136 match, err = regexp.Match(regex, p.output(false))
137 if err != nil {
138 t.Error(err.Error())
139 }
140 if !match {
141 t.Error("output string did not match regex")
142 }
143
144 p.SetMax(max)
145 regex = fmt.Sprintf("'%s'=%g%s;;;;%g", label, value, unit, max)
146 match, err = regexp.Match(regex, p.output(false))
147 if err != nil {
148 t.Error(err.Error())
149 }
150 if !match {
151 t.Error("output string did not match regex")
152 }
153
154 p.SetThresholds(NewThresholds(nil, warn, nil, crit))
155 regex = fmt.Sprintf("'%s'=%g%s;~:%g;~:%g;;%g", label, value, unit, warn, crit, max)
156 match, err = regexp.Match(regex, p.output(false))
157 if err != nil {
158 t.Error(err.Error())
159 }
160 if !match {
161 t.Error("output string did not match regex")
162 }
163
164 p.SetThresholds(NewThresholds(0, nil, -10, nil))
165 regex = fmt.Sprintf("'%s'=%g%s;%d:;%d:;;%g", label, value, unit, 0, -10, max)
166 match, err = regexp.Match(regex, p.output(false))
167 if err != nil {
168 t.Error(err.Error())
169 }
170 if !match {
171 t.Error("output string did not match regex")
172 }
173
174 p.SetThresholds(NewThresholds(5, 10, 3, 11))
175 regex = fmt.Sprintf("'%s'=%g%s;%d:%d;%d:%d;;%g", label, value, unit, 5, 10, 3, 11, max)
176 match, err = regexp.Match(regex, p.output(false))
177 if err != nil {
178 t.Error(err.Error())
179 }
180 if !match {
181 t.Error("output string did not match regex")
182 }
183
184 p.SetThresholds(NewThresholds(0, warn, 0, crit))
185 regex = fmt.Sprintf("'%s'=%g%s;%g;%g;;%g", label, value, unit, warn, crit, max)
186 match, err = regexp.Match(regex, p.output(false))
187 if err != nil {
188 t.Error(err.Error())
189 }
190 if !match {
191 t.Error("output string did not match regex")
192 }
193
194 p.SetMin(min)
195 regex = fmt.Sprintf("'%s'=%g%s;%g;%g;%g;%g", label, value, unit, warn, crit, min, max)
196 match, err = regexp.Match(regex, p.output(false))
197 if err != nil {
198 t.Error(err.Error())
199 }
200 if !match {
201 t.Error("output string did not match regex")
202 }
203
204 regex = fmt.Sprintf(`'{"metric":"%s"}'=%g%s;%g;%g;%g;%g`, label, value, unit, warn, crit, min, max)
205 match, err = regexp.Match(regex, p.output(true))
206 if err != nil {
207 t.Error(err.Error())
208 }
209 if !match {
210 t.Error("output string did not match regex")
211 }
212
213 tag := "tag"
214 p.SetLabel(tag)
215 regex = fmt.Sprintf(`'{"metric":"%s","label":"%s"}'=%g%s;%g;%g;%g;%g`, label, tag, value, unit, warn, crit, min, max)
216 match, err = regexp.Match(regex, p.output(true))
217 if err != nil {
218 t.Error(err.Error())
219 }
220 if !match {
221 t.Error("output string did not match regex")
222 }
223
224 regex = fmt.Sprintf(`'%s_%s'=%g%s;%g;%g;%g;%g`, label, tag, value, unit, warn, crit, min, max)
225 match, err = regexp.Match(regex, p.output(false))
226 if err != nil {
227 t.Error(err.Error())
228 }
229 if !match {
230 t.Error("output string did not match regex")
231 }
232
233 }
234
235 func TestPerformanceData_add(t *testing.T) {
236 perfData := make(performanceData)
237
238 //valid perfdata point
239 err := perfData.add(NewPerformanceDataPoint("metric", 10))
240 if err != nil {
241 t.Error("adding a valid performance data point resulted in an error")
242 return
243 }
244
245 if _, ok := perfData[performanceDataPointKey{"metric", ""}]; !ok {
246 t.Error("performance data point was not added to the map of performance data points")
247 }
248
249 err = perfData.add(NewPerformanceDataPoint("metric", 10))
250 if err == nil {
251 t.Error("there was no error when adding a performance data point with a metric, that already exists in performance data")
252 }
253
254 err = perfData.add(NewPerformanceDataPoint("metric", 10).SetLabel("tag1"))
255 if err != nil {
256 t.Error("adding a valid performance data point resulted in an error")
257 return
258 }
259
260 err = perfData.add(NewPerformanceDataPoint("metric", 10).SetLabel("tag2"))
261 if err != nil {
262 t.Error("adding a valid performance data point resulted in an error")
263 return
264 }
265
266 err = perfData.add(NewPerformanceDataPoint("metric", 10).SetLabel("tag1"))
267 if err == nil {
268 t.Error("there was no error when adding a performance data point with a metric and tag, that already exists in performance data")
269 }
270 }
271
272 func TestResponse_SetPerformanceDataJsonLabel(t *testing.T) {
273 perfData := make(performanceData)
274
275 //valid perfdata point
276 err := perfData.add(NewPerformanceDataPoint("metric", 10))
277 if err != nil {
278 t.Error("adding a valid performance data point resulted in an error")
279 return
280 }
281
282 if _, ok := perfData[performanceDataPointKey{"metric", ""}]; !ok {
283 t.Error("performance data point was not added to the map of performance data points")
284 }
285
286 err = perfData.add(NewPerformanceDataPoint("metric", 10))
287 if err == nil {
288 t.Error("there was no error when adding a performance data point with a metric, that already exists in performance data")
289 }
290 }