Skip to content

Commit

Permalink
pkumar| refactoring test for create prometheue rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Pankaj Kumar authored and Pankaj Kumar committed Dec 2, 2024
1 parent 0739c2c commit c4e1ca8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 57 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down
84 changes: 28 additions & 56 deletions tests/e2e/monitoring_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"}

Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions tests/helper/utils/utils.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions tests/helper/yamls/createPrometheusRule.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c4e1ca8

Please sign in to comment.