Commit 7d7e727f by Matheus Alcantara Committed by GitHub

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 <arve.knudsen@gmail.com>

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
parent 89ebab63
...@@ -3,188 +3,137 @@ package cloudwatch ...@@ -3,188 +3,137 @@ package cloudwatch
import ( import (
"testing" "testing"
. "github.com/smartystreets/goconvey/convey" "github.com/stretchr/testify/assert"
) )
func TestCloudWatchQuery(t *testing.T) { func TestCloudWatchQuery(t *testing.T) {
Convey("TestCloudWatchQuery", t, func() { t.Run("SEARCH(someexpression) was specified in the query editor", func(t *testing.T) {
Convey("and SEARCH(someexpression) was specified in the query editor", func() { query := &cloudWatchQuery{
query := &cloudWatchQuery{ RefId: "A",
RefId: "A", Region: "us-east-1",
Region: "us-east-1", Expression: "SEARCH(someexpression)",
Expression: "SEARCH(someexpression)", Stats: "Average",
Stats: "Average", Period: 300,
Period: 300, Id: "id1",
Id: "id1", }
}
assert.True(t, query.isSearchExpression(), "Expected a search expression")
Convey("it is a search expression", func() { assert.False(t, query.isMathExpression(), "Expected not math expression")
So(query.isSearchExpression(), ShouldBeTrue) })
})
Convey("it is not math expressions", func() {
So(query.isMathExpression(), ShouldBeFalse)
})
})
Convey("and no expression, no multi dimension key values and no * was used", func() { t.Run("No expression, no multi dimension key values and no * was used", func(t *testing.T) {
query := &cloudWatchQuery{ query := &cloudWatchQuery{
RefId: "A", RefId: "A",
Region: "us-east-1", Region: "us-east-1",
Expression: "", Expression: "",
Stats: "Average", Stats: "Average",
Period: 300, Period: 300,
Id: "id1", Id: "id1",
MatchExact: true, MatchExact: true,
Dimensions: map[string][]string{ Dimensions: map[string][]string{
"InstanceId": {"i-12345678"}, "InstanceId": {"i-12345678"},
}, },
} }
Convey("it is not a search expression", func() { assert.False(t, query.isSearchExpression(), "Expected not a search expression")
So(query.isSearchExpression(), ShouldBeFalse) assert.False(t, query.isMathExpression(), "Expected not math expressions")
}) })
Convey("it is not math expressions", func() {
So(query.isMathExpression(), ShouldBeFalse)
})
})
Convey("and no expression but multi dimension key values exist", func() { t.Run("No expression but multi dimension key values exist", func(t *testing.T) {
query := &cloudWatchQuery{ query := &cloudWatchQuery{
RefId: "A", RefId: "A",
Region: "us-east-1", Region: "us-east-1",
Expression: "", Expression: "",
Stats: "Average", Stats: "Average",
Period: 300, Period: 300,
Id: "id1", Id: "id1",
Dimensions: map[string][]string{ Dimensions: map[string][]string{
"InstanceId": {"i-12345678", "i-34562312"}, "InstanceId": {"i-12345678", "i-34562312"},
}, },
} }
Convey("it is a search expression", func() { assert.True(t, query.isSearchExpression(), "Expected a search expression")
So(query.isSearchExpression(), ShouldBeTrue) assert.False(t, query.isMathExpression(), "Expected not math expressions")
}) })
Convey("it is not math expressions", func() {
So(query.isMathExpression(), ShouldBeFalse)
})
})
Convey("and no expression but dimension values has *", func() { t.Run("No expression but dimension values has *", func(t *testing.T) {
query := &cloudWatchQuery{ query := &cloudWatchQuery{
RefId: "A", RefId: "A",
Region: "us-east-1", Region: "us-east-1",
Expression: "", Expression: "",
Stats: "Average", Stats: "Average",
Period: 300, Period: 300,
Id: "id1", Id: "id1",
Dimensions: map[string][]string{ Dimensions: map[string][]string{
"InstanceId": {"i-12345678", "*"}, "InstanceId": {"i-12345678", "*"},
"InstanceType": {"abc", "def"}, "InstanceType": {"abc", "def"},
}, },
} }
Convey("it is not a search expression", func() { assert.True(t, query.isSearchExpression(), "Expected a search expression")
So(query.isSearchExpression(), ShouldBeTrue) assert.False(t, query.isMathExpression(), "Expected not math expression")
}) })
Convey("it is not math expressions", func() {
So(query.isMathExpression(), ShouldBeFalse)
})
})
Convey("and query has a multi-valued dimension", func() { t.Run("Query has a multi-valued dimension", func(t *testing.T) {
query := &cloudWatchQuery{ query := &cloudWatchQuery{
RefId: "A", RefId: "A",
Region: "us-east-1", Region: "us-east-1",
Expression: "", Expression: "",
Stats: "Average", Stats: "Average",
Period: 300, Period: 300,
Id: "id1", Id: "id1",
Dimensions: map[string][]string{ Dimensions: map[string][]string{
"InstanceId": {"i-12345678", "i-12345679"}, "InstanceId": {"i-12345678", "i-12345679"},
"InstanceType": {"abc"}, "InstanceType": {"abc"},
}, },
} }
Convey("it is a search expression", func() { assert.True(t, query.isSearchExpression(), "Expected a search expression")
So(query.isSearchExpression(), ShouldBeTrue) assert.True(t, query.isMultiValuedDimensionExpression(), "Expected a multi-valued dimension expression")
}) })
Convey("it is a multi-valued dimension expression", func() {
So(query.isMultiValuedDimensionExpression(), ShouldBeTrue)
})
})
Convey("and no dimensions were added", func() { t.Run("No dimensions were added", func(t *testing.T) {
query := &cloudWatchQuery{ query := &cloudWatchQuery{
RefId: "A", RefId: "A",
Region: "us-east-1", Region: "us-east-1",
Expression: "", Expression: "",
Stats: "Average", Stats: "Average",
Period: 300, Period: 300,
Id: "id1", Id: "id1",
MatchExact: false, MatchExact: false,
Dimensions: make(map[string][]string), Dimensions: make(map[string][]string),
} }
Convey("and match exact is false", func() { t.Run("Match exact is false", func(t *testing.T) {
query.MatchExact = false query.MatchExact = false
Convey("it is a search expression", func() { assert.True(t, query.isSearchExpression(), "Expected a search expression")
So(query.isSearchExpression(), ShouldBeTrue) assert.False(t, query.isMathExpression(), "Expected not math expression")
}) assert.False(t, query.isMetricStat(), "Expected not metric stat")
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() { t.Run("Match exact is true", func(t *testing.T) {
query := &cloudWatchQuery{ query.MatchExact = true
RefId: "A", assert.False(t, query.isSearchExpression(), "Exxpected not search expression")
Region: "us-east-1", assert.False(t, query.isMathExpression(), "Expected not math expression")
Expression: "", assert.True(t, query.isMetricStat(), "Expected a metric stat")
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", 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")
})
} }
...@@ -4,155 +4,150 @@ import ( ...@@ -4,155 +4,150 @@ import (
"testing" "testing"
"github.com/aws/aws-sdk-go/aws" "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) { func TestQueryTransformer(t *testing.T) {
Convey("TestQueryTransformer", t, func() { executor := newExecutor()
Convey("when transforming queries", func() { t.Run("One cloudwatchQuery is generated when its request query has one stat", func(t *testing.T) {
executor := newExecutor() requestQueries := []*requestQuery{
Convey("one cloudwatchQuery is generated when its request query has one stat", func() { {
requestQueries := []*requestQuery{ RefId: "D",
{ Region: "us-east-1",
RefId: "D", Namespace: "ec2",
Region: "us-east-1", MetricName: "CPUUtilization",
Namespace: "ec2", Statistics: aws.StringSlice([]string{"Average"}),
MetricName: "CPUUtilization", Period: 600,
Statistics: aws.StringSlice([]string{"Average"}), Id: "",
Period: 600, },
Id: "", }
},
}
res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries)
So(err, ShouldBeNil) require.NoError(t, err)
So(len(res), ShouldEqual, 1) assert.Len(t, res, 1)
}) })
Convey("two cloudwatchQuery is generated when there's two stats", func() { t.Run("Two cloudwatchQuery is generated when there's two stats", func(t *testing.T) {
requestQueries := []*requestQuery{ requestQueries := []*requestQuery{
{ {
RefId: "D", RefId: "D",
Region: "us-east-1", Region: "us-east-1",
Namespace: "ec2", Namespace: "ec2",
MetricName: "CPUUtilization", MetricName: "CPUUtilization",
Statistics: aws.StringSlice([]string{"Average", "Sum"}), Statistics: aws.StringSlice([]string{"Average", "Sum"}),
Period: 600, Period: 600,
Id: "", Id: "",
}, },
} }
res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries)
So(err, ShouldBeNil) require.NoError(t, err)
So(len(res), ShouldEqual, 2) assert.Len(t, res, 2)
}) })
Convey("and id is given by user", func() { t.Run("id is given by user that will be used in the cloudwatch query", func(t *testing.T) {
Convey("that id will be used in the cloudwatch query", func() { requestQueries := []*requestQuery{
requestQueries := []*requestQuery{ {
{ RefId: "D",
RefId: "D", Region: "us-east-1",
Region: "us-east-1", Namespace: "ec2",
Namespace: "ec2", MetricName: "CPUUtilization",
MetricName: "CPUUtilization", Statistics: aws.StringSlice([]string{"Average"}),
Statistics: aws.StringSlice([]string{"Average"}), Period: 600,
Period: 600, Id: "myid",
Id: "myid", },
}, }
}
res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries)
So(err, ShouldBeNil) require.Nil(t, err)
So(len(res), ShouldEqual, 1) assert.Equal(t, len(res), 1)
So(res, ShouldContainKey, "myid") assert.Contains(t, res, "myid")
}) })
})
Convey("and id is not given by user", func() { t.Run("ID is not given by user", func(t *testing.T) {
Convey("id will be generated based on ref id if query only has one stat", func() { t.Run("ID will be generated based on ref ID if query only has one stat", func(t *testing.T) {
requestQueries := []*requestQuery{ requestQueries := []*requestQuery{
{ {
RefId: "D", RefId: "D",
Region: "us-east-1", Region: "us-east-1",
Namespace: "ec2", Namespace: "ec2",
MetricName: "CPUUtilization", MetricName: "CPUUtilization",
Statistics: aws.StringSlice([]string{"Average"}), Statistics: aws.StringSlice([]string{"Average"}),
Period: 600, Period: 600,
Id: "", Id: "",
}, },
} }
res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries)
So(err, ShouldBeNil) require.NoError(t, err)
So(len(res), ShouldEqual, 1) assert.Len(t, res, 1)
So(res, ShouldContainKey, "queryD") assert.Contains(t, res, "queryD")
}) })
Convey("id will be generated based on ref and stat name if query has two stats", func() { t.Run("ID will be generated based on ref and stat name if query has two stats", func(t *testing.T) {
requestQueries := []*requestQuery{ requestQueries := []*requestQuery{
{ {
RefId: "D", RefId: "D",
Region: "us-east-1", Region: "us-east-1",
Namespace: "ec2", Namespace: "ec2",
MetricName: "CPUUtilization", MetricName: "CPUUtilization",
Statistics: aws.StringSlice([]string{"Average", "Sum"}), Statistics: aws.StringSlice([]string{"Average", "Sum"}),
Period: 600, Period: 600,
Id: "", Id: "",
}, },
} }
res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries)
So(err, ShouldBeNil) require.NoError(t, err)
So(len(res), ShouldEqual, 2) assert.Len(t, res, 2)
So(res, ShouldContainKey, "queryD_Sum") assert.Contains(t, res, "queryD_Sum")
So(res, ShouldContainKey, "queryD_Average") assert.Contains(t, res, "queryD_Average")
}) })
}) })
Convey("dot should be removed when query has more than one stat and one of them is a percentile", func() { 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{ requestQueries := []*requestQuery{
{ {
RefId: "D", RefId: "D",
Region: "us-east-1", Region: "us-east-1",
Namespace: "ec2", Namespace: "ec2",
MetricName: "CPUUtilization", MetricName: "CPUUtilization",
Statistics: aws.StringSlice([]string{"Average", "p46.32"}), Statistics: aws.StringSlice([]string{"Average", "p46.32"}),
Period: 600, Period: 600,
Id: "", Id: "",
}, },
} }
res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries)
So(err, ShouldBeNil) require.NoError(t, err)
So(len(res), ShouldEqual, 2) assert.Len(t, res, 2)
So(res, ShouldContainKey, "queryD_p46_32") assert.Contains(t, res, "queryD_p46_32")
}) })
Convey("should return an error if two queries have the same id", func() { t.Run("should return an error if two queries have the same id", func(t *testing.T) {
requestQueries := []*requestQuery{ requestQueries := []*requestQuery{
{ {
RefId: "D", RefId: "D",
Region: "us-east-1", Region: "us-east-1",
Namespace: "ec2", Namespace: "ec2",
MetricName: "CPUUtilization", MetricName: "CPUUtilization",
Statistics: aws.StringSlice([]string{"Average", "p46.32"}), Statistics: aws.StringSlice([]string{"Average", "p46.32"}),
Period: 600, Period: 600,
Id: "myId", Id: "myId",
}, },
{ {
RefId: "E", RefId: "E",
Region: "us-east-1", Region: "us-east-1",
Namespace: "ec2", Namespace: "ec2",
MetricName: "CPUUtilization", MetricName: "CPUUtilization",
Statistics: aws.StringSlice([]string{"Average", "p46.32"}), Statistics: aws.StringSlice([]string{"Average", "p46.32"}),
Period: 600, Period: 600,
Id: "myId", Id: "myId",
}, },
} }
res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries) res, err := executor.transformRequestQueriesToCloudWatchQueries(requestQueries)
So(res, ShouldBeNil) require.Nil(t, res)
So(err, ShouldNotBeNil) assert.Error(t, err)
})
})
}) })
} }
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