2018-08-05 02:03:18 -07:00
|
|
|
// Copyright 2018 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestQueryRange(t *testing.T) {
|
2019-05-17 09:09:47 -07:00
|
|
|
s, getRequest := mockServer(200, `{"status": "success", "data": {"resultType": "matrix", "result": []}}`)
|
2018-08-05 02:03:18 -07:00
|
|
|
defer s.Close()
|
|
|
|
|
2018-11-14 09:40:07 -08:00
|
|
|
p := &promqlPrinter{}
|
|
|
|
exitCode := QueryRange(s.URL, "up", "0", "300", 0, p)
|
2018-08-05 02:03:18 -07:00
|
|
|
expectedPath := "/api/v1/query_range"
|
2019-05-17 09:09:47 -07:00
|
|
|
gotPath := getRequest().URL.Path
|
|
|
|
if gotPath != expectedPath {
|
|
|
|
t.Errorf("unexpected URL path %s (wanted %s)", gotPath, expectedPath)
|
2018-08-05 02:03:18 -07:00
|
|
|
}
|
2019-05-17 09:09:47 -07:00
|
|
|
form := getRequest().Form
|
|
|
|
actual := form.Get("query")
|
2018-08-05 02:03:18 -07:00
|
|
|
if actual != "up" {
|
|
|
|
t.Errorf("unexpected value %s for query", actual)
|
|
|
|
}
|
2019-05-17 09:09:47 -07:00
|
|
|
actual = form.Get("step")
|
2018-08-05 02:03:18 -07:00
|
|
|
if actual != "1.000" {
|
|
|
|
t.Errorf("unexpected value %s for step", actual)
|
|
|
|
}
|
|
|
|
if exitCode > 0 {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:40:07 -08:00
|
|
|
exitCode = QueryRange(s.URL, "up", "0", "300", 10*time.Millisecond, p)
|
2019-05-17 09:09:47 -07:00
|
|
|
gotPath = getRequest().URL.Path
|
|
|
|
if gotPath != expectedPath {
|
|
|
|
t.Errorf("unexpected URL path %s (wanted %s)", gotPath, expectedPath)
|
2018-08-05 02:03:18 -07:00
|
|
|
}
|
2019-05-17 09:09:47 -07:00
|
|
|
form = getRequest().Form
|
|
|
|
actual = form.Get("query")
|
2018-08-05 02:03:18 -07:00
|
|
|
if actual != "up" {
|
|
|
|
t.Errorf("unexpected value %s for query", actual)
|
|
|
|
}
|
2019-05-17 09:09:47 -07:00
|
|
|
actual = form.Get("step")
|
2018-08-05 02:03:18 -07:00
|
|
|
if actual != "0.010" {
|
|
|
|
t.Errorf("unexpected value %s for step", actual)
|
|
|
|
}
|
|
|
|
if exitCode > 0 {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:09:47 -07:00
|
|
|
func mockServer(code int, body string) (*httptest.Server, func() *http.Request) {
|
|
|
|
var req *http.Request
|
2018-08-05 02:03:18 -07:00
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-05-17 09:09:47 -07:00
|
|
|
r.ParseForm()
|
|
|
|
req = r
|
2018-08-05 02:03:18 -07:00
|
|
|
w.WriteHeader(code)
|
|
|
|
fmt.Fprintln(w, body)
|
|
|
|
}))
|
|
|
|
|
2019-05-17 09:09:47 -07:00
|
|
|
f := func() *http.Request {
|
|
|
|
return req
|
2018-08-05 02:03:18 -07:00
|
|
|
}
|
|
|
|
return server, f
|
|
|
|
}
|