diff --git a/go.mod b/go.mod index ba0e952..d0420e0 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,6 @@ require ( require ( github.com/onsi/ginkgo/v2 v2.20.2 github.com/onsi/gomega v1.34.2 - github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.52.0 github.com/rancher/rancher/pkg/apis v0.0.0-20240719121207-baeda6b89fe3 k8s.io/kubernetes v1.30.1 ) @@ -72,6 +71,7 @@ require ( github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect + github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.52.0 // indirect github.com/prometheus/client_golang v1.18.0 // indirect github.com/prometheus/client_model v0.5.0 // indirect github.com/prometheus/common v0.45.0 // indirect diff --git a/tests/e2e/monitoring_e2e_test.go b/tests/e2e/monitoring_e2e_test.go index 676c97c..5edc9ed 100644 --- a/tests/e2e/monitoring_e2e_test.go +++ b/tests/e2e/monitoring_e2e_test.go @@ -8,13 +8,9 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" - "github.com/rancher/observability-e2e/tests/helper/charts" + "github.com/rancher/observability-e2e/tests/helper/utils" rancher "github.com/rancher/shepherd/clients/rancher" "github.com/rancher/shepherd/extensions/kubectl" - "github.com/rancher/shepherd/pkg/namegenerator" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/intstr" e2e "k8s.io/kubernetes/test/e2e/framework" ) @@ -43,8 +39,11 @@ type AlertStatus struct { State string `json:"state"` } -const defaultRandStringLength = 5 -const prometheusRulesSteveType = "monitoring.coreos.com.prometheusrule" +const ( + defaultRandStringLength = 5 + prometheusRulesSteveType = "monitoring.coreos.com.prometheusrule" + prometheusRuleFilePath = "../helper/yamls/createPrometheusRule.yaml" +) var ruleLabel = map[string]string{"team": "qa"} @@ -57,6 +56,27 @@ var _ = Describe("Observability Installation Test Suite", func() { Expect(err).NotTo(HaveOccurred()) }) + It("Test : Verify Creating prometheus rule using kubectl", Label("LEVEL1", "monitoring", "E2E"), func() { + + By("1) Apply yaml to create prometheus rule") + prometheusError := utils.DeployPrometheusRule(clientWithSession, prometheusRuleFilePath) + + if prometheusError != nil { + e2e.Logf("Failed to deploy Prometheus rule: %v", prometheusError) + } else { + e2e.Logf("Prometheus Rule deployed successfully!") + } + + By("2) Fetch all the prometheus rule") + fetchPrometheusRules := []string{"kubectl", "get", "prometheusRule", "test-prometheus-rule", "-n", "cattle-monitoring-system"} + verifyPetchPrometheusRules, err := kubectl.Command(clientWithSession, nil, "local", fetchPrometheusRules, "") + if err != nil { + e2e.Failf("Failed to fetch PrometheusRule 'test-prometheus-rule'. Error: %v", err) + } + e2e.Logf("Successfully fetched PrometheusRule: %v", verifyPetchPrometheusRules) + + }) + It("Test : Verify default Watchdog alert is present", Label("LEVEL1", "monitoring", "E2E"), func() { By("1) Create a container to access curl") @@ -75,7 +95,6 @@ var _ = Describe("Observability Installation Test Suite", func() { if err != nil { e2e.Failf("Failed to get curl response. Error: %v", err) } - e2e.Logf("Successfully able to fetch all alerts json . Output") By("3) Unmarshalling json output response") var alerts []Alert @@ -95,8 +114,6 @@ var _ = Describe("Observability Installation Test Suite", func() { By("5)Assert if the Watchdog alert was found ") if watchdogAlert == nil { e2e.Failf("Expected 'Watchdog' alert not found in response") - } else { - e2e.Logf("Found 'Watchdog' alert: %+v\n", watchdogAlert) } defer func() { @@ -112,51 +129,6 @@ var _ = Describe("Observability Installation Test Suite", func() { }) - It("Test : Verify Creating prometheus rule", Label("LEVEL1", "monitoring", "E2E"), func() { - - ruleName := "webhook-rule-" + namegenerator.RandStringLower(defaultRandStringLength) - alertName := "alert-" + namegenerator.RandStringLower(defaultRandStringLength) - - By("1) Client login") - _, err := client.ReLogin() - if err != nil { - e2e.Failf("Failed to relogin. Error: %v", err) - } - By("2) Get the steveclient for the local cluster ") - steveclient, err := client.Steve.ProxyDownstream("local") // Get the steveclient for the local cluster - if err != nil { - e2e.Failf("Error on steveclient: %v", err) - } - - prometheusRule := &monitoringv1.PrometheusRule{ // Create the Prometheus Rule - ObjectMeta: metav1.ObjectMeta{ - Name: ruleName, - Namespace: charts.RancherMonitoringNamespace, - }, - Spec: monitoringv1.PrometheusRuleSpec{ - Groups: []monitoringv1.RuleGroup{ - { - Name: ruleName, - Rules: []monitoringv1.Rule{ - { - Alert: alertName, - Expr: intstr.IntOrString{Type: intstr.String, StrVal: "vector(1)"}, - Labels: ruleLabel, - For: "0s", - }, - }, - }, - }, - }, - } - By("3) Create the Prometheus Rule on local cluster ") - _, err = steveclient.SteveType(prometheusRulesSteveType).Create(prometheusRule) - if err != nil { - e2e.Failf("Error on creation of Prometheus Rule: %v", err) - } - - }) - It("Test : Verify status of rancher-monitoring pods using kubectl", Label("LEVEL1", "monitoring", "E2E"), func() { By("0) Fetch all the pods belongs to rancher-monitoring") @@ -290,7 +262,7 @@ var _ = Describe("Observability Installation Test Suite", func() { e2e.Failf("Failed to unmarshal JSON response. Error: %v", err) } - alertNamePattern := regexp.MustCompile(`alert-`) + alertNamePattern := regexp.MustCompile(`test-qa`) By("4) Searching for the newly created Prometheus rule alert") var prometheusRuleAlert *Alert diff --git a/tests/helper/utils/utils.go b/tests/helper/utils/utils.go new file mode 100644 index 0000000..7c8173a --- /dev/null +++ b/tests/helper/utils/utils.go @@ -0,0 +1,32 @@ +package utils + +import ( + "log" + "os" + + rancher "github.com/rancher/shepherd/clients/rancher" + management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" + "github.com/rancher/shepherd/extensions/kubectl" + e2e "k8s.io/kubernetes/test/e2e/framework" +) + +func DeployPrometheusRule(mySession *rancher.Client, yamlPath string) error { + + yamlContent, err := os.ReadFile(yamlPath) + if err != nil { + log.Fatalf("Failed to read file %s: %v", yamlPath, err) + } + + importYamlInput := &management.ImportClusterYamlInput{ + YAML: string(yamlContent), + } + + apply := []string{"kubectl", "apply", "-f", "/root/.kube/my-pod.yaml"} + prometheusRuleApply, err := kubectl.Command(mySession, importYamlInput, "local", apply, "") + if err != nil { + return err + } + e2e.Logf("Successfully fetchall: %v", prometheusRuleApply) + + return nil +} diff --git a/tests/helper/yamls/createPrometheusRule.yaml b/tests/helper/yamls/createPrometheusRule.yaml new file mode 100644 index 0000000..2bda556 --- /dev/null +++ b/tests/helper/yamls/createPrometheusRule.yaml @@ -0,0 +1,22 @@ +apiVersion: monitoring.coreos.com/v1 +kind: PrometheusRule +metadata: + annotations: + prometheus-operator-validated: 'true' + name: test-prometheus-rule + namespace: cattle-monitoring-system +spec: + groups: + - name: team-qa + rules: + - alert: test-qa-2025 + annotations: + message: Alerts + summary: Validate Summry + expr: vector(1) + for: 0s + labels: + cluster_id: local + cluster_name: local + namespace: cattle-monitoring-system + severity: critical \ No newline at end of file