-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_scalar_test.go
80 lines (69 loc) · 2.71 KB
/
context_scalar_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
/*
* 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 (
"github.com/stretchr/testify/assert"
"testing"
)
func TestScalarContext_Evaluate(t *testing.T) {
// given
warningThreshold := NewBounds(LowerBound(10), UpperBound(20))
criticalThreshold := NewBounds(LowerBound(0), UpperBound(30))
context := NewScalarContext("context", &warningThreshold, &criticalThreshold)
metric1 := MustNewNumericMetric("metric 1", 15, "", nil, "")
metric2 := MustNewNumericMetric("metric 2", 5, "", nil, "")
metric3 := MustNewNumericMetric("metric 3", -5, "", nil, "")
metric4 := MustNewStringMetric("invalid", "Oops!", "")
resource := NewResource()
// when
result1 := context.Evaluate(metric1, resource)
result2 := context.Evaluate(metric2, resource)
result3 := context.Evaluate(metric3, resource)
result4 := context.Evaluate(metric4, resource)
// then
assert.Equal(t, StateOk(), result1.State().OrElse(nil))
assert.Equal(t, StateWarning(), result2.State().OrElse(nil))
assert.Equal(t, StateCritical(), result3.State().OrElse(nil))
assert.Equal(t, StateUnknown(), result4.State().OrElse(nil))
assert.Equal(t, "", result1.Hint())
assert.Equal(t, "outside range 10:20", result2.Hint())
assert.Equal(t, "outside range 0:30", result3.Hint())
assert.Contains(t, result4.Hint(), "ScalarContext can not process metric of type")
}
func TestScalarContext_Performance(t *testing.T) {
// given
context := NewScalarContext("context", nil, nil)
metric1 := MustNewNumericMetric("valid", 42, "", nil, "")
metric2 := MustNewNumericMetric("inv='alid", 42, "", nil, "")
resource := NewResource()
// when
var perfData1, perfData2 PerfData = nil, nil
optionalPerfData1, err1 := context.Performance(metric1, resource)
optionalPerfData2, err2 := context.Performance(metric2, resource)
if err1 == nil {
perfData1, err1 = optionalPerfData1.Get()
}
if err2 == nil {
perfData2, err2 = optionalPerfData2.Get()
}
// then
assert.NoError(t, err1)
assert.Error(t, err2)
assert.Implements(t, (*PerfData)(nil), perfData1)
assert.Nil(t, perfData2)
}