@@ -10,6 +10,8 @@ import (
1010 "testing"
1111 "time"
1212
13+ "github.com/weaveworks/common/httpgrpc"
14+
1315 "github.com/prometheus/prometheus/promql/parser"
1416 "github.com/stretchr/testify/require"
1517 "github.com/weaveworks/common/middleware"
@@ -304,13 +306,23 @@ func Test_evaluateAtModifier(t *testing.T) {
304306 start , end = int64 (1546300800 ), int64 (1646300800 )
305307 )
306308 for _ , tt := range []struct {
307- in , expected string
309+ in , expected string
310+ expectedErrorCode int
308311 }{
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" },
312312 {
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(
314326 sum by(cluster) (
315327 rate(http_requests_total[5m] @ end())
316328 )[10m:]
@@ -323,7 +335,7 @@ func Test_evaluateAtModifier(t *testing.T) {
323335 [5m:1m])
324336 [2m:])
325337 [10m:])` ,
326- `min_over_time(
338+ expected : `min_over_time(
327339 sum by(cluster) (
328340 rate(http_requests_total[5m] @ 1646300.800)
329341 )[10m:]
@@ -337,15 +349,32 @@ func Test_evaluateAtModifier(t *testing.T) {
337349 [2m:])
338350 [10m:])` ,
339351 },
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+ },
340362 } {
341363 tt := tt
342364 t .Run (tt .in , func (t * testing.T ) {
343365 t .Parallel ()
344- expectedExpr , err := parser .ParseExpr (tt .expected )
345- require .NoError (t , err )
346366 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+ }
349378 })
350379 }
351380}
0 commit comments