@@ -6,7 +6,10 @@ import (
66
77 "github.com/google/go-cmp/cmp"
88 "github.com/google/go-cmp/cmp/cmpopts"
9+ "github.com/metal-stack/metal-lib/pkg/pointer"
910 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
12+ "testing/synctest"
1013)
1114
1215func Test_SortFirewallsByImportance (t * testing.T ) {
@@ -107,3 +110,183 @@ func Test_SortFirewallsByImportance(t *testing.T) {
107110 })
108111 }
109112}
113+
114+ func Test_EvaluateFirewallStatus (t * testing.T ) {
115+ tests := []struct {
116+ name string
117+ modFn func (fw * Firewall )
118+ healthTimeout time.Duration
119+ createTimeout time.Duration
120+ want * FirewallStatusEvalResult
121+ wantReason string
122+ }{
123+ {
124+ name : "ready firewall in running phase" ,
125+ modFn : nil ,
126+ want : & FirewallStatusEvalResult {
127+ Result : FirewallStatusReady ,
128+ },
129+ },
130+ {
131+ name : "unhealthy firewall in running phase due to firewall monitor not reconciling" ,
132+ modFn : func (fw * Firewall ) {
133+ fw .Status .Conditions .Set (Condition {
134+ Type : FirewallControllerConnected ,
135+ Status : ConditionFalse ,
136+ })
137+ },
138+ want : & FirewallStatusEvalResult {
139+ Result : FirewallStatusUnhealthy ,
140+ Reason : "not all health conditions are true: [Connected]" ,
141+ },
142+ },
143+ {
144+ name : "unhealthy firewall in running phase due to firewall not reconciling" ,
145+ modFn : func (fw * Firewall ) {
146+ fw .Status .Conditions .Set (Condition {
147+ Type : FirewallControllerSeedConnected ,
148+ Status : ConditionFalse ,
149+ })
150+ },
151+ want : & FirewallStatusEvalResult {
152+ Result : FirewallStatusUnhealthy ,
153+ Reason : "not all health conditions are true: [SeedConnected]" ,
154+ },
155+ },
156+ {
157+ name : "unhealthy firewall in running phase due to readiness condition false" ,
158+ modFn : func (fw * Firewall ) {
159+ fw .Status .Conditions .Set (Condition {
160+ Type : FirewallReady ,
161+ Status : ConditionFalse ,
162+ })
163+ },
164+ want : & FirewallStatusEvalResult {
165+ Result : FirewallStatusUnhealthy ,
166+ Reason : "not all health conditions are true: [Ready]" ,
167+ },
168+ },
169+ {
170+ name : "health timeout reached because seed not connected" ,
171+ healthTimeout : 5 * time .Minute ,
172+ modFn : func (fw * Firewall ) {
173+ cond := fw .Status .Conditions .Get (FirewallControllerSeedConnected )
174+ cond .Status = ConditionFalse
175+ fw .Status .Conditions .Set (* cond )
176+ },
177+ want : & FirewallStatusEvalResult {
178+ Result : FirewallStatusHealthTimeout ,
179+ Reason : "5m0s health timeout exceeded, seed connection lost" ,
180+ },
181+ },
182+ {
183+ name : "health timeout not yet reached" ,
184+ healthTimeout : 15 * time .Minute ,
185+ modFn : func (fw * Firewall ) {
186+ cond := fw .Status .Conditions .Get (FirewallControllerSeedConnected )
187+ cond .Status = ConditionFalse
188+ fw .Status .Conditions .Set (* cond )
189+ },
190+ want : & FirewallStatusEvalResult {
191+ Result : FirewallStatusUnhealthy ,
192+ Reason : "not all health conditions are true: [SeedConnected]" ,
193+ TimeoutIn : pointer .Pointer (5 * time .Minute ),
194+ },
195+ },
196+ {
197+ name : "create timeout reached because not provisioned" ,
198+ createTimeout : 5 * time .Minute ,
199+ modFn : func (fw * Firewall ) {
200+ fw .Status .Phase = FirewallPhaseCreating
201+ cond := fw .Status .Conditions .Get (FirewallProvisioned )
202+ cond .Status = ConditionFalse
203+ fw .Status .Conditions .Set (* cond )
204+ },
205+ want : & FirewallStatusEvalResult {
206+ Result : FirewallStatusCreateTimeout ,
207+ Reason : "5m0s create timeout exceeded, firewall not provisioned in time" ,
208+ },
209+ },
210+ {
211+ name : "create timeout not yet reached" ,
212+ createTimeout : 15 * time .Minute ,
213+ modFn : func (fw * Firewall ) {
214+ fw .Status .Phase = FirewallPhaseCreating
215+ cond := fw .Status .Conditions .Get (FirewallProvisioned )
216+ cond .Status = ConditionFalse
217+ fw .Status .Conditions .Set (* cond )
218+ },
219+ want : & FirewallStatusEvalResult {
220+ Result : FirewallStatusProgressing ,
221+ Reason : "not all health conditions are true: [Provisioned]" ,
222+ TimeoutIn : pointer .Pointer (5 * time .Minute ),
223+ },
224+ },
225+ }
226+ for _ , tt := range tests {
227+ t .Run (tt .name , func (t * testing.T ) {
228+ synctest .Test (t , func (t * testing.T ) {
229+ tenMinutesAgo := time .Now ().Add (- 10 * time .Minute )
230+
231+ fw := & Firewall {
232+ Status : FirewallStatus {
233+ Phase : FirewallPhaseRunning ,
234+ Conditions : Conditions {
235+ {
236+ Type : FirewallControllerConnected ,
237+ Status : ConditionTrue ,
238+ LastTransitionTime : metav1 .NewTime (tenMinutesAgo ),
239+ LastUpdateTime : metav1 .NewTime (tenMinutesAgo ),
240+ },
241+ {
242+ Type : FirewallControllerSeedConnected ,
243+ Status : ConditionTrue ,
244+ LastTransitionTime : metav1 .NewTime (tenMinutesAgo ),
245+ LastUpdateTime : metav1 .NewTime (tenMinutesAgo ),
246+ },
247+ {
248+ Type : FirewallCreated ,
249+ Status : ConditionTrue ,
250+ LastTransitionTime : metav1 .NewTime (tenMinutesAgo ),
251+ LastUpdateTime : metav1 .NewTime (tenMinutesAgo ),
252+ },
253+ {
254+ Type : FirewallReady ,
255+ Status : ConditionTrue ,
256+ LastTransitionTime : metav1 .NewTime (tenMinutesAgo ),
257+ LastUpdateTime : metav1 .NewTime (tenMinutesAgo ),
258+ },
259+ {
260+ Type : FirewallProvisioned ,
261+ Status : ConditionTrue ,
262+ LastTransitionTime : metav1 .NewTime (tenMinutesAgo ),
263+ LastUpdateTime : metav1 .NewTime (tenMinutesAgo ),
264+ },
265+ {
266+ Type : FirewallDistanceConfigured ,
267+ Status : ConditionTrue ,
268+ LastTransitionTime : metav1 .NewTime (tenMinutesAgo ),
269+ LastUpdateTime : metav1 .NewTime (tenMinutesAgo ),
270+ },
271+ {
272+ Type : FirewallMonitorDeployed ,
273+ Status : ConditionTrue ,
274+ LastTransitionTime : metav1 .NewTime (tenMinutesAgo ),
275+ LastUpdateTime : metav1 .NewTime (tenMinutesAgo ),
276+ },
277+ },
278+ },
279+ }
280+
281+ if tt .modFn != nil {
282+ tt .modFn (fw )
283+ }
284+
285+ got := EvaluateFirewallStatus (fw , tt .createTimeout , tt .healthTimeout )
286+ if diff := cmp .Diff (tt .want , got ); diff != "" {
287+ t .Errorf ("diff = %s" , diff )
288+ }
289+ })
290+ })
291+ }
292+ }
0 commit comments