This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
drupalsite_controller_test.go
2017 lines (1800 loc) · 82.5 KB
/
drupalsite_controller_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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2021 CERN.
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 controllers
import (
"context"
"crypto/md5"
"encoding/hex"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
buildv1 "github.com/openshift/api/build/v1"
imagev1 "github.com/openshift/api/image/v1"
routev1 "github.com/openshift/api/route/v1"
"github.com/operator-framework/operator-lib/status"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
dbodv1a1 "gitlab.cern.ch/drupal/paas/dbod-operator/api/v1alpha1"
drupalwebservicesv1alpha1 "gitlab.cern.ch/drupal/paas/drupalsite-operator/api/v1alpha1"
authz "gitlab.cern.ch/paas-tools/operators/authz-operator/api/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
// Ginkgo makes it easy to write expressive specs that describe the behavior of your code in an organized manner.
// Describe and Context containers are used organize the tests and It is used to define the test result
// Describe is a top-level container that defines the test
// Context defines the circumstance i.e parameters or specifications
// It defines what the test result should be
// By defines smaller tests under a given it and in case of failure, the 'By' text is printed out
// Ref: http://onsi.github.io/ginkgo/
const dummySiteUrl = "testsite.webtest.cern.ch"
var _ = Describe("DrupalSite controller", func() {
const (
Name = "test"
Namespace = "default"
veleroNamespace = "openshift-cern-drupal"
timeout = time.Second * 30
duration = time.Second * 30
interval = time.Millisecond * 250
)
var (
drupalSiteObject = &drupalwebservicesv1alpha1.DrupalSite{}
)
ctx := context.Background()
key := types.NamespacedName{
Name: Name,
Namespace: Namespace,
}
BeforeEach(func() {
drupalSiteObject = &drupalwebservicesv1alpha1.DrupalSite{
TypeMeta: metav1.TypeMeta{
APIVersion: "drupal.webservices.cern.ch/v1alpha1",
Kind: "DrupalSite",
},
ObjectMeta: metav1.ObjectMeta{
Name: key.Name,
Namespace: key.Namespace,
},
Spec: drupalwebservicesv1alpha1.DrupalSiteSpec{
Version: drupalwebservicesv1alpha1.Version{
Name: "v8.9-1",
ReleaseSpec: "stable",
},
Configuration: drupalwebservicesv1alpha1.Configuration{
DiskSize: "10Gi",
QoSClass: drupalwebservicesv1alpha1.QoSStandard,
DatabaseClass: drupalwebservicesv1alpha1.DBODStandard,
},
SiteURL: []drupalwebservicesv1alpha1.Url{
"test-1.webtest.cern.ch",
"test-2.webtest.cern.ch",
"test-3.webtest.cern.ch",
},
},
}
})
Describe("Creating drupalSite object", func() {
Context("With basic spec", func() {
It("All dependent resources should be created", func() {
By("By creating a namespace for velero backup resources")
Eventually(func() error {
return k8sClient.Create(ctx, &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: veleroNamespace,
},
})
}, timeout, interval).Should(Succeed())
By("By creating a new drupalSite")
Eventually(func() error {
return k8sClient.Create(ctx, drupalSiteObject)
}, timeout, interval).Should(Succeed())
By("Expecting drupalSite object created")
cr := drupalwebservicesv1alpha1.DrupalSite{}
Eventually(func() error {
return k8sClient.Get(ctx, key, &cr)
}, timeout, interval).Should(Succeed())
trueVar := true
expectedOwnerReference := metav1.OwnerReference{
APIVersion: "drupal.webservices.cern.ch/v1alpha1",
Kind: "DrupalSite",
Name: Name,
UID: cr.UID,
Controller: &trueVar,
}
configmap := corev1.ConfigMap{}
svc := corev1.Service{}
pvc := corev1.PersistentVolumeClaim{}
job := batchv1.Job{}
deploy := appsv1.Deployment{}
dbod := dbodv1a1.Database{}
route := routev1.Route{}
oidcReturnUri := authz.OidcReturnURI{}
schedule := velerov1.Schedule{}
// Check DBOD resource creation
By("Expecting Database resource created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &dbod)
return dbod.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Update DBOD resource status field
By("Updating DBOD instance in Database resource status")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &dbod)
dbod.Status.DbodInstance = "test"
return k8sClient.Status().Update(ctx, &dbod)
}, timeout, interval).Should(Succeed())
//By("Expecting the drupal deployment to have the EnvFrom secret field set correctly")
By("Expecting the drupal deployment to have at least 2 containers")
Eventually(func() bool {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &deploy)
return len(deploy.Spec.Template.Spec.Containers) >= 2
}, timeout, interval).Should(BeTrue())
// Check PHP-FPM configMap creation
By("Expecting PHP_FPM configmaps created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "php-fpm-" + key.Name, Namespace: key.Namespace}, &configmap)
return configmap.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Nginx configMap creation
By("Expecting Nginx configmaps created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "nginx-" + key.Name, Namespace: key.Namespace}, &configmap)
return configmap.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Site settings configMap creation
By("Expecting Site settings configmaps created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "site-settings-" + key.Name, Namespace: key.Namespace}, &configmap)
return configmap.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check PHP Cli configMap creation
By("Expecting PHP Cli configmaps created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "php-cli-config-" + key.Name, Namespace: key.Namespace}, &configmap)
return configmap.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Drupal service
By("Expecting Drupal service created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &svc)
return svc.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check drupal persistentVolumeClaim
By("Expecting drupal persistentVolumeClaim created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "pv-claim-" + key.Name, Namespace: key.Namespace}, &pvc)
return pvc.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Drupal deployments
By("Expecting Drupal deployments created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &deploy)
return deploy.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Drush job
By("Expecting Drush job created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "ensure-site-install-" + key.Name, Namespace: key.Namespace}, &job)
return job.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Update drupalSite custom resource status fields to allow route conditions
By("Updating 'initialized' status field in drupalSite resource")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &cr)
cr.Status.Conditions.SetCondition(status.Condition{Type: "Initialized", Status: "True"})
return k8sClient.Status().Update(ctx, &cr)
}, timeout, interval).Should(Succeed())
// Update deployment status fields to allow 'ready' status field to be set on the drupalSite resource
By("Updating 'ReadyReplicas' and 'AvailableReplicas' status fields in deployment resource")
Eventually(func() error {
k8sClient.Get(ctx, key, &deploy)
deploy.Status.Replicas = 1
deploy.Status.AvailableReplicas = 1
deploy.Status.ReadyReplicas = 1
return k8sClient.Status().Update(ctx, &deploy)
}, timeout, interval).Should(Succeed())
// Check if the Schedule resource is created
By("Expecting Schedule to be created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Namespace + "-" + key.Name, Namespace: veleroNamespace}, &schedule)
return job.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Routes
By("Expecting Drupal Route(s) to be created")
for _, url := range cr.Spec.SiteURL {
route = routev1.Route{}
hash := md5.Sum([]byte(url))
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "-" + hex.EncodeToString(hash[0:4]), Namespace: key.Namespace}, &route)
return route.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
}
// Check OidcReturnUris
By("Expecting OidcReturnURIs created")
for _, url := range cr.Spec.SiteURL {
oidcReturnUri = authz.OidcReturnURI{}
hash := md5.Sum([]byte(url))
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "-" + hex.EncodeToString(hash[0:4]), Namespace: key.Namespace}, &oidcReturnUri)
return oidcReturnUri.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
}
})
})
})
Describe("Blocking and unblocking the drupalsite object", func() {
Context("With basic spec", func() {
It("Should be blocked and unblocked successfully", func() {
key = types.NamespacedName{
Name: Name,
Namespace: Namespace,
}
cr := drupalwebservicesv1alpha1.DrupalSite{}
namespace := corev1.Namespace{}
By("Expecting drupalSite object created")
Eventually(func() error {
return k8sClient.Get(ctx, key, &cr)
}, timeout, interval).Should(Succeed())
By("Adding label to namespace")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Namespace}, &namespace)
namespace.Labels = map[string]string{"drupal.cern.ch/user-project": "true"}
return k8sClient.Update(ctx, &namespace)
}, timeout, interval).Should(Succeed())
By("Adding annotations to namespace")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Namespace}, &namespace)
namespace.Annotations = map[string]string{"blocked.webservices.cern.ch/blocked-timestamp": "2021-08-11T10:20:00+00:00", "blocked.webservices.cern.ch/reason": "Blocked due to security reason"}
return k8sClient.Update(ctx, &namespace)
}, timeout, interval).Should(Succeed())
deploy := appsv1.Deployment{}
By("Expecting to set deployment replicas to 0")
Eventually(func() bool {
k8sClient.Get(ctx, key, &deploy)
return *deploy.Spec.Replicas == 0
}, timeout, interval).Should(BeTrue())
By("Removing annotations to namespace")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Namespace}, &namespace)
delete(namespace.Annotations, "blocked.webservices.cern.ch/blocked-timestamp")
delete(namespace.Annotations, "blocked.webservices.cern.ch/reason")
return k8sClient.Update(ctx, &namespace)
}, timeout, interval).Should(Succeed())
By("Expecting to set deployment replicas to 1")
Eventually(func() bool {
k8sClient.Get(ctx, key, &deploy)
return *deploy.Spec.Replicas == 1
}, timeout, interval).Should(BeTrue())
})
})
})
Describe("Creating a new backup resource", func() {
Context("for the basic drupalSite", func() {
It("New velero backups created for the site should reflect in the drupalSite Status", func() {
By("Expecting drupalSite object created")
cr := drupalwebservicesv1alpha1.DrupalSite{}
Eventually(func() error {
return k8sClient.Get(ctx, key, &cr)
}, timeout, interval).Should(Succeed())
// Create a backup resource for the drupalSite
hash := md5.Sum([]byte(key.Namespace))
backup := velerov1.Backup{
TypeMeta: metav1.TypeMeta{
APIVersion: "velero.io/v1",
Kind: "Backup",
},
ObjectMeta: metav1.ObjectMeta{
Name: key.Name + "backup",
Namespace: veleroNamespace,
Labels: map[string]string{
"drupal.webservices.cern.ch/projectHash": hex.EncodeToString(hash[:]),
"drupal.webservices.cern.ch/project": key.Namespace,
"drupal.webservices.cern.ch/drupalSite": key.Name,
},
Annotations: map[string]string{"drupal.webservices.cern.ch/drupalSite": key.Namespace + "/" + key.Name},
},
Status: velerov1.BackupStatus{
Phase: velerov1.BackupPhaseCompleted,
},
}
By("By creating a backup resource for the drupalSite")
Eventually(func() error {
return k8sClient.Create(ctx, &backup)
}, timeout, interval).Should(Succeed())
// Check if the backup resource is created
By("By creating a backup resource for the drupalSite")
backup1 := velerov1.Backup{}
Eventually(func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "backup", Namespace: veleroNamespace}, &backup1)
}, timeout, interval).Should(Succeed())
// Check for the Backup name in the Drupalsite Status
By("By checking for the Backup in the DrupalSite Status")
Eventually(func() bool {
k8sClient.Get(ctx, key, &cr)
return len(cr.Status.AvailableBackups) > 0 && cr.Status.AvailableBackups[0].BackupName == backup.Name
}, timeout, interval).Should(BeTrue())
})
})
})
Describe("Update the basic drupalsite object", func() {
Context("With a different drupal Version", func() {
It("Should be updated successfully", func() {
key = types.NamespacedName{
Name: Name,
Namespace: Namespace,
}
newVersion := "v8.9-1"
newReleaseSpec := "new"
// Create drupalSite object
cr := drupalwebservicesv1alpha1.DrupalSite{}
By("Expecting drupalSite object created")
Eventually(func() error {
return k8sClient.Get(ctx, key, &cr)
}, timeout, interval).Should(Succeed())
deploy := appsv1.Deployment{}
By("Updating the version")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &cr)
cr.Spec.Version.Name = newVersion
cr.Spec.Version.ReleaseSpec = newReleaseSpec
return k8sClient.Update(ctx, &cr)
}, timeout, interval).Should(Succeed())
// Check if the drupalSiteObject has 'updateInProgress' annotation set
By("Expecting 'updateInProgress' annotation set on the drupalSiteObject")
Eventually(func() bool {
k8sClient.Get(ctx, key, &cr)
return cr.Annotations["updateInProgress"] == "true"
}, timeout, interval).Should(BeTrue())
// Check the annotation on the deployment
By("Expecting the new drupal Version on the pod annotation")
Eventually(func() bool {
k8sClient.Get(ctx, key, &deploy)
return deploy.Spec.Template.ObjectMeta.Annotations["releaseID"] == newVersion+"-"+newReleaseSpec
}, timeout, interval).Should(BeTrue())
pod := corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: key.Name,
Namespace: key.Namespace,
Labels: map[string]string{"drupalSite": cr.Name, "app": "drupal"},
Annotations: map[string]string{"releaseID": cr.Spec.Version.Name + "-" + cr.Spec.Version.ReleaseSpec},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Image: "test-image",
Name: "test-image",
}},
},
}
// Create a pod to simulate a error in the update process
By("Expecting a new pod with required labels to be created")
Eventually(func() error {
return k8sClient.Create(ctx, &pod)
}, timeout, interval).Should(Succeed())
// Set the pod status phase to 'Failed' to simulate a error in the update process
By("Expecting a new pod status to be updated")
Eventually(func() error {
k8sClient.Get(ctx, key, &pod)
pod.Status.Phase = corev1.PodFailed
return k8sClient.Status().Update(ctx, &pod)
}, timeout, interval).Should(Succeed())
// NOTE: Commenting this out temporarily. Refer to https://gitlab.cern.ch/drupal/paas/drupalsite-operator/-/issues/74
// Check if the drupalSiteObject has 'codeUpdateFailed' status set
// By("Expecting 'codeUpdateFailed' status set on the drupalSiteObject")
// Eventually(func() bool {
// k8sClient.Get(ctx, key, &cr)
// return cr.ConditionTrue("CodeUpdateFailed")
// }, timeout, interval).Should(BeTrue())
// Update drupalSite Failsafe status to simulate a successful upgrade
By("Updating 'Failsafe' status field in drupalSite resource")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &cr)
cr.Status.Failsafe = newVersion + "-" + newReleaseSpec
return k8sClient.Status().Update(ctx, &cr)
}, timeout, interval).Should(Succeed())
// Further tests need to be implemented, if we can bypass ExecErr with envtest
})
})
})
Describe("Deleting dependent objects", func() {
Context("Of the basic drupalSite", func() {
It("All dependent resources should be recreated successfully", func() {
By("Expecting drupalSite object created")
cr := drupalwebservicesv1alpha1.DrupalSite{}
Eventually(func() error {
return k8sClient.Get(ctx, key, &cr)
}, timeout, interval).Should(Succeed())
trueVar := true
expectedOwnerReference := metav1.OwnerReference{
APIVersion: "drupal.webservices.cern.ch/v1alpha1",
Kind: "DrupalSite",
Name: key.Name,
UID: cr.UID,
Controller: &trueVar,
}
configmap := corev1.ConfigMap{}
svc := corev1.Service{}
pvc := corev1.PersistentVolumeClaim{}
job := batchv1.Job{}
deploy := appsv1.Deployment{}
route := routev1.Route{}
oidcReturnUri := authz.OidcReturnURI{}
// Check PHP-FPM configMap recreation
By("Expecting PHP_FPM configmaps recreated")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: "php-fpm-" + key.Name, Namespace: key.Namespace}, &configmap)
return k8sClient.Delete(ctx, &configmap)
}, timeout, interval).Should(Succeed())
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "php-fpm-" + key.Name, Namespace: key.Namespace}, &configmap)
return configmap.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Nginx configMap creation
By("Expecting Nginx configmaps recreated")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: "nginx-" + key.Name, Namespace: key.Namespace}, &configmap)
return k8sClient.Delete(ctx, &configmap)
}, timeout, interval).Should(Succeed())
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "nginx-" + key.Name, Namespace: key.Namespace}, &configmap)
return configmap.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Site settings configMap creation
By("Expecting Site settings configmaps recreated")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: "site-settings-" + key.Name, Namespace: key.Namespace}, &configmap)
return k8sClient.Delete(ctx, &configmap)
}, timeout, interval).Should(Succeed())
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "site-settings-" + key.Name, Namespace: key.Namespace}, &configmap)
return configmap.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check PHP Cli configMap creation
By("Expecting PHP Cli configmaps recreated")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: "php-cli-config-" + key.Name, Namespace: key.Namespace}, &configmap)
return k8sClient.Delete(ctx, &configmap)
}, timeout, interval).Should(Succeed())
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "php-cli-config-" + key.Name, Namespace: key.Namespace}, &configmap)
return configmap.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Drupal service
By("Expecting Drupal service recreated")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &svc)
return k8sClient.Delete(ctx, &svc)
}, timeout, interval).Should(Succeed())
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &svc)
return svc.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check drupal persistentVolumeClaim
By("Expecting drupal persistentVolumeClaim recreated")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: "pv-claim-" + key.Name, Namespace: key.Namespace}, &pvc)
return k8sClient.Delete(ctx, &pvc)
}, timeout, interval).Should(Succeed())
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "pv-claim-" + key.Name, Namespace: key.Namespace}, &pvc)
return pvc.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Drupal deployments
By("Expecting Drupal deployments recreated")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &deploy)
return k8sClient.Delete(ctx, &deploy)
}, timeout, interval).Should(Succeed())
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &deploy)
return deploy.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Drush job
By("Expecting Drush job recreated")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: "ensure-site-install-" + key.Name, Namespace: key.Namespace}, &job)
return k8sClient.Delete(ctx, &job)
}, timeout, interval).Should(Succeed())
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "ensure-site-install-" + key.Name, Namespace: key.Namespace}, &job)
return job.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Routes
By("Expecting Drupal Route(s) recreated")
for _, url := range cr.Spec.SiteURL {
route = routev1.Route{}
hash := md5.Sum([]byte(url))
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "-" + hex.EncodeToString(hash[0:4]), Namespace: key.Namespace}, &route)
return k8sClient.Delete(ctx, &route)
}, timeout, interval).Should(Succeed())
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "-" + hex.EncodeToString(hash[0:4]), Namespace: key.Namespace}, &route)
return route.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
}
// Check OidcReturnUris
By("Expecting OidcReturnUri(s) recreated")
for _, url := range cr.Spec.SiteURL {
oidcReturnUri = authz.OidcReturnURI{}
hash := md5.Sum([]byte(url))
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "-" + hex.EncodeToString(hash[0:4]), Namespace: key.Namespace}, &oidcReturnUri)
return k8sClient.Delete(ctx, &oidcReturnUri)
}, timeout, interval).Should(Succeed())
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "-" + hex.EncodeToString(hash[0:4]), Namespace: key.Namespace}, &oidcReturnUri)
return oidcReturnUri.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
}
})
})
})
Describe("Updating siteUrl Spec", func() {
Context("Of the basic drupalSite", func() {
It("Extra routes, oidcReturnUris should be removed and siteUrl list should be ensured", func() {
By("Expecting drupalSite object created")
cr := drupalwebservicesv1alpha1.DrupalSite{}
Eventually(func() error {
return k8sClient.Get(ctx, key, &cr)
}, timeout, interval).Should(Succeed())
trueVar := true
expectedOwnerReference := metav1.OwnerReference{
APIVersion: "drupal.webservices.cern.ch/v1alpha1",
Kind: "DrupalSite",
Name: key.Name,
UID: cr.UID,
Controller: &trueVar,
}
route := routev1.Route{}
oidcReturnUri := authz.OidcReturnURI{}
By("Updating the siteUrl spec")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &cr)
cr.Spec.SiteURL = cr.Spec.SiteURL[:len(cr.Spec.SiteURL)-1]
cr.Spec.SiteURL = append(cr.Spec.SiteURL, "test-4.webtest.cern.ch")
return k8sClient.Update(ctx, &cr)
}, timeout, interval).Should(Succeed())
// Check Routes
By("Expecting Drupal Route(s) created")
for _, url := range cr.Spec.SiteURL {
route = routev1.Route{}
hash := md5.Sum([]byte(url))
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "-" + hex.EncodeToString(hash[0:4]), Namespace: key.Namespace}, &route)
return route.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
}
// Check OidcReturnUris
By("Expecting OidcReturnURIs created")
for _, url := range cr.Spec.SiteURL {
oidcReturnUri = authz.OidcReturnURI{}
hash := md5.Sum([]byte(url))
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "-" + hex.EncodeToString(hash[0:4]), Namespace: key.Namespace}, &oidcReturnUri)
return oidcReturnUri.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
}
// Check deleted entries
By("Expecting Drupal Route deleted")
hash := md5.Sum([]byte("test-3.webtest.cern.ch"))
Eventually(func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "-" + hex.EncodeToString(hash[0:4]), Namespace: key.Namespace}, &route)
}, timeout, interval).ShouldNot(Succeed())
By("Expecting oidcReturnURI deleted")
hash = md5.Sum([]byte("test-3.webtest.cern.ch"))
Eventually(func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "-" + hex.EncodeToString(hash[0:4]), Namespace: key.Namespace}, &oidcReturnUri)
}, timeout, interval).ShouldNot(Succeed())
})
})
})
Describe("Updating deployment object", func() {
Context("With debug annotations", func() {
It("Should not be updated successfully", func() {
By("Expecting drupalSite object created")
cr := drupalwebservicesv1alpha1.DrupalSite{}
Eventually(func() error {
return k8sClient.Get(ctx, key, &cr)
}, timeout, interval).Should(Succeed())
By("Updating the drupalSite with debug annotation")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &cr)
if cr.Annotations == nil {
cr.Annotations = map[string]string{}
}
cr.Annotations[debugAnnotation] = "true"
return k8sClient.Update(ctx, &cr)
}, timeout, interval).Should(Succeed())
By("By updating deployment object")
deploy := appsv1.Deployment{}
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &deploy)
deploy.Spec.Template.ObjectMeta.Annotations["pre.hook.backup.velero.io/container"] = "test-debug"
Eventually(func() error {
return k8sClient.Update(ctx, &deploy)
}, timeout, interval).Should(Succeed())
Eventually(func() bool {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &deploy)
return deploy.Spec.Template.GetAnnotations()["pre.hook.backup.velero.io/container"] == "test-debug"
}, timeout, interval).ShouldNot(BeTrue())
})
})
})
Describe("Deleting the drupalsite object", func() {
Context("With basic spec", func() {
It("Should be deleted successfully", func() {
By("Expecting to delete successfully")
Eventually(func() error {
return k8sClient.Delete(context.Background(), drupalSiteObject)
}, timeout, interval).Should(Succeed())
By("Expecting to delete finish")
Eventually(func() error {
return k8sClient.Get(ctx, key, drupalSiteObject)
}, timeout, interval).ShouldNot(Succeed())
})
})
})
Describe("Creating a drupalSite object", func() {
Context("With s2i source", func() {
It("All dependent resources should be created", func() {
key = types.NamespacedName{
Name: Name + "-advanced",
Namespace: "advanced",
}
drupalSiteObject = &drupalwebservicesv1alpha1.DrupalSite{
TypeMeta: metav1.TypeMeta{
APIVersion: "drupal.webservices.cern.ch/v1alpha1",
Kind: "DrupalSite",
},
ObjectMeta: metav1.ObjectMeta{
Name: key.Name,
Namespace: key.Namespace,
},
Spec: drupalwebservicesv1alpha1.DrupalSiteSpec{
Version: drupalwebservicesv1alpha1.Version{
Name: "v8.9-1",
ReleaseSpec: "stable",
},
Configuration: drupalwebservicesv1alpha1.Configuration{
DiskSize: "10Gi",
QoSClass: drupalwebservicesv1alpha1.QoSStandard,
DatabaseClass: drupalwebservicesv1alpha1.DBODStandard,
ExtraConfigurationRepo: "https://gitlab.cern.ch/rvineetr/test-ravineet-d8-containers-buildconfig.git",
},
SiteURL: []drupalwebservicesv1alpha1.Url{dummySiteUrl},
},
}
By("By creating the testing namespace")
Eventually(func() error {
return k8sClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{
Name: key.Namespace},
})
}, timeout, interval).Should(Succeed())
By("By creating a new drupalSite")
Eventually(func() error {
return k8sClient.Create(ctx, drupalSiteObject)
}, timeout, interval).Should(Succeed())
// Create drupalSite object
By("Expecting drupalSite object created")
cr := drupalwebservicesv1alpha1.DrupalSite{}
Eventually(func() error {
return k8sClient.Get(ctx, key, &cr)
}, timeout, interval).Should(Succeed())
trueVar := true
expectedOwnerReference := metav1.OwnerReference{
APIVersion: "drupal.webservices.cern.ch/v1alpha1",
Kind: "DrupalSite",
Name: key.Name,
UID: cr.UID,
Controller: &trueVar,
}
configmap := corev1.ConfigMap{}
svc := corev1.Service{}
pvc := corev1.PersistentVolumeClaim{}
job := batchv1.Job{}
deploy := appsv1.Deployment{}
is := imagev1.ImageStream{}
bc := buildv1.BuildConfig{}
dbod := dbodv1a1.Database{}
route := routev1.Route{}
oidcReturnUri := authz.OidcReturnURI{}
schedule := velerov1.Schedule{}
secret := corev1.Secret{}
// Check DBOD resource creation
By("Expecting Database resource created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &dbod)
return dbod.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Update DBOD resource status field
By("Updating the DBOD instance in Database resource status")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &dbod)
dbod.Status.DbodInstance = "test"
return k8sClient.Status().Update(ctx, &dbod)
}, timeout, interval).Should(Succeed())
By("Expecting the drupal deployment to have at least 2 containers")
Eventually(func() bool {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &deploy)
return len(deploy.Spec.Template.Spec.Containers) >= 2
}, timeout, interval).Should(BeTrue())
// Check PHP-FPM configMap creation
By("Expecting PHP_FPM configmaps created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "php-fpm-" + key.Name, Namespace: key.Namespace}, &configmap)
return configmap.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Nginx configMap creation
By("Expecting Nginx configmaps created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "nginx-" + key.Name, Namespace: key.Namespace}, &configmap)
return configmap.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Site settings configMap creation
By("Expecting Site settings configmaps created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "site-settings-" + key.Name, Namespace: key.Namespace}, &configmap)
return configmap.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check PHP Cli configMap creation
By("Expecting PHP Cli configmaps created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "php-cli-config-" + key.Name, Namespace: key.Namespace}, &configmap)
return configmap.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Drupal service
By("Expecting Drupal service created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &svc)
return svc.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check drupal persistentVolumeClaim
By("Expecting drupal persistentVolumeClaim created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "pv-claim-" + key.Name, Namespace: key.Namespace}, &pvc)
return pvc.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Drupal deployments
By("Expecting Drupal deployments created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &deploy)
return deploy.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check sitebuilder-s2i imageStream
By("Expecting sitebuilder-s2i imageStream created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "sitebuilder-s2i-" + key.Name, Namespace: key.Namespace}, &is)
return is.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Drush job
By("Expecting Drush job created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "ensure-site-install-" + key.Name, Namespace: key.Namespace}, &job)
return job.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check S2I buildConfig
By("Expecting S2I buildConfig created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "sitebuilder-s2i-" + nameVersionHash(drupalSiteObject), Namespace: key.Namespace}, &bc)
return bc.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Update drupalSite custom resource status fields to allow route conditions
By("Updating 'initialized' status field in drupalSite resource")
Eventually(func() error {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, &cr)
cr.Status.Conditions.SetCondition(status.Condition{Type: "Initialized", Status: "True"})
return k8sClient.Status().Update(ctx, &cr)
}, timeout, interval).Should(Succeed())
// Update deployment status fields to allow 'ready' status field to be set on the drupalSite resource
By("Updating 'ReadyReplicas' and 'AvailableReplicas' status fields in deployment resource")
Eventually(func() error {
k8sClient.Get(ctx, key, &deploy)
deploy.Status.Replicas = 1
deploy.Status.AvailableReplicas = 1
deploy.Status.ReadyReplicas = 1
return k8sClient.Status().Update(ctx, &deploy)
}, timeout, interval).Should(Succeed())
// Check if the Schedule resource is created
By("Expecting Schedule to be created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Namespace + "-" + key.Name, Namespace: veleroNamespace}, &schedule)
return job.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check Routes
By("Expecting Drupal Route(s) to be created")
for _, url := range cr.Spec.SiteURL {
route = routev1.Route{}
hash := md5.Sum([]byte(url))
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "-" + hex.EncodeToString(hash[0:4]), Namespace: key.Namespace}, &route)
return route.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
}
// Check OidcReturnUris
By("Expecting OidcReturnURIs created")
for _, url := range cr.Spec.SiteURL {
oidcReturnUri = authz.OidcReturnURI{}
hash := md5.Sum([]byte(url))
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "-" + hex.EncodeToString(hash[0:4]), Namespace: key.Namespace}, &oidcReturnUri)
return oidcReturnUri.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
}
// Check gitlab webhook secret resource creation
By("Expecting Gitlab webhook secret created")
Eventually(func() []metav1.OwnerReference {
k8sClient.Get(ctx, types.NamespacedName{Name: "gitlab-trigger-secret-" + key.Name, Namespace: key.Namespace}, &secret)
return secret.ObjectMeta.OwnerReferences
}, timeout, interval).Should(ContainElement(expectedOwnerReference))
// Check gitlab webhook URL updated on the drupalSite status
By("Expecting Gitlab webhook secret listed in the DrupalSite status")
Eventually(func() bool {
return cr.Status.GitlabWebhookURL == "https://api."+ClusterName+".okd.cern.ch:443/apis/build.openshift.io/v1/namespaces/"+drupalSiteObject.Namespace+"/buildconfigs/"+"sitebuilder-s2i-"+nameVersionHash(drupalSiteObject)+"/webhooks/"+string(secret.Name)+"/gitlab"
}, timeout, interval).Should(BeTrue())
})
})
})
Describe("Creating a new backup resource", func() {
Context("for the drupalSite with s2i source", func() {
It("New velero backups created for the site should reflect in the drupalSite Status", func() {
By("Expecting drupalSite object created")
cr := drupalwebservicesv1alpha1.DrupalSite{}
Eventually(func() error {
return k8sClient.Get(ctx, key, &cr)
}, timeout, interval).Should(Succeed())
// Create a backup resource for the drupalSite
hash := md5.Sum([]byte(key.Namespace))
backup := velerov1.Backup{
TypeMeta: metav1.TypeMeta{
APIVersion: "velero.io/v1",
Kind: "Backup",
},
ObjectMeta: metav1.ObjectMeta{
Name: key.Name + "backup",
Namespace: veleroNamespace,
Labels: map[string]string{
"drupal.webservices.cern.ch/projectHash": hex.EncodeToString(hash[:]),
"drupal.webservices.cern.ch/project": key.Namespace,
"drupal.webservices.cern.ch/drupalSite": key.Name,
},
Annotations: map[string]string{"drupal.webservices.cern.ch/drupalSite": key.Namespace + "/" + key.Name},
},
Status: velerov1.BackupStatus{
Phase: velerov1.BackupPhaseCompleted,
},
}
By("By creating a backup resource for the drupalSite")
Eventually(func() error {
return k8sClient.Create(ctx, &backup)
}, timeout, interval).Should(Succeed())
// Check if the backup resource is created
By("By creating a backup resource for the drupalSite")
backup1 := velerov1.Backup{}
Eventually(func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: key.Name + "backup", Namespace: veleroNamespace}, &backup1)