Adjust TestQueryRange to new Prometheus API client

Signed-off-by: beorn7 <bjoern@rabenste.in>
This commit is contained in:
beorn7 2019-05-17 18:09:47 +02:00
parent 12536f3027
commit aff4738f33

View file

@ -17,26 +17,27 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url"
"testing" "testing"
"time" "time"
) )
func TestQueryRange(t *testing.T) { 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() defer s.Close()
p := &promqlPrinter{} p := &promqlPrinter{}
exitCode := QueryRange(s.URL, "up", "0", "300", 0, p) exitCode := QueryRange(s.URL, "up", "0", "300", 0, p)
expectedPath := "/api/v1/query_range" expectedPath := "/api/v1/query_range"
if getURL().Path != expectedPath { gotPath := getRequest().URL.Path
t.Errorf("unexpected URL path %s (wanted %s)", getURL().Path, expectedPath) 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" { if actual != "up" {
t.Errorf("unexpected value %s for query", actual) t.Errorf("unexpected value %s for query", actual)
} }
actual = getURL().Query().Get("step") actual = form.Get("step")
if actual != "1.000" { if actual != "1.000" {
t.Errorf("unexpected value %s for step", actual) 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) exitCode = QueryRange(s.URL, "up", "0", "300", 10*time.Millisecond, p)
if getURL().Path != expectedPath { gotPath = getRequest().URL.Path
t.Errorf("unexpected URL path %s (wanted %s)", getURL().Path, expectedPath) 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" { if actual != "up" {
t.Errorf("unexpected value %s for query", actual) t.Errorf("unexpected value %s for query", actual)
} }
actual = getURL().Query().Get("step") actual = form.Get("step")
if actual != "0.010" { if actual != "0.010" {
t.Errorf("unexpected value %s for step", actual) 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) { func mockServer(code int, body string) (*httptest.Server, func() *http.Request) {
var u *url.URL var req *http.Request
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u = r.URL r.ParseForm()
req = r
w.WriteHeader(code) w.WriteHeader(code)
fmt.Fprintln(w, body) fmt.Fprintln(w, body)
})) }))
f := func() *url.URL { f := func() *http.Request {
return u return req
} }
return server, f return server, f
} }