Commit 3c1bcc72 by Emil Hessman Committed by GitHub

Chore: Rewrite tsdb prometheus test to standard library (#29592)

Signed-off-by: Emil Hessman <emil@hessman.se>
parent f326b79c
...@@ -4,156 +4,125 @@ import ( ...@@ -4,156 +4,125 @@ import (
"testing" "testing"
"time" "time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
"github.com/grafana/grafana/pkg/components/simplejson"
p "github.com/prometheus/common/model" p "github.com/prometheus/common/model"
. "github.com/smartystreets/goconvey/convey" "github.com/stretchr/testify/require"
) )
func TestPrometheus(t *testing.T) { func TestPrometheus(t *testing.T) {
Convey("Prometheus", t, func() { dsInfo := &models.DataSource{
dsInfo := &models.DataSource{ JsonData: simplejson.New(),
JsonData: simplejson.New(), }
t.Run("converting metric name", func(t *testing.T) {
metric := map[p.LabelName]p.LabelValue{
p.LabelName("app"): p.LabelValue("backend"),
p.LabelName("device"): p.LabelValue("mobile"),
} }
Convey("converting metric name", func() { query := &PrometheusQuery{
metric := map[p.LabelName]p.LabelValue{ LegendFormat: "legend {{app}} {{ device }} {{broken}}",
p.LabelName("app"): p.LabelValue("backend"), }
p.LabelName("device"): p.LabelValue("mobile"),
}
query := &PrometheusQuery{
LegendFormat: "legend {{app}} {{ device }} {{broken}}",
}
So(formatLegend(metric, query), ShouldEqual, "legend backend mobile ") require.Equal(t, "legend backend mobile ", formatLegend(metric, query))
}) })
Convey("build full series name", func() { t.Run("build full series name", func(t *testing.T) {
metric := map[p.LabelName]p.LabelValue{ metric := map[p.LabelName]p.LabelValue{
p.LabelName(p.MetricNameLabel): p.LabelValue("http_request_total"), p.LabelName(p.MetricNameLabel): p.LabelValue("http_request_total"),
p.LabelName("app"): p.LabelValue("backend"), p.LabelName("app"): p.LabelValue("backend"),
p.LabelName("device"): p.LabelValue("mobile"), p.LabelName("device"): p.LabelValue("mobile"),
} }
query := &PrometheusQuery{ query := &PrometheusQuery{
LegendFormat: "", LegendFormat: "",
} }
So(formatLegend(metric, query), ShouldEqual, `http_request_total{app="backend", device="mobile"}`) require.Equal(t, `http_request_total{app="backend", device="mobile"}`, formatLegend(metric, query))
}) })
Convey("parsing query model with step", func() { t.Run("parsing query model with step", func(t *testing.T) {
json := `{ json := `{
"expr": "go_goroutines", "expr": "go_goroutines",
"format": "time_series", "format": "time_series",
"refId": "A" "refId": "A"
}` }`
jsonModel, _ := simplejson.NewJson([]byte(json)) jsonModel, _ := simplejson.NewJson([]byte(json))
queryContext := &tsdb.TsdbQuery{} queryContext := &tsdb.TsdbQuery{}
queryModels := []*tsdb.Query{ queryModels := []*tsdb.Query{
{Model: jsonModel}, {Model: jsonModel},
} }
Convey("with 48h time range", func() {
queryContext.TimeRange = tsdb.NewTimeRange("12h", "now")
models, err := parseQuery(dsInfo, queryModels, queryContext) queryContext.TimeRange = tsdb.NewTimeRange("12h", "now")
So(err, ShouldBeNil)
model := models[0] models, err := parseQuery(dsInfo, queryModels, queryContext)
So(model.Step, ShouldEqual, time.Second*30) require.NoError(t, err)
}) require.Equal(t, time.Second*30, models[0].Step)
}) })
Convey("parsing query model without step parameter", func() { t.Run("parsing query model without step parameter", func(t *testing.T) {
json := `{ json := `{
"expr": "go_goroutines", "expr": "go_goroutines",
"format": "time_series", "format": "time_series",
"intervalFactor": 1, "intervalFactor": 1,
"refId": "A" "refId": "A"
}` }`
jsonModel, _ := simplejson.NewJson([]byte(json)) jsonModel, _ := simplejson.NewJson([]byte(json))
queryContext := &tsdb.TsdbQuery{} queryContext := &tsdb.TsdbQuery{}
queryModels := []*tsdb.Query{ queryModels := []*tsdb.Query{
{Model: jsonModel}, {Model: jsonModel},
} }
Convey("with 48h time range", func() {
queryContext.TimeRange = tsdb.NewTimeRange("48h", "now")
models, err := parseQuery(dsInfo, queryModels, queryContext)
So(err, ShouldBeNil)
model := models[0]
So(model.Step, ShouldEqual, time.Minute*2)
})
Convey("with 1h time range", func() {
queryContext.TimeRange = tsdb.NewTimeRange("1h", "now")
models, err := parseQuery(dsInfo, queryModels, queryContext)
So(err, ShouldBeNil) queryContext.TimeRange = tsdb.NewTimeRange("48h", "now")
models, err := parseQuery(dsInfo, queryModels, queryContext)
require.NoError(t, err)
require.Equal(t, time.Minute*2, models[0].Step)
model := models[0] queryContext.TimeRange = tsdb.NewTimeRange("1h", "now")
So(model.Step, ShouldEqual, time.Second*15) models, err = parseQuery(dsInfo, queryModels, queryContext)
}) require.NoError(t, err)
}) require.Equal(t, time.Second*15, models[0].Step)
})
Convey("parsing query model with intervalFactor", func() { t.Run("parsing query model with high intervalFactor", func(t *testing.T) {
Convey("high intervalFactor", func() { json := `{
json := `{
"expr": "go_goroutines", "expr": "go_goroutines",
"format": "time_series", "format": "time_series",
"intervalFactor": 10, "intervalFactor": 10,
"refId": "A" "refId": "A"
}` }`
jsonModel, _ := simplejson.NewJson([]byte(json)) jsonModel, _ := simplejson.NewJson([]byte(json))
queryContext := &tsdb.TsdbQuery{} queryContext := &tsdb.TsdbQuery{}
queryModels := []*tsdb.Query{ queryModels := []*tsdb.Query{
{Model: jsonModel}, {Model: jsonModel},
} }
Convey("with 48h time range", func() {
queryContext.TimeRange = tsdb.NewTimeRange("48h", "now")
models, err := parseQuery(dsInfo, queryModels, queryContext)
So(err, ShouldBeNil) queryContext.TimeRange = tsdb.NewTimeRange("48h", "now")
model := models[0] models, err := parseQuery(dsInfo, queryModels, queryContext)
So(model.Step, ShouldEqual, time.Minute*20) require.NoError(t, err)
}) require.Equal(t, time.Minute*20, models[0].Step)
}) })
Convey("low intervalFactor", func() { t.Run("parsing query model with low intervalFactor", func(t *testing.T) {
json := `{ json := `{
"expr": "go_goroutines", "expr": "go_goroutines",
"format": "time_series", "format": "time_series",
"intervalFactor": 1, "intervalFactor": 1,
"refId": "A" "refId": "A"
}` }`
jsonModel, _ := simplejson.NewJson([]byte(json)) jsonModel, _ := simplejson.NewJson([]byte(json))
queryContext := &tsdb.TsdbQuery{} queryContext := &tsdb.TsdbQuery{}
queryModels := []*tsdb.Query{ queryModels := []*tsdb.Query{
{Model: jsonModel}, {Model: jsonModel},
} }
Convey("with 48h time range", func() {
queryContext.TimeRange = tsdb.NewTimeRange("48h", "now")
models, err := parseQuery(dsInfo, queryModels, queryContext)
So(err, ShouldBeNil) queryContext.TimeRange = tsdb.NewTimeRange("48h", "now")
model := models[0] models, err := parseQuery(dsInfo, queryModels, queryContext)
So(model.Step, ShouldEqual, time.Minute*2) require.NoError(t, err)
}) require.Equal(t, time.Minute*2, models[0].Step)
})
})
}) })
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment