|
| 1 | +package sequential |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + argov1beta1api "github.com/argoproj-labs/argocd-operator/api/v1beta1" |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" |
| 10 | + "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture" |
| 11 | + argocdFixture "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/argocd" |
| 12 | + k8sFixture "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/k8s" |
| 13 | + fixtureUtils "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/utils" |
| 14 | + appsv1 "k8s.io/api/apps/v1" |
| 15 | + corev1 "k8s.io/api/core/v1" |
| 16 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 17 | + "k8s.io/utils/ptr" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | +) |
| 20 | + |
| 21 | +var _ = Describe("GitOps Operator Sequential E2E Tests", func() { |
| 22 | + |
| 23 | + Context("1-092_validate_workload_status_monitoring_alert", func() { |
| 24 | + var ( |
| 25 | + k8sClient client.Client |
| 26 | + ctx context.Context |
| 27 | + nsCluster *corev1.Namespace |
| 28 | + nsNamespaced *corev1.Namespace |
| 29 | + cleanupFunc func() |
| 30 | + ) |
| 31 | + |
| 32 | + BeforeEach(func() { |
| 33 | + fixture.EnsureSequentialCleanSlate() |
| 34 | + k8sClient, _ = fixtureUtils.GetE2ETestKubeClient() |
| 35 | + ctx = context.Background() |
| 36 | + |
| 37 | + nsCluster = &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "openshift-gitops"}} |
| 38 | + nsNamespaced, cleanupFunc = fixture.CreateRandomE2ETestNamespaceWithCleanupFunc() |
| 39 | + }) |
| 40 | + |
| 41 | + AfterEach(func() { |
| 42 | + defer cleanupFunc() |
| 43 | + fixture.OutputDebugOnFail(nsNamespaced) |
| 44 | + }) |
| 45 | + |
| 46 | + It("validates monitoring setup, alert rule creation, and teardown", func() { |
| 47 | + const ( |
| 48 | + invalidImage = "test-image" |
| 49 | + prometheusRuleName = "gitops-operator-argocd-alerts" |
| 50 | + clusterInstanceName = "openshift-gitops" |
| 51 | + ) |
| 52 | + |
| 53 | + ruleCluster := &monitoringv1.PrometheusRule{ |
| 54 | + ObjectMeta: metav1.ObjectMeta{Name: prometheusRuleName, Namespace: nsCluster.Name}, |
| 55 | + } |
| 56 | + ruleNamespaced := &monitoringv1.PrometheusRule{ |
| 57 | + ObjectMeta: metav1.ObjectMeta{Name: prometheusRuleName, Namespace: nsNamespaced.Name}, |
| 58 | + } |
| 59 | + |
| 60 | + uwmConfigMap := &corev1.ConfigMap{ |
| 61 | + ObjectMeta: metav1.ObjectMeta{Name: "cluster-monitoring-config", Namespace: "openshift-monitoring"}, |
| 62 | + Data: map[string]string{"config.yaml": "enableUserWorkload: true"}, |
| 63 | + } |
| 64 | + cmKey := client.ObjectKey{Name: uwmConfigMap.Name, Namespace: uwmConfigMap.Namespace} |
| 65 | + |
| 66 | + By("enabling user workload monitoring in the cluster monitoring config map") |
| 67 | + existingCM := &corev1.ConfigMap{} |
| 68 | + err := k8sClient.Get(ctx, cmKey, existingCM) |
| 69 | + |
| 70 | + DeferCleanup(func() { |
| 71 | + _ = k8sClient.Delete(ctx, uwmConfigMap) |
| 72 | + }) |
| 73 | + |
| 74 | + if err == nil { |
| 75 | + existingCM.Data = uwmConfigMap.Data |
| 76 | + Expect(k8sClient.Update(ctx, existingCM)).To(Succeed(), "Failed to update existing UWM ConfigMap") |
| 77 | + } else { |
| 78 | + Expect(k8sClient.Create(ctx, uwmConfigMap)).To(Succeed(), "Failed to create UWM ConfigMap") |
| 79 | + } |
| 80 | + |
| 81 | + By("enabling monitoring on the cluster Argo CD instance and setting an invalid image to trigger alerts") |
| 82 | + argoCDCluster := &argov1beta1api.ArgoCD{ |
| 83 | + ObjectMeta: metav1.ObjectMeta{Name: clusterInstanceName, Namespace: nsCluster.Name}, |
| 84 | + } |
| 85 | + Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(argoCDCluster), argoCDCluster)).To(Succeed()) |
| 86 | + |
| 87 | + //restore the cluster instance even if the test fails halfway through it |
| 88 | + DeferCleanup(func() { |
| 89 | + By("restoring the default image and disabling monitoring on cluster Argo CD instance (Cleanup)") |
| 90 | + _ = k8sClient.Get(ctx, client.ObjectKeyFromObject(argoCDCluster), argoCDCluster) |
| 91 | + argocdFixture.Update(argoCDCluster, func(ac *argov1beta1api.ArgoCD) { |
| 92 | + ac.Spec.ApplicationSet.Image = "" |
| 93 | + ac.Spec.Monitoring.DisableMetrics = ptr.To(true) |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + argocdFixture.Update(argoCDCluster, func(ac *argov1beta1api.ArgoCD) { |
| 98 | + ac.Spec.ApplicationSet = &argov1beta1api.ArgoCDApplicationSet{Image: invalidImage} |
| 99 | + ac.Spec.Monitoring = argov1beta1api.ArgoCDMonitoringSpec{DisableMetrics: ptr.To(false)} |
| 100 | + }) |
| 101 | + |
| 102 | + By("creating a namespaced Argo CD instance with monitoring enabled") |
| 103 | + argoCDNamespaced := &argov1beta1api.ArgoCD{ |
| 104 | + ObjectMeta: metav1.ObjectMeta{Name: "argocd", Namespace: nsNamespaced.Name}, |
| 105 | + Spec: argov1beta1api.ArgoCDSpec{ |
| 106 | + ApplicationSet: &argov1beta1api.ArgoCDApplicationSet{Image: invalidImage}, |
| 107 | + Monitoring: argov1beta1api.ArgoCDMonitoringSpec{DisableMetrics: ptr.To(false)}, |
| 108 | + }, |
| 109 | + } |
| 110 | + Expect(k8sClient.Create(ctx, argoCDNamespaced)).To(Succeed()) |
| 111 | + |
| 112 | + //the verification |
| 113 | + By("waiting for the Argo CD instances to become available") |
| 114 | + Eventually(argoCDCluster, "5m").Should(argocdFixture.BeAvailable()) |
| 115 | + Eventually(argoCDNamespaced, "5m").Should(argocdFixture.BeAvailable()) |
| 116 | + |
| 117 | + By("verifying the operator created the expected PrometheusRules") |
| 118 | + Eventually(ruleCluster, "5m").Should(k8sFixture.ExistByName(), "PrometheusRule should be created in cluster namespace") |
| 119 | + Eventually(ruleNamespaced, "5m").Should(k8sFixture.ExistByName(), "PrometheusRule should be created in test namespace") |
| 120 | + |
| 121 | + By("verifying the ApplicationSet deployments are present (likely in a crash loop due to the invalid image)") |
| 122 | + appSetDeplCluster := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: clusterInstanceName + "-applicationset-controller", Namespace: nsCluster.Name}} |
| 123 | + Eventually(appSetDeplCluster).Should(k8sFixture.ExistByName()) |
| 124 | + |
| 125 | + By("disabling monitoring and restoring the default image on the cluster Argo CD instance") |
| 126 | + Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(argoCDCluster), argoCDCluster)).To(Succeed()) |
| 127 | + argocdFixture.Update(argoCDCluster, func(ac *argov1beta1api.ArgoCD) { |
| 128 | + ac.Spec.ApplicationSet.Image = "" |
| 129 | + ac.Spec.Monitoring.DisableMetrics = ptr.To(true) |
| 130 | + }) |
| 131 | + |
| 132 | + By("disabling monitoring on the namespaced Argo CD instance") |
| 133 | + Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(argoCDNamespaced), argoCDNamespaced)).To(Succeed()) |
| 134 | + argocdFixture.Update(argoCDNamespaced, func(ac *argov1beta1api.ArgoCD) { |
| 135 | + ac.Spec.Monitoring.DisableMetrics = ptr.To(true) |
| 136 | + }) |
| 137 | + |
| 138 | + By("verifying the PrometheusRules are removed") |
| 139 | + Eventually(ruleCluster, "5m").Should(k8sFixture.NotExistByName(), "Cluster PrometheusRule should be deleted") |
| 140 | + Eventually(ruleNamespaced, "5m").Should(k8sFixture.NotExistByName(), "Namespaced PrometheusRule should be deleted") |
| 141 | + |
| 142 | + }) |
| 143 | + }) |
| 144 | +}) |
0 commit comments