2020-03-13 10:19:31 +00:00
|
|
|
package datasource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-30 06:46:03 +00:00
|
|
|
"fmt"
|
2020-03-13 10:19:31 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-12-02 12:45:08 +00:00
|
|
|
"net/url"
|
2021-04-30 06:46:03 +00:00
|
|
|
"reflect"
|
2020-09-21 12:53:49 +00:00
|
|
|
"strconv"
|
2021-06-09 09:20:38 +00:00
|
|
|
"strings"
|
2020-03-13 10:19:31 +00:00
|
|
|
"testing"
|
2020-09-21 12:53:49 +00:00
|
|
|
"time"
|
2021-09-14 11:32:06 +00:00
|
|
|
|
2022-03-10 11:09:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/utils"
|
2021-09-14 11:32:06 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2020-03-13 10:19:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
basicAuthName = "foo"
|
|
|
|
basicAuthPass = "bar"
|
2021-09-14 11:32:06 +00:00
|
|
|
baCfg = &promauth.BasicAuthConfig{
|
|
|
|
Username: basicAuthName,
|
2021-11-05 12:41:14 +00:00
|
|
|
Password: promauth.NewSecret(basicAuthPass),
|
2021-09-14 11:32:06 +00:00
|
|
|
}
|
|
|
|
query = "vm_rows"
|
|
|
|
queryRender = "constantLine(10)"
|
2020-03-13 10:19:31 +00:00
|
|
|
)
|
|
|
|
|
2021-06-09 09:20:38 +00:00
|
|
|
func TestVMInstantQuery(t *testing.T) {
|
2020-03-13 10:19:31 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(_ http.ResponseWriter, _ *http.Request) {
|
|
|
|
t.Errorf("should not be called")
|
|
|
|
})
|
|
|
|
c := -1
|
2021-02-01 13:02:44 +00:00
|
|
|
mux.HandleFunc("/render", func(w http.ResponseWriter, request *http.Request) {
|
|
|
|
c++
|
|
|
|
switch c {
|
2022-05-18 07:50:46 +00:00
|
|
|
case 8:
|
2021-02-01 13:02:44 +00:00
|
|
|
w.Write([]byte(`[{"target":"constantLine(10)","tags":{"name":"constantLine(10)"},"datapoints":[[10,1611758343],[10,1611758373],[10,1611758403]]}]`))
|
|
|
|
}
|
|
|
|
})
|
2020-03-13 10:19:31 +00:00
|
|
|
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 15:05:36 +00:00
|
|
|
t.Errorf("expected %s in query param, got %s", query, r.URL.Query().Get("query"))
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
2020-09-21 12:53:49 +00:00
|
|
|
timeParam := r.URL.Query().Get("time")
|
|
|
|
if timeParam == "" {
|
|
|
|
t.Errorf("expected 'time' in query param, got nil instead")
|
|
|
|
}
|
|
|
|
if _, err := strconv.ParseInt(timeParam, 10, 64); err != nil {
|
|
|
|
t.Errorf("failed to parse 'time' query param: %s", err)
|
|
|
|
}
|
2020-03-13 10:19:31 +00:00
|
|
|
switch c {
|
|
|
|
case 0:
|
|
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
|
|
|
_ = conn.Close()
|
|
|
|
case 1:
|
|
|
|
w.WriteHeader(500)
|
|
|
|
case 2:
|
|
|
|
w.Write([]byte("[]"))
|
|
|
|
case 3:
|
|
|
|
w.Write([]byte(`{"status":"error", "errorType":"type:", "error":"some error msg"}`))
|
|
|
|
case 4:
|
|
|
|
w.Write([]byte(`{"status":"unknown"}`))
|
|
|
|
case 5:
|
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"matrix"}}`))
|
|
|
|
case 6:
|
2021-06-11 08:22:05 +00:00
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"vm_rows"},"value":[1583786142,"13763"]},{"metric":{"__name__":"vm_requests"},"value":[1583786140,"2000"]}]}}`))
|
2022-05-18 07:50:46 +00:00
|
|
|
case 7:
|
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"scalar","result":[1583786142, "1"]}}`))
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
srv := httptest.NewServer(mux)
|
|
|
|
defer srv.Close()
|
2021-04-30 06:46:03 +00:00
|
|
|
|
2022-06-22 17:38:43 +00:00
|
|
|
authCfg, err := baCfg.NewConfig(".")
|
2021-09-14 11:32:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected: %s", err)
|
|
|
|
}
|
2022-05-13 13:19:32 +00:00
|
|
|
s := NewVMStorage(srv.URL, authCfg, time.Minute, 0, false, srv.Client())
|
2021-04-30 06:46:03 +00:00
|
|
|
|
|
|
|
p := NewPrometheusType()
|
|
|
|
pq := s.BuildWithParams(QuerierParams{DataSourceType: &p, EvaluationInterval: 15 * time.Second})
|
2022-03-29 13:09:07 +00:00
|
|
|
ts := time.Now()
|
2021-04-30 06:46:03 +00:00
|
|
|
|
2022-05-18 07:50:46 +00:00
|
|
|
expErr := func(err string) {
|
|
|
|
if _, err := pq.Query(ctx, query, ts); err == nil {
|
|
|
|
t.Fatalf("expected %q got nil", err)
|
|
|
|
}
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
2022-05-18 07:50:46 +00:00
|
|
|
|
|
|
|
expErr("connection error") // 0
|
|
|
|
expErr("invalid response status error") // 1
|
|
|
|
expErr("response body error") // 2
|
|
|
|
expErr("error status") // 3
|
|
|
|
expErr("unknown status") // 4
|
|
|
|
expErr("non-vector resultType error") // 5
|
|
|
|
|
|
|
|
m, err := pq.Query(ctx, query, ts) // 6 - vector
|
2020-03-13 10:19:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
2021-06-11 08:22:05 +00:00
|
|
|
if len(m) != 2 {
|
|
|
|
t.Fatalf("expected 2 metrics got %d in %+v", len(m), m)
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
2021-06-11 08:22:05 +00:00
|
|
|
expected := []Metric{
|
|
|
|
{
|
|
|
|
Labels: []Label{{Value: "vm_rows", Name: "__name__"}},
|
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{13763},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Labels: []Label{{Value: "vm_requests", Name: "__name__"}},
|
|
|
|
Timestamps: []int64{1583786140},
|
|
|
|
Values: []float64{2000},
|
|
|
|
},
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
2021-06-11 08:22:05 +00:00
|
|
|
if !reflect.DeepEqual(m, expected) {
|
|
|
|
t.Fatalf("unexpected metric %+v want %+v", m, expected)
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
2021-04-28 20:41:15 +00:00
|
|
|
|
2022-05-18 07:50:46 +00:00
|
|
|
m, err = pq.Query(ctx, query, ts) // 7 - scalar
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
|
|
|
if len(m) != 1 {
|
|
|
|
t.Fatalf("expected 1 metrics got %d in %+v", len(m), m)
|
|
|
|
}
|
|
|
|
expected = []Metric{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(m, expected) {
|
|
|
|
t.Fatalf("unexpected metric %+v want %+v", m, expected)
|
|
|
|
}
|
|
|
|
|
2021-04-30 06:46:03 +00:00
|
|
|
g := NewGraphiteType()
|
|
|
|
gq := s.BuildWithParams(QuerierParams{DataSourceType: &g})
|
|
|
|
|
2022-05-18 07:50:46 +00:00
|
|
|
m, err = gq.Query(ctx, queryRender, ts) // 8 - graphite
|
2021-02-01 13:02:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
|
|
|
if len(m) != 1 {
|
|
|
|
t.Fatalf("expected 1 metric got %d in %+v", len(m), m)
|
|
|
|
}
|
2021-06-11 08:22:05 +00:00
|
|
|
exp := Metric{
|
2021-06-09 09:20:38 +00:00
|
|
|
Labels: []Label{{Value: "constantLine(10)", Name: "name"}},
|
|
|
|
Timestamps: []int64{1611758403},
|
|
|
|
Values: []float64{10},
|
2021-02-01 13:02:44 +00:00
|
|
|
}
|
2021-06-11 08:22:05 +00:00
|
|
|
if !reflect.DeepEqual(m[0], exp) {
|
2021-02-01 13:02:44 +00:00
|
|
|
t.Fatalf("unexpected metric %+v want %+v", m[0], expected)
|
|
|
|
}
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
2021-04-30 06:46:03 +00:00
|
|
|
|
2021-06-09 09:20:38 +00: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")
|
|
|
|
}
|
|
|
|
if _, err := strconv.ParseInt(startTS, 10, 64); err != nil {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
if _, err := strconv.ParseInt(endTS, 10, 64); err != nil {
|
|
|
|
t.Errorf("failed to parse 'end' query param: %s", err)
|
|
|
|
}
|
2022-12-01 12:57:53 +00: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 09:20:38 +00: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 17:38:43 +00:00
|
|
|
authCfg, err := baCfg.NewConfig(".")
|
2021-09-14 11:32:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected: %s", err)
|
|
|
|
}
|
2022-12-01 12:57:53 +00:00
|
|
|
s := NewVMStorage(srv.URL, authCfg, time.Minute, *queryStep, false, srv.Client())
|
2021-06-09 09:20:38 +00:00
|
|
|
|
|
|
|
p := NewPrometheusType()
|
|
|
|
pq := s.BuildWithParams(QuerierParams{DataSourceType: &p, EvaluationInterval: 15 * time.Second})
|
|
|
|
|
2021-09-14 11:32:06 +00:00
|
|
|
_, err = pq.QueryRange(ctx, query, time.Now(), time.Time{})
|
2021-06-09 09:20:38 +00: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()
|
|
|
|
|
|
|
|
m, err := pq.QueryRange(ctx, query, start, end)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
g := NewGraphiteType()
|
|
|
|
gq := s.BuildWithParams(QuerierParams{DataSourceType: &g})
|
|
|
|
|
|
|
|
_, err = gq.QueryRange(ctx, queryRender, start, end)
|
|
|
|
expectError(t, err, "is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestParams(t *testing.T) {
|
2022-06-22 17:38:43 +00:00
|
|
|
authCfg, err := baCfg.NewConfig(".")
|
2021-09-14 11:32:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected: %s", err)
|
|
|
|
}
|
2021-04-30 06:46:03 +00:00
|
|
|
query := "up"
|
|
|
|
timestamp := time.Date(2001, 2, 3, 4, 5, 6, 0, time.UTC)
|
|
|
|
testCases := []struct {
|
2021-06-09 09:20:38 +00:00
|
|
|
name string
|
|
|
|
queryRange bool
|
|
|
|
vm *VMStorage
|
|
|
|
checkFn func(t *testing.T, r *http.Request)
|
2021-04-30 06:46:03 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"prometheus path",
|
2021-06-09 09:20:38 +00:00
|
|
|
false,
|
2021-04-30 06:46:03 +00:00
|
|
|
&VMStorage{
|
|
|
|
dataSourceType: NewPrometheusType(),
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2022-05-13 13:19:32 +00:00
|
|
|
checkEqualString(t, "/api/v1/query", r.URL.Path)
|
2021-10-18 07:24:52 +00:00
|
|
|
},
|
|
|
|
},
|
2021-04-30 06:46:03 +00:00
|
|
|
{
|
|
|
|
"prometheus prefix",
|
2021-06-09 09:20:38 +00:00
|
|
|
false,
|
2021-04-30 06:46:03 +00:00
|
|
|
&VMStorage{
|
|
|
|
dataSourceType: NewPrometheusType(),
|
|
|
|
appendTypePrefix: true,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2022-05-13 13:19:32 +00:00
|
|
|
checkEqualString(t, "/prometheus/api/v1/query", r.URL.Path)
|
2021-10-18 07:24:52 +00:00
|
|
|
},
|
|
|
|
},
|
2021-06-09 09:20:38 +00:00
|
|
|
{
|
|
|
|
"prometheus range path",
|
|
|
|
true,
|
|
|
|
&VMStorage{
|
|
|
|
dataSourceType: NewPrometheusType(),
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2022-05-13 13:19:32 +00:00
|
|
|
checkEqualString(t, "/api/v1/query_range", r.URL.Path)
|
2021-10-18 07:24:52 +00:00
|
|
|
},
|
|
|
|
},
|
2021-06-09 09:20:38 +00:00
|
|
|
{
|
|
|
|
"prometheus range prefix",
|
|
|
|
true,
|
|
|
|
&VMStorage{
|
|
|
|
dataSourceType: NewPrometheusType(),
|
|
|
|
appendTypePrefix: true,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2022-05-13 13:19:32 +00:00
|
|
|
checkEqualString(t, "/prometheus/api/v1/query_range", r.URL.Path)
|
2021-10-18 07:24:52 +00:00
|
|
|
},
|
|
|
|
},
|
2021-04-30 06:46:03 +00:00
|
|
|
{
|
|
|
|
"graphite path",
|
2021-06-09 09:20:38 +00:00
|
|
|
false,
|
2021-04-30 06:46:03 +00:00
|
|
|
&VMStorage{
|
|
|
|
dataSourceType: NewGraphiteType(),
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
checkEqualString(t, graphitePath, r.URL.Path)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"graphite prefix",
|
2021-06-09 09:20:38 +00:00
|
|
|
false,
|
2021-04-30 06:46:03 +00:00
|
|
|
&VMStorage{
|
|
|
|
dataSourceType: NewGraphiteType(),
|
|
|
|
appendTypePrefix: true,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
checkEqualString(t, graphitePrefix+graphitePath, r.URL.Path)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"default params",
|
2021-06-09 09:20:38 +00:00
|
|
|
false,
|
2021-04-30 06:46:03 +00:00
|
|
|
&VMStorage{},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
exp := fmt.Sprintf("query=%s&time=%d", query, timestamp.Unix())
|
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
2021-06-09 09:20:38 +00:00
|
|
|
{
|
|
|
|
"default range params",
|
|
|
|
true,
|
|
|
|
&VMStorage{},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
exp := fmt.Sprintf("end=%d&query=%s&start=%d", timestamp.Unix(), query, timestamp.Unix())
|
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
2021-04-30 06:46:03 +00:00
|
|
|
{
|
|
|
|
"basic auth",
|
2021-06-09 09:20:38 +00:00
|
|
|
false,
|
2021-09-14 11:32:06 +00:00
|
|
|
&VMStorage{authCfg: authCfg},
|
2021-06-09 09:20:38 +00: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 11:32:06 +00:00
|
|
|
&VMStorage{authCfg: authCfg},
|
2021-04-30 06:46:03 +00:00
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
u, p, _ := r.BasicAuth()
|
|
|
|
checkEqualString(t, "foo", u)
|
|
|
|
checkEqualString(t, "bar", p)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"lookback",
|
2021-06-09 09:20:38 +00:00
|
|
|
false,
|
2021-04-30 06:46:03 +00:00
|
|
|
&VMStorage{
|
|
|
|
lookBack: time.Minute,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
exp := fmt.Sprintf("query=%s&time=%d", query, timestamp.Add(-time.Minute).Unix())
|
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"evaluation interval",
|
2021-06-09 09:20:38 +00:00
|
|
|
false,
|
2021-04-30 06:46:03 +00:00
|
|
|
&VMStorage{
|
|
|
|
evaluationInterval: 15 * time.Second,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2021-04-30 07:01:05 +00:00
|
|
|
evalInterval := 15 * time.Second
|
|
|
|
tt := timestamp.Truncate(evalInterval)
|
|
|
|
exp := fmt.Sprintf("query=%s&step=%v&time=%d", query, evalInterval, tt.Unix())
|
2021-04-30 06:46:03 +00:00
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"lookback + evaluation interval",
|
2021-06-09 09:20:38 +00:00
|
|
|
false,
|
2021-04-30 06:46:03 +00:00
|
|
|
&VMStorage{
|
|
|
|
lookBack: time.Minute,
|
|
|
|
evaluationInterval: 15 * time.Second,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2021-04-30 07:01:05 +00:00
|
|
|
evalInterval := 15 * time.Second
|
2021-04-30 06:46:03 +00:00
|
|
|
tt := timestamp.Add(-time.Minute)
|
2021-04-30 07:01:05 +00:00
|
|
|
tt = tt.Truncate(evalInterval)
|
|
|
|
exp := fmt.Sprintf("query=%s&step=%v&time=%d", query, evalInterval, tt.Unix())
|
2021-04-30 06:46:03 +00:00
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-04-30 07:01:05 +00:00
|
|
|
"step override",
|
2021-06-09 09:20:38 +00:00
|
|
|
false,
|
2021-04-30 06:46:03 +00:00
|
|
|
&VMStorage{
|
|
|
|
queryStep: time.Minute,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2021-12-17 13:26:34 +00:00
|
|
|
exp := fmt.Sprintf("query=%s&step=%ds&time=%d", query, int(time.Minute.Seconds()), timestamp.Unix())
|
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"step to seconds",
|
|
|
|
false,
|
|
|
|
&VMStorage{
|
|
|
|
evaluationInterval: 3 * time.Hour,
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
evalInterval := 3 * time.Hour
|
|
|
|
tt := timestamp.Truncate(evalInterval)
|
|
|
|
exp := fmt.Sprintf("query=%s&step=%ds&time=%d", query, int(evalInterval.Seconds()), tt.Unix())
|
2021-04-30 06:46:03 +00:00
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
2021-05-10 08:11:45 +00:00
|
|
|
{
|
2021-12-02 12:45:08 +00:00
|
|
|
"prometheus extra params",
|
2021-06-09 09:20:38 +00:00
|
|
|
false,
|
2021-05-10 08:11:45 +00:00
|
|
|
&VMStorage{
|
2021-12-02 12:45:08 +00:00
|
|
|
extraParams: url.Values{"round_digits": {"10"}},
|
2021-05-10 08:11:45 +00:00
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
|
|
|
exp := fmt.Sprintf("query=%s&round_digits=10&time=%d", query, timestamp.Unix())
|
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
2021-05-22 21:26:01 +00:00
|
|
|
{
|
2021-12-02 12:45:08 +00:00
|
|
|
"prometheus extra params range",
|
2021-06-09 09:20:38 +00:00
|
|
|
true,
|
|
|
|
&VMStorage{
|
2021-12-02 12:45:08 +00:00
|
|
|
extraParams: url.Values{
|
|
|
|
"nocache": {"1"},
|
|
|
|
"max_lookback": {"1h"},
|
2021-06-09 09:20:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2021-12-02 12:45:08 +00:00
|
|
|
exp := fmt.Sprintf("end=%d&max_lookback=1h&nocache=1&query=%s&start=%d",
|
2021-06-09 09:20:38 +00:00
|
|
|
timestamp.Unix(), query, timestamp.Unix())
|
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
2021-08-31 11:57:47 +00:00
|
|
|
{
|
2021-12-02 12:45:08 +00:00
|
|
|
"graphite extra params",
|
2021-08-31 11:57:47 +00:00
|
|
|
false,
|
|
|
|
&VMStorage{
|
2021-12-02 12:45:08 +00:00
|
|
|
dataSourceType: NewGraphiteType(),
|
|
|
|
extraParams: url.Values{
|
|
|
|
"nocache": {"1"},
|
|
|
|
"max_lookback": {"1h"},
|
2021-08-31 11:57:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
func(t *testing.T, r *http.Request) {
|
2021-12-02 12:45:08 +00:00
|
|
|
exp := fmt.Sprintf("format=json&from=-5min&max_lookback=1h&nocache=1&target=%s&until=now", query)
|
2021-08-31 11:57:47 +00:00
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
},
|
|
|
|
},
|
2021-04-30 06:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-06-09 09:20:38 +00:00
|
|
|
req, err := tc.vm.newRequestPOST()
|
2021-04-30 06:46:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2021-11-05 17:56:37 +00:00
|
|
|
switch tc.vm.dataSourceType.String() {
|
|
|
|
case "prometheus":
|
2021-06-09 09:20:38 +00:00
|
|
|
if tc.queryRange {
|
|
|
|
tc.vm.setPrometheusRangeReqParams(req, query, timestamp, timestamp)
|
|
|
|
} else {
|
|
|
|
tc.vm.setPrometheusInstantReqParams(req, query, timestamp)
|
|
|
|
}
|
2021-11-05 17:56:37 +00:00
|
|
|
case "graphite":
|
2021-06-09 09:20:38 +00:00
|
|
|
tc.vm.setGraphiteReqParams(req, query, timestamp)
|
|
|
|
}
|
2021-04-30 06:46:03 +00:00
|
|
|
tc.checkFn(t, req)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 11:09:12 +00:00
|
|
|
func TestAuthConfig(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range testCases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
vm := tt.vmFn()
|
|
|
|
req, err := vm.newRequestPOST()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
tt.checkFn(t, req)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 06:46:03 +00:00
|
|
|
func checkEqualString(t *testing.T, exp, got string) {
|
|
|
|
t.Helper()
|
|
|
|
if got != exp {
|
2021-12-02 12:45:08 +00:00
|
|
|
t.Errorf("expected to get: \n%q; \ngot: \n%q", exp, got)
|
2021-04-30 06:46:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-09 09:20:38 +00: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)
|
|
|
|
}
|
|
|
|
}
|