Skip to content

Commit c8ad898

Browse files
committed
add test case
1 parent 3b35b04 commit c8ad898

File tree

2 files changed

+145
-4
lines changed

2 files changed

+145
-4
lines changed

Diff for: injectproxy/rules_test.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,11 @@ func validAlerts() http.Handler {
439439

440440
func TestRules(t *testing.T) {
441441
for _, tc := range []struct {
442-
labelv []string
443-
upstream http.Handler
444-
reqHeaders http.Header
445-
opts []Option
442+
labelv []string
443+
upstream http.Handler
444+
upstreamPathPrefix string
445+
reqHeaders http.Header
446+
opts []Option
446447

447448
expCode int
448449
golden string
@@ -557,10 +558,23 @@ func TestRules(t *testing.T) {
557558
expCode: http.StatusOK,
558559
golden: "rules_with_active_alerts.golden",
559560
},
561+
{
562+
labelv: []string{"ns1"},
563+
upstream: validRules(),
564+
upstreamPathPrefix: "/prometheus",
565+
566+
expCode: http.StatusOK,
567+
golden: "rules_match_namespace_ns1_non_root_upstream.golden",
568+
},
560569
} {
561570
t.Run(fmt.Sprintf("%s=%s", proxyLabel, tc.labelv), func(t *testing.T) {
562571
m := newMockUpstream(tc.upstream)
563572
defer m.Close()
573+
574+
if tc.upstreamPathPrefix != "" {
575+
m.url.Path = tc.upstreamPathPrefix + m.url.Path
576+
}
577+
564578
r, err := NewRoutes(
565579
m.url,
566580
proxyLabel,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
"status": "success",
3+
"data": {
4+
"groups": [
5+
{
6+
"name": "group1",
7+
"file": "testdata/rules1.yml",
8+
"rules": [
9+
{
10+
"name": "metric1",
11+
"query": "0",
12+
"labels": {
13+
"namespace": "ns1"
14+
},
15+
"health": "ok",
16+
"evaluationTime": 0.000214303,
17+
"lastEvaluation": "2024-04-29T14:23:52.403557247+02:00",
18+
"type": "recording"
19+
},
20+
{
21+
"name": "metric2",
22+
"query": "1",
23+
"labels": {
24+
"namespace": "ns1",
25+
"operation": "create"
26+
},
27+
"health": "ok",
28+
"evaluationTime": 0.000214,
29+
"lastEvaluation": "2024-04-29T14:23:53.403557247+02:00",
30+
"type": "recording"
31+
},
32+
{
33+
"name": "metric2",
34+
"query": "0",
35+
"labels": {
36+
"namespace": "ns1",
37+
"operation": "update"
38+
},
39+
"health": "ok",
40+
"evaluationTime": 0.000214,
41+
"lastEvaluation": "2024-04-29T14:23:54.403557247+02:00",
42+
"type": "recording"
43+
},
44+
{
45+
"name": "metric2",
46+
"query": "0",
47+
"labels": {
48+
"namespace": "ns1",
49+
"operation": "delete"
50+
},
51+
"health": "ok",
52+
"evaluationTime": 0.000214,
53+
"lastEvaluation": "2024-04-29T14:23:53.603557247+02:00",
54+
"type": "recording"
55+
},
56+
{
57+
"state": "firing",
58+
"name": "Alert1",
59+
"query": "metric1{namespace=\"ns1\"} == 0",
60+
"duration": 0,
61+
"keepFiringFor": 0,
62+
"labels": {
63+
"namespace": "ns1"
64+
},
65+
"annotations": {},
66+
"alerts": [
67+
{
68+
"labels": {
69+
"alertname": "Alert1",
70+
"namespace": "ns1"
71+
},
72+
"annotations": {},
73+
"state": "firing",
74+
"activeAt": "2019-12-18T13:14:44.543981127+01:00",
75+
"value": "0e+00"
76+
}
77+
],
78+
"health": "ok",
79+
"evaluationTime": 0.000214,
80+
"lastEvaluation": "2024-04-29T14:23:53.803557247+02:00",
81+
"type": "alerting"
82+
},
83+
{
84+
"state": "firing",
85+
"name": "Alert2",
86+
"query": "metric2{namespace=\"ns1\"} == 0",
87+
"duration": 0,
88+
"keepFiringFor": 0,
89+
"labels": {
90+
"namespace": "ns1"
91+
},
92+
"annotations": {},
93+
"alerts": [
94+
{
95+
"labels": {
96+
"alertname": "Alert2",
97+
"namespace": "ns1",
98+
"operation": "update"
99+
},
100+
"annotations": {},
101+
"state": "firing",
102+
"activeAt": "2019-12-18T13:14:44.543981127+01:00",
103+
"value": "0e+00"
104+
},
105+
{
106+
"labels": {
107+
"alertname": "Alert2",
108+
"namespace": "ns1",
109+
"operation": "delete"
110+
},
111+
"annotations": {},
112+
"state": "firing",
113+
"activeAt": "2019-12-18T13:14:44.543981127+01:00",
114+
"value": "0e+00"
115+
}
116+
],
117+
"health": "ok",
118+
"evaluationTime": 0.000214,
119+
"lastEvaluation": "2024-04-29T14:23:53.903557247+02:00",
120+
"type": "alerting"
121+
}
122+
],
123+
"interval": 10
124+
}
125+
]
126+
}
127+
}

0 commit comments

Comments
 (0)