This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathcassandra_status_test.go
435 lines (386 loc) · 13.7 KB
/
cassandra_status_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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright 2019 Orange
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cassandracluster
import (
"context"
"fmt"
"reflect"
v1 "k8s.io/api/core/v1"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"strconv"
"testing"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
api "github.com/Orange-OpenSource/casskop/pkg/apis/db/v1alpha1"
"github.com/ghodss/yaml"
"github.com/stretchr/testify/assert"
)
var name string = "cassandra-demo"
var namespace string = "ns"
var cc2Dcs = `
apiVersion: "db.orange.com/v1alpha1"
kind: "CassandraCluster"
metadata:
name: cassandra-demo
labels:
cluster: k8s.pic
namespace: ns
spec:
nodesPerRacks: 6
baseImage: cassandra
version: latest
rollingPartition: 0
dataCapacity: "3Gi"
dataStorageClass: "local-storage"
hardAntiAffinity: false
deletePVC: true
autoPilot: true
resources:
requests:
cpu: '1'
memory: 2Gi
limits:
cpu: '1'
memory: 2Gi
topology:
dc:
- name: dc1
labels:
location.dfy.orange.com/site : mts
rack:
- name: rack1
labels:
location.dfy.orange.com/street : street1
- name: rack2
labels:
location.dfy.orange.com/street : street2
- name: dc2
nodesPerRacks: 2
labels:
location.dfy.orange.com/site : mts
rack:
- name: rack1
labels:
location.dfy.orange.com/street : street3
`
func TestUpdateStatusIfSeedListHasChanged(t *testing.T) {
assert := assert.New(t)
var cc api.CassandraCluster
err := yaml.Unmarshal([]byte(cc2Dcs), &cc)
if err != nil {
fmt.Printf("error: %v", err)
}
cc.InitCassandraRackList()
assert.Equal(api.ClusterPhaseInitial.Name, cc.Status.CassandraRackStatus["dc1-rack1"].CassandraLastAction.Name)
assert.Equal(api.ClusterPhaseInitial.Name, cc.Status.CassandraRackStatus["dc1-rack2"].CassandraLastAction.Name)
assert.Equal(api.ClusterPhaseInitial.Name, cc.Status.CassandraRackStatus["dc2-rack1"].CassandraLastAction.Name)
assert.Equal(3, len(cc.Status.CassandraRackStatus))
cc.Status.SeedList = cc.InitSeedList()
var a = []string{"cassandra-demo-dc1-rack1-0.cassandra-demo.ns",
"cassandra-demo-dc1-rack1-1.cassandra-demo.ns",
"cassandra-demo-dc1-rack2-0.cassandra-demo.ns",
"cassandra-demo-dc2-rack1-0.cassandra-demo.ns",
"cassandra-demo-dc2-rack1-1.cassandra-demo.ns"}
assert.Equal(5, len(cc.Status.SeedList))
assert.Equal(true, reflect.DeepEqual(a, cc.Status.SeedList))
}
//helperCreateCassandraCluster fake create a cluster from the yaml specified
func helperCreateCassandraCluster(t *testing.T, cassandraClusterFileName string) (*ReconcileCassandraCluster,
*reconcile.Request) {
assert := assert.New(t)
rcc, cc := helperInitCluster(t, cassandraClusterFileName)
req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: cc.Name,
Namespace: cc.Namespace,
},
}
//The first Reconcile Just make Init
res, err := rcc.Reconcile(req)
if err != nil {
t.Fatalf("reconcile: (%v)", err)
}
err = rcc.client.Get(context.TODO(), req.NamespacedName, cc)
if err != nil {
t.Fatalf("can't get cassandracluster: (%v)", err)
}
if cc.Spec.DeletePVC {
//Check that we have set the Finalizers
f := cc.GetFinalizers()
assert.Equal(f[0], "kubernetes.io/pvc-to-delete", "set finalizer for PVC")
}
// Check the result of reconciliation to make sure it has the desired state.
if !res.Requeue {
t.Error("reconcile did not requeue request as expected")
}
//Second Reconcile create objects
res, err = rcc.Reconcile(req)
if err != nil {
t.Fatalf("reconcile: (%v)", err)
}
for _, dc := range cc.Spec.Topology.DC {
for _, rack := range dc.Rack {
dcRackName := cc.GetDCRackName(dc.Name, rack.Name)
//Update Statefulset fake status
sts := &appsv1.StatefulSet{}
err = rcc.client.Get(context.TODO(), types.NamespacedName{Name: cc.Name + "-" + dcRackName,
Namespace: cc.Namespace},
sts)
if err != nil {
t.Fatalf("get statefulset: (%v)", err)
}
// Check if the quantity of Replicas for this deployment is equals the specification
dsize := *sts.Spec.Replicas
if dsize != 1 {
t.Errorf("dep size (%d) is not the expected size (%d)", dsize, cc.Spec.NodesPerRacks)
}
//Now simulate sts to be ready for CassKop
sts.Status.Replicas = *sts.Spec.Replicas
sts.Status.ReadyReplicas = *sts.Spec.Replicas
rcc.UpdateStatefulSet(sts)
//Create Statefulsets associated fake Pods
pod := &v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "template",
Namespace: namespace,
Labels: map[string]string{
"cluster": cc.Labels["cluster"],
"dc-rack": dcRackName,
"cassandraclusters.db.orange.com.dc": dc.Name,
"cassandraclusters.db.orange.com.rack": rack.Name,
"app": "cassandracluster",
"cassandracluster": cc.Name,
},
},
Status: v1.PodStatus{
Phase: v1.PodRunning,
ContainerStatuses: []v1.ContainerStatus{
v1.ContainerStatus{
Name: "cassandra",
//Image: cc.Spec.BaseImage + ":" + cc.Spec.Version
Ready: true,
},
},
},
}
for i := 0; i < int(sts.Status.Replicas); i++ {
pod.Name = sts.Name + strconv.Itoa(i)
if err = rcc.CreatePod(pod); err != nil {
t.Fatalf("can't create pod: (%v)", err)
}
}
//We recall Reconcile to update Next rack
if res, err = rcc.Reconcile(req); err != nil {
t.Fatalf("reconcile: (%v)", err)
}
}
}
//Check creation Statuses
if err = rcc.client.Get(context.TODO(), req.NamespacedName, cc); err != nil {
t.Fatalf("can't get cassandracluster: (%v)", err)
}
assert.Equal(cc.Status.Phase, api.ClusterPhaseRunning.Name)
for _, dcRackName := range cc.GetDCRackNames() {
assert.Equal(cc.Status.CassandraRackStatus[dcRackName].Phase, api.ClusterPhaseRunning.Name,
"dc-rack: %s", dcRackName)
assert.Equal(cc.Status.CassandraRackStatus[dcRackName].CassandraLastAction.Name, api.ClusterPhaseInitial.Name,
"dc-rack: %s", dcRackName)
assert.Equal(cc.Status.CassandraRackStatus[dcRackName].CassandraLastAction.Status, api.StatusDone,
"dc-rack %s", dcRackName)
}
assert.Equal(cc.Status.LastClusterAction, api.ClusterPhaseInitial.Name)
assert.Equal(cc.Status.LastClusterActionStatus, api.StatusDone)
return rcc, &req
}
func TestReconcileCassandraCluster(t *testing.T) {
// Mock request to simulate Reconcile() being called on an event for a
// watched resource .
rcc, req := helperCreateCassandraCluster(t, "cassandracluster-2DC.yaml")
//WARNING: ListPod with fieldselector is not working on client-side
//So CassKop will try to execute podActions in pods without succeed (they are fake pod)
///~https://github.com/kubernetes/client-go/issues/326
res, err := rcc.Reconcile(*req)
if err != nil {
t.Fatalf("reconcile: (%v)", err)
}
if !res.Requeue && res.RequeueAfter == 0 {
t.Error("reconcile did not requeue request as expected")
}
}
// test that we detect an addition of a configmap
func TestUpdateStatusIfconfigMapHasChangedWithNoConfigMap(t *testing.T) {
// Mock request to simulate Reconcile() being called on an event for a
// watched resource .
rcc, req := helperCreateCassandraCluster(t, "cassandracluster-2DC.yaml")
//WARNING: ListPod with fieldselector is not working on client-side
//So CassKop will try to execute podActions in pods without succeed (they are fake pod)
///~https://github.com/kubernetes/client-go/issues/326
res, err := rcc.Reconcile(*req)
if err != nil {
t.Fatalf("reconcile: (%v)", err)
}
if !res.Requeue && res.RequeueAfter == 0 {
t.Error("reconcile did not requeue request as expected")
}
//Test on each statefulset
for _, dc := range rcc.cc.Spec.Topology.DC {
for _, rack := range dc.Rack {
dcRackName := rcc.cc.GetDCRackName(dc.Name, rack.Name)
//Update Statefulset fake status
sts := &appsv1.StatefulSet{}
err = rcc.client.Get(context.TODO(), types.NamespacedName{Name: rcc.cc.Name + "-" + dcRackName,
Namespace: rcc.cc.Namespace},
sts)
if err != nil {
t.Fatalf("get statefulset: (%v)", err)
}
assert.Equal(t, false, UpdateStatusIfconfigMapHasChanged(rcc.cc, dcRackName, sts, &rcc.cc.Status))
}
}
//Ask for a new ConfigMap
rcc.cc.Spec.ConfigMapName = "my-super-configmap"
//Test on each statefulset
for _, dc := range rcc.cc.Spec.Topology.DC {
for _, rack := range dc.Rack {
dcRackName := rcc.cc.GetDCRackName(dc.Name, rack.Name)
//Update Statefulset fake status
sts := &appsv1.StatefulSet{}
err = rcc.client.Get(context.TODO(), types.NamespacedName{Name: rcc.cc.Name + "-" + dcRackName,
Namespace: rcc.cc.Namespace},
sts)
if err != nil {
t.Fatalf("get statefulset: (%v)", err)
}
assert.Equal(t, true, UpdateStatusIfconfigMapHasChanged(rcc.cc, dcRackName, sts, &rcc.cc.Status))
}
}
}
// test that we detect a change in a configmap
func TestUpdateStatusIfconfigMapHasChangedWithConfigMap(t *testing.T) {
// Mock request to simulate Reconcile() being called on an event for a
// watched resource .
rcc, req := helperCreateCassandraCluster(t, "cassandracluster-2DC-configmap.yaml")
//WARNING: ListPod with fieldselector is not working on client-side
//So CassKop will try to execute podActions in pods without succeed (they are fake pod)
///~https://github.com/kubernetes/client-go/issues/326
res, err := rcc.Reconcile(*req)
if err != nil {
t.Fatalf("reconcile: (%v)", err)
}
if !res.Requeue && res.RequeueAfter == 0 {
t.Error("reconcile did not requeue request as expected")
}
//Test on each statefulset
for _, dc := range rcc.cc.Spec.Topology.DC {
for _, rack := range dc.Rack {
dcRackName := rcc.cc.GetDCRackName(dc.Name, rack.Name)
//Update Statefulset fake status
sts := &appsv1.StatefulSet{}
err = rcc.client.Get(context.TODO(), types.NamespacedName{Name: rcc.cc.Name + "-" + dcRackName,
Namespace: rcc.cc.Namespace},
sts)
if err != nil {
t.Fatalf("get statefulset: (%v)", err)
}
assert.Equal(t, false, UpdateStatusIfconfigMapHasChanged(rcc.cc, dcRackName, sts, &rcc.cc.Status))
}
}
//Ask for a new ConfigMap
rcc.cc.Spec.ConfigMapName = "my-super-configmap"
//Test on each statefulset
for _, dc := range rcc.cc.Spec.Topology.DC {
for _, rack := range dc.Rack {
dcRackName := rcc.cc.GetDCRackName(dc.Name, rack.Name)
//Update Statefulset fake status
sts := &appsv1.StatefulSet{}
err = rcc.client.Get(context.TODO(), types.NamespacedName{Name: rcc.cc.Name + "-" + dcRackName,
Namespace: rcc.cc.Namespace},
sts)
if err != nil {
t.Fatalf("get statefulset: (%v)", err)
}
assert.Equal(t, true, UpdateStatusIfconfigMapHasChanged(rcc.cc, dcRackName, sts, &rcc.cc.Status))
}
}
//Whant to remove the configmap
rcc.cc.Spec.ConfigMapName = ""
//Test on each statefulset
for _, dc := range rcc.cc.Spec.Topology.DC {
for _, rack := range dc.Rack {
dcRackName := rcc.cc.GetDCRackName(dc.Name, rack.Name)
//Update Statefulset fake status
sts := &appsv1.StatefulSet{}
err = rcc.client.Get(context.TODO(), types.NamespacedName{Name: rcc.cc.Name + "-" + dcRackName,
Namespace: rcc.cc.Namespace},
sts)
if err != nil {
t.Fatalf("get statefulset: (%v)", err)
}
assert.Equal(t, true, UpdateStatusIfconfigMapHasChanged(rcc.cc, dcRackName, sts, &rcc.cc.Status))
}
}
}
// test that we detect a change in a the docker image
func TestUpdateStatusIfDockerImageHasChanged(t *testing.T) {
// Mock request to simulate Reconcile() being called on an event for a
// watched resource .
rcc, req := helperCreateCassandraCluster(t, "cassandracluster-2DC-configmap.yaml")
//WARNING: ListPod with fieldselector is not working on client-side
//So CassKop will try to execute podActions in pods without succeed (they are fake pod)
///~https://github.com/kubernetes/client-go/issues/326
res, err := rcc.Reconcile(*req)
if err != nil {
t.Fatalf("reconcile: (%v)", err)
}
if !res.Requeue && res.RequeueAfter == 0 {
t.Error("reconcile did not requeue request as expected")
}
//Test on each statefulset
for _, dc := range rcc.cc.Spec.Topology.DC {
for _, rack := range dc.Rack {
dcRackName := rcc.cc.GetDCRackName(dc.Name, rack.Name)
//Update Statefulset fake status
sts := &appsv1.StatefulSet{}
err = rcc.client.Get(context.TODO(), types.NamespacedName{Name: rcc.cc.Name + "-" + dcRackName,
Namespace: rcc.cc.Namespace},
sts)
if err != nil {
t.Fatalf("get statefulset: (%v)", err)
}
assert.Equal(t, false, UpdateStatusIfDockerImageHasChanged(rcc.cc, dcRackName, sts, &rcc.cc.Status))
}
}
//Ask for a change in CassandraImage version
rcc.cc.Spec.CassandraImage = "cassandra:new-versionftt"
//Test on each statefulset
for _, dc := range rcc.cc.Spec.Topology.DC {
for _, rack := range dc.Rack {
dcRackName := rcc.cc.GetDCRackName(dc.Name, rack.Name)
//Update Statefulset fake status
sts := &appsv1.StatefulSet{}
err = rcc.client.Get(context.TODO(), types.NamespacedName{Name: rcc.cc.Name + "-" + dcRackName,
Namespace: rcc.cc.Namespace},
sts)
if err != nil {
t.Fatalf("get statefulset: (%v)", err)
}
assert.Equal(t, true, UpdateStatusIfDockerImageHasChanged(rcc.cc, dcRackName, sts, &rcc.cc.Status))
}
}
}