-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathfixture.go
More file actions
1038 lines (776 loc) · 34.8 KB
/
fixture.go
File metadata and controls
1038 lines (776 loc) · 34.8 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
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
package fixture
import (
"context"
"fmt"
"os"
"strings"
"sync"
"time"
//lint:ignore ST1001 "This is a common practice in Gomega tests for readability."
. "github.com/onsi/ginkgo/v2" //nolint:all
//lint:ignore ST1001 "This is a common practice in Gomega tests for readability."
. "github.com/onsi/gomega" //nolint:all
securityv1 "github.com/openshift/api/security/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
rolloutmanagerv1alpha1 "github.com/argoproj-labs/argo-rollouts-manager/api/v1alpha1"
argov1beta1api "github.com/argoproj-labs/argocd-operator/api/v1beta1"
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/onsi/gomega/format"
gitopsoperatorv1alpha1 "github.com/redhat-developer/gitops-operator/api/v1alpha1"
"github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/argocd"
deploymentFixture "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/deployment"
"github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/k8s"
osFixture "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/os"
subscriptionFixture "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/subscription"
"github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/utils"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// E2ETestLabelsKey and E2ETestLabelsValue are added to cluster-scoped resources (e.g. Namespaces) created by E2E tests (where possible). On startup (and before each test for sequential tests), any resources with this label will be deleted.
E2ETestLabelsKey = "app"
E2ETestLabelsValue = "test-argo-app"
)
var NamespaceLabels = map[string]string{E2ETestLabelsKey: E2ETestLabelsValue}
// Retrieve installation namespace
func GetInstallationNamespace() string {
k8sClient, _ := utils.GetE2ETestKubeClient()
installationNamespace := "openshift-operators"
sub := &olmv1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Name: "openshift-gitops-operator",
Namespace: installationNamespace,
},
}
if err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(sub), sub); err != nil {
installationNamespace = "openshift-gitops-operator"
sub = &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: installationNamespace}}
if err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(sub), sub); err != nil {
return ""
}
}
return installationNamespace
}
func EnsureParallelCleanSlate() {
// Increase the maximum length of debug output, for when tests fail
format.MaxLength = 64 * 1024
SetDefaultEventuallyTimeout(time.Second * 60)
SetDefaultEventuallyPollingInterval(time.Second * 3)
SetDefaultConsistentlyDuration(time.Second * 10)
SetDefaultConsistentlyPollingInterval(time.Second * 1)
k8sClient, _ := utils.GetE2ETestKubeClient()
// Finally, wait for default openshift-gitops instance to be ready
// - Parallel tests should not write to any resources in 'openshift-gitops' namespace (sequential only), but they are allowed to read from them.
defaultOpenShiftGitOpsArgoCD := &argov1beta1api.ArgoCD{
ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops", Namespace: "openshift-gitops"},
}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(defaultOpenShiftGitOpsArgoCD), defaultOpenShiftGitOpsArgoCD)
Expect(err).ToNot(HaveOccurred())
Eventually(defaultOpenShiftGitOpsArgoCD, "5m", "5s").Should(argocd.BeAvailableWithCustomSleepTime(3 * time.Second))
// Unlike sequential clean slate, parallel clean slate cannot assume that there are no other tests running. This limits our ability to clean up old test artifacts.
}
// EnsureSequentialCleanSlate will clean up resources that were created during previous sequential tests
// - Deletes namespaces that were created by previous tests
// - Deletes other cluster-scoped resources that were created
// - Reverts changes made to Subscription CR
// - etc
func EnsureSequentialCleanSlate() {
Expect(EnsureSequentialCleanSlateWithError()).To(Succeed())
}
func EnsureSequentialCleanSlateWithError() error {
// With sequential tests, we are always safe to assume that there is no other test running. That allows us to clean up old test artifacts before new test starts.
// Increase the maximum length of debug output, for when tests fail
format.MaxLength = 64 * 1024
SetDefaultEventuallyTimeout(time.Second * 60)
SetDefaultEventuallyPollingInterval(time.Second * 3)
SetDefaultConsistentlyDuration(time.Second * 10)
SetDefaultConsistentlyPollingInterval(time.Second * 1)
ctx := context.Background()
k8sClient, _ := utils.GetE2ETestKubeClient()
// If the CSV in 'openshift-gitops-operator' NS exists, make sure the CSV does not contain the dynamic plugin env var
if err := RemoveDynamicPluginFromCSV(ctx, k8sClient); err != nil {
return err
}
RestoreSubcriptionToDefault()
// ensure namespaces created during test are deleted
err := ensureTestNamespacesDeleted(ctx, k8sClient)
if err != nil {
return err
}
// wait for openshift-gitops ArgoCD to exist, if it doesn't already
defaultOpenShiftGitOpsArgoCD := &argov1beta1api.ArgoCD{
ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops", Namespace: "openshift-gitops"},
}
Eventually(defaultOpenShiftGitOpsArgoCD, "3m", "5s").Should(k8s.ExistByName())
// Ensure that default state of ArgoCD CR in openshift-gitops is restored
if err := updateWithoutConflict(defaultOpenShiftGitOpsArgoCD, func(obj client.Object) {
argocdObj, ok := obj.(*argov1beta1api.ArgoCD)
Expect(ok).To(BeTrue())
// HA should be disabled by default
argocdObj.Spec.HA.Enabled = false
// .spec.monitoring.disableMetrics should be nil by default
argocdObj.Spec.Monitoring.DisableMetrics = nil
// Ensure that api server route has not been disabled, nor exposed via different settings
argocdObj.Spec.Server.Route = argov1beta1api.ArgoCDRouteSpec{
Enabled: true,
TLS: nil,
// TLS: &routev1.TLSConfig{
// Termination: routev1.TLSTerminationReencrypt,
// InsecureEdgeTerminationPolicy: routev1.InsecureEdgeTerminationPolicyRedirect,
// },
}
// Reset app controller processors to default
argocdObj.Spec.Controller.Processors = argov1beta1api.ArgoCDApplicationControllerProcessorsSpec{}
// Reset repo server replicas to default
argocdObj.Spec.Repo.Replicas = nil
// Reset source namespaces
argocdObj.Spec.SourceNamespaces = nil
argocdObj.Spec.ApplicationSet.SourceNamespaces = nil
}); err != nil {
return err
}
gitopsService := &gitopsoperatorv1alpha1.GitopsService{
ObjectMeta: metav1.ObjectMeta{Name: "cluster"},
}
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(gitopsService), gitopsService); err != nil {
return err
}
// Ensure that run on infra is disabled: some tests will enable it
if err := updateWithoutConflict(gitopsService, func(obj client.Object) {
goObj, ok := obj.(*gitopsoperatorv1alpha1.GitopsService)
Expect(ok).To(BeTrue())
goObj.Spec.NodeSelector = nil
goObj.Spec.RunOnInfra = false
goObj.Spec.Tolerations = nil
}); err != nil {
return err
}
// Clean up old cluster-scoped role from 1-034
_ = k8sClient.Delete(ctx, &rbacv1.ClusterRole{ObjectMeta: metav1.ObjectMeta{Name: "custom-argocd-role"}})
// Delete all existing RolloutManagers in openshift-gitops Namespace
var rolloutManagerList rolloutmanagerv1alpha1.RolloutManagerList
if err := k8sClient.List(ctx, &rolloutManagerList, client.InNamespace("openshift-gitops")); err != nil {
return err
}
for _, rm := range rolloutManagerList.Items {
if err := k8sClient.Delete(ctx, &rm); err != nil {
return err
}
}
// Delete 'restricted-dropcaps' which is created by at least one test
scc := &securityv1.SecurityContextConstraints{
ObjectMeta: metav1.ObjectMeta{
Name: "restricted-dropcaps",
},
}
if err := k8sClient.Delete(ctx, scc); err != nil {
if !apierr.IsNotFound(err) {
return err
}
// Otherwise, expected error if it doesn't exist.
}
// Finally, wait for default openshift-gitops instance to be ready
Eventually(defaultOpenShiftGitOpsArgoCD, "5m", "5s").Should(argocd.BeAvailable())
return nil
}
// RemoveDynamicPluginFromCSV ensures that if the CSV in 'openshift-gitops-operator' NS exists, that the CSV does not contain the dynamic plugin env var
func RemoveDynamicPluginFromCSV(ctx context.Context, k8sClient client.Client) error {
if EnvNonOLM() || EnvLocalRun() {
// Skipping as CSV does exist when not using OLM, nor does it exist when running locally
return nil
}
var csv *olmv1alpha1.ClusterServiceVersion
var csvList olmv1alpha1.ClusterServiceVersionList
installationNamespace := GetInstallationNamespace()
Expect(installationNamespace).ToNot(BeNil(), "if you see this, it likely means, either: A) the operator is not installed via OLM (and you meant to install it), OR B) you are running the operator locally via 'make run', and thus should specify LOCAL_RUN=true env var when calling the test")
Expect(k8sClient.List(ctx, &csvList, client.InNamespace(installationNamespace))).To(Succeed())
for idx := range csvList.Items {
idxCSV := csvList.Items[idx]
if strings.Contains(idxCSV.Name, "gitops-operator") {
csv = &idxCSV
break
}
}
Expect(csv).ToNot(BeNil(), "if you see this, it likely means, either: A) the operator is not installed via OLM (and you meant to install it), OR B) you are running the operator locally via 'make run', and thus should specify LOCAL_RUN=true env var when calling the test")
if err := updateWithoutConflict(csv, func(obj client.Object) {
csvObj, ok := obj.(*olmv1alpha1.ClusterServiceVersion)
Expect(ok).To(BeTrue())
envList := csvObj.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec.Template.Spec.Containers[0].Env
newEnvList := []corev1.EnvVar{}
for idx := range envList {
idxEnv := envList[idx]
if idxEnv.Name == "DYNAMIC_PLUGIN_START_OCP_VERSION" {
continue
} else {
newEnvList = append(newEnvList, idxEnv)
}
}
csvObj.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec.Template.Spec.Containers[0].Env = newEnvList
}); err != nil {
return err
}
return nil
}
func CreateRandomE2ETestNamespace() *corev1.Namespace {
randomVal := string(uuid.NewUUID())
randomVal = randomVal[0:13] // Only use 14 characters of randomness. If we use more, then we start to hit limits on parts of code which limit # of characters to 63
testNamespaceName := "gitops-e2e-test-" + randomVal
ns := CreateNamespace(testNamespaceName)
return ns
}
func CreateRandomE2ETestNamespaceWithCleanupFunc() (*corev1.Namespace, func()) {
ns := CreateRandomE2ETestNamespace()
return ns, nsDeletionFunc(ns)
}
// Create namespace for tests having a specific label for identification
// - If the namespace already exists, it will be deleted first
func CreateNamespace(name string) *corev1.Namespace {
k8sClient, _ := utils.GetE2ETestKubeClient()
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: name}}
// If the Namespace already exists, delete it first
if err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(ns), ns); err == nil {
// Namespace exists, so delete it first
Expect(deleteNamespaceAndVerify(context.Background(), ns.Name, k8sClient)).To(Succeed())
}
ns = &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: NamespaceLabels,
}}
err := k8sClient.Create(context.Background(), ns)
Expect(err).ToNot(HaveOccurred())
return ns
}
func CreateNamespaceWithCleanupFunc(name string) (*corev1.Namespace, func()) {
ns := CreateNamespace(name)
return ns, nsDeletionFunc(ns)
}
// Create a namespace 'name' that is managed by another namespace 'managedByNamespace', via managed-by label.
func CreateManagedNamespace(name string, managedByNamespace string) *corev1.Namespace {
k8sClient, _ := utils.GetE2ETestKubeClient()
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: name}}
// If the Namespace already exists, delete it first
if err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(ns), ns); err == nil {
// Namespace exists, so delete it first
Expect(deleteNamespaceAndVerify(context.Background(), ns.Name, k8sClient)).To(Succeed())
}
ns = &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
E2ETestLabelsKey: E2ETestLabelsValue,
"argocd.argoproj.io/managed-by": managedByNamespace,
},
}}
Expect(k8sClient.Create(context.Background(), ns)).To(Succeed())
return ns
}
func CreateManagedNamespaceWithCleanupFunc(name string, managedByNamespace string) (*corev1.Namespace, func()) {
ns := CreateManagedNamespace(name, managedByNamespace)
return ns, nsDeletionFunc(ns)
}
// nsDeletionFunc is a convenience function that returns a function that deletes a namespace. This is used for Namespace cleanup by other functions.
func nsDeletionFunc(ns *corev1.Namespace) func() {
return func() {
DeleteNamespace(ns)
}
}
func DeleteNamespace(ns *corev1.Namespace) {
// If you are debugging an E2E test and want to prevent its namespace from being deleted when the test ends (so that you can examine the state of resources in the namespace) you can set E2E_DEBUG_SKIP_CLEANUP env var.
if os.Getenv("E2E_DEBUG_SKIP_CLEANUP") != "" {
GinkgoWriter.Println("Skipping namespace cleanup as E2E_DEBUG_SKIP_CLEANUP is set")
return
}
k8sClient, _, err := utils.GetE2ETestKubeClientWithError()
Expect(err).ToNot(HaveOccurred())
err = deleteNamespaceAndVerify(context.Background(), ns.Name, k8sClient)
Expect(err).ToNot(HaveOccurred())
}
// EnvNonOLM checks if NON_OLM var is set; this variable is set when testing on GitOps operator that is not installed via OLM
func EnvNonOLM() bool {
_, exists := os.LookupEnv("NON_OLM")
return exists
}
func EnvLocalRun() bool {
_, exists := os.LookupEnv("LOCAL_RUN")
return exists
}
// EnvCI checks if CI env var is set; this variable is set when testing on GitOps Operator running via CI pipeline (and using an OLM Subscription)
func EnvCI() bool {
_, exists := os.LookupEnv("CI")
return exists
}
// GetEnvInOperatorSubscriptionOrDeployment will return the value of an environment variable, in either operator Subscription or operator Deployment, depending on which mode the test is running in.
func GetEnvInOperatorSubscriptionOrDeployment(key string) (*string, error) {
k8sClient, _, err := utils.GetE2ETestKubeClientWithError()
if err != nil {
return nil, nil
}
installationNamespace := GetInstallationNamespace()
if EnvNonOLM() {
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator-controller-manager", Namespace: installationNamespace}}
return deploymentFixture.GetEnv(depl, "manager", key)
} else if EnvCI() {
sub, err := GetSubscriptionInEnvCIEnvironment(k8sClient)
if err != nil {
return nil, err
}
if sub == nil {
return nil, nil
}
envVal, err := subscriptionFixture.GetEnv(sub, key)
return envVal, err
} else {
sub := &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: installationNamespace}}
if err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(sub), sub); err != nil {
return nil, err
}
return subscriptionFixture.GetEnv(sub, key)
}
}
// SetEnvInOperatorSubscriptionOrDeployment will set the value of an environment variable, in either operator Subscription (under .spec.config.env) or operator Deployment (under template spec), depending on which mode the test is running in.
func SetEnvInOperatorSubscriptionOrDeployment(key string, value string) {
k8sClient, _ := utils.GetE2ETestKubeClient()
installationNamespace := GetInstallationNamespace()
if EnvNonOLM() {
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator-controller-manager", Namespace: installationNamespace}}
deploymentFixture.SetEnv(depl, "manager", key, value)
WaitForAllDeploymentsInTheNamespaceToBeReady(installationNamespace, k8sClient)
} else if EnvCI() {
sub, err := GetSubscriptionInEnvCIEnvironment(k8sClient)
Expect(err).ToNot(HaveOccurred())
Expect(sub).ToNot(BeNil())
subscriptionFixture.SetEnv(sub, key, value)
WaitForAllDeploymentsInTheNamespaceToBeReady(sub.Namespace, k8sClient)
} else {
sub := &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: installationNamespace}}
Expect(k8sClient.Get(context.Background(), client.ObjectKeyFromObject(sub), sub)).To(Succeed())
subscriptionFixture.SetEnv(sub, key, value)
WaitForAllDeploymentsInTheNamespaceToBeReady(sub.Namespace, k8sClient)
}
}
// RemoveEnvFromOperatorSubscriptionOrDeployment will delete an environment variable from either operator Subscription or operator Deployment, depending on which mode the test is running in.
func RemoveEnvFromOperatorSubscriptionOrDeployment(key string) error {
k8sClient, _, err := utils.GetE2ETestKubeClientWithError()
if err != nil {
return err
}
installationNamespace := GetInstallationNamespace()
if EnvNonOLM() {
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator-controller-manager", Namespace: installationNamespace}}
deploymentFixture.RemoveEnv(depl, "manager", key)
WaitForAllDeploymentsInTheNamespaceToBeReady(installationNamespace, k8sClient)
} else if EnvCI() {
sub, err := GetSubscriptionInEnvCIEnvironment(k8sClient)
if err != nil {
return err
}
if sub == nil {
return nil
}
subscriptionFixture.RemoveEnv(sub, key)
WaitForAllDeploymentsInTheNamespaceToBeReady(sub.Namespace, k8sClient)
} else {
sub := &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: installationNamespace}}
if err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(sub), sub); err != nil {
return err
}
subscriptionFixture.RemoveEnv(sub, key)
WaitForAllDeploymentsInTheNamespaceToBeReady(sub.Namespace, k8sClient)
}
return nil
}
func GetSubscriptionInEnvCIEnvironment(k8sClient client.Client) (*olmv1alpha1.Subscription, error) {
subscriptionList := olmv1alpha1.SubscriptionList{}
if err := k8sClient.List(context.Background(), &subscriptionList, client.InNamespace(GetInstallationNamespace())); err != nil {
return nil, err
}
var sub *olmv1alpha1.Subscription
for idx := range subscriptionList.Items {
currsub := subscriptionList.Items[idx]
if strings.HasPrefix(currsub.Name, "gitops-operator-") {
sub = &currsub
}
}
return sub, nil
}
// RestoreSubcriptionToDefault ensures that the Subscription (or Deployment env vars) are restored to a default state before each test.
func RestoreSubcriptionToDefault() {
k8sClient, _, err := utils.GetE2ETestKubeClientWithError()
Expect(err).ToNot(HaveOccurred())
installationNamespace := GetInstallationNamespace()
// optionalEnvVarsToRemove is a non-exhaustive list of environment variables that are known to be added to Subscription or operator Deployment by tests
optionalEnvVarsToRemove := []string{"DISABLE_DEFAULT_ARGOCD_CONSOLELINK", "CONTROLLER_CLUSTER_ROLE", "SERVER_CLUSTER_ROLE", "ARGOCD_LABEL_SELECTOR", "ALLOW_NAMESPACE_MANAGEMENT_IN_NAMESPACE_SCOPED_INSTANCES", "IMAGE_PULL_POLICY"}
if EnvNonOLM() {
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator-controller-manager", Namespace: installationNamespace}}
for _, envKey := range optionalEnvVarsToRemove {
deploymentFixture.RemoveEnv(depl, "manager", envKey)
}
err := waitForAllEnvVarsToBeRemovedFromDeployments(depl.Namespace, optionalEnvVarsToRemove, k8sClient)
Expect(err).ToNot(HaveOccurred())
Eventually(depl, "3m", "1s").Should(deploymentFixture.HaveReadyReplicas(1))
} else if EnvCI() {
sub, err := GetSubscriptionInEnvCIEnvironment(k8sClient)
Expect(err).ToNot(HaveOccurred())
if sub != nil {
subscriptionFixture.RemoveSpecConfig(sub)
}
err = waitForAllEnvVarsToBeRemovedFromDeployments(installationNamespace, optionalEnvVarsToRemove, k8sClient)
Expect(err).ToNot(HaveOccurred())
WaitForAllDeploymentsInTheNamespaceToBeReady(installationNamespace, k8sClient)
} else if EnvLocalRun() {
// When running locally, there are no cluster resources to clean up
} else {
sub := &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: installationNamespace}}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(sub), sub)
Expect(err).ToNot(HaveOccurred())
subscriptionFixture.RemoveSpecConfig(sub)
err = waitForAllEnvVarsToBeRemovedFromDeployments(installationNamespace, optionalEnvVarsToRemove, k8sClient)
Expect(err).ToNot(HaveOccurred())
WaitForAllDeploymentsInTheNamespaceToBeReady(installationNamespace, k8sClient)
}
}
// waitForAllEnvVarsToBeRemovedFromDeployments checks all Deployments in the Namespace, to ensure that none of those Deployments contain environment variables defined within envVarKeys.
// This can be used before a test starts to ensure that Operator or Argo CD containers are back to default state.
func waitForAllEnvVarsToBeRemovedFromDeployments(ns string, envVarKeys []string, k8sClient client.Client) error {
Eventually(func() bool {
var deplList appsv1.DeploymentList
if err := k8sClient.List(context.Background(), &deplList, client.InNamespace(ns)); err != nil {
GinkgoWriter.Println(err)
return false
}
// For each Deployment in the list...
for _, depl := range deplList.Items {
// If at least one of the Deployments has not been observed, wait and try again
if depl.Generation != depl.Status.ObservedGeneration {
return false
}
// For each container of the deployment
for _, container := range depl.Spec.Template.Spec.Containers {
// For each env var we are looking for
for _, envVarKey := range envVarKeys {
for _, containerEnvKey := range container.Env {
if containerEnvKey.Name == envVarKey {
GinkgoWriter.Println("Waiting:", containerEnvKey, "is still present in Deployment ", depl.Name)
return false
}
}
}
}
}
// All Deployments in NS are reconciled and ready
return true
}, "3m", "1s").Should(BeTrue())
return nil
}
func WaitForAllDeploymentsInTheNamespaceToBeReady(ns string, k8sClient client.Client) {
Eventually(func() bool {
var deplList appsv1.DeploymentList
if err := k8sClient.List(context.Background(), &deplList, client.InNamespace(ns)); err != nil {
GinkgoWriter.Println(err)
return false
}
for _, depl := range deplList.Items {
// If at least one of the Deployments has not been observed, wait and try again
if depl.Generation != depl.Status.ObservedGeneration {
return false
}
if int64(depl.Status.Replicas) != int64(depl.Status.ReadyReplicas) {
return false
}
}
// All Deployments in NS are reconciled and ready
return true
}, "3m", "1s").Should(BeTrue())
// The above logic will successfully wait for Deployments to be ready. However, this does not mean that the operator's controller logic has completed it's initial cluster reconciliation logic (starting a watch then reconciling existing resources)
// - I'm not aware of a way to detect when this has completed, so instead I am inserting a 15 second pause.
// - If anyone has a better way of doing this, let us know.
// time.Sleep(15 * time.Second)
// TODO: Uncomment this once the sequential test suite timeout has increased.
}
func WaitForAllStatefulSetsInTheNamespaceToBeReady(ns string, k8sClient client.Client) {
Eventually(func() bool {
var ssList appsv1.StatefulSetList
if err := k8sClient.List(context.Background(), &ssList, client.InNamespace(ns)); err != nil {
GinkgoWriter.Println(err)
return false
}
for _, ss := range ssList.Items {
// If at least one of the StatefulSets has not been observed, wait and try again
if ss.Generation != ss.Status.ObservedGeneration {
return false
}
if int64(ss.Status.Replicas) != int64(ss.Status.ReadyReplicas) {
return false
}
}
// All StatefulSets in NS are reconciled and ready
return true
}, "3m", "1s").Should(BeTrue())
}
func WaitForAllPodsInTheNamespaceToBeReady(ns string, k8sClient client.Client) {
Eventually(func() bool {
var podList corev1.PodList
if err := k8sClient.List(context.Background(), &podList, client.InNamespace(ns)); err != nil {
GinkgoWriter.Println(err)
return false
}
for _, pod := range podList.Items {
for _, containerStatus := range pod.Status.ContainerStatuses {
if !containerStatus.Ready {
GinkgoWriter.Println(pod.Name, "has container", containerStatus.Name, "which is not ready")
return false
}
}
}
// All Pod in NS are ready
return true
}, "3m", "1s").Should(BeTrue())
}
// Delete all namespaces having a specific label used to identify namespaces that are created by e2e tests.
func ensureTestNamespacesDeleted(ctx context.Context, k8sClient client.Client) error {
// fetch all namespaces having given label
nsList, err := listE2ETestNamespaces(ctx, k8sClient)
if err != nil {
return fmt.Errorf("unable to delete test namespace: %w", err)
}
// delete selected namespaces
for _, namespace := range nsList.Items {
if err := deleteNamespaceAndVerify(ctx, namespace.Name, k8sClient); err != nil {
return fmt.Errorf("unable to delete namespace '%s': %w", namespace.Name, err)
}
}
return nil
}
// deleteNamespaceAndVerify deletes a namespace, and waits for it to be reported as deleted.
func deleteNamespaceAndVerify(ctx context.Context, namespaceParam string, k8sClient client.Client) error {
GinkgoWriter.Println("Deleting Namespace", namespaceParam)
// Delete the namespace:
// - Issue a request to Delete the namespace
// - Finally, we check if it has been deleted.
if err := wait.PollUntilContextTimeout(ctx, time.Second*5, time.Minute*6, true, func(ctx context.Context) (done bool, err error) {
// Delete the namespace, if it exists
namespace := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceParam,
},
}
if err := k8sClient.Delete(ctx, &namespace); err != nil {
if !apierr.IsNotFound(err) {
GinkgoWriter.Printf("Unable to delete namespace '%s': %v\n", namespaceParam, err)
return false, nil
}
}
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(&namespace), &namespace); err != nil {
if apierr.IsNotFound(err) {
return true, nil
} else {
GinkgoWriter.Printf("Unable to Get namespace '%s': %v\n", namespaceParam, err)
return false, nil
}
}
return false, nil
}); err != nil {
return fmt.Errorf("namespace was never deleted, after delete was issued. '%s':%v", namespaceParam, err)
}
return nil
}
// Retrieve list of namespaces having a specific label used to identify namespaces that are created by e2e tests.
func listE2ETestNamespaces(ctx context.Context, k8sClient client.Client) (corev1.NamespaceList, error) {
nsList := corev1.NamespaceList{}
// set e2e label
req, err := labels.NewRequirement(E2ETestLabelsKey, selection.Equals, []string{E2ETestLabelsValue})
if err != nil {
return nsList, fmt.Errorf("unable to set labels while fetching list of test namespace: %w", err)
}
// fetch all namespaces having given label
err = k8sClient.List(ctx, &nsList, &client.ListOptions{LabelSelector: labels.NewSelector().Add(*req)})
if err != nil {
return nsList, fmt.Errorf("unable to fetch list of test namespace: %w", err)
}
return nsList, nil
}
// Update will keep trying to update object until it succeeds, or times out.
func updateWithoutConflict(obj client.Object, modify func(client.Object)) error {
k8sClient, _, err := utils.GetE2ETestKubeClientWithError()
if err != nil {
return err
}
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
// Retrieve the latest version of the object
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(obj), obj)
if err != nil {
return err
}
modify(obj)
// Attempt to update the object
return k8sClient.Update(context.Background(), obj)
})
return err
}
type testReportEntry struct {
isOutputted bool
}
var testReportLock sync.Mutex
var testReportMap = map[string]testReportEntry{} // acquire testReportLock before reading/writing to this map, or any values within this map
// OutputDebugOnFail can be used to debug a failing test: it will output the operator logs and namespace info
// Parameters:
// - Will output debug information on namespaces specified as parameters.
// - Namespace parameter may be a string, *Namespace, or Namespace
func OutputDebugOnFail(namespaceParams ...any) {
// Convert parameter to string of namespace name:
// - You can specify Namespace, *Namespae, or string, and we will convert it to string namespace
namespaces := []string{}
for _, param := range namespaceParams {
if param == nil {
continue
}
if str, isString := (param).(string); isString {
namespaces = append(namespaces, str)
} else if nsPtr, isNsPtr := (param).(*corev1.Namespace); isNsPtr {
namespaces = append(namespaces, nsPtr.Name)
} else if ns, isNs := (param).(corev1.Namespace); isNs {
namespaces = append(namespaces, ns.Name)
} else {
Fail(fmt.Sprintf("unrecognized parameter value: %v", param))
}
}
csr := CurrentSpecReport()
if !csr.Failed() || os.Getenv("SKIP_DEBUG_OUTPUT") == "true" {
return
}
testName := strings.Join(csr.ContainerHierarchyTexts, " ")
testReportLock.Lock()
defer testReportLock.Unlock()
debugOutput, exists := testReportMap[testName]
if exists && debugOutput.isOutputted {
// Skip output if we have already outputted once for this test
return
}
testReportMap[testName] = testReportEntry{
isOutputted: true,
}
outputPodLog("openshift-gitops-operator-controller-manager")
for _, namespace := range namespaces {
kubectlOutput, err := osFixture.ExecCommandWithOutputParam(false, "kubectl", "get", "all", "-n", namespace)
if err != nil {
GinkgoWriter.Println("unable to list", namespace, err, kubectlOutput)
continue
}
GinkgoWriter.Println("")
GinkgoWriter.Println("----------------------------------------------------------------")
GinkgoWriter.Println("'kubectl get all -n", namespace+"' output:")
GinkgoWriter.Println(kubectlOutput)
GinkgoWriter.Println("----------------------------------------------------------------")
kubectlOutput, err = osFixture.ExecCommandWithOutputParam(false, "kubectl", "get", "deployments", "-n", namespace, "-o", "yaml")
if err != nil {
GinkgoWriter.Println("unable to list", namespace, err, kubectlOutput)
continue
}
GinkgoWriter.Println("")
GinkgoWriter.Println("----------------------------------------------------------------")
GinkgoWriter.Println("'kubectl get deployments -n " + namespace + " -o yaml")
GinkgoWriter.Println(kubectlOutput)
GinkgoWriter.Println("----------------------------------------------------------------")
kubectlOutput, err = osFixture.ExecCommandWithOutputParam(false, "kubectl", "get", "events", "-n", namespace)
if err != nil {
GinkgoWriter.Println("unable to get events for namespace", err, kubectlOutput)
} else {
GinkgoWriter.Println("")
GinkgoWriter.Println("----------------------------------------------------------------")
GinkgoWriter.Println("'kubectl get events -n " + namespace + ":")
GinkgoWriter.Println(kubectlOutput)
GinkgoWriter.Println("----------------------------------------------------------------")
}
}
kubectlOutput, err := osFixture.ExecCommandWithOutputParam(false, "kubectl", "get", "argocds", "-A", "-o", "yaml")
if err != nil {
GinkgoWriter.Println("unable to output all argo cd statuses", err, kubectlOutput)
} else {
GinkgoWriter.Println("")
GinkgoWriter.Println("----------------------------------------------------------------")
GinkgoWriter.Println("'kubectl get argocds -A -o yaml':")
GinkgoWriter.Println(kubectlOutput)
GinkgoWriter.Println("----------------------------------------------------------------")
}
GinkgoWriter.Println("You can skip this debug output by setting 'SKIP_DEBUG_OUTPUT=true'")
}
// EnsureRunningOnOpenShift should be called if a test requires OpenShift (for example, it uses Route CR).
func EnsureRunningOnOpenShift() {
runningOnOpenShift := RunningOnOpenShift()
if !runningOnOpenShift {
Skip("This test requires the cluster to be OpenShift")
return
}
Expect(runningOnOpenShift).To(BeTrueBecause("this test is marked as requiring an OpenShift cluster, and we have detected the cluster is OpenShift"))
}
// RunningOnOpenShift returns true if the cluster is an OpenShift cluster, false otherwise.
func RunningOnOpenShift() bool {
k8sClient, _ := utils.GetE2ETestKubeClient()
crdList := crdv1.CustomResourceDefinitionList{}
Expect(k8sClient.List(context.Background(), &crdList)).To(Succeed())
openshiftAPIsFound := 0
for _, crd := range crdList.Items {
if strings.Contains(crd.Spec.Group, "openshift.io") {
openshiftAPIsFound++
}
}
return openshiftAPIsFound > 5 // I picked 5 as an arbitrary number, could also just be 1
}
//nolint:unused
func outputPodLog(podSubstring string) {
k8sClient, _, err := utils.GetE2ETestKubeClientWithError()
if err != nil {
GinkgoWriter.Println(err)
return
}
// List all pods on the cluster
var podList corev1.PodList
if err := k8sClient.List(context.Background(), &podList); err != nil {
GinkgoWriter.Println(err)
return
}
// Look specifically for operator pod
matchingPods := []corev1.Pod{}
for idx := range podList.Items {
pod := podList.Items[idx]
if strings.Contains(pod.Name, podSubstring) {
matchingPods = append(matchingPods, pod)
}