Skip to content

Commit ad5edae

Browse files
author
Triona Doyle
committed
refactor prometheus parsing and cleanup in 1-092 test
Signed-off-by: Triona Doyle <bot@example.com>
1 parent 1096f6e commit ad5edae

1 file changed

Lines changed: 52 additions & 24 deletions

File tree

test/openshift/e2e/ginkgo/sequential/1-092_validate_workload_status_monitoring_alert.go

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package sequential
22

33
import (
44
"context"
5+
"encoding/json"
56
"os/exec"
6-
"strings"
77

88
argov1beta1api "github.com/argoproj-labs/argocd-operator/api/v1beta1"
99
. "github.com/onsi/ginkgo/v2"
@@ -48,8 +48,7 @@ var _ = Describe("GitOps Operator Sequential E2E Tests", func() {
4848

4949
It("validates monitoring setup, alert rule creation, and teardown", func() {
5050
const (
51-
// picking a valid image that exists to avoid ImagePullBackOff
52-
// but should fail to run as an ApplicationSet controller
51+
// picking image that exists to avoid ImagePullBackOff but should fail to run as an ApplicationSet controller
5352
invalidImage = "quay.io/libpod/alpine:latest"
5453
prometheusRuleName = "argocd-component-status-alert"
5554
clusterInstanceName = "openshift-gitops"
@@ -64,13 +63,16 @@ var _ = Describe("GitOps Operator Sequential E2E Tests", func() {
6463
appSetDeplCluster := &appsv1.Deployment{
6564
ObjectMeta: metav1.ObjectMeta{Name: clusterInstanceName + "-applicationset-controller", Namespace: nsCluster.Name},
6665
}
66+
appSetDeplNamespaced := &appsv1.Deployment{
67+
ObjectMeta: metav1.ObjectMeta{Name: "argocd-applicationset-controller", Namespace: nsNamespaced.Name},
68+
}
6769
uwmConfigMap := &corev1.ConfigMap{
6870
ObjectMeta: metav1.ObjectMeta{Name: "cluster-monitoring-config", Namespace: "openshift-monitoring"},
6971
Data: map[string]string{"config.yaml": "enableUserWorkload: true\n"},
7072
}
7173

7274
By("labeling the namespace for monitoring")
73-
// Prometheus will only scrape User Workload namespaces that have this label
75+
// prometheus will only scrape user workload namespaces that have this label
7476
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(nsNamespaced), nsNamespaced)
7577
Expect(err).NotTo(HaveOccurred())
7678

@@ -85,11 +87,11 @@ var _ = Describe("GitOps Operator Sequential E2E Tests", func() {
8587
existingCM := &corev1.ConfigMap{}
8688
err = k8sClient.Get(ctx, client.ObjectKeyFromObject(uwmConfigMap), existingCM)
8789

88-
DeferCleanup(func() {
89-
_ = k8sClient.Delete(ctx, uwmConfigMap)
90-
})
90+
cmExisted := (err == nil)
91+
var originalData map[string]string
9192

92-
if err == nil {
93+
if cmExisted {
94+
originalData = existingCM.Data
9395
existingCM.Data = uwmConfigMap.Data
9496
Expect(k8sClient.Update(ctx, existingCM)).To(Succeed(), "Failed to update existing UWM ConfigMap")
9597
} else if errors.IsNotFound(err) {
@@ -98,19 +100,30 @@ var _ = Describe("GitOps Operator Sequential E2E Tests", func() {
98100
Expect(err).NotTo(HaveOccurred(), "Failed to fetch UWM ConfigMap")
99101
}
100102

103+
DeferCleanup(func() {
104+
By("restoring or deleting cluster monitoring config")
105+
if cmExisted {
106+
revertCM := &corev1.ConfigMap{}
107+
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(uwmConfigMap), revertCM)).To(Succeed())
108+
revertCM.Data = originalData
109+
Expect(k8sClient.Update(ctx, revertCM)).To(Succeed())
110+
} else {
111+
_ = k8sClient.Delete(ctx, uwmConfigMap)
112+
}
113+
})
114+
101115
By("modifying both ArgoCD instances to enable monitoring and break the AppSet image")
102116
argoCDCluster := &argov1beta1api.ArgoCD{
103117
ObjectMeta: metav1.ObjectMeta{Name: clusterInstanceName, Namespace: nsCluster.Name},
104118
}
105119
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(argoCDCluster), argoCDCluster)).To(Succeed())
106120

107-
// Restore even if the test fails halfway through
121+
// restore even if the test fails halfway
108122
DeferCleanup(func() {
109123
By("restoring the default image and disabling monitoring on cluster Argo CD instance (Cleanup)")
110124
_ = k8sClient.Get(ctx, client.ObjectKeyFromObject(argoCDCluster), argoCDCluster)
111125
argocdFixture.Update(argoCDCluster, func(ac *argov1beta1api.ArgoCD) {
112126
ac.Spec.ApplicationSet.Image = ""
113-
ac.Spec.Monitoring.DisableMetrics = ptr.To(true)
114127
ac.Spec.Monitoring.Enabled = false
115128
})
116129
})
@@ -139,6 +152,7 @@ var _ = Describe("GitOps Operator Sequential E2E Tests", func() {
139152

140153
By("verifying the ApplicationSet deployments are present")
141154
Eventually(appSetDeplCluster).Should(k8sFixture.ExistByName())
155+
Eventually(appSetDeplNamespaced).Should(k8sFixture.ExistByName())
142156

143157
By("verifying the workload degradation alerts are actively firing in Prometheus")
144158
Eventually(func() bool {
@@ -148,17 +162,36 @@ var _ = Describe("GitOps Operator Sequential E2E Tests", func() {
148162
GinkgoWriter.Printf("Failed to query Prometheus: %v\n", err)
149163
return false
150164
}
151-
out := string(outBytes)
152165

153-
// check both default and the custom instance alerts are firing
154-
hasDefaultAlert := strings.Contains(out, "openshift-gitops-applicationset-controller") &&
155-
strings.Contains(out, "ApplicationSetControllerNotReady") &&
156-
strings.Contains(out, "firing")
166+
// parse the json response
167+
type promResponse struct {
168+
Data struct {
169+
Alerts []struct {
170+
Labels map[string]string `json:"labels"`
171+
State string `json:"state"`
172+
} `json:"alerts"`
173+
} `json:"data"`
174+
}
175+
176+
var resp promResponse
177+
if err := json.Unmarshal(outBytes, &resp); err != nil {
178+
GinkgoWriter.Printf("Failed to unmarshal JSON: %v\n", err)
179+
return false
180+
}
157181

158-
hasCustomAlert := strings.Contains(out, "argocd-applicationset-controller") &&
159-
strings.Contains(out, nsNamespaced.Name) &&
160-
strings.Contains(out, "ApplicationSetControllerNotReady") &&
161-
strings.Contains(out, "firing")
182+
hasDefaultAlert := false
183+
hasCustomAlert := false
184+
185+
for _, alert := range resp.Data.Alerts {
186+
if alert.Labels["alertname"] == "ApplicationSetControllerNotReady" && alert.State == "firing" {
187+
if alert.Labels["namespace"] == "openshift-gitops" {
188+
hasDefaultAlert = true
189+
}
190+
if alert.Labels["namespace"] == nsNamespaced.Name {
191+
hasCustomAlert = true
192+
}
193+
}
194+
}
162195

163196
return hasDefaultAlert && hasCustomAlert
164197
}, "15m", "30s").Should(BeTrue(), "Expected ApplicationSetControllerNotReady alerts to reach 'firing' state for both instances")
@@ -168,19 +201,14 @@ var _ = Describe("GitOps Operator Sequential E2E Tests", func() {
168201
argocdFixture.Update(argoCDCluster, func(ac *argov1beta1api.ArgoCD) {
169202
ac.Spec.ApplicationSet.Image = ""
170203
ac.Spec.Monitoring.Enabled = false
171-
ac.Spec.Monitoring.DisableMetrics = ptr.To(true)
172204
})
173205

174206
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(argoCDNamespaced), argoCDNamespaced)).To(Succeed())
175207
argocdFixture.Update(argoCDNamespaced, func(ac *argov1beta1api.ArgoCD) {
176208
ac.Spec.ApplicationSet.Image = ""
177209
ac.Spec.Monitoring.Enabled = false
178-
ac.Spec.Monitoring.DisableMetrics = ptr.To(true)
179210
})
180211

181-
By("deleting cluster monitoring config")
182-
Expect(k8sClient.Delete(ctx, uwmConfigMap)).To(Succeed())
183-
184212
By("verifying PrometheusRules are deleted")
185213
Eventually(ruleCluster, "5m").Should(k8sFixture.NotExistByName(), "Cluster PrometheusRule should be deleted")
186214
Eventually(ruleNamespaced, "5m").Should(k8sFixture.NotExistByName(), "Namespaced PrometheusRule should be deleted")

0 commit comments

Comments
 (0)