2020-03-13 11:19:31 +01:00
|
|
|
package datasource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-30 08:46:03 +02:00
|
|
|
"fmt"
|
2020-03-13 11:19:31 +01:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-12-02 13:45:08 +01:00
|
|
|
"net/url"
|
2021-04-30 08:46:03 +02:00
|
|
|
"reflect"
|
2022-12-05 08:34:54 +01:00
|
|
|
"sort"
|
2021-06-09 11:20:38 +02:00
|
|
|
"strings"
|
2020-03-13 11:19:31 +01:00
|
|
|
"testing"
|
2020-09-21 14:53:49 +02:00
|
|
|
"time"
|
2021-09-14 13:32:06 +02:00
|
|
|
|
2022-03-10 12:09:12 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/utils"
|
2021-09-14 13:32:06 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2020-03-13 11:19:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
basicAuthName = "foo"
|
|
|
|
basicAuthPass = "bar"
|
2021-09-14 13:32:06 +02:00
|
|
|
baCfg = &promauth.BasicAuthConfig{
|
|
|
|
Username: basicAuthName,
|
2021-11-05 13:41:14 +01:00
|
|
|
Password: promauth.NewSecret(basicAuthPass),
|
2021-09-14 13:32:06 +02:00
|
|
|
}
|
|
|
|
query = "vm_rows"
|
|
|
|
queryRender = "constantLine(10)"
|
2020-03-13 11:19:31 +01:00
|
|
|
)
|
|
|
|
|
2021-06-09 11:20:38 +02:00
|
|
|
func TestVMInstantQuery(t *testing.T) {
|
2020-03-13 11:19:31 +01:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(_ http.ResponseWriter, _ *http.Request) {
|
|
|
|
t.Errorf("should not be called")
|
|
|
|
})
|
|
|
|
c := -1
|
|
|
|
mux.HandleFunc("/api/v1/query", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c++
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
t.Errorf("expected POST method got %s", r.Method)
|
|
|
|
}
|
|
|
|
if name, pass, _ := r.BasicAuth(); name != basicAuthName || pass != basicAuthPass {
|
|
|
|
t.Errorf("expected %s:%s as basic auth got %s:%s", basicAuthName, basicAuthPass, name, pass)
|
|
|
|
}
|
|
|
|
if r.URL.Query().Get("query") != query {
|
2020-07-02 17:05:36 +02:00
|
|
|
t.Errorf("expected %s in query param, got %s", query, r.URL.Query().Get("query"))
|
2020-03-13 11:19:31 +01:00
|
|
|
}
|
2020-09-21 14:53:49 +02:00
|
|
|
timeParam := r.URL.Query().Get("time")
|
|
|
|
if timeParam == "" {
|
|
|
|
t.Errorf("expected 'time' in query param, got nil instead")
|
|
|
|
}
|
2023-07-07 10:39:25 +02:00
|
|
|
if _, err := time.Parse(time.RFC3339, timeParam); err != nil {
|
|
|
|
t.Errorf("failed to parse 'time' query param %q: %s", timeParam, err)
|
2020-09-21 14:53:49 +02:00
|
|
|
}
|
2020-03-13 11:19:31 +01:00
|
|
|
switch c {
|
|
|
|
case 0:
|
|
|
|
w.WriteHeader(500)
|
2023-04-19 10:18:32 +02:00
|
|
|
case 1:
|
2020-03-13 11:19:31 +01:00
|
|
|
w.Write([]byte("[]"))
|
2023-04-19 10:18:32 +02:00
|
|
|
case 2:
|
2020-03-13 11:19:31 +01:00
|
|
|
w.Write([]byte(`{"status":"error", "errorType":"type:", "error":"some error msg"}`))
|
2023-04-19 10:18:32 +02:00
|
|
|
case 3:
|
2020-03-13 11:19:31 +01:00
|
|
|
w.Write([]byte(`{"status":"unknown"}`))
|
2023-04-19 10:18:32 +02:00
|
|
|
case 4:
|
2020-03-13 11:19:31 +01:00
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"matrix"}}`))
|
2023-04-19 10:18:32 +02:00
|
|
|
case 5:
|
2022-12-05 08:34:54 +01:00
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"vm_rows","foo":"bar"},"value":[1583786142,"13763"]},{"metric":{"__name__":"vm_requests","foo":"baz"},"value":[1583786140,"2000"]}]}}`))
|
2023-04-19 10:18:32 +02:00
|
|
|
case 6:
|
2022-05-18 09:50:46 +02:00
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"scalar","result":[1583786142, "1"]}}`))
|
2023-05-08 09:36:39 +02:00
|
|
|
case 7:
|
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"scalar","result":[1583786142, "1"]},"stats":{"seriesFetched": "42"}}`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
mux.HandleFunc("/render", func(w http.ResponseWriter, request *http.Request) {
|
|
|
|
c++
|
|
|
|
switch c {
|
|
|
|
case 8:
|
|
|
|
w.Write([]byte(`[{"target":"constantLine(10)","tags":{"name":"constantLine(10)"},"datapoints":[[10,1611758343],[10,1611758373],[10,1611758403]]}]`))
|
2020-03-13 11:19:31 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
srv := httptest.NewServer(mux)
|
|
|
|
defer srv.Close()
|
2021-04-30 08:46:03 +02:00
|
|
|
|
2022-06-22 19:38:43 +02:00
|
|
|
authCfg, err := baCfg.NewConfig(".")
|
2021-09-14 13:32:06 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected: %s", err)
|
|
|
|
}
|
2022-05-13 15:19:32 +02:00
|
|
|
s := NewVMStorage(srv.URL, authCfg, time.Minute, 0, false, srv.Client())
|
2021-04-30 08:46:03 +02:00
|
|
|
|
2022-07-22 10:44:55 +02:00
|
|
|
p := datasourcePrometheus
|
|
|
|
pq := s.BuildWithParams(QuerierParams{DataSourceType: string(p), EvaluationInterval: 15 * time.Second})
|
2022-03-29 15:09:07 +02:00
|
|
|
ts := time.Now()
|
2021-04-30 08:46:03 +02:00
|
|
|
|
2022-05-18 09:50:46 +02:00
|
|
|
expErr := func(err string) {
|
2023-04-19 10:18:32 +02:00
|
|
|
_, _, gotErr := pq.Query(ctx, query, ts)
|
|
|
|
if gotErr == nil {
|
2022-05-18 09:50:46 +02:00
|
|
|
t.Fatalf("expected %q got nil", err)
|
|
|
|
}
|
2023-04-19 10:18:32 +02:00
|
|
|
if !strings.Contains(gotErr.Error(), err) {
|
|
|
|
t.Fatalf("expected err %q; got %q", err, gotErr)
|
|
|
|
}
|
2020-03-13 11:19:31 +01:00
|
|
|
}
|
2022-05-18 09:50:46 +02:00
|
|
|
|
2023-04-19 10:18:32 +02:00
|
|
|
expErr("500") // 0
|
|
|
|
expErr("error parsing prometheus metrics") // 1
|
|
|
|
expErr("response error") // 2
|
|
|
|
expErr("unknown status") // 3
|
|
|
|
expErr("unexpected end of JSON input") // 4
|
2022-05-18 09:50:46 +02:00
|
|
|
|
2023-05-08 09:36:39 +02:00
|
|
|
res, _, err := pq.Query(ctx, query, ts) // 5 - vector
|
2020-03-13 11:19:31 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
if len(res.Data) != 2 {
|
|
|
|
t.Fatalf("expected 2 metrics got %d in %+v", len(res.Data), res.Data)
|
2020-03-13 11:19:31 +01:00
|
|
|
}
|
2021-06-11 10:22:05 +02:00
|
|
|
expected := []Metric{
|
|
|
|
{
|
2022-12-05 08:34:54 +01:00
|
|
|
Labels: []Label{{Value: "vm_rows", Name: "__name__"}, {Value: "bar", Name: "foo"}},
|
2021-06-11 10:22:05 +02:00
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{13763},
|
|
|
|
},
|
|
|
|
{
|
2022-12-05 08:34:54 +01:00
|
|
|
Labels: []Label{{Value: "vm_requests", Name: "__name__"}, {Value: "baz", Name: "foo"}},
|
2021-06-11 10:22:05 +02:00
|
|
|
Timestamps: []int64{1583786140},
|
|
|
|
Values: []float64{2000},
|
|
|
|
},
|
2020-03-13 11:19:31 +01:00
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
metricsEqual(t, res.Data, expected)
|
2021-04-28 22:41:15 +02:00
|
|
|
|
2023-05-08 09:36:39 +02:00
|
|
|
res, req, err := pq.Query(ctx, query, ts) // 6 - scalar
|
2022-05-18 09:50:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
2022-09-15 12:40:22 +02:00
|
|
|
if req == nil {
|
|
|
|
t.Fatalf("expected request to be non-nil")
|
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
if len(res.Data) != 1 {
|
|
|
|
t.Fatalf("expected 1 metrics got %d in %+v", len(res.Data), res.Data)
|
|
|
|
}
|
|
|
|
expected = []Metric{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(res.Data, expected) {
|
|
|
|
t.Fatalf("unexpected metric %+v want %+v", res.Data, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.SeriesFetched != nil {
|
|
|
|
t.Fatalf("expected `seriesFetched` field to be nil when it is missing in datasource response; got %v instead",
|
|
|
|
res.SeriesFetched)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, _, err = pq.Query(ctx, query, ts) // 7 - scalar with stats
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
|
|
|
if len(res.Data) != 1 {
|
|
|
|
t.Fatalf("expected 1 metrics got %d in %+v", len(res.Data), res)
|
2022-05-18 09:50:46 +02:00
|
|
|
}
|
|
|
|
expected = []Metric{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{1},
|
|
|
|
},
|
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
if !reflect.DeepEqual(res.Data, expected) {
|
|
|
|
t.Fatalf("unexpected metric %+v want %+v", res.Data, expected)
|
|
|
|
}
|
|
|
|
if *res.SeriesFetched != 42 {
|
|
|
|
t.Fatalf("expected `seriesFetched` field to be 42; got %d instead",
|
|
|
|
*res.SeriesFetched)
|
2022-05-18 09:50:46 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 10:44:55 +02:00
|
|
|
gq := s.BuildWithParams(QuerierParams{DataSourceType: string(datasourceGraphite)})
|
2021-04-30 08:46:03 +02:00
|
|
|
|
2023-05-08 09:36:39 +02:00
|
|
|
res, _, err = gq.Query(ctx, queryRender, ts) // 8 - graphite
|
2021-02-01 14:02:44 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
if len(res.Data) != 1 {
|
|
|
|
t.Fatalf("expected 1 metric got %d in %+v", len(res.Data), res.Data)
|
2021-02-01 14:02:44 +01:00
|
|
|
}
|
2022-12-05 08:34:54 +01:00
|
|
|
exp := []Metric{
|
|
|
|
{
|
|
|
|
Labels: []Label{{Value: "constantLine(10)", Name: "name"}},
|
|
|
|
Timestamps: []int64{1611758403},
|
|
|
|
Values: []float64{10},
|
|
|
|
},
|
2021-02-01 14:02:44 +01:00
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
metricsEqual(t, res.Data, exp)
|
2023-04-19 10:18:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVMInstantQueryWithRetry(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(_ http.ResponseWriter, _ *http.Request) {
|
|
|
|
t.Errorf("should not be called")
|
|
|
|
})
|
|
|
|
c := -1
|
|
|
|
mux.HandleFunc("/api/v1/query", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c++
|
|
|
|
if r.URL.Query().Get("query") != query {
|
|
|
|
t.Errorf("expected %s in query param, got %s", query, r.URL.Query().Get("query"))
|
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case 0:
|
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"scalar","result":[1583786142, "1"]}}`))
|
|
|
|
case 1:
|
|
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
|
|
|
_ = conn.Close()
|
|
|
|
case 2:
|
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"scalar","result":[1583786142, "2"]}}`))
|
|
|
|
case 3:
|
|
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
|
|
|
_ = conn.Close()
|
|
|
|
case 4:
|
|
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
|
|
|
_ = conn.Close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
srv := httptest.NewServer(mux)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
s := NewVMStorage(srv.URL, nil, time.Minute, 0, false, srv.Client())
|
|
|
|
pq := s.BuildWithParams(QuerierParams{DataSourceType: string(datasourcePrometheus)})
|
|
|
|
|
|
|
|
expErr := func(err string) {
|
|
|
|
_, _, gotErr := pq.Query(ctx, query, time.Now())
|
|
|
|
if gotErr == nil {
|
|
|
|
t.Fatalf("expected %q got nil", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(gotErr.Error(), err) {
|
|
|
|
t.Fatalf("expected err %q; got %q", err, gotErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expValue := func(v float64) {
|
2023-05-08 09:36:39 +02:00
|
|
|
res, _, err := pq.Query(ctx, query, time.Now())
|
2023-04-19 10:18:32 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
m := res.Data
|
2023-04-19 10:18:32 +02:00
|
|
|
if len(m) != 1 {
|
|
|
|
t.Fatalf("expected 1 metrics got %d in %+v", len(m), m)
|
|
|
|
}
|
|
|
|
expected := []Metric{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{v},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(m, expected) {
|
|
|
|
t.Fatalf("unexpected metric %+v want %+v", m, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expValue(1) // 0
|
|
|
|
expValue(2) // 1 - fail, 2 - retry
|
|
|
|
expErr("EOF") // 3, 4 - retries
|
2022-12-05 08:34:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func metricsEqual(t *testing.T, gotM, expectedM []Metric) {
|
|
|
|
for i, exp := range expectedM {
|
|
|
|
got := gotM[i]
|
|
|
|
gotTS, expTS := got.Timestamps, exp.Timestamps
|
|
|
|
if !reflect.DeepEqual(gotTS, expTS) {
|
|
|
|
t.Fatalf("unexpected timestamps %+v want %+v", gotTS, expTS)
|
|
|
|
}
|
|
|
|
gotV, expV := got.Values, exp.Values
|
|
|
|
if !reflect.DeepEqual(gotV, expV) {
|
|
|
|
t.Fatalf("unexpected values %+v want %+v", gotV, expV)
|
|
|
|
}
|
|
|
|
sort.Slice(got.Labels, func(i, j int) bool {
|
|
|
|
return got.Labels[i].Name < got.Labels[j].Name
|
|
|
|
})
|
|
|
|
sort.Slice(exp.Labels, func(i, j int) bool {
|
|
|
|
return exp.Labels[i].Name < exp.Labels[j].Name
|
|
|
|
})
|
|
|
|
if !reflect.DeepEqual(exp.Labels, got.Labels) {
|
|
|
|
t.Fatalf("unexpected labels %+v want %+v", got.Labels, exp.Labels)
|
|
|
|
}
|
2021-02-01 14:02:44 +01:00
|
|
|
}
|
2020-03-13 11:19:31 +01:00
|
|
|
}
|
2021-04-30 08:46:03 +02:00
|
|
|
|
2021-06-09 11:20:38 +02:00
|
|
|
func TestVMRangeQuery(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(_ http.ResponseWriter, _ *http.Request) {
|
|
|
|
t.Errorf("should not be called")
|
|
|
|
})
|
|
|
|
c := -1
|
|
|
|
mux.HandleFunc("/api/v1/query_range", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c++
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
t.Errorf("expected POST method got %s", r.Method)
|
|
|
|
}
|
|
|
|
if name, pass, _ := r.BasicAuth(); name != basicAuthName || pass != basicAuthPass {
|
|
|
|
t.Errorf("expected %s:%s as basic auth got %s:%s", basicAuthName, basicAuthPass, name, pass)
|
|
|
|
}
|
|
|
|
if r.URL.Query().Get("query") != query {
|
|
|
|
t.Errorf("expected %s in query param, got %s", query, r.URL.Query().Get("query"))
|
|
|
|
}
|
|
|
|
startTS := r.URL.Query().Get("start")
|
|
|
|
if startTS == "" {
|
|
|
|
t.Errorf("expected 'start' in query param, got nil instead")
|
|
|
|
}
|
2023-07-07 10:39:25 +02:00
|
|
|
if _, err := time.Parse(time.RFC3339, startTS); err != nil {
|
2021-06-09 11:20:38 +02:00
|
|
|
t.Errorf("failed to parse 'start' query param: %s", err)
|
|
|
|
}
|
|
|
|
endTS := r.URL.Query().Get("end")
|
|
|
|
if endTS == "" {
|
|
|
|
t.Errorf("expected 'end' in query param, got nil instead")
|
|
|
|
}
|
2023-07-07 10:39:25 +02:00
|
|
|
if _, err := time.Parse(time.RFC3339, endTS); err != nil {
|
2021-06-09 11:20:38 +02:00
|
|
|
t.Errorf("failed to parse 'end' query param: %s", err)
|
|
|
|
}
|
2022-12-01 13:57:53 +01:00
|
|
|
step := r.URL.Query().Get("step")
|
|
|
|
if step != "15s" {
|
|
|
|
t.Errorf("expected 'step' query param to be 15s; got %q instead", step)
|
|
|
|
}
|
2021-06-09 11:20:38 +02:00
|
|
|
switch c {
|
|
|
|
case 0:
|
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"vm_rows"},"values":[[1583786142,"13763"]]}]}}`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
srv := httptest.NewServer(mux)
|
|
|
|
defer srv.Close()
|
|
|
|
|
2022-06-22 19:38:43 +02:00
|
|
|
authCfg, err := baCfg.NewConfig(".")
|
2021-09-14 13:32:06 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected: %s", err)
|
|
|
|
}
|
2022-12-01 13:57:53 +01:00
|
|
|
s := NewVMStorage(srv.URL, authCfg, time.Minute, *queryStep, false, srv.Client())
|
2021-06-09 11:20:38 +02:00
|
|
|
|
2022-07-22 10:44:55 +02:00
|
|
|
pq := s.BuildWithParams(QuerierParams{DataSourceType: string(datasourcePrometheus), EvaluationInterval: 15 * time.Second})
|
2021-06-09 11:20:38 +02:00
|
|
|
|
2021-09-14 13:32:06 +02:00
|
|
|
_, err = pq.QueryRange(ctx, query, time.Now(), time.Time{})
|
2021-06-09 11:20:38 +02:00
|
|
|
expectError(t, err, "is missing")
|
|
|
|
|
|
|
|
_, err = pq.QueryRange(ctx, query, time.Time{}, time.Now())
|
|
|
|
expectError(t, err, "is missing")
|
|
|
|
|
|
|
|
start, end := time.Now().Add(-time.Minute), time.Now()
|
|
|
|
|
2023-05-08 09:36:39 +02:00
|
|
|
res, err := pq.QueryRange(ctx, query, start, end)
|
2021-06-09 11:20:38 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
m := res.Data
|
2021-06-09 11:20:38 +02:00
|
|
|
if len(m) != 1 {
|
|
|
|
t.Fatalf("expected 1 metric got %d in %+v", len(m), m)
|
|
|
|
}
|
|
|
|
expected := Metric{
|
|
|
|
Labels: []Label{{Value: "vm_rows", Name: "__name__"}},
|
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{13763},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(m[0], expected) {
|
|
|
|
t.Fatalf("unexpected metric %+v want %+v", m[0], expected)
|
|
|
|
}
|
|
|
|
|
2022-07-22 10:44:55 +02:00
|
|
|
gq := s.BuildWithParams(QuerierParams{DataSourceType: string(datasourceGraphite)})
|
2021-06-09 11:20:38 +02:00
|
|
|
|
|
|
|
_, err = gq.QueryRange(ctx, queryRender, start, end)
|
|
|
|
expectError(t, err, "is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestParams(t *testing.T) {
|
2022-06-22 19:38:43 +02:00
|
|
|
authCfg, err := baCfg.NewConfig(".")
|
2021-09-14 13:32:06 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected: %s", err)
|
|
|
|
}
|
2021-04-30 08:46:03 +02:00
|
|
|
query := "up"
|
|
|
|
timestamp := time.Date(2001, 2, 3, 4, 5, 6, 0, time.UTC)
|
2023-06-01 09:44:11 +02:00
|
|
|
storage := VMStorage{
|
|
|
|
extraParams: url.Values{"round_digits": {"10"}},
|
|
|
|
}
|
2021-04-30 08:46:03 +02:00
|
|
|
testCases := []struct {
|
2021-06-09 11:20:38 +02:00
|
|
|
name string
|
|
|
|
queryRange bool
|
|
|
|
vm *VMStorage
|
|
|
|
checkFn func(t *testing.T, r *http.Request)
|
2021-04-30 08:46:03 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"prometheus path",
|
2021-06-09 11:20:38 +02:00
|
|
|
false,
|
2021-04-30 08:46:03 +02:00
|
|
|
&VMStorage{
|
2022-07-22 10:44:55 +02:00
|
|
|
dataSourceType: datasourcePrometheus,
|
2021-04-30 08:46:03 +02:00
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2022-05-13 15:19:32 +02:00
|
|
|
checkEqualString(t, "/api/v1/query", r.URL.Path)
|
2021-10-18 09:24:52 +02:00
|
|
|
},
|
|
|
|
},
|
2021-04-30 08:46:03 +02:00
|
|
|
{
|
|
|
|
"prometheus prefix",
|
2021-06-09 11:20:38 +02:00
|
|
|
false,
|
2021-04-30 08:46:03 +02:00
|
|
|
&VMStorage{
|
2022-07-22 10:44:55 +02:00
|
|
|
dataSourceType: datasourcePrometheus,
|
2021-04-30 08:46:03 +02:00
|
|
|
appendTypePrefix: true,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2022-05-13 15:19:32 +02:00
|
|
|
checkEqualString(t, "/prometheus/api/v1/query", r.URL.Path)
|
2021-10-18 09:24:52 +02:00
|
|
|
},
|
|
|
|
},
|
2021-06-09 11:20:38 +02:00
|
|
|
{
|
|
|
|
"prometheus range path",
|
|
|
|
true,
|
|
|
|
&VMStorage{
|
2022-07-22 10:44:55 +02:00
|
|
|
dataSourceType: datasourcePrometheus,
|
2021-06-09 11:20:38 +02:00
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2022-05-13 15:19:32 +02:00
|
|
|
checkEqualString(t, "/api/v1/query_range", r.URL.Path)
|
2021-10-18 09:24:52 +02:00
|
|
|
},
|
|
|
|
},
|
2021-06-09 11:20:38 +02:00
|
|
|
{
|
|
|
|
"prometheus range prefix",
|
|
|
|
true,
|
|
|
|
&VMStorage{
|
2022-07-22 10:44:55 +02:00
|
|
|
dataSourceType: datasourcePrometheus,
|
2021-06-09 11:20:38 +02:00
|
|
|
appendTypePrefix: true,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2022-05-13 15:19:32 +02:00
|
|
|
checkEqualString(t, "/prometheus/api/v1/query_range", r.URL.Path)
|
2021-10-18 09:24:52 +02:00
|
|
|
},
|
|
|
|
},
|
2021-04-30 08:46:03 +02:00
|
|
|
{
|
|
|
|
"graphite path",
|
2021-06-09 11:20:38 +02:00
|
|
|
false,
|
2021-04-30 08:46:03 +02:00
|
|
|
&VMStorage{
|
2022-07-22 10:44:55 +02:00
|
|
|
dataSourceType: datasourceGraphite,
|
2021-04-30 08:46:03 +02:00
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
checkEqualString(t, graphitePath, r.URL.Path)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"graphite prefix",
|
2021-06-09 11:20:38 +02:00
|
|
|
false,
|
2021-04-30 08:46:03 +02:00
|
|
|
&VMStorage{
|
2022-07-22 10:44:55 +02:00
|
|
|
dataSourceType: datasourceGraphite,
|
2021-04-30 08:46:03 +02:00
|
|
|
appendTypePrefix: true,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
checkEqualString(t, graphitePrefix+graphitePath, r.URL.Path)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"default params",
|
2021-06-09 11:20:38 +02:00
|
|
|
false,
|
2021-04-30 08:46:03 +02:00
|
|
|
&VMStorage{},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2023-07-07 10:39:25 +02:00
|
|
|
exp := url.Values{"query": {query}, "time": {timestamp.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
2021-04-30 08:46:03 +02:00
|
|
|
},
|
|
|
|
},
|
2021-06-09 11:20:38 +02:00
|
|
|
{
|
|
|
|
"default range params",
|
|
|
|
true,
|
|
|
|
&VMStorage{},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2023-07-07 10:39:25 +02:00
|
|
|
ts := timestamp.Format(time.RFC3339)
|
|
|
|
exp := url.Values{"query": {query}, "start": {ts}, "end": {ts}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
2021-06-09 11:20:38 +02:00
|
|
|
},
|
|
|
|
},
|
2021-04-30 08:46:03 +02:00
|
|
|
{
|
|
|
|
"basic auth",
|
2021-06-09 11:20:38 +02:00
|
|
|
false,
|
2021-09-14 13:32:06 +02:00
|
|
|
&VMStorage{authCfg: authCfg},
|
2021-06-09 11:20:38 +02:00
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
u, p, _ := r.BasicAuth()
|
|
|
|
checkEqualString(t, "foo", u)
|
|
|
|
checkEqualString(t, "bar", p)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"basic auth range",
|
|
|
|
true,
|
2021-09-14 13:32:06 +02:00
|
|
|
&VMStorage{authCfg: authCfg},
|
2021-04-30 08:46:03 +02:00
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
u, p, _ := r.BasicAuth()
|
|
|
|
checkEqualString(t, "foo", u)
|
|
|
|
checkEqualString(t, "bar", p)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"lookback",
|
2021-06-09 11:20:38 +02:00
|
|
|
false,
|
2021-04-30 08:46:03 +02:00
|
|
|
&VMStorage{
|
|
|
|
lookBack: time.Minute,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2023-07-07 10:39:25 +02:00
|
|
|
exp := url.Values{"query": {query}, "time": {timestamp.Add(-time.Minute).Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
2021-04-30 08:46:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"evaluation interval",
|
2021-06-09 11:20:38 +02:00
|
|
|
false,
|
2021-04-30 08:46:03 +02:00
|
|
|
&VMStorage{
|
|
|
|
evaluationInterval: 15 * time.Second,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2021-04-30 09:01:05 +02:00
|
|
|
evalInterval := 15 * time.Second
|
2023-10-10 12:41:19 +02:00
|
|
|
exp := url.Values{"query": {query}, "step": {evalInterval.String()}, "time": {timestamp.Format(time.RFC3339)}}
|
2023-07-07 10:39:25 +02:00
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
2021-04-30 08:46:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"lookback + evaluation interval",
|
2021-06-09 11:20:38 +02:00
|
|
|
false,
|
2021-04-30 08:46:03 +02:00
|
|
|
&VMStorage{
|
|
|
|
lookBack: time.Minute,
|
|
|
|
evaluationInterval: 15 * time.Second,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2021-04-30 09:01:05 +02:00
|
|
|
evalInterval := 15 * time.Second
|
2021-04-30 08:46:03 +02:00
|
|
|
tt := timestamp.Add(-time.Minute)
|
2023-07-07 10:39:25 +02:00
|
|
|
exp := url.Values{"query": {query}, "step": {evalInterval.String()}, "time": {tt.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
2021-04-30 08:46:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-04-30 09:01:05 +02:00
|
|
|
"step override",
|
2021-06-09 11:20:38 +02:00
|
|
|
false,
|
2021-04-30 08:46:03 +02:00
|
|
|
&VMStorage{
|
|
|
|
queryStep: time.Minute,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2023-07-07 10:39:25 +02:00
|
|
|
exp := url.Values{
|
|
|
|
"query": {query},
|
|
|
|
"step": {fmt.Sprintf("%ds", int(time.Minute.Seconds()))},
|
|
|
|
"time": {timestamp.Format(time.RFC3339)},
|
|
|
|
}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
2021-12-17 14:26:34 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"step to seconds",
|
|
|
|
false,
|
|
|
|
&VMStorage{
|
|
|
|
evaluationInterval: 3 * time.Hour,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
evalInterval := 3 * time.Hour
|
2023-10-10 12:41:19 +02:00
|
|
|
exp := url.Values{"query": {query}, "step": {fmt.Sprintf("%ds", int(evalInterval.Seconds()))}, "time": {timestamp.Format(time.RFC3339)}}
|
2023-07-07 10:39:25 +02:00
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
2021-04-30 08:46:03 +02:00
|
|
|
},
|
|
|
|
},
|
2021-05-10 10:11:45 +02:00
|
|
|
{
|
2021-12-02 13:45:08 +01:00
|
|
|
"prometheus extra params",
|
2021-06-09 11:20:38 +02:00
|
|
|
false,
|
2021-05-10 10:11:45 +02:00
|
|
|
&VMStorage{
|
2021-12-02 13:45:08 +01:00
|
|
|
extraParams: url.Values{"round_digits": {"10"}},
|
2021-05-10 10:11:45 +02:00
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2023-07-07 10:39:25 +02:00
|
|
|
exp := url.Values{"query": {query}, "round_digits": {"10"}, "time": {timestamp.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
2021-05-10 10:11:45 +02:00
|
|
|
},
|
|
|
|
},
|
2021-05-22 23:26:01 +02:00
|
|
|
{
|
2021-12-02 13:45:08 +01:00
|
|
|
"prometheus extra params range",
|
2021-06-09 11:20:38 +02:00
|
|
|
true,
|
|
|
|
&VMStorage{
|
2021-12-02 13:45:08 +01:00
|
|
|
extraParams: url.Values{
|
|
|
|
"nocache": {"1"},
|
|
|
|
"max_lookback": {"1h"},
|
2021-06-09 11:20:38 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2023-07-07 10:39:25 +02:00
|
|
|
exp := url.Values{
|
|
|
|
"query": {query},
|
|
|
|
"end": {timestamp.Format(time.RFC3339)},
|
|
|
|
"start": {timestamp.Format(time.RFC3339)},
|
|
|
|
"nocache": {"1"},
|
|
|
|
"max_lookback": {"1h"},
|
|
|
|
}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
2021-06-09 11:20:38 +02:00
|
|
|
},
|
|
|
|
},
|
2023-06-01 09:44:11 +02:00
|
|
|
{
|
|
|
|
"custom params overrides the original params",
|
|
|
|
false,
|
|
|
|
storage.Clone().ApplyParams(QuerierParams{
|
|
|
|
QueryParams: url.Values{"round_digits": {"2"}},
|
|
|
|
}),
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2023-07-07 10:39:25 +02:00
|
|
|
exp := url.Values{"query": {query}, "round_digits": {"2"}, "time": {timestamp.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
2023-06-01 09:44:11 +02:00
|
|
|
},
|
|
|
|
},
|
2023-09-08 09:32:48 +02:00
|
|
|
{
|
|
|
|
"allow duplicates in query params",
|
|
|
|
false,
|
|
|
|
storage.Clone().ApplyParams(QuerierParams{
|
|
|
|
QueryParams: url.Values{"extra_labels": {"env=dev", "foo=bar"}},
|
|
|
|
}),
|
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
exp := url.Values{"query": {query}, "round_digits": {"10"}, "extra_labels": {"env=dev", "foo=bar"}, "time": {timestamp.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
2021-08-31 13:57:47 +02:00
|
|
|
{
|
2021-12-02 13:45:08 +01:00
|
|
|
"graphite extra params",
|
2021-08-31 13:57:47 +02:00
|
|
|
false,
|
|
|
|
&VMStorage{
|
2022-07-22 10:44:55 +02:00
|
|
|
dataSourceType: datasourceGraphite,
|
2021-12-02 13:45:08 +01:00
|
|
|
extraParams: url.Values{
|
|
|
|
"nocache": {"1"},
|
|
|
|
"max_lookback": {"1h"},
|
2021-08-31 13:57:47 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2021-12-02 13:45:08 +01:00
|
|
|
exp := fmt.Sprintf("format=json&from=-5min&max_lookback=1h&nocache=1&target=%s&until=now", query)
|
2021-08-31 13:57:47 +02:00
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
2023-07-21 12:28:10 +02:00
|
|
|
{
|
|
|
|
"graphite extra params allows to override from",
|
|
|
|
false,
|
|
|
|
&VMStorage{
|
|
|
|
dataSourceType: datasourceGraphite,
|
|
|
|
extraParams: url.Values{
|
|
|
|
"from": {"-10m"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
exp := fmt.Sprintf("format=json&from=-10m&target=%s&until=now", query)
|
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
2021-04-30 08:46:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-10-17 11:58:19 +02:00
|
|
|
req, err := tc.vm.newRequest()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-07-22 10:44:55 +02:00
|
|
|
switch tc.vm.dataSourceType {
|
|
|
|
case "", datasourcePrometheus:
|
2021-06-09 11:20:38 +02:00
|
|
|
if tc.queryRange {
|
|
|
|
tc.vm.setPrometheusRangeReqParams(req, query, timestamp, timestamp)
|
|
|
|
} else {
|
|
|
|
tc.vm.setPrometheusInstantReqParams(req, query, timestamp)
|
|
|
|
}
|
2022-07-22 10:44:55 +02:00
|
|
|
case datasourceGraphite:
|
2021-06-09 11:20:38 +02:00
|
|
|
tc.vm.setGraphiteReqParams(req, query, timestamp)
|
|
|
|
}
|
2021-04-30 08:46:03 +02:00
|
|
|
tc.checkFn(t, req)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 15:59:55 +02:00
|
|
|
func TestHeaders(t *testing.T) {
|
2023-07-07 10:39:25 +02:00
|
|
|
testCases := []struct {
|
2022-03-10 12:09:12 +01:00
|
|
|
name string
|
|
|
|
vmFn func() *VMStorage
|
|
|
|
checkFn func(t *testing.T, r *http.Request)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic auth",
|
|
|
|
vmFn: func() *VMStorage {
|
|
|
|
cfg, err := utils.AuthConfig(utils.WithBasicAuth("foo", "bar", ""))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error get auth config: %s", err)
|
|
|
|
}
|
|
|
|
return &VMStorage{authCfg: cfg}
|
|
|
|
},
|
|
|
|
checkFn: func(t *testing.T, r *http.Request) {
|
|
|
|
u, p, _ := r.BasicAuth()
|
|
|
|
checkEqualString(t, "foo", u)
|
|
|
|
checkEqualString(t, "bar", p)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bearer auth",
|
|
|
|
vmFn: func() *VMStorage {
|
|
|
|
cfg, err := utils.AuthConfig(utils.WithBearer("foo", ""))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error get auth config: %s", err)
|
|
|
|
}
|
|
|
|
return &VMStorage{authCfg: cfg}
|
|
|
|
},
|
|
|
|
checkFn: func(t *testing.T, r *http.Request) {
|
|
|
|
reqToken := r.Header.Get("Authorization")
|
|
|
|
splitToken := strings.Split(reqToken, "Bearer ")
|
|
|
|
if len(splitToken) != 2 {
|
|
|
|
t.Errorf("expected two items got %d", len(splitToken))
|
|
|
|
}
|
|
|
|
token := splitToken[1]
|
|
|
|
checkEqualString(t, "foo", token)
|
|
|
|
},
|
|
|
|
},
|
2022-07-21 15:59:55 +02:00
|
|
|
{
|
|
|
|
name: "custom extraHeaders",
|
|
|
|
vmFn: func() *VMStorage {
|
2022-07-22 10:44:55 +02:00
|
|
|
return &VMStorage{extraHeaders: []keyValue{
|
|
|
|
{key: "Foo", value: "bar"},
|
|
|
|
{key: "Baz", value: "qux"},
|
2022-07-21 15:59:55 +02:00
|
|
|
}}
|
|
|
|
},
|
|
|
|
checkFn: func(t *testing.T, r *http.Request) {
|
|
|
|
h1 := r.Header.Get("Foo")
|
|
|
|
checkEqualString(t, "bar", h1)
|
|
|
|
h2 := r.Header.Get("Baz")
|
|
|
|
checkEqualString(t, "qux", h2)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "custom header overrides basic auth",
|
|
|
|
vmFn: func() *VMStorage {
|
|
|
|
cfg, err := utils.AuthConfig(utils.WithBasicAuth("foo", "bar", ""))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error get auth config: %s", err)
|
|
|
|
}
|
|
|
|
return &VMStorage{
|
|
|
|
authCfg: cfg,
|
2022-07-22 10:44:55 +02:00
|
|
|
extraHeaders: []keyValue{
|
|
|
|
{key: "Authorization", value: "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="},
|
2023-07-07 10:39:25 +02:00
|
|
|
},
|
|
|
|
}
|
2022-07-21 15:59:55 +02:00
|
|
|
},
|
|
|
|
checkFn: func(t *testing.T, r *http.Request) {
|
|
|
|
u, p, _ := r.BasicAuth()
|
|
|
|
checkEqualString(t, "Aladdin", u)
|
|
|
|
checkEqualString(t, "open sesame", p)
|
|
|
|
},
|
|
|
|
},
|
2022-03-10 12:09:12 +01:00
|
|
|
}
|
|
|
|
for _, tt := range testCases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
vm := tt.vmFn()
|
2023-10-17 11:58:19 +02:00
|
|
|
req, err := vm.newQueryRequest("foo", time.Now())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-10 12:09:12 +01:00
|
|
|
tt.checkFn(t, req)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 08:46:03 +02:00
|
|
|
func checkEqualString(t *testing.T, exp, got string) {
|
|
|
|
t.Helper()
|
|
|
|
if got != exp {
|
2021-12-02 13:45:08 +01:00
|
|
|
t.Errorf("expected to get: \n%q; \ngot: \n%q", exp, got)
|
2021-04-30 08:46:03 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-09 11:20:38 +02:00
|
|
|
|
|
|
|
func expectError(t *testing.T, err error, exp string) {
|
|
|
|
t.Helper()
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected non-nil error")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), exp) {
|
|
|
|
t.Errorf("expected error %q to contain %q", err, exp)
|
|
|
|
}
|
|
|
|
}
|