Skip to content

Commit 0b6086a

Browse files
committed
add function
Signed-off-by: Varsha B <vab@redhat.com>
1 parent fac7786 commit 0b6086a

File tree

1 file changed

+55
-16
lines changed

1 file changed

+55
-16
lines changed

test/openshift/e2e/ginkgo/fixture/fixture.go

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@ const (
4949

5050
var NamespaceLabels = map[string]string{E2ETestLabelsKey: E2ETestLabelsValue}
5151

52+
// Retrieve installation namespace
53+
func GetInstallationNamespace() string {
54+
55+
k8sClient, _ := utils.GetE2ETestKubeClient()
56+
installationNamespace := "openshift-operators"
57+
58+
sub := &olmv1alpha1.Subscription{
59+
ObjectMeta: metav1.ObjectMeta{
60+
Name: "openshift-gitops-operator",
61+
Namespace: installationNamespace,
62+
},
63+
}
64+
65+
if err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(sub), sub); err != nil {
66+
67+
installationNamespace = "openshift-gitops-operator"
68+
69+
sub = &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: "openshift-gitops-operator"}}
70+
71+
if err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(sub), sub); err != nil {
72+
return ""
73+
}
74+
}
75+
return installationNamespace
76+
}
77+
5278
func EnsureParallelCleanSlate() {
5379

5480
// Increase the maximum length of debug output, for when tests fail
@@ -212,7 +238,9 @@ func RemoveDynamicPluginFromCSV(ctx context.Context, k8sClient client.Client) er
212238

213239
var csv *olmv1alpha1.ClusterServiceVersion
214240
var csvList olmv1alpha1.ClusterServiceVersionList
215-
Expect(k8sClient.List(ctx, &csvList, client.InNamespace("openshift-gitops-operator"))).To(Succeed())
241+
installationNamespace := GetInstallationNamespace()
242+
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")
243+
Expect(k8sClient.List(ctx, &csvList, client.InNamespace(installationNamespace))).To(Succeed())
216244

217245
for idx := range csvList.Items {
218246
idxCSV := csvList.Items[idx]
@@ -374,8 +402,10 @@ func GetEnvInOperatorSubscriptionOrDeployment(key string) (*string, error) {
374402
return nil, nil
375403
}
376404

405+
installationNamespace := GetInstallationNamespace()
406+
377407
if EnvNonOLM() {
378-
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator-controller-manager", Namespace: "openshift-gitops-operator"}}
408+
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator-controller-manager", Namespace: installationNamespace}}
379409

380410
return deploymentFixture.GetEnv(depl, key)
381411

@@ -395,7 +425,7 @@ func GetEnvInOperatorSubscriptionOrDeployment(key string) (*string, error) {
395425

396426
} else {
397427

398-
sub := &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: "openshift-gitops-operator"}}
428+
sub := &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: installationNamespace}}
399429
if err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(sub), sub); err != nil {
400430
return nil, err
401431
}
@@ -411,12 +441,14 @@ func SetEnvInOperatorSubscriptionOrDeployment(key string, value string) {
411441

412442
k8sClient, _ := utils.GetE2ETestKubeClient()
413443

444+
installationNamespace := GetInstallationNamespace()
445+
414446
if EnvNonOLM() {
415-
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator-controller-manager", Namespace: "openshift-gitops-operator"}}
447+
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator-controller-manager", Namespace: installationNamespace}}
416448

417449
deploymentFixture.SetEnv(depl, key, value)
418450

419-
WaitForAllDeploymentsInTheNamespaceToBeReady("openshift-gitops-operator", k8sClient)
451+
WaitForAllDeploymentsInTheNamespaceToBeReady(installationNamespace, k8sClient)
420452

421453
} else if EnvCI() {
422454

@@ -430,7 +462,7 @@ func SetEnvInOperatorSubscriptionOrDeployment(key string, value string) {
430462

431463
} else {
432464

433-
sub := &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: "openshift-gitops-operator"}}
465+
sub := &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: installationNamespace}}
434466
Expect(k8sClient.Get(context.Background(), client.ObjectKeyFromObject(sub), sub)).To(Succeed())
435467

436468
subscriptionFixture.SetEnv(sub, key, value)
@@ -448,12 +480,14 @@ func RemoveEnvFromOperatorSubscriptionOrDeployment(key string) error {
448480
return err
449481
}
450482

