Commit cedaf41a by Emil Hessman Committed by GitHub

Chore: Rewrite opentsdb test to standard library (#29792)

parent 2b5a88bf
......@@ -5,14 +5,13 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/tsdb"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/require"
)
func TestOpenTsdbExecutor(t *testing.T) {
Convey("OpenTsdb query testing", t, func() {
exec := &OpenTsdbExecutor{}
Convey("Build metric with downsampling enabled", func() {
t.Run("Build metric with downsampling enabled", func(t *testing.T) {
query := &tsdb.Query{
Model: simplejson.New(),
}
......@@ -26,13 +25,13 @@ func TestOpenTsdbExecutor(t *testing.T) {
metric := exec.buildMetric(query)
So(len(metric), ShouldEqual, 3)
So(metric["metric"], ShouldEqual, "cpu.average.percent")
So(metric["aggregator"], ShouldEqual, "avg")
So(metric["downsample"], ShouldEqual, "1m-avg")
require.Len(t, metric, 3)
require.Equal(t, "cpu.average.percent", metric["metric"])
require.Equal(t, "avg", metric["aggregator"])
require.Equal(t, "1m-avg", metric["downsample"])
})
Convey("Build metric with downsampling disabled", func() {
t.Run("Build metric with downsampling disabled", func(t *testing.T) {
query := &tsdb.Query{
Model: simplejson.New(),
}
......@@ -46,12 +45,12 @@ func TestOpenTsdbExecutor(t *testing.T) {
metric := exec.buildMetric(query)
So(len(metric), ShouldEqual, 2)
So(metric["metric"], ShouldEqual, "cpu.average.percent")
So(metric["aggregator"], ShouldEqual, "avg")
require.Len(t, metric, 2)
require.Equal(t, "cpu.average.percent", metric["metric"])
require.Equal(t, "avg", metric["aggregator"])
})
Convey("Build metric with downsampling enabled with params", func() {
t.Run("Build metric with downsampling enabled with params", func(t *testing.T) {
query := &tsdb.Query{
Model: simplejson.New(),
}
......@@ -65,13 +64,13 @@ func TestOpenTsdbExecutor(t *testing.T) {
metric := exec.buildMetric(query)
So(len(metric), ShouldEqual, 3)
So(metric["metric"], ShouldEqual, "cpu.average.percent")
So(metric["aggregator"], ShouldEqual, "avg")
So(metric["downsample"], ShouldEqual, "5m-sum-null")
require.Len(t, metric, 3)
require.Equal(t, "cpu.average.percent", metric["metric"])
require.Equal(t, "avg", metric["aggregator"])
require.Equal(t, "5m-sum-null", metric["downsample"])
})
Convey("Build metric with tags with downsampling disabled", func() {
t.Run("Build metric with tags with downsampling disabled", func(t *testing.T) {
query := &tsdb.Query{
Model: simplejson.New(),
}
......@@ -90,17 +89,19 @@ func TestOpenTsdbExecutor(t *testing.T) {
metric := exec.buildMetric(query)
So(len(metric), ShouldEqual, 3)
So(metric["metric"], ShouldEqual, "cpu.average.percent")
So(metric["aggregator"], ShouldEqual, "avg")
So(metric["downsample"], ShouldEqual, nil)
So(len(metric["tags"].(map[string]interface{})), ShouldEqual, 2)
So(metric["tags"].(map[string]interface{})["env"], ShouldEqual, "prod")
So(metric["tags"].(map[string]interface{})["app"], ShouldEqual, "grafana")
So(metric["tags"].(map[string]interface{})["ip"], ShouldEqual, nil)
require.Len(t, metric, 3)
require.Equal(t, "cpu.average.percent", metric["metric"])
require.Equal(t, "avg", metric["aggregator"])
require.Nil(t, metric["downsample"])
metricTags := metric["tags"].(map[string]interface{})
require.Len(t, metricTags, 2)
require.Equal(t, "prod", metricTags["env"])
require.Equal(t, "grafana", metricTags["app"])
require.Nil(t, metricTags["ip"])
})
Convey("Build metric with rate enabled but counter disabled", func() {
t.Run("Build metric with rate enabled but counter disabled", func(t *testing.T) {
query := &tsdb.Query{
Model: simplejson.New(),
}
......@@ -118,18 +119,21 @@ func TestOpenTsdbExecutor(t *testing.T) {
metric := exec.buildMetric(query)
So(len(metric), ShouldEqual, 5)
So(metric["metric"], ShouldEqual, "cpu.average.percent")
So(metric["aggregator"], ShouldEqual, "avg")
So(len(metric["tags"].(map[string]interface{})), ShouldEqual, 2)
So(metric["tags"].(map[string]interface{})["env"], ShouldEqual, "prod")
So(metric["tags"].(map[string]interface{})["app"], ShouldEqual, "grafana")
So(metric["tags"].(map[string]interface{})["ip"], ShouldEqual, nil)
So(metric["rate"], ShouldEqual, true)
So(metric["rateOptions"].(map[string]interface{})["counter"], ShouldEqual, false)
require.Len(t, metric, 5)
require.Equal(t, "cpu.average.percent", metric["metric"])
require.Equal(t, "avg", metric["aggregator"])
metricTags := metric["tags"].(map[string]interface{})
require.Len(t, metricTags, 2)
require.Equal(t, "prod", metricTags["env"])
require.Equal(t, "grafana", metricTags["app"])
require.Nil(t, metricTags["ip"])
require.True(t, metric["rate"].(bool))
require.False(t, metric["rateOptions"].(map[string]interface{})["counter"].(bool))
})
Convey("Build metric with rate and counter enabled", func() {
t.Run("Build metric with rate and counter enabled", func(t *testing.T) {
query := &tsdb.Query{
Model: simplejson.New(),
}
......@@ -149,18 +153,21 @@ func TestOpenTsdbExecutor(t *testing.T) {
metric := exec.buildMetric(query)
So(len(metric), ShouldEqual, 5)
So(metric["metric"], ShouldEqual, "cpu.average.percent")
So(metric["aggregator"], ShouldEqual, "avg")
So(len(metric["tags"].(map[string]interface{})), ShouldEqual, 2)
So(metric["tags"].(map[string]interface{})["env"], ShouldEqual, "prod")
So(metric["tags"].(map[string]interface{})["app"], ShouldEqual, "grafana")
So(metric["tags"].(map[string]interface{})["ip"], ShouldEqual, nil)
So(metric["rate"], ShouldEqual, true)
So(len(metric["rateOptions"].(map[string]interface{})), ShouldEqual, 3)
So(metric["rateOptions"].(map[string]interface{})["counter"], ShouldEqual, true)
So(metric["rateOptions"].(map[string]interface{})["counterMax"], ShouldEqual, 45)
So(metric["rateOptions"].(map[string]interface{})["resetValue"], ShouldEqual, 60)
})
require.Len(t, metric, 5)
require.Equal(t, "cpu.average.percent", metric["metric"])
require.Equal(t, "avg", metric["aggregator"])
metricTags := metric["tags"].(map[string]interface{})
require.Len(t, metricTags, 2)
require.Equal(t, "prod", metricTags["env"])
require.Equal(t, "grafana", metricTags["app"])
require.Nil(t, metricTags["ip"])
require.True(t, metric["rate"].(bool))
metricRateOptions := metric["rateOptions"].(map[string]interface{})
require.Len(t, metricRateOptions, 3)
require.True(t, metricRateOptions["counter"].(bool))
require.Equal(t, float64(45), metricRateOptions["counterMax"])
require.Equal(t, float64(60), metricRateOptions["resetValue"])
})
}
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