Skip to content

Commit

Permalink
[query] Fix /m3query returning 500 on invalid query argument (#2916)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpranckaitis authored Nov 18, 2020
1 parent 9cd25c8 commit d975e69
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
39 changes: 27 additions & 12 deletions src/cmd/tools/dtest/docker/harness/query_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,22 @@ type urlTest struct {
}

func TestInvalidInstantQueryReturns400(t *testing.T) {
coord := singleDBNodeDockerResources.Coordinator()
urlPrefixes := []string{"", "prometheus/", "m3query/"}

instantQueryBadRequestTest := []urlTest{
urlTests := addPrefixes(urlPrefixes, []urlTest{
{"missing query", queryURL("query", "")},
{"invalid query", queryURL("query", "@!")},
{"invalid time", queryURL("time", "INVALID")},
{"invalid timeout", queryURL("timeout", "INVALID")},
}
})

for _, tt := range instantQueryBadRequestTest {
t.Run(tt.name, func(t *testing.T) {
assert.NoError(t, coord.RunQuery(verifyStatus(400), tt.url), "for query '%v'", tt.url)
})
}
testInvalidQueryReturns400(t, urlTests)
}

func TestInvalidRangeQueryReturns400(t *testing.T) {
coord := singleDBNodeDockerResources.Coordinator()
urlPrefixes := []string{"", "prometheus/", "m3query/"}

queryBadRequestTest := []urlTest{
urlTests := addPrefixes(urlPrefixes, []urlTest{
{"missing query", queryRangeURL("query", "")},
{"invalid query", queryRangeURL("query", "@!")},
{"missing start", queryRangeURL("start", "")},
Expand All @@ -65,15 +61,34 @@ func TestInvalidRangeQueryReturns400(t *testing.T) {
{"missing step", queryRangeURL("step", "")},
{"invalid step", queryRangeURL("step", "INVALID")},
{"invalid timeout", queryRangeURL("timeout", "INVALID")},
}
})

for _, tt := range queryBadRequestTest {
testInvalidQueryReturns400(t, urlTests)
}

func testInvalidQueryReturns400(t *testing.T, tests []urlTest) {
coord := singleDBNodeDockerResources.Coordinator()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.NoError(t, coord.RunQuery(verifyStatus(400), tt.url), "for query '%v'", tt.url)
})
}
}

func addPrefixes(prefixes []string, tests []urlTest) []urlTest {
res := make([]urlTest, 0)
for _, prefix := range prefixes {
for _, t := range tests {
res = append(res, urlTest{
fmt.Sprintf("%v %v", prefix, t.name),
fmt.Sprintf("%v%v", prefix, t.url),
})
}
}
return res
}

func queryURL(key, value string) string {
params := map[string]string{
"query": "foo",
Expand Down
2 changes: 1 addition & 1 deletion src/query/api/v1/handler/prometheus/native/read_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func read(
parseOpts := engine.Options().ParseOptions()
parser, err := promql.Parse(params.Query, params.Step, tagOpts, parseOpts)
if err != nil {
return emptyResult, err
return emptyResult, xerrors.NewInvalidParamsError(err)
}

// Detect clients closing connections.
Expand Down

0 comments on commit d975e69

Please sign in to comment.