Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Banko <sean.banko@gmail.com>
  • Loading branch information
seanbanko committed Feb 6, 2025
1 parent 3b35b04 commit 734ea73
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 4 deletions.
22 changes: 18 additions & 4 deletions injectproxy/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,11 @@ func validAlerts() http.Handler {

func TestRules(t *testing.T) {
for _, tc := range []struct {
labelv []string
upstream http.Handler
reqHeaders http.Header
opts []Option
labelv []string
upstream http.Handler
upstreamPathPrefix string
reqHeaders http.Header
opts []Option

expCode int
golden string
Expand Down Expand Up @@ -557,10 +558,23 @@ func TestRules(t *testing.T) {
expCode: http.StatusOK,
golden: "rules_with_active_alerts.golden",
},
{
labelv: []string{"ns1"},
upstream: validRules(),
upstreamPathPrefix: "/prometheus",

expCode: http.StatusOK,
golden: "rules_match_namespace_ns1_non_root_upstream.golden",
},
} {
t.Run(fmt.Sprintf("%s=%s", proxyLabel, tc.labelv), func(t *testing.T) {
m := newMockUpstream(tc.upstream)
defer m.Close()

if tc.upstreamPathPrefix != "" {
m.url.Path = tc.upstreamPathPrefix + m.url.Path
}

r, err := NewRoutes(
m.url,
proxyLabel,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"status": "success",
"data": {
"groups": [
{
"name": "group1",
"file": "testdata/rules1.yml",
"rules": [
{
"name": "metric1",
"query": "0",
"labels": {
"namespace": "ns1"
},
"health": "ok",
"evaluationTime": 0.000214303,
"lastEvaluation": "2024-04-29T14:23:52.403557247+02:00",
"type": "recording"
},
{
"name": "metric2",
"query": "1",
"labels": {
"namespace": "ns1",
"operation": "create"
},
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:53.403557247+02:00",
"type": "recording"
},
{
"name": "metric2",
"query": "0",
"labels": {
"namespace": "ns1",
"operation": "update"
},
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:54.403557247+02:00",
"type": "recording"
},
{
"name": "metric2",
"query": "0",
"labels": {
"namespace": "ns1",
"operation": "delete"
},
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:53.603557247+02:00",
"type": "recording"
},
{
"state": "firing",
"name": "Alert1",
"query": "metric1{namespace=\"ns1\"} == 0",
"duration": 0,
"keepFiringFor": 0,
"labels": {
"namespace": "ns1"
},
"annotations": {},
"alerts": [
{
"labels": {
"alertname": "Alert1",
"namespace": "ns1"
},
"annotations": {},
"state": "firing",
"activeAt": "2019-12-18T13:14:44.543981127+01:00",
"value": "0e+00"
}
],
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:53.803557247+02:00",
"type": "alerting"
},
{
"state": "firing",
"name": "Alert2",
"query": "metric2{namespace=\"ns1\"} == 0",
"duration": 0,
"keepFiringFor": 0,
"labels": {
"namespace": "ns1"
},
"annotations": {},
"alerts": [
{
"labels": {
"alertname": "Alert2",
"namespace": "ns1",
"operation": "update"
},
"annotations": {},
"state": "firing",
"activeAt": "2019-12-18T13:14:44.543981127+01:00",
"value": "0e+00"
},
{
"labels": {
"alertname": "Alert2",
"namespace": "ns1",
"operation": "delete"
},
"annotations": {},
"state": "firing",
"activeAt": "2019-12-18T13:14:44.543981127+01:00",
"value": "0e+00"
}
],
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:53.903557247+02:00",
"type": "alerting"
}
],
"interval": 10
}
]
}
}

0 comments on commit 734ea73

Please sign in to comment.