@@ -81,6 +81,7 @@ func TestRuler_PrometheusRules(t *testing.T) {
81
81
}
82
82
83
83
testCases := map [string ]struct {
84
+ tenantID string
84
85
configuredRules rulespb.RuleGroupList
85
86
expectedConfigured int
86
87
expectedStatusCode int
@@ -89,6 +90,7 @@ func TestRuler_PrometheusRules(t *testing.T) {
89
90
queryParams string
90
91
}{
91
92
"should load and evaluate the configured rules" : {
93
+ tenantID : userID ,
92
94
configuredRules : rulespb.RuleGroupList {
93
95
& rulespb.RuleGroupDesc {
94
96
Name : "group1" ,
@@ -133,6 +135,7 @@ func TestRuler_PrometheusRules(t *testing.T) {
133
135
Interval : interval ,
134
136
},
135
137
},
138
+ tenantID : userID ,
136
139
expectedConfigured : 1 ,
137
140
expectedRules : []* RuleGroup {
138
141
{
@@ -168,6 +171,7 @@ func TestRuler_PrometheusRules(t *testing.T) {
168
171
Interval : interval ,
169
172
},
170
173
},
174
+ tenantID : userID ,
171
175
expectedConfigured : 1 ,
172
176
queryParams : "?type=alert" ,
173
177
expectedRules : []* RuleGroup {
@@ -198,6 +202,7 @@ func TestRuler_PrometheusRules(t *testing.T) {
198
202
Interval : interval ,
199
203
},
200
204
},
205
+ tenantID : userID ,
201
206
expectedConfigured : 1 ,
202
207
queryParams : "?type=record" ,
203
208
expectedRules : []* RuleGroup {
@@ -217,20 +222,32 @@ func TestRuler_PrometheusRules(t *testing.T) {
217
222
},
218
223
},
219
224
"Invalid type param" : {
225
+ tenantID : userID ,
220
226
configuredRules : rulespb.RuleGroupList {},
221
227
expectedConfigured : 0 ,
222
228
queryParams : "?type=foo" ,
223
229
expectedStatusCode : http .StatusBadRequest ,
224
230
expectedErrorType : v1 .ErrBadData ,
225
231
expectedRules : []* RuleGroup {},
226
232
},
233
+ "Too many org ids" : {
234
+ tenantID : "user1|user2|user3" ,
235
+ configuredRules : rulespb.RuleGroupList {},
236
+ expectedConfigured : 0 ,
237
+ queryParams : "?type=record" ,
238
+ expectedStatusCode : http .StatusBadRequest ,
239
+ expectedErrorType : v1 .ErrBadData ,
240
+ expectedRules : []* RuleGroup {},
241
+ },
227
242
"when filtering by an unknown namespace then the API returns nothing" : {
243
+ tenantID : userID ,
228
244
configuredRules : makeFilterTestRules (),
229
245
expectedConfigured : len (makeFilterTestRules ()),
230
246
queryParams : "?file=unknown" ,
231
247
expectedRules : []* RuleGroup {},
232
248
},
233
249
"when filtering by a single known namespace then the API returns only rules from that namespace" : {
250
+ tenantID : userID ,
234
251
configuredRules : makeFilterTestRules (),
235
252
expectedConfigured : len (makeFilterTestRules ()),
236
253
queryParams : "?" + url.Values {"file" : []string {namespaceName (1 )}}.Encode (),
@@ -265,6 +282,7 @@ func TestRuler_PrometheusRules(t *testing.T) {
265
282
},
266
283
},
267
284
"when filtering by a multiple known namespaces then the API returns rules from both namespaces" : {
285
+ tenantID : userID ,
268
286
configuredRules : makeFilterTestRules (),
269
287
expectedConfigured : len (makeFilterTestRules ()),
270
288
queryParams : "?" + url.Values {"file" : []string {namespaceName (1 ), namespaceName (2 )}}.Encode (),
@@ -326,12 +344,14 @@ func TestRuler_PrometheusRules(t *testing.T) {
326
344
},
327
345
},
328
346
"when filtering by an unknown group then the API returns nothing" : {
347
+ tenantID : userID ,
329
348
configuredRules : makeFilterTestRules (),
330
349
expectedConfigured : len (makeFilterTestRules ()),
331
350
queryParams : "?rule_group=unknown" ,
332
351
expectedRules : []* RuleGroup {},
333
352
},
334
353
"when filtering by a known group then the API returns only rules from that group" : {
354
+ tenantID : userID ,
335
355
configuredRules : makeFilterTestRules (),
336
356
expectedConfigured : len (makeFilterTestRules ()),
337
357
queryParams : "?" + url.Values {"rule_group" : []string {groupName (2 )}}.Encode (),
@@ -366,6 +386,7 @@ func TestRuler_PrometheusRules(t *testing.T) {
366
386
},
367
387
},
368
388
"when filtering by multiple known groups then the API returns rules from both groups" : {
389
+ tenantID : userID ,
369
390
configuredRules : makeFilterTestRules (),
370
391
expectedConfigured : len (makeFilterTestRules ()),
371
392
queryParams : "?" + url.Values {"rule_group" : []string {groupName (2 ), groupName (3 )}}.Encode (),
@@ -428,12 +449,14 @@ func TestRuler_PrometheusRules(t *testing.T) {
428
449
},
429
450
430
451
"when filtering by an unknown rule name then the API returns all empty groups" : {
452
+ tenantID : userID ,
431
453
configuredRules : makeFilterTestRules (),
432
454
expectedConfigured : len (makeFilterTestRules ()),
433
455
queryParams : "?rule_name=unknown" ,
434
456
expectedRules : []* RuleGroup {},
435
457
},
436
458
"when filtering by a known rule name then the API returns only rules with that name" : {
459
+ tenantID : userID ,
437
460
configuredRules : makeFilterTestRules (),
438
461
expectedConfigured : len (makeFilterTestRules ()),
439
462
queryParams : "?" + url.Values {"rule_name" : []string {"UniqueNamedRuleN1G2" }}.Encode (),
@@ -449,6 +472,7 @@ func TestRuler_PrometheusRules(t *testing.T) {
449
472
},
450
473
},
451
474
"when filtering by multiple known rule names then the API returns both rules" : {
475
+ tenantID : userID ,
452
476
configuredRules : makeFilterTestRules (),
453
477
expectedConfigured : len (makeFilterTestRules ()),
454
478
queryParams : "?" + url.Values {"rule_name" : []string {"UniqueNamedRuleN1G2" , "UniqueNamedRuleN2G3" }}.Encode (),
@@ -472,6 +496,7 @@ func TestRuler_PrometheusRules(t *testing.T) {
472
496
},
473
497
},
474
498
"when filtering by a known namespace and group then the API returns only rules from that namespace and group" : {
499
+ tenantID : userID ,
475
500
configuredRules : makeFilterTestRules (),
476
501
expectedConfigured : len (makeFilterTestRules ()),
477
502
queryParams : "?" + url.Values {
@@ -516,7 +541,7 @@ func TestRuler_PrometheusRules(t *testing.T) {
516
541
517
542
a := NewAPI (r , r .store , log .NewNopLogger ())
518
543
519
- req := requestFor (t , "GET" , "https://localhost:8080/api/prom/api/v1/rules" + tc .queryParams , nil , "user1" )
544
+ req := requestFor (t , "GET" , "https://localhost:8080/api/prom/api/v1/rules" + tc .queryParams , nil , tc . tenantID )
520
545
w := httptest .NewRecorder ()
521
546
a .PrometheusRules (w , req )
522
547
@@ -558,35 +583,63 @@ func TestRuler_PrometheusRules(t *testing.T) {
558
583
func TestRuler_PrometheusAlerts (t * testing.T ) {
559
584
cfg := defaultRulerConfig (t , newMockRuleStore (mockRules ))
560
585
561
- r := newTestRuler (t , cfg )
562
- defer services .StopAndAwaitTerminated (context .Background (), r ) //nolint:errcheck
586
+ tests := []struct {
587
+ name string
588
+ tenantID string
589
+ expectedStatusCode int
590
+ expectedResponse response
591
+ }{
592
+ {
593
+ name : "single org id" ,
594
+ tenantID : "user1" ,
595
+ expectedStatusCode : http .StatusOK ,
596
+ expectedResponse : response {
597
+ Status : "success" ,
598
+ Data : & AlertDiscovery {
599
+ Alerts : []* Alert {},
600
+ },
601
+ },
602
+ },
603
+ {
604
+ name : "multiple org ids" ,
605
+ tenantID : "user1|user2|user3" ,
606
+ expectedStatusCode : http .StatusBadRequest ,
607
+ expectedResponse : response {
608
+ Status : "error" ,
609
+ ErrorType : v1 .ErrBadData ,
610
+ Error : "too many org ids found" ,
611
+ },
612
+ },
613
+ }
563
614
564
- a := NewAPI (r , r .store , log .NewNopLogger ())
615
+ for _ , test := range tests {
616
+ t .Run (test .name , func (t * testing.T ) {
617
+ r := newTestRuler (t , cfg )
618
+ defer services .StopAndAwaitTerminated (context .Background (), r ) //nolint:errcheck
565
619
566
- req := requestFor (t , http .MethodGet , "https://localhost:8080/api/prom/api/v1/alerts" , nil , "user1" )
567
- w := httptest .NewRecorder ()
568
- a .PrometheusAlerts (w , req )
569
-
570
- resp := w .Result ()
571
- body , _ := io .ReadAll (resp .Body )
572
-
573
- // Check status code and status response
574
- responseJSON := response {}
575
- err := json .Unmarshal (body , & responseJSON )
576
- require .NoError (t , err )
577
- require .Equal (t , http .StatusOK , resp .StatusCode )
578
- require .Equal (t , responseJSON .Status , "success" )
579
-
580
- // Currently there is not an easy way to mock firing alerts. The empty
581
- // response case is tested instead.
582
- expectedResponse , _ := json .Marshal (response {
583
- Status : "success" ,
584
- Data : & AlertDiscovery {
585
- Alerts : []* Alert {},
586
- },
587
- })
620
+ a := NewAPI (r , r .store , log .NewNopLogger ())
621
+
622
+ req := requestFor (t , http .MethodGet , "https://localhost:8080/api/prom/api/v1/alerts" , nil , test .tenantID )
623
+ w := httptest .NewRecorder ()
624
+ a .PrometheusAlerts (w , req )
588
625
589
- require .Equal (t , string (expectedResponse ), string (body ))
626
+ resp := w .Result ()
627
+ body , _ := io .ReadAll (resp .Body )
628
+
629
+ // Check status code and status response
630
+ responseJSON := response {}
631
+ err := json .Unmarshal (body , & responseJSON )
632
+ require .NoError (t , err )
633
+ require .Equal (t , test .expectedStatusCode , resp .StatusCode )
634
+ require .Equal (t , test .expectedResponse .Status , responseJSON .Status )
635
+
636
+ // Currently there is not an easy way to mock firing alerts. The empty
637
+ // response case is tested instead.
638
+ expectedResponse , err := json .Marshal (test .expectedResponse )
639
+ require .NoError (t , err )
640
+ require .Equal (t , string (expectedResponse ), string (body ))
641
+ })
642
+ }
590
643
}
591
644
592
645
func TestRuler_GetRulesLabelFilter (t * testing.T ) {
0 commit comments