-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathstatus_test.go
More file actions
354 lines (292 loc) · 12.5 KB
/
status_test.go
File metadata and controls
354 lines (292 loc) · 12.5 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package utils
import (
"context"
"testing"
appstacksv1 "github.com/application-stacks/runtime-component-operator/api/v1"
"github.com/application-stacks/runtime-component-operator/common"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
"knative.dev/pkg/apis"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
)
var (
// Status test variables
st_replicas int32 = 3
)
func TestCheckApplicationStatus(t *testing.T) {
// Setup fake client and reconciler base
spec := appstacksv1.RuntimeComponentSpec{Replicas: &st_replicas}
r, runtimecomponent := setupFakeClientWithRC(spec)
// Overall application status should report ApplicationNotReconciled with no successful reconciliation
_, newCondition := r.CheckApplicationStatus(runtimecomponent)
notReconciled := newCondition.GetReason()
// ResourcesReady condition should report MinimumReplicasUnavailable with less number of ready replicas
deploy := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Status: appsv1.DeploymentStatus{
Replicas: 3,
ReadyReplicas: 3,
UpdatedReplicas: 3,
},
}
r.CreateOrUpdate(deploy, runtimecomponent, func() error {
CustomizeDeployment(deploy, runtimecomponent)
return nil
})
// Run successful reconciliation and check ready application status
r.ManageSuccess(common.StatusConditionTypeReconciled, runtimecomponent)
_, newCondition = r.CheckApplicationStatus(runtimecomponent)
reconciledMessage := newCondition.GetMessage()
testAS := []Test{
{test: "Not reconciled", expected: "ApplicationNotReconciled", actual: notReconciled},
{test: "Reconciled and ready", expected: "Application is reconciled and resources are ready.", actual: reconciledMessage},
}
verifyTests(testAS, t)
}
// Test areReplicasReady for Deployment resource status check
func TestDeploymentReplicasReady(t *testing.T) {
// Setup fake client and reconciler base
// Set RuntimeComponent to use Deployment
spec = appstacksv1.RuntimeComponentSpec{Replicas: &st_replicas}
r, runtimecomponent := setupFakeClientWithRC(spec)
// Report successful reconciliation to check for ResourcesReady condition
r.ManageSuccess(common.StatusConditionTypeReconciled, runtimecomponent)
conditionType := common.StatusConditionTypeResourcesReady
newCondition := runtimecomponent.GetStatus().NewCondition(conditionType)
// ResourcesReady condition should report NotCreated with no Deployment created
resourceCondition := r.areReplicasReady(runtimecomponent, newCondition)
dpNotCreated := resourceCondition.GetReason()
// Create Deployment with less ready replicas than expected
// ResourcesReady condition should report MinimumReplicasUnavailable
deploy := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Status: appsv1.DeploymentStatus{
Replicas: 3,
ReadyReplicas: 1,
UpdatedReplicas: 1,
},
}
r.CreateOrUpdate(deploy, runtimecomponent, func() error {
CustomizeDeployment(deploy, runtimecomponent)
return nil
})
resourceCondition = r.areReplicasReady(runtimecomponent, newCondition)
dpReplicasUnavailable := resourceCondition.GetReason()
// Update Deployment with all more replicas than expected
// ResourcesReady condition should report ReplicaSetUpdating
deploy.Status = appsv1.DeploymentStatus{
Replicas: 4,
ReadyReplicas: 3,
UpdatedReplicas: 3,
}
r.GetClient().Status().Update(context.Background(), deploy)
resourceCondition = r.areReplicasReady(runtimecomponent, newCondition)
dpReplicaSetUpdating := resourceCondition.GetReason()
// Update Deployment with all ready replicas
// ResourcesReady condition should report MinimumReplicasAvailable
deploy.Status = appsv1.DeploymentStatus{
Replicas: 3,
ReadyReplicas: 3,
UpdatedReplicas: 3,
}
r.GetClient().Status().Update(context.Background(), deploy)
resourceCondition = r.areReplicasReady(runtimecomponent, newCondition)
dpReady := resourceCondition.GetReason()
// Test Deployment resource status
testDR := []Test{
{test: "Deployment not created", expected: "NotCreated", actual: dpNotCreated},
{test: "Deployment replicas unavailable", expected: "MinimumReplicasUnavailable", actual: dpReplicasUnavailable},
{test: "Deployment ReplicaSet updating", expected: "ReplicaSetUpdating", actual: dpReplicaSetUpdating},
{test: "Deployment ready", expected: "MinimumReplicasAvailable", actual: dpReady},
}
verifyTests(testDR, t)
}
// Test areReplicasReady for StatefulSet resource status check
func TestStatefulSetReplicasReady(t *testing.T) {
// Setup fake client and reconciler base
// Set RuntimeComponent to use StatefulSet
spec = appstacksv1.RuntimeComponentSpec{Replicas: &st_replicas, StatefulSet: &appstacksv1.RuntimeComponentStatefulSet{}}
r, runtimecomponent := setupFakeClientWithRC(spec)
// Report successful reconciliation to check for ResourcesReady condition
r.ManageSuccess(common.StatusConditionTypeReconciled, runtimecomponent)
conditionType := common.StatusConditionTypeResourcesReady
newCondition := runtimecomponent.GetStatus().NewCondition(conditionType)
// ResourcesReady condition should report NotCreated with no StatefulSet created
resourceCondition := r.areReplicasReady(runtimecomponent, newCondition)
ssNotCreated := resourceCondition.GetReason()
// Create StatefulSet with less ready replicas than expected
// ResourcesReady condition should report MinimumReplicasUnavailable
statefulset := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Status: appsv1.StatefulSetStatus{
Replicas: 3,
ReadyReplicas: 1,
UpdatedReplicas: 1,
},
}
r.CreateOrUpdate(statefulset, runtimecomponent, func() error {
CustomizeStatefulSet(statefulset, runtimecomponent)
return nil
})
// Update StatefulSet with all ready replicas
// ResourcesReady condition should report MinimumReplicasAvailable with all ready replicas
resourceCondition = r.areReplicasReady(runtimecomponent, newCondition)
ssReplicasUnavailable := resourceCondition.GetReason()
statefulset.Status = appsv1.StatefulSetStatus{
Replicas: 3,
ReadyReplicas: 3,
UpdatedReplicas: 3,
}
r.GetClient().Status().Update(context.Background(), statefulset)
resourceCondition = r.areReplicasReady(runtimecomponent, newCondition)
ssReady := resourceCondition.GetReason()
// Test StatefulSet resource status
testSR := []Test{
{test: "StatefulSet not created", expected: "NotCreated", actual: ssNotCreated},
{test: "StatefulSet replicas unavailable", expected: "MinimumReplicasUnavailable", actual: ssReplicasUnavailable},
{test: "StatefulSet ready", expected: "MinimumReplicasAvailable", actual: ssReady},
}
verifyTests(testSR, t)
}
// Test isKnativeReady for Knative resource status check
func TestKnativeReady(t *testing.T) {
// Setup fake client and reconciler base
// Set RuntimeComponent to use KnativeService
createKnativeService := true
spec = appstacksv1.RuntimeComponentSpec{Replicas: &st_replicas, CreateKnativeService: &createKnativeService}
r, runtimecomponent := setupFakeClientWithRC(spec)
// Report successful reconciliation to check for ResourcesReady condition
r.ManageSuccess(common.StatusConditionTypeReconciled, runtimecomponent)
conditionType := common.StatusConditionTypeResourcesReady
newCondition := runtimecomponent.GetStatus().NewCondition(conditionType)
// ResourcesReady condition should report ServiceNotCreated with no KnativeService created
resourceCondition := r.isKnativeReady(runtimecomponent, newCondition)
ksNotCreated := resourceCondition.GetReason()
// Create KnativeService and add the type to the fake client
// ResourcesReady condition should report ServiceStatusNotFound with empty KnativeService status
knative1 := &servingv1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
r.scheme.AddKnownTypes(schema.GroupVersion{Group: "serving.knative.dev", Version: "v1"}, knative1)
r.CreateOrUpdate(knative1, runtimecomponent, func() error {
return nil
})
resourceCondition = r.isKnativeReady(runtimecomponent, newCondition)
ksStatusNotCreated := resourceCondition.GetReason()
// Unable to update status for KnativeService through fake client because it is not a known type
// Deleting and re-creating KnativeService to update its status conditions
// Update KnativeService with Not Ready condition
// ResourcesReady condition should report ServiceNotReady
r.DeleteResource(knative1)
knative2 := &servingv1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
condition := apis.Condition{
Type: apis.ConditionReady,
Status: v1.ConditionFalse,
}
conditions := apis.Conditions{condition}
knative2.Status.SetConditions(conditions)
r.CreateOrUpdate(knative2, runtimecomponent, func() error {
return nil
})
resourceCondition = r.isKnativeReady(runtimecomponent, newCondition)
ksServiceNotReady := resourceCondition.GetReason()
// Update KnativeService with Ready condition
// There is no reason field reported - using message instead for this case
// ResourcesReady condition should report "Knative service is ready." message
r.DeleteResource(knative2)
knative3 := &servingv1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
condition = apis.Condition{
Type: apis.ConditionReady,
Status: v1.ConditionTrue,
}
conditions = apis.Conditions{condition}
knative3.Status.SetConditions(conditions)
r.CreateOrUpdate(knative3, runtimecomponent, func() error {
return nil
})
resourceCondition = r.isKnativeReady(runtimecomponent, newCondition)
ksServiceReadyMsg := resourceCondition.GetMessage()
// Test KnatvieService resource status
testKS := []Test{
{test: "Knative not created", expected: "ServiceNotCreated", actual: ksNotCreated},
{test: "Knative Status not created", expected: "ServiceStatusNotFound", actual: ksStatusNotCreated},
{test: "Knative Status not ready", expected: "ServiceNotReady", actual: ksServiceNotReady},
{test: "Knative Status ready", expected: "Knative service is ready.", actual: ksServiceReadyMsg},
}
verifyTests(testKS, t)
}
func TestReadyConditionSorting(t *testing.T) {
// Setup fake client and reconciler base
spec := appstacksv1.RuntimeComponentSpec{Replicas: &st_replicas}
_, runtimecomponent := setupFakeClientWithRC(spec)
s := runtimecomponent.GetStatus()
// Add conditions in worst-case order, Ready adeed last
reconciled := s.NewCondition(common.StatusConditionTypeReconciled)
reconciled.SetStatus(v1.ConditionTrue)
s.SetCondition(reconciled)
resourcesReady := s.NewCondition(common.StatusConditionTypeResourcesReady)
resourcesReady.SetStatus(v1.ConditionTrue)
s.SetCondition(resourcesReady)
ready := s.NewCondition(common.StatusConditionTypeReady)
ready.SetStatus(v1.ConditionTrue)
s.SetCondition(ready)
conditions := s.GetConditions()
readyTrueFirst := conditions[0].GetType()
// Set Ready to False and call SetCondition again
ready.SetStatus(v1.ConditionFalse)
s.SetCondition(ready)
conditions = s.GetConditions()
readyFalseFirst := conditions[0].GetType()
// Adding a fourth condition- warning
warning := s.NewCondition(common.StatusConditionTypeWarning)
warning.SetStatus(v1.ConditionTrue)
s.SetCondition(warning)
conditions = s.GetConditions()
readyWithWarningFirst := conditions[0].GetType()
testCases := []Test{
{test: "Ready is first condition", expected: common.StatusConditionTypeReady, actual: readyTrueFirst},
{test: "Ready is first condition with False status", expected: common.StatusConditionTypeReady, actual: readyFalseFirst},
{test: "Ready is first condition with additional warning condition", expected: common.StatusConditionTypeReady, actual: readyWithWarningFirst},
}
verifyTests(testCases, t)
}
// Setup fake client with RuntimeComponent kind
// Creates a RuntimeComponent object with input spec
func setupFakeClientWithRC(spec appstacksv1.RuntimeComponentSpec) (ReconcilerBase, *appstacksv1.RuntimeComponent) {
runtimecomponent := createRuntimeComponent(name, namespace, spec)
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
cl := fakeclient.NewFakeClient(objs...)
rcl := fakeclient.NewFakeClient(objs...)
r := NewReconcilerBase(rcl, cl, s, &rest.Config{}, record.NewFakeRecorder(10))
return r, runtimecomponent
}