From aff4738f330f984a2dedf31dac03ddc6b3dd8fca Mon Sep 17 00:00:00 2001 From: beorn7 Date: Fri, 17 May 2019 18:09:47 +0200 Subject: [PATCH] Adjust TestQueryRange to new Prometheus API client Signed-off-by: beorn7 --- cmd/promtool/main_test.go | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/cmd/promtool/main_test.go b/cmd/promtool/main_test.go index 8824fba7b..84ff006db 100644 --- a/cmd/promtool/main_test.go +++ b/cmd/promtool/main_test.go @@ -17,26 +17,27 @@ import ( "fmt" "net/http" "net/http/httptest" - "net/url" "testing" "time" ) func TestQueryRange(t *testing.T) { - s, getURL := mockServer(200, `{"status": "success", "data": {"resultType": "matrix", "result": []}}`) + s, getRequest := mockServer(200, `{"status": "success", "data": {"resultType": "matrix", "result": []}}`) defer s.Close() p := &promqlPrinter{} exitCode := QueryRange(s.URL, "up", "0", "300", 0, p) expectedPath := "/api/v1/query_range" - if getURL().Path != expectedPath { - t.Errorf("unexpected URL path %s (wanted %s)", getURL().Path, expectedPath) + gotPath := getRequest().URL.Path + if gotPath != expectedPath { + t.Errorf("unexpected URL path %s (wanted %s)", gotPath, expectedPath) } - actual := getURL().Query().Get("query") + form := getRequest().Form + actual := form.Get("query") if actual != "up" { t.Errorf("unexpected value %s for query", actual) } - actual = getURL().Query().Get("step") + actual = form.Get("step") if actual != "1.000" { t.Errorf("unexpected value %s for step", actual) } @@ -45,14 +46,16 @@ func TestQueryRange(t *testing.T) { } exitCode = QueryRange(s.URL, "up", "0", "300", 10*time.Millisecond, p) - if getURL().Path != expectedPath { - t.Errorf("unexpected URL path %s (wanted %s)", getURL().Path, expectedPath) + gotPath = getRequest().URL.Path + if gotPath != expectedPath { + t.Errorf("unexpected URL path %s (wanted %s)", gotPath, expectedPath) } - actual = getURL().Query().Get("query") + form = getRequest().Form + actual = form.Get("query") if actual != "up" { t.Errorf("unexpected value %s for query", actual) } - actual = getURL().Query().Get("step") + actual = form.Get("step") if actual != "0.010" { t.Errorf("unexpected value %s for step", actual) } @@ -61,16 +64,17 @@ func TestQueryRange(t *testing.T) { } } -func mockServer(code int, body string) (*httptest.Server, func() *url.URL) { - var u *url.URL +func mockServer(code int, body string) (*httptest.Server, func() *http.Request) { + var req *http.Request server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - u = r.URL + r.ParseForm() + req = r w.WriteHeader(code) fmt.Fprintln(w, body) })) - f := func() *url.URL { - return u + f := func() *http.Request { + return req } return server, f }