|
| 1 | +package managementrouter_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + |
| 13 | + monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" |
| 14 | + |
| 15 | + "github.com/openshift/monitoring-plugin/internal/managementrouter" |
| 16 | + "github.com/openshift/monitoring-plugin/pkg/k8s" |
| 17 | + "github.com/openshift/monitoring-plugin/pkg/management" |
| 18 | + "github.com/openshift/monitoring-plugin/pkg/management/testutils" |
| 19 | +) |
| 20 | + |
| 21 | +var _ = Describe("CreateAlertRule", func() { |
| 22 | + var ( |
| 23 | + router http.Handler |
| 24 | + mockK8sRules *testutils.MockPrometheusRuleInterface |
| 25 | + mockARules *testutils.MockAlertingRuleInterface |
| 26 | + mockK8s *testutils.MockClient |
| 27 | + ) |
| 28 | + |
| 29 | + BeforeEach(func() { |
| 30 | + mockK8sRules = &testutils.MockPrometheusRuleInterface{} |
| 31 | + mockARules = &testutils.MockAlertingRuleInterface{} |
| 32 | + mockK8s = &testutils.MockClient{ |
| 33 | + PrometheusRulesFunc: func() k8s.PrometheusRuleInterface { |
| 34 | + return mockK8sRules |
| 35 | + }, |
| 36 | + AlertingRulesFunc: func() k8s.AlertingRuleInterface { |
| 37 | + return mockARules |
| 38 | + }, |
| 39 | + NamespaceFunc: func() k8s.NamespaceInterface { |
| 40 | + return &testutils.MockNamespaceInterface{ |
| 41 | + IsClusterMonitoringNamespaceFunc: func(name string) bool { |
| 42 | + return false |
| 43 | + }, |
| 44 | + } |
| 45 | + }, |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + Context("create new user defined alert rule", func() { |
| 50 | + It("creates a new rule", func() { |
| 51 | + mgmt := management.New(context.Background(), mockK8s) |
| 52 | + router = managementrouter.New(mgmt) |
| 53 | + |
| 54 | + body := map[string]interface{}{ |
| 55 | + "alertingRule": map[string]interface{}{ |
| 56 | + "alert": "cpuHigh", |
| 57 | + "expr": "vector(1)", |
| 58 | + "for": "5m", |
| 59 | + "labels": map[string]string{"severity": "warning"}, |
| 60 | + "annotations": map[string]string{"summary": "cpu high"}, |
| 61 | + }, |
| 62 | + "prometheusRule": map[string]interface{}{ |
| 63 | + "prometheusRuleName": "user-pr", |
| 64 | + "prometheusRuleNamespace": "default", |
| 65 | + }, |
| 66 | + } |
| 67 | + buf, _ := json.Marshal(body) |
| 68 | + |
| 69 | + req := httptest.NewRequest(http.MethodPost, "/api/v1/alerting/rules", bytes.NewReader(buf)) |
| 70 | + req.Header.Set("Content-Type", "application/json") |
| 71 | + w := httptest.NewRecorder() |
| 72 | + router.ServeHTTP(w, req) |
| 73 | + |
| 74 | + Expect(w.Code).To(Equal(http.StatusCreated)) |
| 75 | + var resp struct { |
| 76 | + Id string `json:"id"` |
| 77 | + } |
| 78 | + Expect(json.NewDecoder(w.Body).Decode(&resp)).To(Succeed()) |
| 79 | + Expect(resp.Id).NotTo(BeEmpty()) |
| 80 | + |
| 81 | + pr, found, err := mockK8sRules.Get(context.Background(), "default", "user-pr") |
| 82 | + Expect(err).NotTo(HaveOccurred()) |
| 83 | + Expect(found).To(BeTrue()) |
| 84 | + allAlerts := []string{} |
| 85 | + for _, g := range pr.Spec.Groups { |
| 86 | + for _, r := range g.Rules { |
| 87 | + allAlerts = append(allAlerts, r.Alert) |
| 88 | + } |
| 89 | + } |
| 90 | + Expect(allAlerts).To(ContainElement("cpuHigh")) |
| 91 | + }) |
| 92 | + |
| 93 | + It("creates a new rule into a non-default group when groupName is provided", func() { |
| 94 | + mgmt := management.New(context.Background(), mockK8s) |
| 95 | + router = managementrouter.New(mgmt) |
| 96 | + |
| 97 | + body := map[string]interface{}{ |
| 98 | + "alertingRule": map[string]interface{}{ |
| 99 | + "alert": "cpuCustomGroup", |
| 100 | + "expr": "vector(1)", |
| 101 | + }, |
| 102 | + "prometheusRule": map[string]interface{}{ |
| 103 | + "prometheusRuleName": "user-pr", |
| 104 | + "prometheusRuleNamespace": "default", |
| 105 | + "groupName": "custom-group", |
| 106 | + }, |
| 107 | + } |
| 108 | + buf, _ := json.Marshal(body) |
| 109 | + |
| 110 | + req := httptest.NewRequest(http.MethodPost, "/api/v1/alerting/rules", bytes.NewReader(buf)) |
| 111 | + req.Header.Set("Content-Type", "application/json") |
| 112 | + w := httptest.NewRecorder() |
| 113 | + router.ServeHTTP(w, req) |
| 114 | + |
| 115 | + Expect(w.Code).To(Equal(http.StatusCreated)) |
| 116 | + |
| 117 | + pr, found, err := mockK8sRules.Get(context.Background(), "default", "user-pr") |
| 118 | + Expect(err).NotTo(HaveOccurred()) |
| 119 | + Expect(found).To(BeTrue()) |
| 120 | + |
| 121 | + var grp *monitoringv1.RuleGroup |
| 122 | + for i := range pr.Spec.Groups { |
| 123 | + if pr.Spec.Groups[i].Name == "custom-group" { |
| 124 | + grp = &pr.Spec.Groups[i] |
| 125 | + break |
| 126 | + } |
| 127 | + } |
| 128 | + Expect(grp).NotTo(BeNil()) |
| 129 | + alerts := []string{} |
| 130 | + for _, r := range grp.Rules { |
| 131 | + alerts = append(alerts, r.Alert) |
| 132 | + } |
| 133 | + Expect(alerts).To(ContainElement("cpuCustomGroup")) |
| 134 | + }) |
| 135 | + }) |
| 136 | + |
| 137 | + Context("invalid JSON body", func() { |
| 138 | + It("fails for invalid JSON", func() { |
| 139 | + mgmt := management.New(context.Background(), mockK8s) |
| 140 | + router = managementrouter.New(mgmt) |
| 141 | + |
| 142 | + req := httptest.NewRequest(http.MethodPost, "/api/v1/alerting/rules", bytes.NewBufferString("{")) |
| 143 | + req.Header.Set("Content-Type", "application/json") |
| 144 | + w := httptest.NewRecorder() |
| 145 | + router.ServeHTTP(w, req) |
| 146 | + |
| 147 | + Expect(w.Code).To(Equal(http.StatusBadRequest)) |
| 148 | + Expect(w.Body.String()).To(ContainSubstring("invalid request body")) |
| 149 | + }) |
| 150 | + }) |
| 151 | + |
| 152 | + Context("missing target PrometheusRule (name/namespace)", func() { |
| 153 | + It("fails for missing target PR", func() { |
| 154 | + mgmt := management.New(context.Background(), mockK8s) |
| 155 | + router = managementrouter.New(mgmt) |
| 156 | + |
| 157 | + body := map[string]interface{}{ |
| 158 | + "alertingRule": map[string]interface{}{ |
| 159 | + "alert": "x", |
| 160 | + "expr": "vector(1)", |
| 161 | + }, |
| 162 | + "prometheusRule": map[string]interface{}{ |
| 163 | + // missing PR name/namespace |
| 164 | + }, |
| 165 | + } |
| 166 | + buf, _ := json.Marshal(body) |
| 167 | + |
| 168 | + req := httptest.NewRequest(http.MethodPost, "/api/v1/alerting/rules", bytes.NewReader(buf)) |
| 169 | + req.Header.Set("Content-Type", "application/json") |
| 170 | + w := httptest.NewRecorder() |
| 171 | + router.ServeHTTP(w, req) |
| 172 | + |
| 173 | + Expect(w.Code).To(Equal(http.StatusBadRequest)) |
| 174 | + Expect(w.Body.String()).To(ContainSubstring("PrometheusRule Name and Namespace must be specified")) |
| 175 | + }) |
| 176 | + }) |
| 177 | + |
| 178 | + Context("target is platform-managed PR", func() { |
| 179 | + It("rejects with MethodNotAllowed", func() { |
| 180 | + mockNamespace := &testutils.MockNamespaceInterface{ |
| 181 | + IsClusterMonitoringNamespaceFunc: func(name string) bool { |
| 182 | + return name == "openshift-monitoring" |
| 183 | + }, |
| 184 | + } |
| 185 | + mockK8s.NamespaceFunc = func() k8s.NamespaceInterface { |
| 186 | + return mockNamespace |
| 187 | + } |
| 188 | + mgmt := management.New(context.Background(), mockK8s) |
| 189 | + router = managementrouter.New(mgmt) |
| 190 | + |
| 191 | + body := map[string]interface{}{ |
| 192 | + "alertingRule": map[string]interface{}{ |
| 193 | + "alert": "x", |
| 194 | + "expr": "vector(1)", |
| 195 | + }, |
| 196 | + "prometheusRule": map[string]interface{}{ |
| 197 | + "prometheusRuleName": "platform-pr", |
| 198 | + "prometheusRuleNamespace": "openshift-monitoring", |
| 199 | + }, |
| 200 | + } |
| 201 | + buf, _ := json.Marshal(body) |
| 202 | + |
| 203 | + req := httptest.NewRequest(http.MethodPost, "/api/v1/alerting/rules", bytes.NewReader(buf)) |
| 204 | + req.Header.Set("Content-Type", "application/json") |
| 205 | + w := httptest.NewRecorder() |
| 206 | + router.ServeHTTP(w, req) |
| 207 | + |
| 208 | + Expect(w.Code).To(Equal(http.StatusMethodNotAllowed)) |
| 209 | + Expect(w.Body.String()).To(ContainSubstring("cannot add user-defined alert rule to a platform-managed PrometheusRule")) |
| 210 | + }) |
| 211 | + }) |
| 212 | +}) |
0 commit comments