Ensure tests verify request params (#1047)

* Ensure tests verify request params

Signed-off-by: Joseph Woodward <joseph.woodward@xeuse.com>

* Fix error message

Signed-off-by: Joseph Woodward <joseph.woodward@xeuse.com>

* gofumpt-ed with extra.

Signed-off-by: bwplotka <bwplotka@gmail.com>

Co-authored-by: bwplotka <bwplotka@gmail.com>
This commit is contained in:
Joseph Woodward 2022-08-02 10:24:17 +01:00 committed by GitHub
parent 807b1ee73c
commit 44ce5e1ee5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 134 additions and 62 deletions

View File

@ -64,7 +64,7 @@ func (c *apiTestClient) URL(ep string, args map[string]string) *url.URL {
return u return u
} }
func (c *apiTestClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Warnings, error) { func (c *apiTestClient) Do(_ context.Context, req *http.Request) (*http.Response, []byte, Warnings, error) {
test := c.curTest test := c.curTest
if req.URL.Path != test.reqPath { if req.URL.Path != test.reqPath {
@ -74,6 +74,25 @@ func (c *apiTestClient) Do(ctx context.Context, req *http.Request) (*http.Respon
c.Errorf("unexpected request method: want %s, got %s", test.reqMethod, req.Method) c.Errorf("unexpected request method: want %s, got %s", test.reqMethod, req.Method)
} }
var vals url.Values
switch test.reqMethod {
case http.MethodGet:
if req.URL.RawQuery != "" {
vals = req.URL.Query()
}
case http.MethodPost:
if req.Body != nil {
reqBody, _ := io.ReadAll(req.Body)
vals, _ = url.ParseQuery(string(reqBody))
} else if req.URL.RawQuery != "" {
vals = req.URL.Query()
}
}
if !reflect.DeepEqual(vals, test.reqParam) {
c.Fatalf("unexpected request parameters: want %s, got %s", vals, test.reqParam)
}
b, err := json.Marshal(test.inRes) b, err := json.Marshal(test.inRes)
if err != nil { if err != nil {
c.Fatal(err) c.Fatal(err)
@ -156,15 +175,15 @@ func TestAPIs(t *testing.T) {
} }
} }
doLabelNames := func(matches []string) func() (interface{}, Warnings, error) { doLabelNames := func(matches []string, startTime, endTime time.Time) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) { return func() (interface{}, Warnings, error) {
return promAPI.LabelNames(context.Background(), matches, time.Now().Add(-100*time.Hour), time.Now()) return promAPI.LabelNames(context.Background(), matches, startTime, endTime)
} }
} }
doLabelValues := func(matches []string, label string) func() (interface{}, Warnings, error) { doLabelValues := func(matches []string, label string, startTime, endTime time.Time) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) { return func() (interface{}, Warnings, error) {
return promAPI.LabelValues(context.Background(), label, matches, time.Now().Add(-100*time.Hour), time.Now()) return promAPI.LabelValues(context.Background(), label, matches, startTime, endTime)
} }
} }
@ -257,7 +276,7 @@ func TestAPIs(t *testing.T) {
reqPath: "/api/v1/query", reqPath: "/api/v1/query",
reqParam: url.Values{ reqParam: url.Values{
"query": []string{"2"}, "query": []string{"2"},
"time": []string{testTime.Format(time.RFC3339Nano)}, "time": []string{formatTime(testTime)},
"timeout": []string{(5 * time.Second).String()}, "timeout": []string{(5 * time.Second).String()},
}, },
res: &model.Scalar{ res: &model.Scalar{
@ -273,7 +292,7 @@ func TestAPIs(t *testing.T) {
reqPath: "/api/v1/query", reqPath: "/api/v1/query",
reqParam: url.Values{ reqParam: url.Values{
"query": []string{"2"}, "query": []string{"2"},
"time": []string{testTime.Format(time.RFC3339Nano)}, "time": []string{formatTime(testTime)},
}, },
err: fmt.Errorf("some error"), err: fmt.Errorf("some error"),
}, },
@ -291,7 +310,7 @@ func TestAPIs(t *testing.T) {
reqPath: "/api/v1/query", reqPath: "/api/v1/query",
reqParam: url.Values{ reqParam: url.Values{
"query": []string{"2"}, "query": []string{"2"},
"time": []string{testTime.Format(time.RFC3339Nano)}, "time": []string{formatTime(testTime)},
}, },
err: errors.New("server_error: server error: 500"), err: errors.New("server_error: server error: 500"),
}, },
@ -309,7 +328,7 @@ func TestAPIs(t *testing.T) {
reqPath: "/api/v1/query", reqPath: "/api/v1/query",
reqParam: url.Values{ reqParam: url.Values{
"query": []string{"2"}, "query": []string{"2"},
"time": []string{testTime.Format(time.RFC3339Nano)}, "time": []string{formatTime(testTime)},
}, },
err: errors.New("client_error: client error: 404"), err: errors.New("client_error: client error: 404"),
}, },
@ -329,7 +348,7 @@ func TestAPIs(t *testing.T) {
reqPath: "/api/v1/query", reqPath: "/api/v1/query",
reqParam: url.Values{ reqParam: url.Values{
"query": []string{"2"}, "query": []string{"2"},
"time": []string{testTime.Format(time.RFC3339Nano)}, "time": []string{formatTime(testTime)},
}, },
res: &model.Scalar{ res: &model.Scalar{
Value: 2, Value: 2,
@ -353,7 +372,7 @@ func TestAPIs(t *testing.T) {
reqPath: "/api/v1/query", reqPath: "/api/v1/query",
reqParam: url.Values{ reqParam: url.Values{
"query": []string{"2"}, "query": []string{"2"},
"time": []string{testTime.Format(time.RFC3339Nano)}, "time": []string{formatTime(testTime)},
}, },
err: errors.New("client_error: client error: 404"), err: errors.New("client_error: client error: 404"),
warnings: []string{"warning"}, warnings: []string{"warning"},
@ -363,7 +382,7 @@ func TestAPIs(t *testing.T) {
do: doQueryRange("2", Range{ do: doQueryRange("2", Range{
Start: testTime.Add(-time.Minute), Start: testTime.Add(-time.Minute),
End: testTime, End: testTime,
Step: time.Minute, Step: 1 * time.Minute,
}, WithTimeout(5*time.Second)), }, WithTimeout(5*time.Second)),
inErr: fmt.Errorf("some error"), inErr: fmt.Errorf("some error"),
@ -371,95 +390,135 @@ func TestAPIs(t *testing.T) {
reqPath: "/api/v1/query_range", reqPath: "/api/v1/query_range",
reqParam: url.Values{ reqParam: url.Values{
"query": []string{"2"}, "query": []string{"2"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)}, "start": []string{formatTime(testTime.Add(-time.Minute))},
"end": []string{testTime.Format(time.RFC3339Nano)}, "end": []string{formatTime(testTime)},
"step": []string{time.Minute.String()}, "step": []string{"60"},
"timeout": []string{(5 * time.Second).String()}, "timeout": []string{(5 * time.Second).String()},
}, },
err: fmt.Errorf("some error"), err: fmt.Errorf("some error"),
}, },
{ {
do: doLabelNames(nil), do: doLabelNames(nil, testTime.Add(-100*time.Hour), testTime),
inRes: []string{"val1", "val2"}, inRes: []string{"val1", "val2"},
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/labels", reqPath: "/api/v1/labels",
reqParam: url.Values{
"start": []string{formatTime(testTime.Add(-100 * time.Hour))},
"end": []string{formatTime(testTime)},
},
res: []string{"val1", "val2"}, res: []string{"val1", "val2"},
}, },
{ {
do: doLabelNames(nil), do: doLabelNames(nil, testTime.Add(-100*time.Hour), testTime),
inRes: []string{"val1", "val2"}, inRes: []string{"val1", "val2"},
inWarnings: []string{"a"}, inWarnings: []string{"a"},
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/labels", reqPath: "/api/v1/labels",
reqParam: url.Values{
"start": []string{formatTime(testTime.Add(-100 * time.Hour))},
"end": []string{formatTime(testTime)},
},
res: []string{"val1", "val2"}, res: []string{"val1", "val2"},
warnings: []string{"a"}, warnings: []string{"a"},
}, },
{ {
do: doLabelNames(nil), do: doLabelNames(nil, testTime.Add(-100*time.Hour), testTime),
inErr: fmt.Errorf("some error"), inErr: fmt.Errorf("some error"),
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/labels", reqPath: "/api/v1/labels",
reqParam: url.Values{
"start": []string{formatTime(testTime.Add(-100 * time.Hour))},
"end": []string{formatTime(testTime)},
},
err: fmt.Errorf("some error"), err: fmt.Errorf("some error"),
}, },
{ {
do: doLabelNames(nil), do: doLabelNames(nil, testTime.Add(-100*time.Hour), testTime),
inErr: fmt.Errorf("some error"), inErr: fmt.Errorf("some error"),
inWarnings: []string{"a"}, inWarnings: []string{"a"},
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/labels", reqPath: "/api/v1/labels",
reqParam: url.Values{
"start": []string{formatTime(testTime.Add(-100 * time.Hour))},
"end": []string{formatTime(testTime)},
},
err: fmt.Errorf("some error"), err: fmt.Errorf("some error"),
warnings: []string{"a"}, warnings: []string{"a"},
}, },
{ {
do: doLabelNames([]string{"up"}), do: doLabelNames([]string{"up"}, testTime.Add(-100*time.Hour), testTime),
inRes: []string{"val1", "val2"}, inRes: []string{"val1", "val2"},
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/labels", reqPath: "/api/v1/labels",
reqParam: url.Values{"match[]": {"up"}}, reqParam: url.Values{
"match[]": {"up"},
"start": []string{formatTime(testTime.Add(-100 * time.Hour))},
"end": []string{formatTime(testTime)},
},
res: []string{"val1", "val2"}, res: []string{"val1", "val2"},
}, },
{ {
do: doLabelValues(nil, "mylabel"), do: doLabelValues(nil, "mylabel", testTime.Add(-100*time.Hour), testTime),
inRes: []string{"val1", "val2"}, inRes: []string{"val1", "val2"},
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/label/mylabel/values", reqPath: "/api/v1/label/mylabel/values",
reqParam: url.Values{
"start": []string{formatTime(testTime.Add(-100 * time.Hour))},
"end": []string{formatTime(testTime)},
},
res: model.LabelValues{"val1", "val2"}, res: model.LabelValues{"val1", "val2"},
}, },
{ {
do: doLabelValues(nil, "mylabel"), do: doLabelValues(nil, "mylabel", testTime.Add(-100*time.Hour), testTime),
inRes: []string{"val1", "val2"}, inRes: []string{"val1", "val2"},
inWarnings: []string{"a"}, inWarnings: []string{"a"},
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/label/mylabel/values", reqPath: "/api/v1/label/mylabel/values",
reqParam: url.Values{
"start": []string{formatTime(testTime.Add(-100 * time.Hour))},
"end": []string{formatTime(testTime)},
},
res: model.LabelValues{"val1", "val2"}, res: model.LabelValues{"val1", "val2"},
warnings: []string{"a"}, warnings: []string{"a"},
}, },
{ {
do: doLabelValues(nil, "mylabel"), do: doLabelValues(nil, "mylabel", testTime.Add(-100*time.Hour), testTime),
inErr: fmt.Errorf("some error"), inErr: fmt.Errorf("some error"),
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/label/mylabel/values", reqPath: "/api/v1/label/mylabel/values",
reqParam: url.Values{
"start": []string{formatTime(testTime.Add(-100 * time.Hour))},
"end": []string{formatTime(testTime)},
},
err: fmt.Errorf("some error"), err: fmt.Errorf("some error"),
}, },
{ {
do: doLabelValues(nil, "mylabel"), do: doLabelValues(nil, "mylabel", testTime.Add(-100*time.Hour), testTime),
inErr: fmt.Errorf("some error"), inErr: fmt.Errorf("some error"),
inWarnings: []string{"a"}, inWarnings: []string{"a"},
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/label/mylabel/values", reqPath: "/api/v1/label/mylabel/values",
reqParam: url.Values{
"start": []string{formatTime(testTime.Add(-100 * time.Hour))},
"end": []string{formatTime(testTime)},
},
err: fmt.Errorf("some error"), err: fmt.Errorf("some error"),
warnings: []string{"a"}, warnings: []string{"a"},
}, },
{ {
do: doLabelValues([]string{"up"}, "mylabel"), do: doLabelValues([]string{"up"}, "mylabel", testTime.Add(-100*time.Hour), testTime),
inRes: []string{"val1", "val2"}, inRes: []string{"val1", "val2"},
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/label/mylabel/values", reqPath: "/api/v1/label/mylabel/values",
reqParam: url.Values{"match[]": {"up"}}, reqParam: url.Values{
"match[]": {"up"},
"start": []string{formatTime(testTime.Add(-100 * time.Hour))},
"end": []string{formatTime(testTime)},
},
res: model.LabelValues{"val1", "val2"}, res: model.LabelValues{"val1", "val2"},
}, },
@ -475,9 +534,9 @@ func TestAPIs(t *testing.T) {
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/series", reqPath: "/api/v1/series",
reqParam: url.Values{ reqParam: url.Values{
"match": []string{"up"}, "match[]": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)}, "start": []string{formatTime(testTime.Add(-time.Minute))},
"end": []string{testTime.Format(time.RFC3339Nano)}, "end": []string{formatTime(testTime)},
}, },
res: []model.LabelSet{ res: []model.LabelSet{
{ {
@ -501,9 +560,9 @@ func TestAPIs(t *testing.T) {
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/series", reqPath: "/api/v1/series",
reqParam: url.Values{ reqParam: url.Values{
"match": []string{"up"}, "match[]": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)}, "start": []string{formatTime(testTime.Add(-time.Minute))},
"end": []string{testTime.Format(time.RFC3339Nano)}, "end": []string{formatTime(testTime)},
}, },
res: []model.LabelSet{ res: []model.LabelSet{
{ {
@ -521,9 +580,9 @@ func TestAPIs(t *testing.T) {
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/series", reqPath: "/api/v1/series",
reqParam: url.Values{ reqParam: url.Values{
"match": []string{"up"}, "match[]": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)}, "start": []string{formatTime(testTime.Add(-time.Minute))},
"end": []string{testTime.Format(time.RFC3339Nano)}, "end": []string{formatTime(testTime)},
}, },
err: fmt.Errorf("some error"), err: fmt.Errorf("some error"),
}, },
@ -535,9 +594,9 @@ func TestAPIs(t *testing.T) {
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/series", reqPath: "/api/v1/series",
reqParam: url.Values{ reqParam: url.Values{
"match": []string{"up"}, "match[]": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)}, "start": []string{formatTime(testTime.Add(-time.Minute))},
"end": []string{testTime.Format(time.RFC3339Nano)}, "end": []string{formatTime(testTime)},
}, },
err: fmt.Errorf("some error"), err: fmt.Errorf("some error"),
warnings: []string{"a"}, warnings: []string{"a"},
@ -563,6 +622,9 @@ func TestAPIs(t *testing.T) {
inErr: fmt.Errorf("some error"), inErr: fmt.Errorf("some error"),
reqMethod: "POST", reqMethod: "POST",
reqPath: "/api/v1/admin/tsdb/snapshot", reqPath: "/api/v1/admin/tsdb/snapshot",
reqParam: url.Values{
"skip_head": []string{"true"},
},
err: fmt.Errorf("some error"), err: fmt.Errorf("some error"),
}, },
@ -592,9 +654,9 @@ func TestAPIs(t *testing.T) {
reqMethod: "POST", reqMethod: "POST",
reqPath: "/api/v1/admin/tsdb/delete_series", reqPath: "/api/v1/admin/tsdb/delete_series",
reqParam: url.Values{ reqParam: url.Values{
"match": []string{"up"}, "match[]": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)}, "start": []string{formatTime(testTime.Add(-time.Minute))},
"end": []string{testTime.Format(time.RFC3339Nano)}, "end": []string{formatTime(testTime)},
}, },
}, },
@ -604,9 +666,9 @@ func TestAPIs(t *testing.T) {
reqMethod: "POST", reqMethod: "POST",
reqPath: "/api/v1/admin/tsdb/delete_series", reqPath: "/api/v1/admin/tsdb/delete_series",
reqParam: url.Values{ reqParam: url.Values{
"match": []string{"up"}, "match[]": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)}, "start": []string{formatTime(testTime.Add(-time.Minute))},
"end": []string{testTime.Format(time.RFC3339Nano)}, "end": []string{formatTime(testTime)},
}, },
err: fmt.Errorf("some error"), err: fmt.Errorf("some error"),
}, },
@ -1246,6 +1308,11 @@ func TestAPIs(t *testing.T) {
do: doQueryExemplars("tns_request_duration_seconds_bucket", testTime.Add(-1*time.Minute), testTime), do: doQueryExemplars("tns_request_duration_seconds_bucket", testTime.Add(-1*time.Minute), testTime),
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/query_exemplars", reqPath: "/api/v1/query_exemplars",
reqParam: url.Values{
"query": []string{"tns_request_duration_seconds_bucket"},
"start": []string{formatTime(testTime.Add(-1 * time.Minute))},
"end": []string{formatTime(testTime)},
},
inErr: fmt.Errorf("some error"), inErr: fmt.Errorf("some error"),
err: fmt.Errorf("some error"), err: fmt.Errorf("some error"),
}, },
@ -1254,6 +1321,11 @@ func TestAPIs(t *testing.T) {
do: doQueryExemplars("tns_request_duration_seconds_bucket", testTime.Add(-1*time.Minute), testTime), do: doQueryExemplars("tns_request_duration_seconds_bucket", testTime.Add(-1*time.Minute), testTime),
reqMethod: "GET", reqMethod: "GET",
reqPath: "/api/v1/query_exemplars", reqPath: "/api/v1/query_exemplars",
reqParam: url.Values{
"query": []string{"tns_request_duration_seconds_bucket"},
"start": []string{formatTime(testTime.Add(-1 * time.Minute))},
"end": []string{formatTime(testTime)},
},
inRes: []interface{}{ inRes: []interface{}{
map[string]interface{}{ map[string]interface{}{
"seriesLabels": map[string]interface{}{ "seriesLabels": map[string]interface{}{