@@ -14,23 +14,23 @@ import (
14
14
"github.com/go-kit/log"
15
15
"github.com/gorilla/mux"
16
16
"github.com/grafana/regexp"
17
+ "github.com/prometheus/client_golang/prometheus"
17
18
"github.com/prometheus/common/model"
18
19
"github.com/prometheus/prometheus/model/labels"
19
20
"github.com/prometheus/prometheus/promql"
21
+ "github.com/prometheus/prometheus/promql/parser"
20
22
"github.com/prometheus/prometheus/storage"
21
23
"github.com/prometheus/prometheus/util/annotations"
22
24
v1 "github.com/prometheus/prometheus/web/api/v1"
23
25
"github.com/stretchr/testify/require"
26
+ "github.com/thanos-io/promql-engine/logicalplan"
27
+ "github.com/thanos-io/promql-engine/query"
24
28
"github.com/weaveworks/common/user"
25
29
26
30
engine2 "github.com/cortexproject/cortex/pkg/engine"
27
31
"github.com/cortexproject/cortex/pkg/querier"
28
32
"github.com/cortexproject/cortex/pkg/querier/series"
29
33
"github.com/cortexproject/cortex/pkg/querier/stats"
30
- "github.com/prometheus/client_golang/prometheus"
31
- "github.com/prometheus/prometheus/promql/parser"
32
- "github.com/thanos-io/promql-engine/logicalplan"
33
- "github.com/thanos-io/promql-engine/query"
34
34
)
35
35
36
36
type mockSampleAndChunkQueryable struct {
@@ -182,7 +182,7 @@ func Test_CustomAPI(t *testing.T) {
182
182
183
183
for _ , test := range tests {
184
184
t .Run (test .name , func (t * testing.T ) {
185
- c := NewQueryAPI (* engine , mockQueryable , querier .StatsRenderer , log .NewNopLogger (), []v1.Codec {v1.JSONCodec {}}, regexp .MustCompile (".*" ))
185
+ c := NewQueryAPI (engine , mockQueryable , querier .StatsRenderer , log .NewNopLogger (), []v1.Codec {v1.JSONCodec {}}, regexp .MustCompile (".*" ))
186
186
187
187
router := mux .NewRouter ()
188
188
router .Path ("/api/v1/query" ).Methods ("POST" ).Handler (c .Wrap (c .InstantQueryHandler ))
@@ -243,7 +243,7 @@ func Test_InvalidCodec(t *testing.T) {
243
243
},
244
244
}
245
245
246
- queryAPI := NewQueryAPI (* engine , mockQueryable , querier .StatsRenderer , log .NewNopLogger (), []v1.Codec {& mockCodec {}}, regexp .MustCompile (".*" ))
246
+ queryAPI := NewQueryAPI (engine , mockQueryable , querier .StatsRenderer , log .NewNopLogger (), []v1.Codec {& mockCodec {}}, regexp .MustCompile (".*" ))
247
247
router := mux .NewRouter ()
248
248
router .Path ("/api/v1/query" ).Methods ("POST" ).Handler (queryAPI .Wrap (queryAPI .InstantQueryHandler ))
249
249
@@ -284,7 +284,7 @@ func Test_CustomAPI_StatsRenderer(t *testing.T) {
284
284
},
285
285
}
286
286
287
- queryAPI := NewQueryAPI (* engine , mockQueryable , querier .StatsRenderer , log .NewNopLogger (), []v1.Codec {v1.JSONCodec {}}, regexp .MustCompile (".*" ))
287
+ queryAPI := NewQueryAPI (engine , mockQueryable , querier .StatsRenderer , log .NewNopLogger (), []v1.Codec {v1.JSONCodec {}}, regexp .MustCompile (".*" ))
288
288
289
289
router := mux .NewRouter ()
290
290
router .Path ("/api/v1/query_range" ).Methods ("POST" ).Handler (queryAPI .Wrap (queryAPI .RangeQueryHandler ))
@@ -314,7 +314,6 @@ func Test_Logicalplan_Requests(t *testing.T) {
314
314
prometheus .NewRegistry (),
315
315
)
316
316
317
- // TODO: find a way to include two different-metric queryables
318
317
mockMatrix := model.Matrix {
319
318
{
320
319
Metric : model.Metric {"__name__" : "test" , "foo" : "bar" },
@@ -342,8 +341,8 @@ func Test_Logicalplan_Requests(t *testing.T) {
342
341
expectedBody string
343
342
}{
344
343
{
345
- name : "[Range Query] with valid logical plan" ,
346
- path : "/api/v1/query_range?end=1536673680&query=test &start=1536673665&step=5" ,
344
+ name : "[Range Query] with valid logical plan and empty query string " ,
345
+ path : "/api/v1/query_range?end=1536673680&query=&start=1536673665&step=5" ,
347
346
start : 1536673665 ,
348
347
end : 1536673680 ,
349
348
stepDuration : 5 ,
@@ -366,7 +365,7 @@ func Test_Logicalplan_Requests(t *testing.T) {
366
365
expectedBody : `{"status":"error","errorType":"server_error","error":"invalid character 'r' after top-level value"}` ,
367
366
},
368
367
{
369
- name : "[Range Query] with empty body" , // will fall back promql query execution
368
+ name : "[Range Query] with empty body and non-empty query string " , // fall back to promql query execution
370
369
path : "/api/v1/query_range?end=1536673680&query=test&start=1536673665&step=5" ,
371
370
start : 1536673665 ,
372
371
end : 1536673680 ,
@@ -378,7 +377,19 @@ func Test_Logicalplan_Requests(t *testing.T) {
378
377
expectedBody : `{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"test","foo":"bar"},"values":[[1536673665,"0"],[1536673670,"1"],[1536673675,"1"],[1536673680,"1"]]}]}}` ,
379
378
},
380
379
{
381
- name : "[Instant Query] with valid logical plan" ,
380
+ name : "[Range Query] with empty body and empty query string" , // fall back to promql query execution, but will have error because of empty query string
381
+ path : "/api/v1/query_range?end=1536673680&query=&start=1536673665&step=5" ,
382
+ start : 1536673665 ,
383
+ end : 1536673680 ,
384
+ stepDuration : 5 ,
385
+ requestBody : func (t * testing.T ) []byte {
386
+ return []byte {}
387
+ },
388
+ expectedCode : http .StatusBadRequest ,
389
+ expectedBody : "{\" status\" :\" error\" ,\" errorType\" :\" bad_data\" ,\" error\" :\" invalid parameter \\ \" query\\ \" ; unknown position: parse error: no expression found in input\" }" ,
390
+ },
391
+ {
392
+ name : "[Instant Query] with valid logical plan and empty query string" ,
382
393
path : "/api/v1/query?query=test&time=1536673670" ,
383
394
start : 1536673670 ,
384
395
end : 1536673670 ,
@@ -402,7 +413,7 @@ func Test_Logicalplan_Requests(t *testing.T) {
402
413
expectedBody : `{"status":"error","errorType":"server_error","error":"invalid character 'r' after top-level value"}` ,
403
414
},
404
415
{
405
- name : "[Instant Query] with empty body" ,
416
+ name : "[Instant Query] with empty body and non-empty query string " ,
406
417
path : "/api/v1/query?query=test&time=1536673670" ,
407
418
start : 1536673670 ,
408
419
end : 1536673670 ,
@@ -413,11 +424,23 @@ func Test_Logicalplan_Requests(t *testing.T) {
413
424
expectedCode : http .StatusOK ,
414
425
expectedBody : `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"test","foo":"bar"},"value":[1536673670,"1"]}]}}` ,
415
426
},
427
+ {
428
+ name : "[Instant Query] with empty body and empty query string" ,
429
+ path : "/api/v1/query?query=&time=1536673670" ,
430
+ start : 1536673670 ,
431
+ end : 1536673670 ,
432
+ stepDuration : 0 ,
433
+ requestBody : func (t * testing.T ) []byte {
434
+ return []byte {}
435
+ },
436
+ expectedCode : http .StatusBadRequest ,
437
+ expectedBody : "{\" status\" :\" error\" ,\" errorType\" :\" bad_data\" ,\" error\" :\" invalid parameter \\ \" query\\ \" ; unknown position: parse error: no expression found in input\" }" ,
438
+ },
416
439
}
417
440
418
441
for _ , tt := range tests {
419
442
t .Run (tt .name , func (t * testing.T ) {
420
- c := NewQueryAPI (* engine , mockQueryable , querier .StatsRenderer , log .NewNopLogger (), []v1.Codec {v1.JSONCodec {}}, regexp .MustCompile (".*" ))
443
+ c := NewQueryAPI (engine , mockQueryable , querier .StatsRenderer , log .NewNopLogger (), []v1.Codec {v1.JSONCodec {}}, regexp .MustCompile (".*" ))
421
444
router := mux .NewRouter ()
422
445
router .Path ("/api/v1/query" ).Methods ("POST" ).Handler (c .Wrap (c .InstantQueryHandler ))
423
446
router .Path ("/api/v1/query_range" ).Methods ("POST" ).Handler (c .Wrap (c .RangeQueryHandler ))
0 commit comments