-
Notifications
You must be signed in to change notification settings - Fork 455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[query] Fix nil panic for error response for PromReadInstantHandler #1611
Changes from all commits
2f48fb5
551586d
f07eb0b
49a0d17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// Copyright (c) 2019 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package native | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/m3db/m3/src/query/block" | ||
"github.com/m3db/m3/src/query/test" | ||
"github.com/m3db/m3/src/query/util/logging" | ||
xtest "github.com/m3db/m3/src/x/test" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: newline here |
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type vectorResult struct { | ||
Data struct { | ||
Result []struct { | ||
Metric map[string]string `json:"metric"` | ||
Value vectorResultValues `json:"value"` | ||
} `json:"result"` | ||
} `json:"data"` | ||
} | ||
|
||
type vectorResultValues []interface{} | ||
|
||
func (v vectorResultValues) parse() (time.Time, int, error) { | ||
if len(v) != 2 { | ||
return time.Time{}, 0, | ||
fmt.Errorf("expected length 2: actual=%d", len(v)) | ||
} | ||
|
||
t, ok := v[0].(float64) | ||
if !ok { | ||
return time.Time{}, 0, | ||
fmt.Errorf("could not unmarshal time: %v", v[0]) | ||
} | ||
|
||
str, ok := v[1].(string) | ||
if !ok { | ||
return time.Time{}, 0, | ||
fmt.Errorf("could not unmarshal value: %v", v[1]) | ||
} | ||
|
||
n, err := strconv.Atoi(str) | ||
if err != nil { | ||
return time.Time{}, 0, | ||
fmt.Errorf("could not convert value to number: err=%v", err) | ||
} | ||
|
||
return time.Unix(int64(t), 0), n, nil | ||
} | ||
|
||
func TestPromReadInstantHandler(t *testing.T) { | ||
logging.InitWithCores(nil) | ||
|
||
values, bounds := test.GenerateValuesAndBounds(nil, nil) | ||
|
||
setup := newTestSetup() | ||
promReadInstant := setup.Handlers.InstantRead | ||
|
||
b := test.NewBlockFromValues(bounds, values) | ||
setup.Storage.SetFetchBlocksResult(block.Result{Blocks: []block.Block{b}}, nil) | ||
|
||
req := httptest.NewRequest(PromReadInstantHTTPMethod, PromReadInstantURL, nil) | ||
|
||
params := url.Values{} | ||
params.Set(queryParam, "dummy0{}") | ||
|
||
req.URL.RawQuery = params.Encode() | ||
|
||
recorder := httptest.NewRecorder() | ||
promReadInstant.ServeHTTP(recorder, req) | ||
|
||
require.Equal(t, http.StatusOK, recorder.Result().StatusCode) | ||
|
||
var result vectorResult | ||
require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &result)) | ||
require.Equal(t, 2, len(result.Data.Result)) | ||
|
||
at0, value0, err := result.Data.Result[0].Value.parse() | ||
require.NoError(t, err) | ||
at1, value1, err := result.Data.Result[1].Value.parse() | ||
require.NoError(t, err) | ||
|
||
expected := mustPrettyJSON(t, fmt.Sprintf(` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Think it may be a little cleaner to flip this around; have expected be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure yeah. |
||
{ | ||
"status": "success", | ||
"data": { | ||
"resultType": "vector", | ||
"result": [ | ||
{ | ||
"metric": { | ||
"__name__": "dummy0", | ||
"dummy0": "dummy0" | ||
}, | ||
"value": [ | ||
%d, | ||
"%d" | ||
] | ||
}, | ||
{ | ||
"metric": { | ||
"__name__": "dummy1", | ||
"dummy1": "dummy1" | ||
}, | ||
"value": [ | ||
%d, | ||
"%d" | ||
] | ||
} | ||
] | ||
} | ||
} | ||
`, at0.Unix(), value0, at1.Unix(), value1)) | ||
actual := mustPrettyJSON(t, recorder.Body.String()) | ||
assert.Equal(t, expected, actual, xtest.Diff(expected, actual)) | ||
} | ||
|
||
func TestPromReadInstantHandlerStorageError(t *testing.T) { | ||
logging.InitWithCores(nil) | ||
|
||
setup := newTestSetup() | ||
promReadInstant := setup.Handlers.InstantRead | ||
|
||
storageErr := fmt.Errorf("storage err") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
setup.Storage.SetFetchBlocksResult(block.Result{}, storageErr) | ||
|
||
req := httptest.NewRequest(PromReadInstantHTTPMethod, PromReadInstantURL, nil) | ||
|
||
params := url.Values{} | ||
params.Set(queryParam, "dummy0{}") | ||
|
||
req.URL.RawQuery = params.Encode() | ||
|
||
recorder := httptest.NewRecorder() | ||
promReadInstant.ServeHTTP(recorder, req) | ||
|
||
require.Equal(t, http.StatusInternalServerError, recorder.Result().StatusCode) | ||
|
||
var errResp struct { | ||
Error string `json:"error"` | ||
} | ||
resp := recorder.Body.Bytes() | ||
require.NoError(t, json.Unmarshal(resp, &errResp)) | ||
require.Equal(t, storageErr.Error(), errResp.Error) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♂