forked from machadovilaca/monitoring-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules_get_test.go
More file actions
200 lines (160 loc) · 6.45 KB
/
rules_get_test.go
File metadata and controls
200 lines (160 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package managementrouter_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/openshift/monitoring-plugin/internal/managementrouter"
"github.com/openshift/monitoring-plugin/pkg/k8s"
"github.com/openshift/monitoring-plugin/pkg/management"
)
var _ = Describe("GetRules", func() {
var (
mockManagement *stubManagementClient
router http.Handler
)
BeforeEach(func() {
mockManagement = &stubManagementClient{}
router = managementrouter.New(mockManagement)
})
Context("flat label parsing", func() {
It("parses flat query params into Labels map and state", func() {
var captured k8s.GetRulesRequest
mockManagement.getRules = func(ctx context.Context, req k8s.GetRulesRequest) ([]k8s.PrometheusRuleGroup, error) {
captured = req
return []k8s.PrometheusRuleGroup{}, nil
}
req := httptest.NewRequest(http.MethodGet, "/api/v1/alerting/rules?namespace=ns1&severity=critical&state=firing&team=sre", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
Expect(w.Code).To(Equal(http.StatusOK))
Expect(captured.State).To(Equal("firing"))
Expect(captured.Labels["namespace"]).To(Equal("ns1"))
Expect(captured.Labels["severity"]).To(Equal("critical"))
Expect(captured.Labels["team"]).To(Equal("sre"))
})
})
Context("when getting rules without filters", func() {
It("returns groups in response", func() {
mockManagement.getRules = func(ctx context.Context, req k8s.GetRulesRequest) ([]k8s.PrometheusRuleGroup, error) {
return []k8s.PrometheusRuleGroup{
{
Name: "group-a",
},
}, nil
}
req := httptest.NewRequest(http.MethodGet, "/api/v1/alerting/rules", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
Expect(w.Code).To(Equal(http.StatusOK))
Expect(w.Header().Get("Content-Type")).To(Equal("application/json"))
var response managementrouter.GetRulesResponse
err := json.NewDecoder(w.Body).Decode(&response)
Expect(err).NotTo(HaveOccurred())
Expect(response.Data.Groups).To(HaveLen(1))
Expect(response.Data.Groups[0].Name).To(Equal("group-a"))
})
It("returns warnings when user workload Prometheus route is missing", func() {
mockManagement.alertingHealth = func(ctx context.Context) (k8s.AlertingHealth, error) {
return k8s.AlertingHealth{
UserWorkloadEnabled: true,
UserWorkload: &k8s.AlertingStackHealth{
Prometheus: k8s.AlertingRouteHealth{Status: k8s.RouteNotFound},
},
}, nil
}
req := httptest.NewRequest(http.MethodGet, "/api/v1/alerting/rules", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
var response managementrouter.GetRulesResponse
err := json.NewDecoder(w.Body).Decode(&response)
Expect(err).NotTo(HaveOccurred())
Expect(response.Warnings).To(ContainElement("user workload Prometheus route is missing"))
})
It("suppresses warnings when fallback is healthy", func() {
mockManagement.alertingHealth = func(ctx context.Context) (k8s.AlertingHealth, error) {
return k8s.AlertingHealth{
UserWorkloadEnabled: true,
UserWorkload: &k8s.AlertingStackHealth{
Prometheus: k8s.AlertingRouteHealth{
Status: k8s.RouteUnreachable,
FallbackReachable: true,
},
},
}, nil
}
req := httptest.NewRequest(http.MethodGet, "/api/v1/alerting/rules", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
var response managementrouter.GetRulesResponse
err := json.NewDecoder(w.Body).Decode(&response)
Expect(err).NotTo(HaveOccurred())
Expect(response.Warnings).To(BeEmpty())
})
})
Context("when handling errors", func() {
It("returns 500 when GetRules fails", func() {
mockManagement.getRules = func(ctx context.Context, req k8s.GetRulesRequest) ([]k8s.PrometheusRuleGroup, error) {
return nil, fmt.Errorf("connection error")
}
req := httptest.NewRequest(http.MethodGet, "/api/v1/alerting/rules", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
Expect(w.Code).To(Equal(http.StatusInternalServerError))
Expect(w.Body.String()).To(ContainSubstring("An unexpected error occurred"))
})
})
})
type stubManagementClient struct {
getRules func(ctx context.Context, req k8s.GetRulesRequest) ([]k8s.PrometheusRuleGroup, error)
alertingHealth func(ctx context.Context) (k8s.AlertingHealth, error)
}
func (s *stubManagementClient) ListRules(ctx context.Context, prOptions management.PrometheusRuleOptions, arOptions management.AlertRuleOptions) ([]monitoringv1.Rule, error) {
return nil, nil
}
func (s *stubManagementClient) GetRuleById(ctx context.Context, alertRuleId string) (monitoringv1.Rule, error) {
return monitoringv1.Rule{}, nil
}
func (s *stubManagementClient) CreateUserDefinedAlertRule(ctx context.Context, alertRule monitoringv1.Rule, prOptions management.PrometheusRuleOptions) (string, error) {
return "", nil
}
func (s *stubManagementClient) CreatePlatformAlertRule(ctx context.Context, alertRule monitoringv1.Rule) (string, error) {
return "", nil
}
func (s *stubManagementClient) DeleteUserDefinedAlertRuleById(ctx context.Context, alertRuleId string) error {
return nil
}
func (s *stubManagementClient) UpdatePlatformAlertRule(ctx context.Context, alertRuleId string, alertRule monitoringv1.Rule) error {
return nil
}
func (s *stubManagementClient) DropPlatformAlertRule(ctx context.Context, alertRuleId string) error {
return nil
}
func (s *stubManagementClient) RestorePlatformAlertRule(ctx context.Context, alertRuleId string) error {
return nil
}
func (s *stubManagementClient) GetAlerts(ctx context.Context, req k8s.GetAlertsRequest) ([]k8s.PrometheusAlert, error) {
return nil, nil
}
func (s *stubManagementClient) GetRules(ctx context.Context, req k8s.GetRulesRequest) ([]k8s.PrometheusRuleGroup, error) {
if s.getRules != nil {
return s.getRules(ctx, req)
}
return []k8s.PrometheusRuleGroup{}, nil
}
func (s *stubManagementClient) GetAlertingHealth(ctx context.Context) (k8s.AlertingHealth, error) {
if s.alertingHealth != nil {
return s.alertingHealth(ctx)
}
return k8s.AlertingHealth{}, nil
}
func (s *stubManagementClient) UpdateAlertRuleClassification(ctx context.Context, req management.UpdateRuleClassificationRequest) error {
return nil
}
func (s *stubManagementClient) BulkUpdateAlertRuleClassification(ctx context.Context, items []management.UpdateRuleClassificationRequest) []error {
return nil
}