-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathperfdata_test.go
More file actions
193 lines (170 loc) · 3.44 KB
/
Copy pathperfdata_test.go
File metadata and controls
193 lines (170 loc) · 3.44 KB
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
package check
import (
"fmt"
"math"
"testing"
)
func ExamplePerfdataList() {
list := PerfdataList{}
list.Add(&Perfdata{Label: "test1", Value: 23})
list.Add(&Perfdata{Label: "test2", Value: 42})
fmt.Println(list)
// Output:
// [test1=23 test2=42]
}
func TestPerfdataListFormating(t *testing.T) {
list := PerfdataList{}
list.Add(&Perfdata{Label: "test1", Value: 23})
list.Add(&Perfdata{Label: "test2", Value: 42})
actual := list.String()
expected := "test1=23 test2=42"
if actual != expected {
t.Fatalf("expected %v, got %v", expected, actual)
}
}
func BenchmarkPerfdataListFormating(b *testing.B) {
b.ReportAllocs()
list := PerfdataList{}
list.Add(&Perfdata{Label: "test1", Value: 23})
list.Add(&Perfdata{Label: "test2", Value: 42})
for i := 0; i < b.N; i++ {
l := list.String()
_ = l
}
}
func BenchmarkPerfdataString(b *testing.B) {
b.ReportAllocs()
perf := Perfdata{
Label: "test test=test",
Value: 10.1,
Uom: "%",
Warn: &Threshold{Upper: 80},
Crit: &Threshold{Upper: 90},
Min: 0,
Max: 100}
for i := 0; i < b.N; i++ {
p := perf.String()
_ = p
}
}
func TestRenderPerfdata(t *testing.T) {
testcases := map[string]struct {
perf Perfdata
expected string
}{
"simple": {
perf: Perfdata{
Label: "test",
Value: 2,
},
expected: "test=2",
},
"with-quotes": {
perf: Perfdata{
Label: "te's\"t",
Value: 2,
},
expected: "te_s_t=2",
},
"with-special-chars": {
perf: Perfdata{
Label: "test_🖥️_'test",
Value: 2,
},
expected: "test_🖥️__test=2",
},
"with-uom": {
perf: Perfdata{
Label: "test",
Value: 2,
Uom: "%",
},
expected: "test=2%",
},
"with-thresholds": {
perf: Perfdata{
Label: "foo bar",
Value: 2.76,
Uom: "m",
Warn: &Threshold{Lower: 10, Upper: 25, Inside: true},
Crit: &Threshold{Lower: 15, Upper: 20, Inside: false},
},
expected: "'foo bar'=2.76m;@10:25;15:20",
},
}
testcasesWithErrors := map[string]struct {
perf Perfdata
expected string
}{
"invalid-value": {
perf: Perfdata{
Label: "to infinity",
Value: math.Inf(+1),
},
expected: "",
},
}
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
pfVal, err := tc.perf.ValidatedString()
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if tc.expected != pfVal {
t.Fatalf("expected %v, got %v", tc.expected, pfVal)
}
})
}
for name, tc := range testcasesWithErrors {
t.Run(name, func(t *testing.T) {
pfVal, err := tc.perf.ValidatedString()
if err == nil {
t.Fatalf("expected error, got none")
}
if tc.expected != pfVal {
t.Fatalf("expected %v, got %v", tc.expected, pfVal)
}
})
}
}
type pfFormatTest struct {
Result string
InputValue any
}
func TestFormatNumeric(t *testing.T) {
testdata := []pfFormatTest{
{
Result: "10",
InputValue: 10,
},
{
Result: "-10",
InputValue: -10,
},
{
Result: "10",
InputValue: uint8(10),
},
{
Result: "1234.567",
InputValue: float64(1234.567),
},
{
Result: "3456.789",
InputValue: float32(3456.789),
},
{
Result: "1234567890.988",
InputValue: 1234567890.9877,
},
}
for _, val := range testdata {
formatted, err := formatNumeric(val.InputValue)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if val.Result != formatted {
t.Fatalf("expected %v, got %v", formatted, val.Result)
}
}
}