483+
installationNamespace := GetInstallationNamespace()
484+
451485
if EnvNonOLM() {
452-
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator-controller-manager", Namespace: "openshift-gitops-operator"}}
486+
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator-controller-manager", Namespace: GetInstallationNamespace()}}
453487

454488
deploymentFixture.RemoveEnv(depl, key)
455489

456-
WaitForAllDeploymentsInTheNamespaceToBeReady("openshift-gitops-operator", k8sClient)
490+
WaitForAllDeploymentsInTheNamespaceToBeReady(installationNamespace, k8sClient)
457491

458492
} else if EnvCI() {
459493

@@ -471,7 +505,7 @@ func RemoveEnvFromOperatorSubscriptionOrDeployment(key string) error {
471505

472506
} else {
473507

474-
sub := &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: "openshift-gitops-operator"}}
508+
sub := &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: installationNamespace}}
475509
if err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(sub), sub); err != nil {
476510
return err
477511
}
@@ -486,7 +520,10 @@ func RemoveEnvFromOperatorSubscriptionOrDeployment(key string) error {
486520

487521
func GetSubscriptionInEnvCIEnvironment(k8sClient client.Client) (*olmv1alpha1.Subscription, error) {
488522
subscriptionList := olmv1alpha1.SubscriptionList{}
489-
if err := k8sClient.List(context.Background(), &subscriptionList, client.InNamespace("openshift-gitops-operator")); err != nil {
523+
524+
installationNamespace := GetInstallationNamespace()
525+
526+
if err := k8sClient.List(context.Background(), &subscriptionList, client.InNamespace(installationNamespace)); err != nil {
490527
return nil, err
491528
}
492529

@@ -510,12 +547,14 @@ func RestoreSubcriptionToDefault() {
510547
k8sClient, _, err := utils.GetE2ETestKubeClientWithError()
511548
Expect(err).ToNot(HaveOccurred())
512549

550+
installationNamespace := GetInstallationNamespace()
551+
513552
// optionalEnvVarsToRemove is a non-exhaustive list of environment variables that are known to be added to Subscription or operator Deployment by tests
514553
optionalEnvVarsToRemove := []string{"DISABLE_DEFAULT_ARGOCD_CONSOLELINK", "CONTROLLER_CLUSTER_ROLE", "SERVER_CLUSTER_ROLE", "ARGOCD_LABEL_SELECTOR", "ALLOW_NAMESPACE_MANAGEMENT_IN_NAMESPACE_SCOPED_INSTANCES"}
515554

516555
if EnvNonOLM() {
517556

518-
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator-controller-manager", Namespace: "openshift-gitops-operator"}}
557+
depl := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator-controller-manager", Namespace: installationNamespace}}
519558

520559
for _, envKey := range optionalEnvVarsToRemove {
521560
deploymentFixture.RemoveEnv(depl, envKey)
@@ -535,26 +574,26 @@ func RestoreSubcriptionToDefault() {
535574
subscriptionFixture.RemoveSpecConfig(sub)
536575
}
537576

538-
err = waitForAllEnvVarsToBeRemovedFromDeployments("openshift-gitops-operator", optionalEnvVarsToRemove, k8sClient)
577+
err = waitForAllEnvVarsToBeRemovedFromDeployments(installationNamespace, optionalEnvVarsToRemove, k8sClient)
539578
Expect(err).ToNot(HaveOccurred())
540579

541-
WaitForAllDeploymentsInTheNamespaceToBeReady("openshift-gitops-operator", k8sClient)
580+
WaitForAllDeploymentsInTheNamespaceToBeReady(installationNamespace, k8sClient)
542581

543582
} else if EnvLocalRun() {
544583
// When running locally, there are no cluster resources to clean up
545584

546585
} else {
547586

548-
sub := &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: "openshift-gitops-operator"}}
587+
sub := &olmv1alpha1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops-operator", Namespace: installationNamespace}}
549588
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(sub), sub)
550589
Expect(err).ToNot(HaveOccurred())
551590

552591
subscriptionFixture.RemoveSpecConfig(sub)
553592

554-
err = waitForAllEnvVarsToBeRemovedFromDeployments("openshift-gitops-operator", optionalEnvVarsToRemove, k8sClient)
593+
err = waitForAllEnvVarsToBeRemovedFromDeployments(installationNamespace, optionalEnvVarsToRemove, k8sClient)
555594
Expect(err).ToNot(HaveOccurred())
556595

557-
WaitForAllDeploymentsInTheNamespaceToBeReady("openshift-gitops-operator", k8sClient)
596+
WaitForAllDeploymentsInTheNamespaceToBeReady(installationNamespace, k8sClient)
558597

559598
}
560599

0 commit comments

Comments
 (0)