-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime_test.go
320 lines (264 loc) · 9.14 KB
/
runtime_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
/*
* nagopher - Library for writing Nagios plugins in Go
* Copyright (C) 2018-2019 Pascal Mathis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package nagopher
import (
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
type mockResource struct {
Resource
}
type unsanitizedMockResource struct {
Resource
}
type mockEmptyResource struct {
Resource
}
type mockProbeErrorResource struct {
Resource
}
type mockSetupErrorResource struct {
Resource
}
type mockTeardownErrorResource struct {
Resource
}
type mockPerformanceErrorResource struct {
Resource
}
func TestBaseRuntime_Execute(t *testing.T) {
// given
warningThreshold := NewBounds(LowerBound(10), UpperBound(80))
check1 := NewCheck("usage", NewSummarizer())
check2 := NewCheck("usage", NewSummarizer())
check1.AttachResources(newMockResource())
check1.AttachContexts(NewScalarContext("usage", nil, nil))
check2.AttachResources(newMockResource())
check2.AttachContexts(NewScalarContext("usage", &warningThreshold, nil))
// when
result1 := NewRuntime(false).Execute(check1) // non-verbose
result2 := NewRuntime(true).Execute(check2) // verbose
// then
assert.Equal(t, StateOk().ExitCode(), result1.ExitCode())
assert.Equal(t, strings.Join([]string{
"USAGE OK - usage1 is 49.4% | usage1=49.4% usage2=92.6% usage3=83.1",
"nagopher: stripped illegal character from string [usage3=83.1]",
}, "\n")+"\n", result1.Output())
assert.Equal(t, StateWarning().ExitCode(), result2.ExitCode())
assert.Equal(t, strings.Join([]string{
"USAGE WARNING - usage2 is 92.6% (outside range 10:80) | usage1=49.4%;10:80 usage2=92.6%;10:80 usage3=83.1;10:80",
"warning: usage2 is 92.6% (outside range 10:80)",
"warning: usage3 is 83.1 (outside range 10:80)",
"nagopher: stripped illegal character from string [usage3=83.1;10:80]",
"nagopher: stripped illegal character from string [warning: usage3 is 83.1 (outside range 10:80)]",
}, "\n")+"\n", result2.Output())
}
func TestBaseRuntime_Execute_Sanitize(t *testing.T) {
// given
check := NewCheck("what\nthe\ncheck", NewSummarizer())
check.AttachResources(newUnsanitizedMockResource())
check.AttachContexts(
NewScalarContext("stranger\nthings", nil, nil),
NewStringInfoContext("Still\nAlive"),
)
// when
result := NewRuntime(true).Execute(check)
// then
assert.Equal(t, strings.Join([]string{
"WHATTHECHECK OK - weirdthings is 49.4% | 'weirdthings'=49.4%",
"info: TheCakeIsALie",
"nagopher: stripped illegal character from string [WHATTHECHECK]",
"nagopher: stripped illegal character from string [weirdthings is 49.4%]",
"nagopher: stripped illegal character from string ['weirdthings'=49.4%]",
"nagopher: stripped illegal character from string [info: TheCakeIsALie]",
}, "\n")+"\n", result.Output())
}
func TestBaseRuntime_Execute_MissingContext(t *testing.T) {
// given
check := NewCheck("check", NewSummarizer())
check.AttachResources(newMockResource())
// when
result := NewRuntime(false).Execute(check)
// then
assert.Equal(t, StateUnknown().ExitCode(), result.ExitCode())
assert.Equal(t, strings.Join([]string{
"CHECK UNKNOWN - nagopher: missing context with name [usage]",
}, "\n")+"\n", result.Output())
}
func TestBaseRuntime_Execute_Empty(t *testing.T) {
// given
check := NewCheck("check", NewSummarizer())
check.AttachResources(newMockEmptyResource())
// when
result := NewRuntime(false).Execute(check)
// then
assert.Equal(t, StateUnknown().ExitCode(), result.ExitCode())
assert.Equal(t, strings.Join([]string{
"CHECK UNKNOWN - nagopher: resource [*nagopher.mockEmptyResource] did not return any metrics",
}, "\n")+"\n", result.Output())
}
func TestBaseRuntime_Execute_ProbeError(t *testing.T) {
// given
check := NewCheck("check", NewSummarizer())
check.AttachResources(newMockProbeErrorResource())
// when
result := NewRuntime(false).Execute(check)
// then
assert.Equal(t, StateUnknown().ExitCode(), result.ExitCode())
assert.Equal(t, strings.Join([]string{
"CHECK UNKNOWN - artificial error happened here",
}, "\n")+"\n", result.Output())
}
func TestBaseRuntime_Execute_SetupError(t *testing.T) {
// given
check := NewCheck("check", NewSummarizer())
check.AttachResources(newMockSetupErrorResource())
// when
result := NewRuntime(false).Execute(check)
// then
assert.Equal(t, StateUnknown().ExitCode(), result.ExitCode())
assert.Equal(t, strings.Join([]string{
"CHECK UNKNOWN - artificial error happened here",
}, "\n")+"\n", result.Output())
}
func TestBaseRuntime_Execute_TeardownError(t *testing.T) {
// given
check := NewCheck("check", NewSummarizer())
check.AttachResources(newMockTeardownErrorResource())
check.AttachContexts(NewScalarContext("dummy", nil, nil))
// when
result := NewRuntime(false).Execute(check)
// then
assert.Equal(t, StateUnknown().ExitCode(), result.ExitCode())
assert.Equal(t, strings.Join([]string{
"CHECK UNKNOWN - artificial error happened here",
}, "\n")+"\n", result.Output())
}
func TestBaseRuntime_Execute_PerformanceError(t *testing.T) {
// given
check := NewCheck("check", NewSummarizer())
check.AttachResources(newMockPerformanceErrorResource())
check.AttachContexts(NewScalarContext("usage", nil, nil))
// when
result := NewRuntime(false).Execute(check)
// then
assert.Equal(t, StateUnknown().ExitCode(), result.ExitCode())
assert.Equal(t, strings.Join([]string{
"CHECK UNKNOWN - nagopher: collecting performance data failed with [perfdata metric name [inv'=alid] contains invalid characters]",
}, "\n")+"\n", result.Output())
}
func TestBaseRuntime_ExecuteAndExit(t *testing.T) {
var resultExitCode = -1
var resultOutput = ""
// given
check := NewCheck("check", NewSummarizer())
check.AttachResources(newMockResource())
check.AttachContexts(NewScalarContext("usage", nil, nil))
resultExitFunction = func(exitCode int) { resultExitCode = exitCode }
resultOutputFunction = func(values ...interface{}) (int, error) {
resultOutput = fmt.Sprint(values...)
return len(resultOutput), nil
}
// when
expectedResult := NewRuntime(false).Execute(check)
NewRuntime(false).ExecuteAndExit(check)
// then
assert.Equal(t, int(expectedResult.ExitCode()), resultExitCode)
assert.Equal(t, expectedResult.Output(), resultOutput)
}
func TestNewCheckResult(t *testing.T) {
// when
exitCode := StateOk().ExitCode()
description := StateOk().Description()
checkResult := NewCheckResult(exitCode, description)
// then
assert.Equal(t, exitCode, checkResult.ExitCode())
assert.Equal(t, description, checkResult.Output())
}
func newMockResource() Resource {
return &mockResource{
Resource: NewResource(),
}
}
func (r mockResource) Probe(warnings WarningCollection) ([]Metric, error) {
return []Metric{
MustNewNumericMetric("usage1", 49.4, "%", nil, "usage"),
MustNewNumericMetric("usage2", 92.6, "%", nil, "usage"),
MustNewNumericMetric("usage3", 83.1, "|", nil, "usage"),
}, nil
}
func newUnsanitizedMockResource() Resource {
return &unsanitizedMockResource{
Resource: NewResource(),
}
}
func (r unsanitizedMockResource) Probe(warnings WarningCollection) ([]Metric, error) {
return []Metric{
MustNewNumericMetric("weird\nthings", 49.4, "%", nil, "stranger\nthings"),
MustNewStringMetric("Aperture\nScience", "The\nCakeIs\nA\nLie", "Still\nAlive"),
}, nil
}
func newMockEmptyResource() Resource {
return &mockEmptyResource{
Resource: NewResource(),
}
}
func (r mockEmptyResource) Probe(warnings WarningCollection) ([]Metric, error) {
return []Metric{}, nil
}
func newMockProbeErrorResource() Resource {
return &mockProbeErrorResource{
Resource: NewResource(),
}
}
func (r mockProbeErrorResource) Probe(warnings WarningCollection) ([]Metric, error) {
return []Metric{}, fmt.Errorf("artificial error happened here")
}
func newMockSetupErrorResource() Resource {
return &mockSetupErrorResource{
Resource: NewResource(),
}
}
func (r mockSetupErrorResource) Setup(warnings WarningCollection) error {
return fmt.Errorf("artificial error happened here")
}
func newMockTeardownErrorResource() Resource {
return &mockTeardownErrorResource{
Resource: NewResource(),
}
}
func (r mockTeardownErrorResource) Teardown(warnings WarningCollection) error {
return fmt.Errorf("artificial error happened here")
}
func (r mockTeardownErrorResource) Probe(warnings WarningCollection) ([]Metric, error) {
return []Metric{
MustNewNumericMetric("dummy", 0, "", nil, "dummy"),
}, nil
}
func newMockPerformanceErrorResource() Resource {
return &mockPerformanceErrorResource{
Resource: NewResource(),
}
}
func (r mockPerformanceErrorResource) Probe(warnings WarningCollection) ([]Metric, error) {
return []Metric{
MustNewNumericMetric("inv'=alid", 49.4, "%", nil, "usage"),
}, nil
}