-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstatus_test.go
More file actions
278 lines (252 loc) · 5.02 KB
/
Copy pathstatus_test.go
File metadata and controls
278 lines (252 loc) · 5.02 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
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
package check
import (
"testing"
)
func TestStatus_String(t *testing.T) {
testcases := map[string]struct {
input Status
expected string
}{
"OK": {
input: OK,
expected: "OK",
},
"WARNING": {
input: Warning,
expected: "WARNING",
},
"CRITICAL": {
input: Critical,
expected: "CRITICAL",
},
"UNKNOWN": {
input: Unknown,
expected: "UNKNOWN",
},
}
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
actual := tc.input.String()
if actual != tc.expected {
t.Fatalf("expected %v, got %v", tc.expected, actual)
}
})
}
}
func TestStatus_FromString(t *testing.T) {
testcases := map[string]struct {
expected Status
input string
}{
"OK": {
input: "OK",
expected: OK,
},
"WARNING": {
input: "WARNING",
expected: Warning,
},
"WaRnInG": {
input: "WaRnInG",
expected: Warning,
},
"CRITICAL": {
input: "CRITICAL",
expected: Critical,
},
"Critical": {
input: "Critical",
expected: Critical,
},
"UNKNOWN": {
input: "UNKNOWN",
expected: Unknown,
},
"unknown": {
input: "unknown",
expected: Unknown,
},
}
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
actual, _ := NewStatusFromString(tc.input)
if actual != tc.expected {
t.Fatalf("expected %v, got %v", tc.expected, actual)
}
})
}
}
func TestStatus_FromString_WithErr(t *testing.T) {
actual, err := NewStatusFromString("unittest")
if err == nil {
t.Fatalf("expected error, got nil")
}
if actual != Unknown {
t.Fatalf("expected Unknown, got %v", actual)
}
}
func TestStatus_FromInt(t *testing.T) {
testcases := map[string]struct {
expected Status
input int
}{
"OK": {
input: 0,
expected: OK,
},
"WARNING": {
input: 1,
expected: Warning,
},
"CRITICAL": {
input: 2,
expected: Critical,
},
"UNKNOWN": {
input: 3,
expected: Unknown,
},
}
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
actual, _ := NewStatus(tc.input)
if actual != tc.expected {
t.Fatalf("expected %v, got %v", tc.expected, actual)
}
})
}
}
func TestStatus_FromInt_WithErr(t *testing.T) {
actual, err := NewStatus(1337)
if err == nil {
t.Fatalf("expected error, got nil")
}
if actual != Unknown {
t.Fatalf("expected Unknown, got %v", actual)
}
}
type CompareSet struct {
Left Status
Right Status
Result int
}
func TestCompareStatus(t *testing.T) {
inputList := []Status{OK, Warning, Unknown, Critical}
for _, val := range inputList {
if Compare(val, val) != 0 {
t.Fatalf("Equal comparison failed with %d and %d", val, val)
}
}
ComparisonTests := []CompareSet{
{OK, Critical, 1},
{OK, Warning, 1},
{OK, Unknown, 1},
{Warning, Critical, 1},
{Warning, Unknown, 1},
{Warning, OK, -1},
{Unknown, Critical, 1},
{Unknown, OK, -1},
{Unknown, Warning, -1},
{Critical, OK, -1},
{Critical, Warning, -1},
{Critical, Unknown, -1},
}
for _, set := range ComparisonTests {
if Compare(set.Left, set.Right) != set.Result {
t.Fatalf("Comparison failed with %s and %s, got %d", set.Left, set.Right, Compare(set.Left, set.Right))
}
}
}
func TestWorstState(t *testing.T) {
tests := []struct {
name string
input []Status
expected Status
}{
{
name: "Unknown",
input: []Status{Unknown},
expected: Unknown,
},
{
name: "Unknown",
input: []Status{Unknown},
expected: Unknown,
},
{
name: "Critical",
input: []Status{Critical},
expected: Critical,
},
{
name: "Warning",
input: []Status{Warning},
expected: Warning,
},
{
name: "OK",
input: []Status{OK},
expected: OK,
},
{
name: "Mixed with Critical",
input: []Status{OK, Warning, Critical, Unknown},
expected: Critical,
},
{
name: "Mixed order with Critical",
input: []Status{OK, Critical, Warning, Unknown},
expected: Critical,
},
{
name: "Mixed with Unknown",
input: []Status{OK, Warning, Unknown},
expected: Unknown,
},
{
name: "Mixed order with Unknown",
input: []Status{OK, Unknown, Warning},
expected: Unknown,
},
{
name: "Warning with OKs",
input: []Status{Warning, OK, OK},
expected: Warning,
},
{
name: "Warning orderwith OKs",
input: []Status{OK, Warning, OK},
expected: Warning,
},
{
name: "All OK",
input: []Status{OK, OK, OK},
expected: OK,
},
{
name: "Empty",
input: []Status{},
expected: Unknown,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := WorstState(tt.input...); got != tt.expected {
t.Errorf("WorstState(%v) is: %v, expected %v", tt.input, got, tt.expected)
}
})
}
}
func BenchmarkWorstState(b *testing.B) {
b.ReportAllocs()
// Initialize slice for benchmarking
states := make([]Status, 0, 100)
for i := range 100 {
st, _ := NewStatus(i % 4)
states = append(states, st)
}
for i := 0; i < b.N; i++ {
s := WorstState(states...)
_ = s
}
}