1 package v1
2
3 import (
4 "testing"
5 "time"
6
7 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8 )
9
10 func TestSetStatusCondition(t *testing.T) {
11 testCases := []struct {
12 name string
13 testCondition Condition
14 startConditions *[]Condition
15 expectedConditions *[]Condition
16 }{
17 {
18 name: "add when empty",
19 testCondition: Condition{
20 Type: ConditionAvailable,
21 Status: "True",
22 Reason: "Testing",
23 Message: "Basic message",
24 },
25 startConditions: &[]Condition{},
26 expectedConditions: &[]Condition{
27 {
28 Type: ConditionAvailable,
29 Status: "True",
30 Reason: "Testing",
31 Message: "Basic message",
32 },
33 },
34 },
35 {
36 name: "add to conditions",
37 testCondition: Condition{
38 Type: ConditionAvailable,
39 Status: "True",
40 Reason: "TestingAvailableTrue",
41 Message: "Available condition true",
42 },
43 startConditions: &[]Condition{
44 {
45 Type: ConditionDegraded,
46 Status: "False",
47 Reason: "TestingDegradedFalse",
48 Message: "Degraded condition false",
49 LastHeartbeatTime: metav1.NewTime(time.Now()),
50 },
51 },
52 expectedConditions: &[]Condition{
53 {
54 Type: ConditionAvailable,
55 Status: "True",
56 Reason: "TestingAvailableTrue",
57 Message: "Available condition true",
58 },
59 {
60 Type: ConditionDegraded,
61 Status: "False",
62 Reason: "TestingDegradedFalse",
63 Message: "Degraded condition false",
64 },
65 },
66 },
67 {
68 name: "replace condition",
69 testCondition: Condition{
70 Type: ConditionDegraded,
71 Status: "True",
72 Reason: "TestingDegradedTrue",
73 Message: "Degraded condition true",
74 },
75 startConditions: &[]Condition{
76 {
77 Type: ConditionDegraded,
78 Status: "False",
79 Reason: "TestingDegradedFalse",
80 Message: "Degraded condition false",
81 },
82 },
83 expectedConditions: &[]Condition{
84 {
85 Type: ConditionDegraded,
86 Status: "True",
87 Reason: "TestingDegradedTrue",
88 Message: "Degraded condition true",
89 },
90 },
91 },
92 {
93 name: "last heartbeat",
94 testCondition: Condition{
95 Type: ConditionDegraded,
96 Status: "True",
97 Reason: "TestingDegradedTrue",
98 Message: "Degraded condition true",
99 },
100 startConditions: &[]Condition{
101 {
102 Type: ConditionDegraded,
103 Status: "True",
104 Reason: "TestingDegradedFalse",
105 Message: "Degraded condition false",
106 },
107 },
108 expectedConditions: &[]Condition{
109 {
110 Type: ConditionDegraded,
111 Status: "True",
112 Reason: "TestingDegradedTrue",
113 Message: "Degraded condition true",
114 },
115 },
116 },
117 }
118
119 for _, tc := range testCases {
120 t.Run(tc.name, func(t *testing.T) {
121 SetStatusCondition(tc.startConditions, tc.testCondition)
122 compareConditions(t, tc.startConditions, tc.expectedConditions)
123
124 SetStatusConditionNoHeartbeat(tc.startConditions, tc.testCondition)
125 compareConditionsNoHeartbeat(t, tc.startConditions, tc.expectedConditions)
126 })
127 }
128
129 return
130 }
131
132 func TestRemoveStatusCondition(t *testing.T) {
133 testCases := []struct {
134 name string
135 testConditionType ConditionType
136 startConditions *[]Condition
137 expectedConditions *[]Condition
138 }{
139 {
140 name: "remove when empty",
141 testConditionType: ConditionAvailable,
142 startConditions: &[]Condition{},
143 expectedConditions: &[]Condition{},
144 },
145 {
146 name: "basic remove",
147 testConditionType: ConditionAvailable,
148 startConditions: &[]Condition{
149 {
150 Type: ConditionAvailable,
151 Status: "True",
152 Reason: "TestingAvailableTrue",
153 Message: "Available condition true",
154 LastHeartbeatTime: metav1.NewTime(time.Now()),
155 },
156 {
157 Type: ConditionDegraded,
158 Status: "False",
159 Reason: "TestingDegradedFalse",
160 Message: "Degraded condition false",
161 LastHeartbeatTime: metav1.NewTime(time.Now()),
162 },
163 },
164 expectedConditions: &[]Condition{
165 {
166 Type: ConditionDegraded,
167 Status: "False",
168 Reason: "TestingDegradedFalse",
169 Message: "Degraded condition false",
170 },
171 },
172 },
173 {
174 name: "remove last condition",
175 testConditionType: ConditionAvailable,
176 startConditions: &[]Condition{
177 {
178 Type: ConditionAvailable,
179 Status: "True",
180 Reason: "TestingAvailableTrue",
181 Message: "Available condition true",
182 },
183 },
184 expectedConditions: &[]Condition{},
185 },
186 }
187
188 for _, tc := range testCases {
189 t.Run(tc.name, func(t *testing.T) {
190 RemoveStatusCondition(tc.startConditions, tc.testConditionType)
191 compareConditions(t, tc.startConditions, tc.expectedConditions)
192 })
193 }
194
195 return
196 }
197
198 func compareConditions(t *testing.T, gotConditions *[]Condition, expectedConditions *[]Condition) {
199 for _, expectedCondition := range *expectedConditions {
200 testCondition := FindStatusCondition(*gotConditions, expectedCondition.Type)
201 if testCondition == nil {
202 t.Errorf("Condition type '%v' not found in '%v'", expectedCondition.Type, *gotConditions)
203 }
204 compareCondition(t, testCondition, expectedCondition)
205 }
206 }
207
208 func compareCondition(t *testing.T, testCondition *Condition, expectedCondition Condition) {
209 compareConditionNoHeartbeat(t, testCondition, expectedCondition)
210
211 if testCondition.LastHeartbeatTime.IsZero() {
212 t.Error("lastHeartbeatTime should never be zero")
213 }
214 timeNow := metav1.NewTime(time.Now())
215 if timeNow.Before(&testCondition.LastHeartbeatTime) {
216 t.Errorf("Unexpected lastHeartbeatTime '%v', should be before '%v'", testCondition.LastHeartbeatTime, timeNow)
217 }
218 }
219
220 func compareConditionsNoHeartbeat(t *testing.T, gotConditions *[]Condition, expectedConditions *[]Condition) {
221 for _, expectedCondition := range *expectedConditions {
222 testCondition := FindStatusCondition(*gotConditions, expectedCondition.Type)
223 if testCondition == nil {
224 t.Errorf("Condition type '%v' not found in '%v'", expectedCondition.Type, *gotConditions)
225 }
226 compareConditionNoHeartbeat(t, testCondition, expectedCondition)
227 }
228 }
229
230 func compareConditionNoHeartbeat(t *testing.T, testCondition *Condition, expectedCondition Condition) {
231 if testCondition.Status != expectedCondition.Status {
232 t.Errorf("Unexpected status '%v', expected '%v'", testCondition.Status, expectedCondition.Status)
233 }
234 if testCondition.Message != expectedCondition.Message {
235 t.Errorf("Unexpected message '%v', expected '%v'", testCondition.Message, expectedCondition.Message)
236 }
237 if testCondition.Reason != expectedCondition.Reason {
238 t.Errorf("Unexpected reason '%v', expected '%v'", testCondition.Reason, expectedCondition.Reason)
239 }
240
241 }
242
View as plain text