From 7d7e727f225198d75a463a2f0f88d61650b24d63 Mon Sep 17 00:00:00 2001 From: Matheus Alcantara Date: Fri, 2 Oct 2020 14:40:15 -0300 Subject: [PATCH] Cloudwatch: Convert go convey tests to standard test library (#28003) * Cloudwatch: Convert go convey tests to standard test library * Fix tests Signed-off-by: Arve Knudsen Co-authored-by: Arve Knudsen --- pkg/tsdb/cloudwatch/cloudwatch_query_test.go | 299 ++++----- .../metric_data_query_builder_test.go | 358 ++++++----- pkg/tsdb/cloudwatch/query_transformer_test.go | 271 ++++----- pkg/tsdb/cloudwatch/request_parser_test.go | 360 ++++++----- pkg/tsdb/cloudwatch/response_parser_test.go | 566 +++++++++--------- 5 files changed, 893 insertions(+), 961 deletions(-) diff --git a/pkg/tsdb/cloudwatch/cloudwatch_query_test.go b/pkg/tsdb/cloudwatch/cloudwatch_query_test.go index 17a3ee4f57d..f4e2b6d7493 100644 --- a/pkg/tsdb/cloudwatch/cloudwatch_query_test.go +++ b/pkg/tsdb/cloudwatch/cloudwatch_query_test.go @@ -3,188 +3,137 @@ package cloudwatch import ( "testing" - . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" ) func TestCloudWatchQuery(t *testing.T) { - Convey("TestCloudWatchQuery", t, func() { - Convey("and SEARCH(someexpression) was specified in the query editor", func() { - query := &cloudWatchQuery{ - RefId: "A", - Region: "us-east-1", - Expression: "SEARCH(someexpression)", - Stats: "Average", - Period: 300, - Id: "id1", - } + t.Run("SEARCH(someexpression) was specified in the query editor", func(t *testing.T) { + query := &cloudWatchQuery{ + RefId: "A", + Region: "us-east-1", + Expression: "SEARCH(someexpression)", + Stats: "Average", + Period: 300, + Id: "id1", + } - Convey("it is a search expression", func() { - So(query.isSearchExpression(), ShouldBeTrue) - }) + assert.True(t, query.isSearchExpression(), "Expected a search expression") + assert.False(t, query.isMathExpression(), "Expected not math expression") + }) - Convey("it is not math expressions", func() { - So(query.isMathExpression(), ShouldBeFalse) - }) + t.Run("No expression, no multi dimension key values and no * was used", func(t *testing.T) { + query := &cloudWatchQuery{ + RefId: "A", + Region: "us-east-1", + Expression: "", + Stats: "Average", + Period: 300, + Id: "id1", + MatchExact: true, + Dimensions: map[string][]string{ + "InstanceId": {"i-12345678"}, + }, + } + + assert.False(t, query.isSearchExpression(), "Expected not a search expression") + assert.False(t, query.isMathExpression(), "Expected not math expressions") + }) + + t.Run("No expression but multi dimension key values exist", func(t *testing.T) { + query := &cloudWatchQuery{ + RefId: "A", + Region: "us-east-1", + Expression: "", + Stats: "Average", + Period: 300, + Id: "id1", + Dimensions: map[string][]string{ + "InstanceId": {"i-12345678", "i-34562312"}, + }, + } + + assert.True(t, query.isSearchExpression(), "Expected a search expression") + assert.False(t, query.isMathExpression(), "Expected not math expressions") + }) + + t.Run("No expression but dimension values has *", func(t *testing.T) { + query := &cloudWatchQuery{ + RefId: "A", + Region: "us-east-1", + Expression: "", + Stats: "Average", + Period: 300, + Id: "id1", + Dimensions: map[string][]string{ + "InstanceId": {"i-12345678", "*"}, + "InstanceType": {"abc", "def"}, + }, + } + + assert.True(t, query.isSearchExpression(), "Expected a search expression") + assert.False(t, query.isMathExpression(), "Expected not math expression") + }) + + t.Run("Query has a multi-valued dimension", func(t *testing.T) { + query := &cloudWatchQuery{ + RefId: "A", + Region: "us-east-1", + Expression: "", + Stats: "Average", + Period: 300, + Id: "id1", + Dimensions: map[string][]string{ + "InstanceId": {"i-12345678", "i-12345679"}, + "InstanceType": {"abc"}, + }, + } + + assert.True(t, query.isSearchExpression(), "Expected a search expression") + assert.True(t, query.isMultiValuedDimensionExpression(), "Expected a multi-valued dimension expression") + }) + + t.Run("No dimensions were added", func(t *testing.T) { + query := &cloudWatchQuery{ + RefId: "A", + Region: "us-east-1", + Expression: "", + Stats: "Average", + Period: 300, + Id: "id1", + MatchExact: false, + Dimensions: make(map[string][]string), + } + t.Run("Match exact is false", func(t *testing.T) { + query.MatchExact = false + assert.True(t, query.isSearchExpression(), "Expected a search expression") + assert.False(t, query.isMathExpression(), "Expected not math expression") + assert.False(t, query.isMetricStat(), "Expected not metric stat") }) - Convey("and no expression, no multi dimension key values and no * was used", func() { - query := &cloudWatchQuery{ - RefId: "A", - Region: "us-east-1", - Expression: "", - Stats: "Average", - Period: 300, - Id: "id1", - MatchExact: true, - Dimensions: map[string][]string{ - "InstanceId": {"i-12345678"}, - }, - } - - Convey("it is not a search expression", func() { - So(query.isSearchExpression(), ShouldBeFalse) - }) - - Convey("it is not math expressions", func() { - So(query.isMathExpression(), ShouldBeFalse) - }) - }) - - Convey("and no expression but multi dimension key values exist", func() { - query := &cloudWatchQuery{ - RefId: "A", - Region: "us-east-1", - Expression: "", - Stats: "Average", - Period: 300, - Id: "id1", - Dimensions: map[string][]string{ - "InstanceId": {"i-12345678", "i-34562312"}, - }, - } - - Convey("it is a search expression", func() { - So(query.isSearchExpression(), ShouldBeTrue) - }) - - Convey("it is not math expressions", func() { - So(query.isMathExpression(), ShouldBeFalse) - }) - }) - - Convey("and no expression but dimension values has *", func() { - query := &cloudWatchQuery{ - RefId: "A", - Region: "us-east-1", - Expression: "", - Stats: "Average", - Period: 300, - Id: "id1", - Dimensions: map[string][]string{ - "InstanceId": {"i-12345678", "*"}, - "InstanceType": {"abc", "def"}, - }, - } - - Convey("it is not a search expression", func() { - So(query.isSearchExpression(), ShouldBeTrue) - }) - - Convey("it is not math expressions", func() { - So(query.isMathExpression(), ShouldBeFalse) - }) - }) - - Convey("and query has a multi-valued dimension", func() { - query := &cloudWatchQuery{ - RefId: "A", - Region: "us-east-1", - Expression: "", - Stats: "Average", - Period: 300, - Id: "id1", - Dimensions: map[string][]string{ - "InstanceId": {"i-12345678", "i-12345679"}, - "InstanceType": {"abc"}, - }, - } - - Convey("it is a search expression", func() { - So(query.isSearchExpression(), ShouldBeTrue) - }) - - Convey("it is a multi-valued dimension expression", func() { - So(query.isMultiValuedDimensionExpression(), ShouldBeTrue) - }) - }) - - Convey("and no dimensions were added", func() { - query := &cloudWatchQuery{ - RefId: "A", - Region: "us-east-1", - Expression: "", - Stats: "Average", - Period: 300, - Id: "id1", - MatchExact: false, - Dimensions: make(map[string][]string), - } - Convey("and match exact is false", func() { - query.MatchExact = false - Convey("it is a search expression", func() { - So(query.isSearchExpression(), ShouldBeTrue) - }) - - Convey("it is not math expressions", func() { - So(query.isMathExpression(), ShouldBeFalse) - }) - - Convey("it is not metric stat", func() { - So(query.isMetricStat(), ShouldBeFalse) - }) - }) - - Convey("and match exact is true", func() { - query.MatchExact = true - Convey("it is a search expression", func() { - So(query.isSearchExpression(), ShouldBeFalse) - }) - - Convey("it is not math expressions", func() { - So(query.isMathExpression(), ShouldBeFalse) - }) - - Convey("it is a metric stat", func() { - So(query.isMetricStat(), ShouldBeTrue) - }) - }) - }) - - Convey("and match exact is", func() { - query := &cloudWatchQuery{ - RefId: "A", - Region: "us-east-1", - Expression: "", - Stats: "Average", - Period: 300, - Id: "id1", - MatchExact: false, - Dimensions: map[string][]string{ - "InstanceId": {"i-12345678"}, - }, - } - - Convey("it is a search expression", func() { - So(query.isSearchExpression(), ShouldBeTrue) - }) - - Convey("it is not math expressions", func() { - So(query.isMathExpression(), ShouldBeFalse) - }) - - Convey("it is not metric stat", func() { - So(query.isMetricStat(), ShouldBeFalse) - }) + t.Run("Match exact is true", func(t *testing.T) { + query.MatchExact = true + assert.False(t, query.isSearchExpression(), "Exxpected not search expression") + assert.False(t, query.isMathExpression(), "Expected not math expression") + assert.True(t, query.isMetricStat(), "Expected a metric stat") }) }) + + t.Run("Match exact is", func(t *testing.T) { + query := &cloudWatchQuery{ + RefId: "A", + Region: "us-east-1", + Expression: "", + Stats: "Average", + Period: 300, + Id: "id1", + MatchExact: false, + Dimensions: map[string][]string{ + "InstanceId": {"i-12345678"}, + }, + } + + assert.True(t, query.isSearchExpression(), "Expected search expression") + assert.False(t, query.isMathExpression(), "Expected not math expression") + assert.False(t, query.isMetricStat(), "Expected not metric stat") + }) } diff --git a/pkg/tsdb/cloudwatch/metric_data_query_builder_test.go b/pkg/tsdb/cloudwatch/metric_data_query_builder_test.go index f81757e9ea4..dc0de6e1e04 100644 --- a/pkg/tsdb/cloudwatch/metric_data_query_builder_test.go +++ b/pkg/tsdb/cloudwatch/metric_data_query_builder_test.go @@ -3,199 +3,195 @@ package cloudwatch import ( "testing" - . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" ) -func TestMetricDataQueryBuilder(t *testing.T) { - Convey("TestMetricDataQueryBuilder", t, func() { - Convey("buildSearchExpression", func() { - Convey("and query should be matched exact", func() { - matchExact := true - Convey("and query has three dimension values for a given dimension key", func() { - query := &cloudWatchQuery{ - Namespace: "AWS/EC2", - MetricName: "CPUUtilization", - Dimensions: map[string][]string{ - "LoadBalancer": {"lb1", "lb2", "lb3"}, - }, - Period: 300, - Expression: "", - MatchExact: matchExact, - } +func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) { + t.Run("Query should be matched exact", func(t *testing.T) { + const matchExact = true - res := buildSearchExpression(query, "Average") - So(res, ShouldEqual, `REMOVE_EMPTY(SEARCH('{AWS/EC2,"LoadBalancer"} MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`) - }) - - Convey("and query has three dimension values for two given dimension keys", func() { - query := &cloudWatchQuery{ - Namespace: "AWS/EC2", - MetricName: "CPUUtilization", - Dimensions: map[string][]string{ - "LoadBalancer": {"lb1", "lb2", "lb3"}, - "InstanceId": {"i-123", "i-456", "i-789"}, - }, - Period: 300, - Expression: "", - MatchExact: matchExact, - } - - res := buildSearchExpression(query, "Average") - So(res, ShouldEqual, `REMOVE_EMPTY(SEARCH('{AWS/EC2,"InstanceId","LoadBalancer"} MetricName="CPUUtilization" "InstanceId"=("i-123" OR "i-456" OR "i-789") "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`) - }) - - Convey("and no OR operator was added if a star was used for dimension value", func() { - query := &cloudWatchQuery{ - Namespace: "AWS/EC2", - MetricName: "CPUUtilization", - Dimensions: map[string][]string{ - "LoadBalancer": {"*"}, - }, - Period: 300, - Expression: "", - MatchExact: matchExact, - } - - res := buildSearchExpression(query, "Average") - So(res, ShouldNotContainSubstring, "OR") - }) - - Convey("and query has one dimension key with a * value", func() { - query := &cloudWatchQuery{ - Namespace: "AWS/EC2", - MetricName: "CPUUtilization", - Dimensions: map[string][]string{ - "LoadBalancer": {"*"}, - }, - Period: 300, - Expression: "", - MatchExact: matchExact, - } - - res := buildSearchExpression(query, "Average") - So(res, ShouldEqual, `REMOVE_EMPTY(SEARCH('{AWS/EC2,"LoadBalancer"} MetricName="CPUUtilization"', 'Average', 300))`) - }) - - Convey("and query has three dimension values for two given dimension keys, and one value is a star", func() { - query := &cloudWatchQuery{ - Namespace: "AWS/EC2", - MetricName: "CPUUtilization", - Dimensions: map[string][]string{ - "LoadBalancer": {"lb1", "lb2", "lb3"}, - "InstanceId": {"i-123", "*", "i-789"}, - }, - Period: 300, - Expression: "", - MatchExact: matchExact, - } - - res := buildSearchExpression(query, "Average") - So(res, ShouldEqual, `REMOVE_EMPTY(SEARCH('{AWS/EC2,"InstanceId","LoadBalancer"} MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`) - }) - - Convey("and query has a dimension key with a space", func() { - query := &cloudWatchQuery{ - Namespace: "AWS/Kafka", - MetricName: "CpuUser", - Dimensions: map[string][]string{ - "Cluster Name": {"dev-cluster"}, - }, - Period: 300, - Expression: "", - MatchExact: matchExact, - } - - res := buildSearchExpression(query, "Average") - So(res, ShouldEqual, `REMOVE_EMPTY(SEARCH('{AWS/Kafka,"Cluster Name"} MetricName="CpuUser" "Cluster Name"="dev-cluster"', 'Average', 300))`) - }) - }) - - Convey("and query should not be matched exact", func() { - matchExact := false - Convey("and query has three dimension values for a given dimension key", func() { - query := &cloudWatchQuery{ - Namespace: "AWS/EC2", - MetricName: "CPUUtilization", - Dimensions: map[string][]string{ - "LoadBalancer": {"lb1", "lb2", "lb3"}, - }, - Period: 300, - Expression: "", - MatchExact: matchExact, - } - - res := buildSearchExpression(query, "Average") - So(res, ShouldEqual, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`) - }) - - Convey("and query has three dimension values for two given dimension keys", func() { - query := &cloudWatchQuery{ - Namespace: "AWS/EC2", - MetricName: "CPUUtilization", - Dimensions: map[string][]string{ - "LoadBalancer": {"lb1", "lb2", "lb3"}, - "InstanceId": {"i-123", "i-456", "i-789"}, - }, - Period: 300, - Expression: "", - MatchExact: matchExact, - } - - res := buildSearchExpression(query, "Average") - So(res, ShouldEqual, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization" "InstanceId"=("i-123" OR "i-456" OR "i-789") "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`) - }) - - Convey("and query has one dimension key with a * value", func() { - query := &cloudWatchQuery{ - Namespace: "AWS/EC2", - MetricName: "CPUUtilization", - Dimensions: map[string][]string{ - "LoadBalancer": {"*"}, - }, - Period: 300, - Expression: "", - MatchExact: matchExact, - } - - res := buildSearchExpression(query, "Average") - So(res, ShouldEqual, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization" "LoadBalancer"', 'Average', 300))`) - }) - - Convey("and query has three dimension values for two given dimension keys, and one value is a star", func() { - query := &cloudWatchQuery{ - Namespace: "AWS/EC2", - MetricName: "CPUUtilization", - Dimensions: map[string][]string{ - "LoadBalancer": {"lb1", "lb2", "lb3"}, - "InstanceId": {"i-123", "*", "i-789"}, - }, - Period: 300, - Expression: "", - MatchExact: matchExact, - } - - res := buildSearchExpression(query, "Average") - So(res, ShouldEqual, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3") "InstanceId"', 'Average', 300))`) - }) - }) - }) - - Convey("and query has invalid characters in dimension values", func() { + t.Run("Query has three dimension values for a given dimension key", func(t *testing.T) { query := &cloudWatchQuery{ Namespace: "AWS/EC2", MetricName: "CPUUtilization", Dimensions: map[string][]string{ - "lb4": {`lb4""`}, + "LoadBalancer": {"lb1", "lb2", "lb3"}, }, Period: 300, Expression: "", - MatchExact: true, + MatchExact: matchExact, } - res := buildSearchExpression(query, "Average") - Convey("it should escape double quotes", func() { - So(res, ShouldContainSubstring, `lb4\"\"`) - }) + res := buildSearchExpression(query, "Average") + assert.Equal(t, `REMOVE_EMPTY(SEARCH('{AWS/EC2,"LoadBalancer"} MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res) + }) + + t.Run("Query has three dimension values for two given dimension keys", func(t *testing.T) { + query := &cloudWatchQuery{ + Namespace: "AWS/EC2", + MetricName: "CPUUtilization", + Dimensions: map[string][]string{ + "LoadBalancer": {"lb1", "lb2", "lb3"}, + "InstanceId": {"i-123", "i-456", "i-789"}, + }, + Period: 300, + Expression: "", + MatchExact: matchExact, + } + + res := buildSearchExpression(query, "Average") + assert.Equal(t, `REMOVE_EMPTY(SEARCH('{AWS/EC2,"InstanceId","LoadBalancer"} MetricName="CPUUtilization" "InstanceId"=("i-123" OR "i-456" OR "i-789") "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res) + }) + + t.Run("No OR operator was added if a star was used for dimension value", func(t *testing.T) { + query := &cloudWatchQuery{ + Namespace: "AWS/EC2", + MetricName: "CPUUtilization", + Dimensions: map[string][]string{ + "LoadBalancer": {"*"}, + }, + Period: 300, + Expression: "", + MatchExact: matchExact, + } + + res := buildSearchExpression(query, "Average") + assert.NotContains(t, res, "OR") + }) + + t.Run("Query has one dimension key with a * value", func(t *testing.T) { + query := &cloudWatchQuery{ + Namespace: "AWS/EC2", + MetricName: "CPUUtilization", + Dimensions: map[string][]string{ + "LoadBalancer": {"*"}, + }, + Period: 300, + Expression: "", + MatchExact: matchExact, + } + + res := buildSearchExpression(query, "Average") + assert.Equal(t, res, `REMOVE_EMPTY(SEARCH('{AWS/EC2,"LoadBalancer"} MetricName="CPUUtilization"', 'Average', 300))`) + }) + + t.Run("Query has three dimension values for two given dimension keys, and one value is a star", func(t *testing.T) { + query := &cloudWatchQuery{ + Namespace: "AWS/EC2", + MetricName: "CPUUtilization", + Dimensions: map[string][]string{ + "LoadBalancer": {"lb1", "lb2", "lb3"}, + "InstanceId": {"i-123", "*", "i-789"}, + }, + Period: 300, + Expression: "", + MatchExact: matchExact, + } + + res := buildSearchExpression(query, "Average") + assert.Equal(t, `REMOVE_EMPTY(SEARCH('{AWS/EC2,"InstanceId","LoadBalancer"} MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res) + }) + + t.Run("Query has a dimension key with a space", func(t *testing.T) { + query := &cloudWatchQuery{ + Namespace: "AWS/Kafka", + MetricName: "CpuUser", + Dimensions: map[string][]string{ + "Cluster Name": {"dev-cluster"}, + }, + Period: 300, + Expression: "", + MatchExact: matchExact, + } + + res := buildSearchExpression(query, "Average") + assert.Equal(t, `REMOVE_EMPTY(SEARCH('{AWS/Kafka,"Cluster Name"} MetricName="CpuUser" "Cluster Name"="dev-cluster"', 'Average', 300))`, res) }) }) + + t.Run("Query should not be matched exact", func(t *testing.T) { + const matchExact = false + + t.Run("Query has three dimension values for a given dimension key", func(t *testing.T) { + query := &cloudWatchQuery{ + Namespace: "AWS/EC2", + MetricName: "CPUUtilization", + Dimensions: map[string][]string{ + "LoadBalancer": {"lb1", "lb2", "lb3"}, + }, + Period: 300, + Expression: "", + MatchExact: matchExact, + } + + res := buildSearchExpression(query, "Average") + assert.Equal(t, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res) + }) + + t.Run("Query has three dimension values for two given dimension keys", func(t *testing.T) { + query := &cloudWatchQuery{ + Namespace: "AWS/EC2", + MetricName: "CPUUtilization", + Dimensions: map[string][]string{ + "LoadBalancer": {"lb1", "lb2", "lb3"}, + "InstanceId": {"i-123", "i-456", "i-789"}, + }, + Period: 300, + Expression: "", + MatchExact: matchExact, + } + + res := buildSearchExpression(query, "Average") + assert.Equal(t, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization" "InstanceId"=("i-123" OR "i-456" OR "i-789") "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res) + }) + + t.Run("Query has one dimension key with a * value", func(t *testing.T) { + query := &cloudWatchQuery{ + Namespace: "AWS/EC2", + MetricName: "CPUUtilization", + Dimensions: map[string][]string{ + "LoadBalancer": {"*"}, + }, + Period: 300, + Expression: "", + MatchExact: matchExact, + } + + res := buildSearchExpression(query, "Average") + assert.Equal(t, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization" "LoadBalancer"', 'Average', 300))`, res) + }) + + t.Run("query has three dimension values for two given dimension keys, and one value is a star", func(t *testing.T) { + query := &cloudWatchQuery{ + Namespace: "AWS/EC2", + MetricName: "CPUUtilization", + Dimensions: map[string][]string{ + "LoadBalancer": {"lb1", "lb2", "lb3"}, + "InstanceId": {"i-123", "*", "i-789"}, + }, + Period: 300, + Expression: "", + MatchExact: matchExact, + } + + res := buildSearchExpression(query, "Average") + assert.Equal(t, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3") "InstanceId"', 'Average', 300))`, res) + }) + }) + + t.Run("Query has invalid characters in dimension values", func(t *testing.T) { + query := &cloudWatchQuery{ + Namespace: "AWS/EC2", + MetricName: "CPUUtilization", + Dimensions: map[string][]string{ + "lb4": {`lb4""`}, + }, + Period: 300, + Expression: "", + MatchExact: true, + } + res := buildSearchExpression(query, "Average") + + assert.Contains(t, res, `lb4\"\"`, "Expected escape double quotes") + }) } diff --git a/pkg/tsdb/cloudwatch/query_transformer_test.go b/pkg/tsdb/cloudwatch/query_transformer_test.go index 6e8ca0bed20..7bbd6db93a2 100644 --- a/pkg/tsdb/cloudwatch/query_transformer_test.go +++ b/pkg/tsdb/cloudwatch/query_transformer_test.go @@ -4,155 +4,150 @@ import ( "testing" "github.com/aws/aws-sdk-go/aws" - . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestQueryTransformer(t *testing.T) { - Convey("TestQueryTransformer", t, func() { - Convey("when transforming queries", func() { - executor := newExecutor() - Convey("one cloudwatchQuery is generated when its request query has one stat", func() { - requestQueries := []*requestQuery{ - { - RefId: "D", - Region: "us-east-1", - Namespace: "ec2", - MetricName: "CPUUtilization", - Statistics: aws.StringSlice([]string{"Average"}), - Period: 600, - Id: "", - }, - } + executor := newExecutor() + t.Run("One cloudwatchQuery is generated when its request query has one stat", func(t *testing.T) { + requestQueries := []*requestQuery{ + { + RefId: "D", + Region: "us-east-1", + Namespace: "ec2", + MetricName: "CPUUtilization", + Statistics: aws.StringSlice([]string{"Average"}), + Period: 600, + Id: "", + }, + } - res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) - So(err, ShouldBeNil) - So(len(res), ShouldEqual, 1) - }) + res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) + require.NoError(t, err) + assert.Len(t, res, 1) + }) - Convey("two cloudwatchQuery is generated when there's two stats", func() { - requestQueries := []*requestQuery{ - { - RefId: "D", - Region: "us-east-1", - Namespace: "ec2", - MetricName: "CPUUtilization", - Statistics: aws.StringSlice([]string{"Average", "Sum"}), - Period: 600, - Id: "", - }, - } + t.Run("Two cloudwatchQuery is generated when there's two stats", func(t *testing.T) { + requestQueries := []*requestQuery{ + { + RefId: "D", + Region: "us-east-1", + Namespace: "ec2", + MetricName: "CPUUtilization", + Statistics: aws.StringSlice([]string{"Average", "Sum"}), + Period: 600, + Id: "", + }, + } - res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) - So(err, ShouldBeNil) - So(len(res), ShouldEqual, 2) - }) - Convey("and id is given by user", func() { - Convey("that id will be used in the cloudwatch query", func() { - requestQueries := []*requestQuery{ - { - RefId: "D", - Region: "us-east-1", - Namespace: "ec2", - MetricName: "CPUUtilization", - Statistics: aws.StringSlice([]string{"Average"}), - Period: 600, - Id: "myid", - }, - } + res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) + require.NoError(t, err) + assert.Len(t, res, 2) + }) + t.Run("id is given by user that will be used in the cloudwatch query", func(t *testing.T) { + requestQueries := []*requestQuery{ + { + RefId: "D", + Region: "us-east-1", + Namespace: "ec2", + MetricName: "CPUUtilization", + Statistics: aws.StringSlice([]string{"Average"}), + Period: 600, + Id: "myid", + }, + } - res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) - So(err, ShouldBeNil) - So(len(res), ShouldEqual, 1) - So(res, ShouldContainKey, "myid") - }) - }) + res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) + require.Nil(t, err) + assert.Equal(t, len(res), 1) + assert.Contains(t, res, "myid") + }) - Convey("and id is not given by user", func() { - Convey("id will be generated based on ref id if query only has one stat", func() { - requestQueries := []*requestQuery{ - { - RefId: "D", - Region: "us-east-1", - Namespace: "ec2", - MetricName: "CPUUtilization", - Statistics: aws.StringSlice([]string{"Average"}), - Period: 600, - Id: "", - }, - } + t.Run("ID is not given by user", func(t *testing.T) { + t.Run("ID will be generated based on ref ID if query only has one stat", func(t *testing.T) { + requestQueries := []*requestQuery{ + { + RefId: "D", + Region: "us-east-1", + Namespace: "ec2", + MetricName: "CPUUtilization", + Statistics: aws.StringSlice([]string{"Average"}), + Period: 600, + Id: "", + }, + } - res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) - So(err, ShouldBeNil) - So(len(res), ShouldEqual, 1) - So(res, ShouldContainKey, "queryD") - }) + res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) + require.NoError(t, err) + assert.Len(t, res, 1) + assert.Contains(t, res, "queryD") + }) - Convey("id will be generated based on ref and stat name if query has two stats", func() { - requestQueries := []*requestQuery{ - { - RefId: "D", - Region: "us-east-1", - Namespace: "ec2", - MetricName: "CPUUtilization", - Statistics: aws.StringSlice([]string{"Average", "Sum"}), - Period: 600, - Id: "", - }, - } + t.Run("ID will be generated based on ref and stat name if query has two stats", func(t *testing.T) { + requestQueries := []*requestQuery{ + { + RefId: "D", + Region: "us-east-1", + Namespace: "ec2", + MetricName: "CPUUtilization", + Statistics: aws.StringSlice([]string{"Average", "Sum"}), + Period: 600, + Id: "", + }, + } - res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) - So(err, ShouldBeNil) - So(len(res), ShouldEqual, 2) - So(res, ShouldContainKey, "queryD_Sum") - So(res, ShouldContainKey, "queryD_Average") - }) - }) - - Convey("dot should be removed when query has more than one stat and one of them is a percentile", func() { - requestQueries := []*requestQuery{ - { - RefId: "D", - Region: "us-east-1", - Namespace: "ec2", - MetricName: "CPUUtilization", - Statistics: aws.StringSlice([]string{"Average", "p46.32"}), - Period: 600, - Id: "", - }, - } - - res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) - So(err, ShouldBeNil) - So(len(res), ShouldEqual, 2) - So(res, ShouldContainKey, "queryD_p46_32") - }) - - Convey("should return an error if two queries have the same id", func() { - requestQueries := []*requestQuery{ - { - RefId: "D", - Region: "us-east-1", - Namespace: "ec2", - MetricName: "CPUUtilization", - Statistics: aws.StringSlice([]string{"Average", "p46.32"}), - Period: 600, - Id: "myId", - }, - { - RefId: "E", - Region: "us-east-1", - Namespace: "ec2", - MetricName: "CPUUtilization", - Statistics: aws.StringSlice([]string{"Average", "p46.32"}), - Period: 600, - Id: "myId", - }, - } - - res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) - So(res, ShouldBeNil) - So(err, ShouldNotBeNil) - }) + res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) + require.NoError(t, err) + assert.Len(t, res, 2) + assert.Contains(t, res, "queryD_Sum") + assert.Contains(t, res, "queryD_Average") }) }) + + t.Run("dot should be removed when query has more than one stat and one of them is a percentile", func(t *testing.T) { + requestQueries := []*requestQuery{ + { + RefId: "D", + Region: "us-east-1", + Namespace: "ec2", + MetricName: "CPUUtilization", + Statistics: aws.StringSlice([]string{"Average", "p46.32"}), + Period: 600, + Id: "", + }, + } + + res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) + require.NoError(t, err) + assert.Len(t, res, 2) + assert.Contains(t, res, "queryD_p46_32") + }) + + t.Run("should return an error if two queries have the same id", func(t *testing.T) { + requestQueries := []*requestQuery{ + { + RefId: "D", + Region: "us-east-1", + Namespace: "ec2", + MetricName: "CPUUtilization", + Statistics: aws.StringSlice([]string{"Average", "p46.32"}), + Period: 600, + Id: "myId", + }, + { + RefId: "E", + Region: "us-east-1", + Namespace: "ec2", + MetricName: "CPUUtilization", + Statistics: aws.StringSlice([]string{"Average", "p46.32"}), + Period: 600, + Id: "myId", + }, + } + + res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) + require.Nil(t, res) + assert.Error(t, err) + }) } diff --git a/pkg/tsdb/cloudwatch/request_parser_test.go b/pkg/tsdb/cloudwatch/request_parser_test.go index 75b64e67969..b7d52ee5ded 100644 --- a/pkg/tsdb/cloudwatch/request_parser_test.go +++ b/pkg/tsdb/cloudwatch/request_parser_test.go @@ -6,211 +6,207 @@ import ( "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/tsdb" - . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestRequestParser(t *testing.T) { - Convey("TestRequestParser", t, func() { + timeRange := tsdb.NewTimeRange("now-1h", "now-2h") + from, err := timeRange.ParseFrom() + require.NoError(t, err) + to, err := timeRange.ParseTo() + require.NoError(t, err) + + t.Run("New dimensions structure", func(t *testing.T) { + query := simplejson.NewFromAny(map[string]interface{}{ + "refId": "ref1", + "region": "us-east-1", + "namespace": "ec2", + "metricName": "CPUUtilization", + "id": "", + "expression": "", + "dimensions": map[string]interface{}{ + "InstanceId": []interface{}{"test"}, + "InstanceType": []interface{}{"test2", "test3"}, + }, + "statistics": []interface{}{"Average"}, + "period": "600", + "hide": false, + }) + + res, err := parseRequestQuery(query, "ref1", from, to) + require.NoError(t, err) + assert.Equal(t, "us-east-1", res.Region) + assert.Equal(t, "ref1", res.RefId) + assert.Equal(t, "ec2", res.Namespace) + assert.Equal(t, "CPUUtilization", res.MetricName) + assert.Empty(t, res.Id) + assert.Empty(t, res.Expression) + assert.Equal(t, 600, res.Period) + assert.True(t, res.ReturnData) + assert.Len(t, res.Dimensions, 2) + assert.Len(t, res.Dimensions["InstanceId"], 1) + assert.Len(t, res.Dimensions["InstanceType"], 2) + assert.Equal(t, "test3", res.Dimensions["InstanceType"][1]) + assert.Len(t, res.Statistics, 1) + assert.Equal(t, "Average", *res.Statistics[0]) + }) + + t.Run("Old dimensions structure (backwards compatibility)", func(t *testing.T) { + query := simplejson.NewFromAny(map[string]interface{}{ + "refId": "ref1", + "region": "us-east-1", + "namespace": "ec2", + "metricName": "CPUUtilization", + "id": "", + "expression": "", + "dimensions": map[string]interface{}{ + "InstanceId": "test", + "InstanceType": "test2", + }, + "statistics": []interface{}{"Average"}, + "period": "600", + "hide": false, + }) + + res, err := parseRequestQuery(query, "ref1", from, to) + require.NoError(t, err) + assert.Equal(t, "us-east-1", res.Region) + assert.Equal(t, "ref1", res.RefId) + assert.Equal(t, "ec2", res.Namespace) + assert.Equal(t, "CPUUtilization", res.MetricName) + assert.Empty(t, res.Id) + assert.Empty(t, res.Expression) + assert.Equal(t, 600, res.Period) + assert.True(t, res.ReturnData) + assert.Len(t, res.Dimensions, 2) + assert.Len(t, res.Dimensions["InstanceId"], 1) + assert.Len(t, res.Dimensions["InstanceType"], 1) + assert.Equal(t, "test2", res.Dimensions["InstanceType"][0]) + assert.Equal(t, "Average", *res.Statistics[0]) + }) + + t.Run("Period defined in the editor by the user is being used when time range is short", func(t *testing.T) { + query := simplejson.NewFromAny(map[string]interface{}{ + "refId": "ref1", + "region": "us-east-1", + "namespace": "ec2", + "metricName": "CPUUtilization", + "id": "", + "expression": "", + "dimensions": map[string]interface{}{ + "InstanceId": "test", + "InstanceType": "test2", + }, + "statistics": []interface{}{"Average"}, + "hide": false, + }) + query.Set("period", "900") timeRange := tsdb.NewTimeRange("now-1h", "now-2h") from, err := timeRange.ParseFrom() - So(err, ShouldBeNil) + require.NoError(t, err) to, err := timeRange.ParseTo() - So(err, ShouldBeNil) - Convey("when parsing query editor row json", func() { - Convey("using new dimensions structure", func() { - query := simplejson.NewFromAny(map[string]interface{}{ - "refId": "ref1", - "region": "us-east-1", - "namespace": "ec2", - "metricName": "CPUUtilization", - "id": "", - "expression": "", - "dimensions": map[string]interface{}{ - "InstanceId": []interface{}{"test"}, - "InstanceType": []interface{}{"test2", "test3"}, - }, - "statistics": []interface{}{"Average"}, - "period": "600", - "hide": false, - }) + require.NoError(t, err) - res, err := parseRequestQuery(query, "ref1", from, to) - So(err, ShouldBeNil) - So(res.Region, ShouldEqual, "us-east-1") - So(res.RefId, ShouldEqual, "ref1") - So(res.Namespace, ShouldEqual, "ec2") - So(res.MetricName, ShouldEqual, "CPUUtilization") - So(res.Id, ShouldEqual, "") - So(res.Expression, ShouldEqual, "") - So(res.Period, ShouldEqual, 600) - So(res.ReturnData, ShouldEqual, true) - So(len(res.Dimensions), ShouldEqual, 2) - So(len(res.Dimensions["InstanceId"]), ShouldEqual, 1) - So(len(res.Dimensions["InstanceType"]), ShouldEqual, 2) - So(res.Dimensions["InstanceType"][1], ShouldEqual, "test3") - So(len(res.Statistics), ShouldEqual, 1) - So(*res.Statistics[0], ShouldEqual, "Average") - }) + res, err := parseRequestQuery(query, "ref1", from, to) + require.NoError(t, err) + assert.Equal(t, 900, res.Period) + }) - Convey("using old dimensions structure (backwards compatibility)", func() { - query := simplejson.NewFromAny(map[string]interface{}{ - "refId": "ref1", - "region": "us-east-1", - "namespace": "ec2", - "metricName": "CPUUtilization", - "id": "", - "expression": "", - "dimensions": map[string]interface{}{ - "InstanceId": "test", - "InstanceType": "test2", - }, - "statistics": []interface{}{"Average"}, - "period": "600", - "hide": false, - }) + t.Run("Period is parsed correctly if not defined by user", func(t *testing.T) { + query := simplejson.NewFromAny(map[string]interface{}{ + "refId": "ref1", + "region": "us-east-1", + "namespace": "ec2", + "metricName": "CPUUtilization", + "id": "", + "expression": "", + "dimensions": map[string]interface{}{ + "InstanceId": "test", + "InstanceType": "test2", + }, + "statistics": []interface{}{"Average"}, + "hide": false, + "period": "auto", + }) - res, err := parseRequestQuery(query, "ref1", from, to) - So(err, ShouldBeNil) - So(res.Region, ShouldEqual, "us-east-1") - So(res.RefId, ShouldEqual, "ref1") - So(res.Namespace, ShouldEqual, "ec2") - So(res.MetricName, ShouldEqual, "CPUUtilization") - So(res.Id, ShouldEqual, "") - So(res.Expression, ShouldEqual, "") - So(res.Period, ShouldEqual, 600) - So(res.ReturnData, ShouldEqual, true) - So(len(res.Dimensions), ShouldEqual, 2) - So(len(res.Dimensions["InstanceId"]), ShouldEqual, 1) - So(len(res.Dimensions["InstanceType"]), ShouldEqual, 1) - So(res.Dimensions["InstanceType"][0], ShouldEqual, "test2") - So(*res.Statistics[0], ShouldEqual, "Average") - }) + t.Run("Time range is 5 minutes", func(t *testing.T) { + query.Set("period", "auto") + to := time.Now() + from := to.Local().Add(time.Minute * time.Duration(5)) - Convey("period defined in the editor by the user is being used", func() { - query := simplejson.NewFromAny(map[string]interface{}{ - "refId": "ref1", - "region": "us-east-1", - "namespace": "ec2", - "metricName": "CPUUtilization", - "id": "", - "expression": "", - "dimensions": map[string]interface{}{ - "InstanceId": "test", - "InstanceType": "test2", - }, - "statistics": []interface{}{"Average"}, - "hide": false, - }) - Convey("when time range is short", func() { - query.Set("period", "900") - timeRange := tsdb.NewTimeRange("now-1h", "now-2h") - from, err := timeRange.ParseFrom() - So(err, ShouldBeNil) - to, err := timeRange.ParseTo() - So(err, ShouldBeNil) + res, err := parseRequestQuery(query, "ref1", from, to) + require.NoError(t, err) + assert.Equal(t, 60, res.Period) + }) - res, err := parseRequestQuery(query, "ref1", from, to) - So(err, ShouldBeNil) - So(res.Period, ShouldEqual, 900) - }) - }) + t.Run("Time range is 1 day", func(t *testing.T) { + query.Set("period", "auto") + to := time.Now() + from := to.AddDate(0, 0, -1) - Convey("period is parsed correctly if not defined by user", func() { - query := simplejson.NewFromAny(map[string]interface{}{ - "refId": "ref1", - "region": "us-east-1", - "namespace": "ec2", - "metricName": "CPUUtilization", - "id": "", - "expression": "", - "dimensions": map[string]interface{}{ - "InstanceId": "test", - "InstanceType": "test2", - }, - "statistics": []interface{}{"Average"}, - "hide": false, - "period": "auto", - }) + res, err := parseRequestQuery(query, "ref1", from, to) + require.NoError(t, err) + assert.Equal(t, 60, res.Period) + }) - Convey("when time range is 5 minutes", func() { - query.Set("period", "auto") - to := time.Now() - from := to.Local().Add(time.Minute * time.Duration(5)) + t.Run("Time range is 2 days", func(t *testing.T) { + query.Set("period", "auto") + to := time.Now() + from := to.AddDate(0, 0, -2) + res, err := parseRequestQuery(query, "ref1", from, to) + require.NoError(t, err) + assert.Equal(t, 300, res.Period) + }) - res, err := parseRequestQuery(query, "ref1", from, to) - So(err, ShouldBeNil) - So(res.Period, ShouldEqual, 60) - }) + t.Run("Time range is 7 days", func(t *testing.T) { + query.Set("period", "auto") + to := time.Now() + from := to.AddDate(0, 0, -7) - Convey("when time range is 1 day", func() { - query.Set("period", "auto") - to := time.Now() - from := to.AddDate(0, 0, -1) + res, err := parseRequestQuery(query, "ref1", from, to) + require.NoError(t, err) + assert.Equal(t, 900, res.Period) + }) - res, err := parseRequestQuery(query, "ref1", from, to) - So(err, ShouldBeNil) - So(res.Period, ShouldEqual, 60) - }) + t.Run("Time range is 30 days", func(t *testing.T) { + query.Set("period", "auto") + to := time.Now() + from := to.AddDate(0, 0, -30) - Convey("when time range is 2 days", func() { - query.Set("period", "auto") - to := time.Now() - from := to.AddDate(0, 0, -2) - res, err := parseRequestQuery(query, "ref1", from, to) - So(err, ShouldBeNil) - So(res.Period, ShouldEqual, 300) - }) + res, err := parseRequestQuery(query, "ref1", from, to) + require.NoError(t, err) + assert.Equal(t, 3600, res.Period) + }) - Convey("when time range is 7 days", func() { - query.Set("period", "auto") - to := time.Now() - from := to.AddDate(0, 0, -7) + t.Run("Time range is 90 days", func(t *testing.T) { + query.Set("period", "auto") + to := time.Now() + from := to.AddDate(0, 0, -90) - res, err := parseRequestQuery(query, "ref1", from, to) - So(err, ShouldBeNil) - So(res.Period, ShouldEqual, 900) - }) + res, err := parseRequestQuery(query, "ref1", from, to) + require.NoError(t, err) + assert.Equal(t, 21600, res.Period) + }) - Convey("when time range is 30 days", func() { - query.Set("period", "auto") - to := time.Now() - from := to.AddDate(0, 0, -30) + t.Run("Time range is 1 year", func(t *testing.T) { + query.Set("period", "auto") + to := time.Now() + from := to.AddDate(-1, 0, 0) - res, err := parseRequestQuery(query, "ref1", from, to) - So(err, ShouldBeNil) - So(res.Period, ShouldEqual, 3600) - }) + res, err := parseRequestQuery(query, "ref1", from, to) + require.Nil(t, err) + assert.Equal(t, res.Period, 21600) + }) - Convey("when time range is 90 days", func() { - query.Set("period", "auto") - to := time.Now() - from := to.AddDate(0, 0, -90) + t.Run("Time range is 2 years", func(t *testing.T) { + query.Set("period", "auto") + to := time.Now() + from := to.AddDate(-2, 0, 0) - res, err := parseRequestQuery(query, "ref1", from, to) - So(err, ShouldBeNil) - So(res.Period, ShouldEqual, 21600) - }) - - Convey("when time range is 1 year", func() { - query.Set("period", "auto") - to := time.Now() - from := to.AddDate(-1, 0, 0) - - res, err := parseRequestQuery(query, "ref1", from, to) - So(err, ShouldBeNil) - So(res.Period, ShouldEqual, 21600) - }) - - Convey("when time range is 2 years", func() { - query.Set("period", "auto") - to := time.Now() - from := to.AddDate(-2, 0, 0) - - res, err := parseRequestQuery(query, "ref1", from, to) - So(err, ShouldBeNil) - So(res.Period, ShouldEqual, 86400) - }) - }) + res, err := parseRequestQuery(query, "ref1", from, to) + require.NoError(t, err) + assert.Equal(t, 86400, res.Period) }) }) } diff --git a/pkg/tsdb/cloudwatch/response_parser_test.go b/pkg/tsdb/cloudwatch/response_parser_test.go index 5d8f1471889..a1bbfdfd610 100644 --- a/pkg/tsdb/cloudwatch/response_parser_test.go +++ b/pkg/tsdb/cloudwatch/response_parser_test.go @@ -7,314 +7,310 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/grafana/grafana/pkg/components/null" - . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestCloudWatchResponseParser(t *testing.T) { - Convey("TestCloudWatchResponseParser", t, func() { - Convey("can expand dimension value using exact match", func() { - timestamp := time.Unix(0, 0) - labels := []string{"lb1", "lb2"} - mdrs := map[string]*cloudwatch.MetricDataResult{ - "lb1": { - Id: aws.String("id1"), - Label: aws.String("lb1"), - Timestamps: []*time.Time{ - aws.Time(timestamp), - aws.Time(timestamp.Add(60 * time.Second)), - aws.Time(timestamp.Add(180 * time.Second)), - }, - Values: []*float64{ - aws.Float64(10), - aws.Float64(20), - aws.Float64(30), - }, - StatusCode: aws.String("Complete"), + t.Run("Expand dimension value using exact match", func(t *testing.T) { + timestamp := time.Unix(0, 0) + labels := []string{"lb1", "lb2"} + mdrs := map[string]*cloudwatch.MetricDataResult{ + "lb1": { + Id: aws.String("id1"), + Label: aws.String("lb1"), + Timestamps: []*time.Time{ + aws.Time(timestamp), + aws.Time(timestamp.Add(60 * time.Second)), + aws.Time(timestamp.Add(180 * time.Second)), }, - "lb2": { - Id: aws.String("id2"), - Label: aws.String("lb2"), - Timestamps: []*time.Time{ - aws.Time(timestamp), - aws.Time(timestamp.Add(60 * time.Second)), - aws.Time(timestamp.Add(180 * time.Second)), - }, - Values: []*float64{ - aws.Float64(10), - aws.Float64(20), - aws.Float64(30), - }, - StatusCode: aws.String("Complete"), + Values: []*float64{ + aws.Float64(10), + aws.Float64(20), + aws.Float64(30), }, - } - - query := &cloudWatchQuery{ - RefId: "refId1", - Region: "us-east-1", - Namespace: "AWS/ApplicationELB", - MetricName: "TargetResponseTime", - Dimensions: map[string][]string{ - "LoadBalancer": {"lb1", "lb2"}, - "TargetGroup": {"tg"}, + StatusCode: aws.String("Complete"), + }, + "lb2": { + Id: aws.String("id2"), + Label: aws.String("lb2"), + Timestamps: []*time.Time{ + aws.Time(timestamp), + aws.Time(timestamp.Add(60 * time.Second)), + aws.Time(timestamp.Add(180 * time.Second)), }, - Stats: "Average", - Period: 60, - Alias: "{{LoadBalancer}} Expanded", - } - series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) - timeSeries := (*series)[0] - - So(err, ShouldBeNil) - So(partialData, ShouldBeFalse) - So(timeSeries.Name, ShouldEqual, "lb1 Expanded") - So(timeSeries.Tags["LoadBalancer"], ShouldEqual, "lb1") - - timeSeries2 := (*series)[1] - So(timeSeries2.Name, ShouldEqual, "lb2 Expanded") - So(timeSeries2.Tags["LoadBalancer"], ShouldEqual, "lb2") - }) - - Convey("can expand dimension value using substring", func() { - timestamp := time.Unix(0, 0) - labels := []string{"lb1 Sum", "lb2 Average"} - mdrs := map[string]*cloudwatch.MetricDataResult{ - "lb1 Sum": { - Id: aws.String("id1"), - Label: aws.String("lb1 Sum"), - Timestamps: []*time.Time{ - aws.Time(timestamp), - aws.Time(timestamp.Add(60 * time.Second)), - aws.Time(timestamp.Add(180 * time.Second)), - }, - Values: []*float64{ - aws.Float64(10), - aws.Float64(20), - aws.Float64(30), - }, - StatusCode: aws.String("Complete"), + Values: []*float64{ + aws.Float64(10), + aws.Float64(20), + aws.Float64(30), }, - "lb2 Average": { - Id: aws.String("id2"), - Label: aws.String("lb2 Average"), - Timestamps: []*time.Time{ - aws.Time(timestamp), - aws.Time(timestamp.Add(60 * time.Second)), - aws.Time(timestamp.Add(180 * time.Second)), - }, - Values: []*float64{ - aws.Float64(10), - aws.Float64(20), - aws.Float64(30), - }, - StatusCode: aws.String("Complete"), + StatusCode: aws.String("Complete"), + }, + } + + query := &cloudWatchQuery{ + RefId: "refId1", + Region: "us-east-1", + Namespace: "AWS/ApplicationELB", + MetricName: "TargetResponseTime", + Dimensions: map[string][]string{ + "LoadBalancer": {"lb1", "lb2"}, + "TargetGroup": {"tg"}, + }, + Stats: "Average", + Period: 60, + Alias: "{{LoadBalancer}} Expanded", + } + series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) + require.NoError(t, err) + + timeSeries := (*series)[0] + assert.False(t, partialData) + assert.Equal(t, "lb1 Expanded", timeSeries.Name) + assert.Equal(t, "lb1", timeSeries.Tags["LoadBalancer"]) + + timeSeries2 := (*series)[1] + assert.Equal(t, "lb2 Expanded", timeSeries2.Name) + assert.Equal(t, "lb2", timeSeries2.Tags["LoadBalancer"]) + }) + + t.Run("Expand dimension value using substring", func(t *testing.T) { + timestamp := time.Unix(0, 0) + labels := []string{"lb1 Sum", "lb2 Average"} + mdrs := map[string]*cloudwatch.MetricDataResult{ + "lb1 Sum": { + Id: aws.String("id1"), + Label: aws.String("lb1 Sum"), + Timestamps: []*time.Time{ + aws.Time(timestamp), + aws.Time(timestamp.Add(60 * time.Second)), + aws.Time(timestamp.Add(180 * time.Second)), }, - } - - query := &cloudWatchQuery{ - RefId: "refId1", - Region: "us-east-1", - Namespace: "AWS/ApplicationELB", - MetricName: "TargetResponseTime", - Dimensions: map[string][]string{ - "LoadBalancer": {"lb1", "lb2"}, - "TargetGroup": {"tg"}, + Values: []*float64{ + aws.Float64(10), + aws.Float64(20), + aws.Float64(30), }, - Stats: "Average", - Period: 60, - Alias: "{{LoadBalancer}} Expanded", - } - series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) - timeSeries := (*series)[0] - So(err, ShouldBeNil) - So(partialData, ShouldBeFalse) - So(timeSeries.Name, ShouldEqual, "lb1 Expanded") - So(timeSeries.Tags["LoadBalancer"], ShouldEqual, "lb1") - - timeSeries2 := (*series)[1] - So(timeSeries2.Name, ShouldEqual, "lb2 Expanded") - So(timeSeries2.Tags["LoadBalancer"], ShouldEqual, "lb2") - }) - - Convey("can expand dimension value using wildcard", func() { - timestamp := time.Unix(0, 0) - labels := []string{"lb3", "lb4"} - mdrs := map[string]*cloudwatch.MetricDataResult{ - "lb3": { - Id: aws.String("lb3"), - Label: aws.String("lb3"), - Timestamps: []*time.Time{ - aws.Time(timestamp), - aws.Time(timestamp.Add(60 * time.Second)), - aws.Time(timestamp.Add(180 * time.Second)), - }, - Values: []*float64{ - aws.Float64(10), - aws.Float64(20), - aws.Float64(30), - }, - StatusCode: aws.String("Complete"), + StatusCode: aws.String("Complete"), + }, + "lb2 Average": { + Id: aws.String("id2"), + Label: aws.String("lb2 Average"), + Timestamps: []*time.Time{ + aws.Time(timestamp), + aws.Time(timestamp.Add(60 * time.Second)), + aws.Time(timestamp.Add(180 * time.Second)), }, - "lb4": { - Id: aws.String("lb4"), - Label: aws.String("lb4"), - Timestamps: []*time.Time{ - aws.Time(timestamp), - aws.Time(timestamp.Add(60 * time.Second)), - aws.Time(timestamp.Add(180 * time.Second)), - }, - Values: []*float64{ - aws.Float64(10), - aws.Float64(20), - aws.Float64(30), - }, - StatusCode: aws.String("Complete"), + Values: []*float64{ + aws.Float64(10), + aws.Float64(20), + aws.Float64(30), }, - } + StatusCode: aws.String("Complete"), + }, + } - query := &cloudWatchQuery{ - RefId: "refId1", - Region: "us-east-1", - Namespace: "AWS/ApplicationELB", - MetricName: "TargetResponseTime", - Dimensions: map[string][]string{ - "LoadBalancer": {"*"}, - "TargetGroup": {"tg"}, + query := &cloudWatchQuery{ + RefId: "refId1", + Region: "us-east-1", + Namespace: "AWS/ApplicationELB", + MetricName: "TargetResponseTime", + Dimensions: map[string][]string{ + "LoadBalancer": {"lb1", "lb2"}, + "TargetGroup": {"tg"}, + }, + Stats: "Average", + Period: 60, + Alias: "{{LoadBalancer}} Expanded", + } + series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) + require.NoError(t, err) + + timeSeries := (*series)[0] + assert.False(t, partialData) + assert.Equal(t, "lb1 Expanded", timeSeries.Name) + assert.Equal(t, "lb1", timeSeries.Tags["LoadBalancer"]) + + timeSeries2 := (*series)[1] + assert.Equal(t, "lb2 Expanded", timeSeries2.Name) + assert.Equal(t, "lb2", timeSeries2.Tags["LoadBalancer"]) + }) + + t.Run("Expand dimension value using wildcard", func(t *testing.T) { + timestamp := time.Unix(0, 0) + labels := []string{"lb3", "lb4"} + mdrs := map[string]*cloudwatch.MetricDataResult{ + "lb3": { + Id: aws.String("lb3"), + Label: aws.String("lb3"), + Timestamps: []*time.Time{ + aws.Time(timestamp), + aws.Time(timestamp.Add(60 * time.Second)), + aws.Time(timestamp.Add(180 * time.Second)), }, - Stats: "Average", - Period: 60, - Alias: "{{LoadBalancer}} Expanded", - } - series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) - - So(err, ShouldBeNil) - So(partialData, ShouldBeFalse) - So((*series)[0].Name, ShouldEqual, "lb3 Expanded") - So((*series)[1].Name, ShouldEqual, "lb4 Expanded") - }) - - Convey("can expand dimension value when no values are returned and a multi-valued template variable is used", func() { - timestamp := time.Unix(0, 0) - labels := []string{"lb3"} - mdrs := map[string]*cloudwatch.MetricDataResult{ - "lb3": { - Id: aws.String("lb3"), - Label: aws.String("lb3"), - Timestamps: []*time.Time{ - aws.Time(timestamp), - aws.Time(timestamp.Add(60 * time.Second)), - aws.Time(timestamp.Add(180 * time.Second)), - }, - Values: []*float64{}, - StatusCode: aws.String("Complete"), + Values: []*float64{ + aws.Float64(10), + aws.Float64(20), + aws.Float64(30), }, - } - - query := &cloudWatchQuery{ - RefId: "refId1", - Region: "us-east-1", - Namespace: "AWS/ApplicationELB", - MetricName: "TargetResponseTime", - Dimensions: map[string][]string{ - "LoadBalancer": {"lb1", "lb2"}, + StatusCode: aws.String("Complete"), + }, + "lb4": { + Id: aws.String("lb4"), + Label: aws.String("lb4"), + Timestamps: []*time.Time{ + aws.Time(timestamp), + aws.Time(timestamp.Add(60 * time.Second)), + aws.Time(timestamp.Add(180 * time.Second)), }, - Stats: "Average", - Period: 60, - Alias: "{{LoadBalancer}} Expanded", - } - series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) - - So(err, ShouldBeNil) - So(partialData, ShouldBeFalse) - So(len(*series), ShouldEqual, 2) - So((*series)[0].Name, ShouldEqual, "lb1 Expanded") - So((*series)[1].Name, ShouldEqual, "lb2 Expanded") - }) - - Convey("can expand dimension value when no values are returned and a multi-valued template variable and two single-valued dimensions are used", func() { - timestamp := time.Unix(0, 0) - labels := []string{"lb3"} - mdrs := map[string]*cloudwatch.MetricDataResult{ - "lb3": { - Id: aws.String("lb3"), - Label: aws.String("lb3"), - Timestamps: []*time.Time{ - aws.Time(timestamp), - aws.Time(timestamp.Add(60 * time.Second)), - aws.Time(timestamp.Add(180 * time.Second)), - }, - Values: []*float64{}, - StatusCode: aws.String("Complete"), + Values: []*float64{ + aws.Float64(10), + aws.Float64(20), + aws.Float64(30), }, - } + StatusCode: aws.String("Complete"), + }, + } - query := &cloudWatchQuery{ - RefId: "refId1", - Region: "us-east-1", - Namespace: "AWS/ApplicationELB", - MetricName: "TargetResponseTime", - Dimensions: map[string][]string{ - "LoadBalancer": {"lb1", "lb2"}, - "InstanceType": {"micro"}, - "Resource": {"res"}, + query := &cloudWatchQuery{ + RefId: "refId1", + Region: "us-east-1", + Namespace: "AWS/ApplicationELB", + MetricName: "TargetResponseTime", + Dimensions: map[string][]string{ + "LoadBalancer": {"*"}, + "TargetGroup": {"tg"}, + }, + Stats: "Average", + Period: 60, + Alias: "{{LoadBalancer}} Expanded", + } + series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) + require.NoError(t, err) + assert.False(t, partialData) + assert.Equal(t, "lb3 Expanded", (*series)[0].Name) + assert.Equal(t, "lb4 Expanded", (*series)[1].Name) + }) + + t.Run("Expand dimension value when no values are returned and a multi-valued template variable is used", func(t *testing.T) { + timestamp := time.Unix(0, 0) + labels := []string{"lb3"} + mdrs := map[string]*cloudwatch.MetricDataResult{ + "lb3": { + Id: aws.String("lb3"), + Label: aws.String("lb3"), + Timestamps: []*time.Time{ + aws.Time(timestamp), + aws.Time(timestamp.Add(60 * time.Second)), + aws.Time(timestamp.Add(180 * time.Second)), }, - Stats: "Average", - Period: 60, - Alias: "{{LoadBalancer}} Expanded {{InstanceType}} - {{Resource}}", - } - series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) + Values: []*float64{}, + StatusCode: aws.String("Complete"), + }, + } - So(err, ShouldBeNil) - So(partialData, ShouldBeFalse) - So(len(*series), ShouldEqual, 2) - So((*series)[0].Name, ShouldEqual, "lb1 Expanded micro - res") - So((*series)[1].Name, ShouldEqual, "lb2 Expanded micro - res") - }) + query := &cloudWatchQuery{ + RefId: "refId1", + Region: "us-east-1", + Namespace: "AWS/ApplicationELB", + MetricName: "TargetResponseTime", + Dimensions: map[string][]string{ + "LoadBalancer": {"lb1", "lb2"}, + }, + Stats: "Average", + Period: 60, + Alias: "{{LoadBalancer}} Expanded", + } + series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) + require.NoError(t, err) + assert.False(t, partialData) + assert.Len(t, *series, 2) + assert.Equal(t, "lb1 Expanded", (*series)[0].Name) + assert.Equal(t, "lb2 Expanded", (*series)[1].Name) + }) - Convey("can parse cloudwatch response", func() { - timestamp := time.Unix(0, 0) - labels := []string{"lb"} - mdrs := map[string]*cloudwatch.MetricDataResult{ - "lb": { - Id: aws.String("id1"), - Label: aws.String("lb"), - Timestamps: []*time.Time{ - aws.Time(timestamp), - aws.Time(timestamp.Add(60 * time.Second)), - aws.Time(timestamp.Add(180 * time.Second)), - }, - Values: []*float64{ - aws.Float64(10), - aws.Float64(20), - aws.Float64(30), - }, - StatusCode: aws.String("Complete"), + t.Run("Expand dimension value when no values are returned and a multi-valued template variable and two single-valued dimensions are used", func(t *testing.T) { + timestamp := time.Unix(0, 0) + labels := []string{"lb3"} + mdrs := map[string]*cloudwatch.MetricDataResult{ + "lb3": { + Id: aws.String("lb3"), + Label: aws.String("lb3"), + Timestamps: []*time.Time{ + aws.Time(timestamp), + aws.Time(timestamp.Add(60 * time.Second)), + aws.Time(timestamp.Add(180 * time.Second)), }, - } + Values: []*float64{}, + StatusCode: aws.String("Complete"), + }, + } - query := &cloudWatchQuery{ - RefId: "refId1", - Region: "us-east-1", - Namespace: "AWS/ApplicationELB", - MetricName: "TargetResponseTime", - Dimensions: map[string][]string{ - "LoadBalancer": {"lb"}, - "TargetGroup": {"tg"}, + query := &cloudWatchQuery{ + RefId: "refId1", + Region: "us-east-1", + Namespace: "AWS/ApplicationELB", + MetricName: "TargetResponseTime", + Dimensions: map[string][]string{ + "LoadBalancer": {"lb1", "lb2"}, + "InstanceType": {"micro"}, + "Resource": {"res"}, + }, + Stats: "Average", + Period: 60, + Alias: "{{LoadBalancer}} Expanded {{InstanceType}} - {{Resource}}", + } + series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) + require.NoError(t, err) + assert.False(t, partialData) + assert.Len(t, *series, 2) + assert.Equal(t, "lb1 Expanded micro - res", (*series)[0].Name) + assert.Equal(t, "lb2 Expanded micro - res", (*series)[1].Name) + }) + + t.Run("Parse cloudwatch response", func(t *testing.T) { + timestamp := time.Unix(0, 0) + labels := []string{"lb"} + mdrs := map[string]*cloudwatch.MetricDataResult{ + "lb": { + Id: aws.String("id1"), + Label: aws.String("lb"), + Timestamps: []*time.Time{ + aws.Time(timestamp), + aws.Time(timestamp.Add(60 * time.Second)), + aws.Time(timestamp.Add(180 * time.Second)), }, - Stats: "Average", - Period: 60, - Alias: "{{namespace}}_{{metric}}_{{stat}}", - } - series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) - timeSeries := (*series)[0] + Values: []*float64{ + aws.Float64(10), + aws.Float64(20), + aws.Float64(30), + }, + StatusCode: aws.String("Complete"), + }, + } - So(err, ShouldBeNil) - So(partialData, ShouldBeFalse) - So(timeSeries.Name, ShouldEqual, "AWS/ApplicationELB_TargetResponseTime_Average") - So(timeSeries.Tags["LoadBalancer"], ShouldEqual, "lb") - So(timeSeries.Points[0][0].String(), ShouldEqual, null.FloatFrom(10.0).String()) - So(timeSeries.Points[1][0].String(), ShouldEqual, null.FloatFrom(20.0).String()) - So(timeSeries.Points[2][0].String(), ShouldEqual, null.FloatFromPtr(nil).String()) - So(timeSeries.Points[3][0].String(), ShouldEqual, null.FloatFrom(30.0).String()) - }) + query := &cloudWatchQuery{ + RefId: "refId1", + Region: "us-east-1", + Namespace: "AWS/ApplicationELB", + MetricName: "TargetResponseTime", + Dimensions: map[string][]string{ + "LoadBalancer": {"lb"}, + "TargetGroup": {"tg"}, + }, + Stats: "Average", + Period: 60, + Alias: "{{namespace}}_{{metric}}_{{stat}}", + } + series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) + timeSeries := (*series)[0] + require.NoError(t, err) + assert.False(t, partialData) + assert.Equal(t, "AWS/ApplicationELB_TargetResponseTime_Average", timeSeries.Name) + assert.Equal(t, "lb", timeSeries.Tags["LoadBalancer"]) + assert.Equal(t, null.FloatFrom(10.0).String(), timeSeries.Points[0][0].String()) + assert.Equal(t, null.FloatFrom(20.0).String(), timeSeries.Points[1][0].String()) + assert.Equal(t, null.FloatFromPtr(nil).String(), timeSeries.Points[2][0].String()) + assert.Equal(t, null.FloatFrom(30.0).String(), timeSeries.Points[3][0].String()) }) }