Skip to content

Commit

Permalink
add unit tests for decoding responses
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Ye <ben.ye@bytedance.com>
  • Loading branch information
Ben Ye committed Aug 20, 2022
1 parent 6ea881e commit b0a77fd
Showing 1 changed file with 329 additions and 0 deletions.
329 changes: 329 additions & 0 deletions pkg/queryfrontend/queryinstant_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package queryfrontend

import (
"bytes"
"context"
"io"
"net/http"
"testing"

Expand Down Expand Up @@ -509,3 +511,330 @@ func TestMergeResponse(t *testing.T) {
})
}
}

func TestDecodeResponse(t *testing.T) {
codec := NewThanosQueryInstantCodec(false)
for _, tc := range []struct {
name string
body string
expectedResponse queryrange.Response
expectedErr error
}{
{
name: "empty vector",
body: `{
"status": "success",
"data": {
"resultType": "vector",
"result": [
]
}
}`,
expectedResponse: &queryrange.PrometheusInstantQueryResponse{
Status: queryrange.StatusSuccess,
Headers: []*queryrange.PrometheusResponseHeader{
{Name: "Content-Type", Values: []string{"application/json"}},
},
Data: queryrange.PrometheusInstantQueryData{
ResultType: model.ValVector.String(),
Result: queryrange.PrometheusInstantQueryResult{
Result: &queryrange.PrometheusInstantQueryResult_Vector{
Vector: &queryrange.Vector{
Samples: []*queryrange.Sample{},
},
},
},
},
},
},
{
name: "vector",
body: `{
"status": "success",
"data": {
"resultType": "vector",
"result": [
{
"metric": {
"__name__": "up",
"instance": "localhost:9090",
"job": "prometheus"
},
"value": [
1661020672.043,
"1"
]
}
]
}
}`,
expectedResponse: &queryrange.PrometheusInstantQueryResponse{
Status: queryrange.StatusSuccess,
Headers: []*queryrange.PrometheusResponseHeader{
{Name: "Content-Type", Values: []string{"application/json"}},
},
Data: queryrange.PrometheusInstantQueryData{
ResultType: model.ValVector.String(),
Result: queryrange.PrometheusInstantQueryResult{
Result: &queryrange.PrometheusInstantQueryResult_Vector{
Vector: &queryrange.Vector{
Samples: []*queryrange.Sample{
{
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromMap(map[string]string{
"__name__": "up",
"instance": "localhost:9090",
"job": "prometheus",
})),
Sample: cortexpb.Sample{TimestampMs: 1661020672043, Value: 1},
},
},
},
},
},
},
},
},
{
name: "scalar",
body: `{
"status": "success",
"data": {
"resultType": "scalar",
"result": [
1661020145.547,
"1"
]
}
}`,
expectedResponse: &queryrange.PrometheusInstantQueryResponse{
Status: queryrange.StatusSuccess,
Headers: []*queryrange.PrometheusResponseHeader{
{Name: "Content-Type", Values: []string{"application/json"}},
},
Data: queryrange.PrometheusInstantQueryData{
ResultType: model.ValScalar.String(),
Result: queryrange.PrometheusInstantQueryResult{
Result: &queryrange.PrometheusInstantQueryResult_Scalar{
Scalar: &cortexpb.Sample{TimestampMs: 1661020145547, Value: 1},
},
},
},
},
},
{
name: "string",
body: `{
"status": "success",
"data": {
"resultType": "string",
"result": [
1661020232.424,
"test"
]
}
}`,
expectedResponse: &queryrange.PrometheusInstantQueryResponse{
Status: queryrange.StatusSuccess,
Headers: []*queryrange.PrometheusResponseHeader{
{Name: "Content-Type", Values: []string{"application/json"}},
},
Data: queryrange.PrometheusInstantQueryData{
ResultType: model.ValString.String(),
Result: queryrange.PrometheusInstantQueryResult{
Result: &queryrange.PrometheusInstantQueryResult_StringSample{
StringSample: &queryrange.StringSample{TimestampMs: 1661020232424, Value: "test"},
},
},
},
},
},
{
name: "matrix",
body: `{
"status": "success",
"data": {
"resultType": "matrix",
"result": [
{
"metric": {
"__name__": "up",
"instance": "localhost:9090",
"job": "prometheus"
},
"values": [
[
1661020250.310,
"1"
],
[
1661020265.309,
"1"
],
[
1661020280.309,
"1"
],
[
1661020295.310,
"1"
]
]
}
]
}
}`,
expectedResponse: &queryrange.PrometheusInstantQueryResponse{
Status: queryrange.StatusSuccess,
Headers: []*queryrange.PrometheusResponseHeader{
{Name: "Content-Type", Values: []string{"application/json"}},
},
Data: queryrange.PrometheusInstantQueryData{
ResultType: model.ValMatrix.String(),
Result: queryrange.PrometheusInstantQueryResult{
Result: &queryrange.PrometheusInstantQueryResult_Matrix{
Matrix: &queryrange.Matrix{
SampleStreams: []*queryrange.SampleStream{
{
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromMap(map[string]string{
"__name__": "up",
"instance": "localhost:9090",
"job": "prometheus",
})),
Samples: []cortexpb.Sample{
{TimestampMs: 1661020250310, Value: 1},
{TimestampMs: 1661020265309, Value: 1},
{TimestampMs: 1661020280309, Value: 1},
{TimestampMs: 1661020295310, Value: 1},
},
},
},
},
},
},
},
},
},
{
name: "matrix with multiple metrics",
body: `{
"status": "success",
"data": {
"resultType": "matrix",
"result": [
{
"metric": {
"__name__": "prometheus_http_requests_total",
"code": "200",
"handler": "/favicon.ico",
"instance": "localhost:9090",
"job": "prometheus"
},
"values": [
[
1661020430.311,
"1"
],
[
1661020445.312,
"1"
],
[
1661020460.313,
"1"
],
[
1661020475.313,
"1"
]
]
},
{
"metric": {
"__name__": "prometheus_http_requests_total",
"code": "200",
"handler": "/metrics",
"instance": "localhost:9090",
"job": "prometheus"
},
"values": [
[
1661020430.311,
"33"
],
[
1661020445.312,
"34"
],
[
1661020460.313,
"35"
],
[
1661020475.313,
"36"
]
]
}
]
}
}`,
expectedResponse: &queryrange.PrometheusInstantQueryResponse{
Status: queryrange.StatusSuccess,
Headers: []*queryrange.PrometheusResponseHeader{
{Name: "Content-Type", Values: []string{"application/json"}},
},
Data: queryrange.PrometheusInstantQueryData{
ResultType: model.ValMatrix.String(),
Result: queryrange.PrometheusInstantQueryResult{
Result: &queryrange.PrometheusInstantQueryResult_Matrix{
Matrix: &queryrange.Matrix{
SampleStreams: []*queryrange.SampleStream{
{
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromMap(map[string]string{
"__name__": "prometheus_http_requests_total",
"code": "200",
"handler": "/favicon.ico",
"instance": "localhost:9090",
"job": "prometheus",
})),
Samples: []cortexpb.Sample{
{TimestampMs: 1661020430311, Value: 1},
{TimestampMs: 1661020445312, Value: 1},
{TimestampMs: 1661020460313, Value: 1},
{TimestampMs: 1661020475313, Value: 1},
},
},
{
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromMap(map[string]string{
"__name__": "prometheus_http_requests_total",
"code": "200",
"handler": "/metrics",
"instance": "localhost:9090",
"job": "prometheus",
})),
Samples: []cortexpb.Sample{
{TimestampMs: 1661020430311, Value: 33},
{TimestampMs: 1661020445312, Value: 34},
{TimestampMs: 1661020460313, Value: 35},
{TimestampMs: 1661020475313, Value: 36},
},
},
},
},
},
},
},
},
},
} {
resp := &http.Response{
StatusCode: 200,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(bytes.NewBuffer([]byte(tc.body))),
}
gotResponse, err := codec.DecodeResponse(context.Background(), resp, nil)
testutil.Equals(t, tc.expectedErr, err)
testutil.Equals(t, tc.expectedResponse, gotResponse)
}
}

0 comments on commit b0a77fd

Please sign in to comment.