-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_test.go
144 lines (134 loc) · 2.98 KB
/
group_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
package healthz
import (
"fmt"
"testing"
)
func TestComponentGroupSetGroupHealth(t *testing.T) {
c := newComponentGroup(Major, Unknown)
if c.Health != Unknown {
t.Fatal("unexpected health:", c.Health)
}
if c.OverallHealth() != Unknown {
t.Fatal("unexpected OverallHealth():", c.Health)
}
c.SetGroupHealth(Warning)
if c.Health != Warning {
t.Fatal("unexpected health:", c.Health)
}
if c.OverallHealth() != Warning {
t.Fatal("unexpected OverallHealth():", c.Health)
}
}
func TestRegister(t *testing.T) {
c := newComponentGroup(Minor, Unknown)
c.RegisterSubcomponent("test", Major)
sc, found := c.Subcomponents["test"]
if !found {
t.Fatal("component test not found")
}
if sc.Severity != Major {
t.Fatal("unexpected severity:", c.Severity)
}
c.UnregisterSubcomponent("test")
_, found = c.Subcomponents["test"]
if found {
t.Fatal("unregistered component test was found")
}
}
type hs struct {
h Health
s Severity
}
var overallHealthTestCases = []struct {
in []hs
out Health
}{
// Capping
{
in: []hs{{Error, Unspecified}},
out: Warning,
},
{
in: []hs{{Warning, Unspecified}},
out: Unknown,
},
{
in: []hs{{Unknown, Unspecified}},
out: Normal,
},
{
in: []hs{{Normal, Unspecified}},
out: Normal,
},
{
in: []hs{{Redundant, Unspecified}},
out: Normal,
},
{
in: []hs{{Error, Minor}},
out: Unknown,
},
{
in: []hs{{Redundant, Minor}},
out: Unknown,
},
// Mixed
{
in: []hs{{Unknown, Major}, {Unknown, Unspecified}, {Unknown, Minor}},
out: Unknown,
},
{
in: []hs{{Unknown, Major}, {Unknown, Unspecified}, {Redundant, Minor}},
out: Unknown,
},
{
in: []hs{{Unknown, Major}, {Redundant, Unspecified}, {Unknown, Minor}},
out: Unknown,
},
{
in: []hs{{Redundant, Major}, {Unknown, Unspecified}, {Unknown, Minor}},
out: Normal,
},
{
in: []hs{{Redundant, Major}, {Redundant, Unspecified}, {Unknown, Minor}},
out: Redundant,
},
{
in: []hs{{Redundant, Major}, {Normal, Unspecified}, {Unknown, Minor}},
out: Redundant,
},
{
in: []hs{{Redundant, Major}, {Redundant, Unspecified}, {Warning, Minor}},
out: Redundant,
},
{
in: []hs{{Redundant, Major}, {Error, Unspecified}, {Redundant, Minor}},
out: Warning,
},
}
func TestOverallHealth(t *testing.T) {
{
c := newComponentGroup(Major, Unknown)
if o := c.OverallHealth(); o != Unknown {
t.Fatal("unexpected OverallHealth:", o)
}
c.RegisterSubcomponent("major", Major)
c.RegisterSubcomponent("unspecified", Unspecified)
c.RegisterSubcomponent("minor", Minor)
if o := c.OverallHealth(); o != Unknown {
t.Fatal("unexpected OverallHealth:", o)
}
}
for i, tCase := range overallHealthTestCases {
c := newComponentGroup(Major, Unknown)
if o := c.OverallHealth(); o != Unknown {
t.Fatal("unexpected OverallHealth:", o)
}
for i, p := range tCase.in {
c.RegisterSubcomponent(fmt.Sprintf("c#%d", i), p.s).SetGroupHealth(p.h)
}
if o := c.OverallHealth(); o != tCase.out {
t.Fatalf("case #%d: unexpected OverallHealth: %v", i, o)
}
}
}