@@ -10,6 +10,8 @@ import (
10
10
"testing"
11
11
"time"
12
12
13
+ "github.com/weaveworks/common/httpgrpc"
14
+
13
15
"github.com/prometheus/prometheus/promql/parser"
14
16
"github.com/stretchr/testify/require"
15
17
"github.com/weaveworks/common/middleware"
@@ -304,13 +306,23 @@ func Test_evaluateAtModifier(t *testing.T) {
304
306
start , end = int64 (1546300800 ), int64 (1646300800 )
305
307
)
306
308
for _ , tt := range []struct {
307
- in , expected string
309
+ in , expected string
310
+ expectedErrorCode int
308
311
}{
309
- {"topk(5, rate(http_requests_total[1h] @ start()))" , "topk(5, rate(http_requests_total[1h] @ 1546300.800))" },
310
- {"topk(5, rate(http_requests_total[1h] @ 0))" , "topk(5, rate(http_requests_total[1h] @ 0.000))" },
311
- {"http_requests_total[1h] @ 10.001" , "http_requests_total[1h] @ 10.001" },
312
312
{
313
- `min_over_time(
313
+ in : "topk(5, rate(http_requests_total[1h] @ start()))" ,
314
+ expected : "topk(5, rate(http_requests_total[1h] @ 1546300.800))" ,
315
+ },
316
+ {
317
+ in : "topk(5, rate(http_requests_total[1h] @ 0))" ,
318
+ expected : "topk(5, rate(http_requests_total[1h] @ 0.000))" ,
319
+ },
320
+ {
321
+ in : "http_requests_total[1h] @ 10.001" ,
322
+ expected : "http_requests_total[1h] @ 10.001" ,
323
+ },
324
+ {
325
+ in : `min_over_time(
314
326
sum by(cluster) (
315
327
rate(http_requests_total[5m] @ end())
316
328
)[10m:]
@@ -323,7 +335,7 @@ func Test_evaluateAtModifier(t *testing.T) {
323
335
[5m:1m])
324
336
[2m:])
325
337
[10m:])` ,
326
- `min_over_time(
338
+ expected : `min_over_time(
327
339
sum by(cluster) (
328
340
rate(http_requests_total[5m] @ 1646300.800)
329
341
)[10m:]
@@ -337,15 +349,32 @@ func Test_evaluateAtModifier(t *testing.T) {
337
349
[2m:])
338
350
[10m:])` ,
339
351
},
352
+ {
353
+ // parse error: missing unit character in duration
354
+ in : "http_requests_total[5] @ 10.001" ,
355
+ expectedErrorCode : http .StatusBadRequest ,
356
+ },
357
+ {
358
+ // parse error: @ modifier must be preceded by an instant vector selector or range vector selector or a subquery
359
+ in : "sum(http_requests_total[5m]) @ 10.001" ,
360
+ expectedErrorCode : http .StatusBadRequest ,
361
+ },
340
362
} {
341
363
tt := tt
342
364
t .Run (tt .in , func (t * testing.T ) {
343
365
t .Parallel ()
344
- expectedExpr , err := parser .ParseExpr (tt .expected )
345
- require .NoError (t , err )
346
366
out , err := evaluateAtModifierFunction (tt .in , start , end )
347
- require .NoError (t , err )
348
- require .Equal (t , expectedExpr .String (), out )
367
+ if tt .expectedErrorCode != 0 {
368
+ require .Error (t , err )
369
+ httpResp , ok := httpgrpc .HTTPResponseFromError (err )
370
+ require .True (t , ok , "returned error is not an httpgrpc response" )
371
+ require .Equal (t , tt .expectedErrorCode , int (httpResp .Code ))
372
+ } else {
373
+ require .NoError (t , err )
374
+ expectedExpr , err := parser .ParseExpr (tt .expected )
375
+ require .NoError (t , err )
376
+ require .Equal (t , expectedExpr .String (), out )
377
+ }
349
378
})
350
379
}
351
380
}
0 commit comments