-
Notifications
You must be signed in to change notification settings - Fork 3
/
exported_test.go
419 lines (333 loc) · 10.6 KB
/
exported_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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright 2020 Adam Chalkley
//
// https://github.com/atc0005/go-nagios
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
// Package nagios_test provides test coverage for exported package
// functionality.
package nagios_test
import (
_ "embed"
"fmt"
"strings"
"testing"
"github.com/atc0005/go-nagios"
"github.com/google/go-cmp/cmp"
)
// The specific format used by the test input files is VERY specific; trailing
// space + newline patterns are intentional. Because "format on save" editor
// functionality easily breaks this input it is stored in separate files to
// reduce test breakage due to editors "helping".
var (
//go:embed testdata/plugin-output-datastore-0001.txt
pluginOutputDatastore0001 string
//go:embed testdata/plugin-output-gh103-multi-line-with-perf-data.txt
pluginOutputGH103MultiLineWithPerfData string
//go:embed testdata/plugin-output-gh103-one-line-with-perf-data.txt
pluginOutputGH103OneLineWithPerfData string
)
// TestPluginOutputIsValid configures an Plugin value as client code would
// and then asserts that the generated output matches manually crafted test
// data.
func TestPluginOutputIsValid(t *testing.T) {
t.Parallel()
want := pluginOutputDatastore0001
// Setup Plugin value manually. This approach does not provide the
// default time metric that would be provided when using the Plugin
// constructor.
plugin := nagios.Plugin{
LastError: nil,
ExitStatusCode: nagios.StateOKExitCode,
}
var outputBuffer strings.Builder
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
plugin.CriticalThreshold = fmt.Sprintf(
"%d%% datastore usage",
95,
)
plugin.WarningThreshold = fmt.Sprintf(
"%d%% datastore usage",
90,
)
//nolint:goconst
plugin.ServiceOutput =
"OK: Datastore HUSVM-DC1-vol6 space usage (0 VMs)" +
" is 0.01% of 18.0TB with 18.0TB remaining" +
" [WARNING: 90% , CRITICAL: 95%]"
var longServiceOutputReport strings.Builder
_, _ = fmt.Fprintf(
&longServiceOutputReport,
"Datastore Space Summary:%s%s"+
"* Name: %s%s"+
"* Space Used: %v (%.2f%%)%s"+
"* Space Remaining: %v (%.2f%%)%s"+
"* VMs: %v %s%s",
nagios.CheckOutputEOL,
nagios.CheckOutputEOL,
"HUSVM-DC1-vol6",
nagios.CheckOutputEOL,
"2.3GB",
0.01,
nagios.CheckOutputEOL,
"18.0TB",
99.99,
nagios.CheckOutputEOL,
0,
nagios.CheckOutputEOL,
nagios.CheckOutputEOL,
)
_, _ = fmt.Fprintf(
&longServiceOutputReport,
"%s---%s%s",
nagios.CheckOutputEOL,
nagios.CheckOutputEOL,
nagios.CheckOutputEOL,
)
_, _ = fmt.Fprintf(
&longServiceOutputReport,
"* vSphere environment: %s%s",
"https://vc1.example.com:443/sdk",
nagios.CheckOutputEOL,
)
_, _ = fmt.Fprintf(
&longServiceOutputReport,
"* Plugin User Agent: %s%s",
"check-vmware/v0.30.6-0-g25fdcdc",
nagios.CheckOutputEOL,
)
plugin.LongServiceOutput = longServiceOutputReport.String()
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
// Retrieve the output buffer content so that we can compare actual output
// against our expected output to assert we have a 1:1 match.
got := outputBuffer.String()
// if want != got {
// t.Error(cmp.Diff(want, got))
// }
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
}
}
// TestPerformanceDataIsOnSameLineAsServiceOutput asserts that performance
// data is emitted on the same line as the Service Output (aka, "one-line
// summary") if Long Service Output is empty.
//
// See also:
//
// - https://github.com/atc0005/go-nagios/issues/103
func TestPerformanceDataIsOnSameLineAsServiceOutput(t *testing.T) {
t.Parallel()
want := pluginOutputGH103OneLineWithPerfData
// Setup Plugin value manually. This approach does not provide the
// default time metric that would be provided when using the Plugin
// constructor.
plugin := nagios.Plugin{
LastError: nil,
ExitStatusCode: nagios.StateOKExitCode,
}
var outputBuffer strings.Builder
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
//nolint:goconst
plugin.ServiceOutput =
"OK: Datastore HUSVM-DC1-vol6 space usage (0 VMs)" +
" is 0.01% of 18.0TB with 18.0TB remaining" +
" [WARNING: 90% , CRITICAL: 95%]"
pd := nagios.PerformanceData{
Label: "time",
Value: "874ms",
}
if err := plugin.AddPerfData(false, pd); err != nil {
t.Errorf("failed to add performance data: %v", err)
}
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
// Retrieve the output buffer content so that we can compare actual output
// against our expected output to assert we have a 1:1 match.
got := outputBuffer.String()
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
}
}
// TestPerformanceDataIsAfterLongServiceOutput asserts that performance data
// is emitted after Long Service Output when that content is available.
//
// See also:
//
// - https://github.com/atc0005/go-nagios/issues/103
func TestPerformanceDataIsAfterLongServiceOutput(t *testing.T) {
t.Parallel()
want := pluginOutputGH103MultiLineWithPerfData
var outputBuffer strings.Builder
// Setup Plugin value manually. This approach does not provide the
// default time metric that would be provided when using the Plugin
// constructor.
plugin := nagios.Plugin{
LastError: nil,
ExitStatusCode: nagios.StateOKExitCode,
}
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
plugin.CriticalThreshold = fmt.Sprintf(
"%d%% datastore usage",
95,
)
plugin.WarningThreshold = fmt.Sprintf(
"%d%% datastore usage",
90,
)
//nolint:goconst
plugin.ServiceOutput =
"OK: Datastore HUSVM-DC1-vol6 space usage (0 VMs)" +
" is 0.01% of 18.0TB with 18.0TB remaining" +
" [WARNING: 90% , CRITICAL: 95%]"
var longServiceOutputReport strings.Builder
_, _ = fmt.Fprintf(
&longServiceOutputReport,
"Datastore Space Summary:%s%s"+
"* Name: %s%s"+
"* Space Used: %v (%.2f%%)%s"+
"* Space Remaining: %v (%.2f%%)%s"+
"* VMs: %v %s%s",
nagios.CheckOutputEOL,
nagios.CheckOutputEOL,
"HUSVM-DC1-vol6",
nagios.CheckOutputEOL,
"2.3GB",
0.01,
nagios.CheckOutputEOL,
"18.0TB",
99.99,
nagios.CheckOutputEOL,
0,
nagios.CheckOutputEOL,
nagios.CheckOutputEOL,
)
_, _ = fmt.Fprintf(
&longServiceOutputReport,
"%s---%s%s",
nagios.CheckOutputEOL,
nagios.CheckOutputEOL,
nagios.CheckOutputEOL,
)
_, _ = fmt.Fprintf(
&longServiceOutputReport,
"* vSphere environment: %s%s",
"https://vc1.example.com:443/sdk",
nagios.CheckOutputEOL,
)
_, _ = fmt.Fprintf(
&longServiceOutputReport,
"* Plugin User Agent: %s%s",
"check-vmware/v0.30.6-0-g25fdcdc",
nagios.CheckOutputEOL,
)
plugin.LongServiceOutput = longServiceOutputReport.String()
pd := nagios.PerformanceData{
Label: "time",
Value: "874ms",
}
if err := plugin.AddPerfData(false, pd); err != nil {
t.Errorf("failed to add performance data: %v", err)
}
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
// Retrieve the output buffer content so that we can compare actual output
// against our expected output to assert we have a 1:1 match.
got := outputBuffer.String()
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
}
}
// TestEmptyServiceOutputAndManuallyConstructedPluginProducesNoOutput
// asserts that an empty ServiceOutput field produces no output when manually
// constructing the Plugin value.
func TestEmptyServiceOutputAndManuallyConstructedPluginProducesNoOutput(t *testing.T) {
t.Parallel()
// Setup Plugin value manually. This approach does not provide the
// default time metric that would be provided when using the Plugin
// constructor.
plugin := nagios.Plugin{
LastError: nil,
ExitStatusCode: nagios.StateOKExitCode,
}
var outputBuffer strings.Builder
// Explicitly indicate that the field is empty (default/zero value).
plugin.ServiceOutput = ""
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
want := ""
// Retrieve the output buffer content so that we can compare actual output
// against our expected output to assert we have a 1:1 match.
got := outputBuffer.String()
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
} else {
t.Logf("OK: Empty ServiceOutput field produces no output.")
}
}
// TestEmptyServiceOutputAndConstructedPluginProducesNoOutput asserts that
// an empty ServiceOutput field produces no output. We provide a default time
// metric if client code does not specify one AND if there is ServiceOutput
// content to emit.
func TestEmptyServiceOutputAndConstructedPluginProducesNoOutput(t *testing.T) {
t.Parallel()
// Setup Plugin type the same way that client code using the
// constructor would.
plugin := nagios.NewPlugin()
var outputBuffer strings.Builder
// Explicitly indicate that the field is empty (default/zero value).
plugin.ServiceOutput = ""
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
want := ""
// Retrieve the output buffer content so that we can compare actual output
// against our expected output to assert we have a 1:1 match.
got := outputBuffer.String()
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
} else {
t.Logf("OK: Empty ServiceOutput field produces no output.")
}
}
// TestEmptyClientPerfDataAndConstructedPluginProducesDefaultTimeMetric
// asserts that omitted performance data from client code produces a default
// time metric when using the Plugin constructor.
func TestEmptyClientPerfDataAndConstructedPluginProducesDefaultTimeMetric(t *testing.T) {
t.Parallel()
// Setup Plugin type the same way that client code using the
// constructor would.
plugin := nagios.NewPlugin()
// Performance Data metrics are not emitted if we do not supply a
// ServiceOutput value.
plugin.ServiceOutput = "TacoTuesday"
var outputBuffer strings.Builder
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
want := fmt.Sprintf(
"%s | %s",
plugin.ServiceOutput,
"'time'=",
)
got := outputBuffer.String()
if !strings.Contains(got, want) {
t.Errorf("ERROR: Plugin output does not contain the expected time metric")
t.Errorf("\nwant %q\ngot %q", want, got)
} else {
t.Logf("OK: Emitted performance data contains the expected time metric.")
}
}