2019-11-14 03:59:41 -06:00
|
|
|
package cloudwatch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2021-11-30 03:53:31 -06:00
|
|
|
"time"
|
2019-11-14 03:59:41 -06:00
|
|
|
|
2020-10-02 12:40:15 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-11-30 03:53:31 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-11-14 03:59:41 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCloudWatchQuery(t *testing.T) {
|
2021-11-30 03:53:31 -06:00
|
|
|
t.Run("Deeplink is not generated for MetricQueryTypeQuery", func(t *testing.T) {
|
|
|
|
startTime := time.Now()
|
|
|
|
endTime := startTime.Add(2 * time.Hour)
|
|
|
|
query := &cloudWatchQuery{
|
|
|
|
RefId: "A",
|
|
|
|
Region: "us-east-1",
|
|
|
|
Expression: "",
|
|
|
|
Statistic: "Average",
|
|
|
|
Period: 300,
|
|
|
|
Id: "id1",
|
|
|
|
MatchExact: true,
|
|
|
|
Dimensions: map[string][]string{
|
|
|
|
"InstanceId": {"i-12345678"},
|
|
|
|
},
|
|
|
|
MetricQueryType: MetricQueryTypeQuery,
|
|
|
|
MetricEditorMode: MetricEditorModeBuilder,
|
|
|
|
}
|
|
|
|
|
|
|
|
deepLink, err := query.buildDeepLink(startTime, endTime)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, deepLink)
|
|
|
|
})
|
|
|
|
|
2020-10-02 12:40:15 -05:00
|
|
|
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)",
|
2021-09-08 09:06:43 -05:00
|
|
|
Statistic: "Average",
|
2020-10-02 12:40:15 -05:00
|
|
|
Period: 300,
|
|
|
|
Id: "id1",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, query.isSearchExpression(), "Expected a search expression")
|
|
|
|
assert.False(t, query.isMathExpression(), "Expected not math expression")
|
|
|
|
})
|
2019-11-14 03:59:41 -06:00
|
|
|
|
2020-10-02 12:40:15 -05:00
|
|
|
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: "",
|
2021-09-08 09:06:43 -05:00
|
|
|
Statistic: "Average",
|
2020-10-02 12:40:15 -05:00
|
|
|
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")
|
|
|
|
})
|
2019-11-14 03:59:41 -06:00
|
|
|
|
2020-10-02 12:40:15 -05:00
|
|
|
t.Run("No expression but multi dimension key values exist", func(t *testing.T) {
|
|
|
|
query := &cloudWatchQuery{
|
|
|
|
RefId: "A",
|
|
|
|
Region: "us-east-1",
|
|
|
|
Expression: "",
|
2021-09-08 09:06:43 -05:00
|
|
|
Statistic: "Average",
|
2020-10-02 12:40:15 -05:00
|
|
|
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")
|
|
|
|
})
|
2019-11-14 03:59:41 -06:00
|
|
|
|
2020-10-02 12:40:15 -05:00
|
|
|
t.Run("No expression but dimension values has *", func(t *testing.T) {
|
|
|
|
query := &cloudWatchQuery{
|
|
|
|
RefId: "A",
|
|
|
|
Region: "us-east-1",
|
|
|
|
Expression: "",
|
2021-09-08 09:06:43 -05:00
|
|
|
Statistic: "Average",
|
2020-10-02 12:40:15 -05:00
|
|
|
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")
|
|
|
|
})
|
2019-11-14 03:59:41 -06:00
|
|
|
|
2020-10-02 12:40:15 -05:00
|
|
|
t.Run("Query has a multi-valued dimension", func(t *testing.T) {
|
|
|
|
query := &cloudWatchQuery{
|
|
|
|
RefId: "A",
|
|
|
|
Region: "us-east-1",
|
|
|
|
Expression: "",
|
2021-09-08 09:06:43 -05:00
|
|
|
Statistic: "Average",
|
2020-10-02 12:40:15 -05:00
|
|
|
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")
|
|
|
|
})
|
2020-03-10 15:14:58 -05:00
|
|
|
|
2020-10-02 12:40:15 -05:00
|
|
|
t.Run("No dimensions were added", func(t *testing.T) {
|
|
|
|
query := &cloudWatchQuery{
|
|
|
|
RefId: "A",
|
|
|
|
Region: "us-east-1",
|
|
|
|
Expression: "",
|
2021-09-08 09:06:43 -05:00
|
|
|
Statistic: "Average",
|
2020-10-02 12:40:15 -05:00
|
|
|
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")
|
2019-11-14 03:59:41 -06:00
|
|
|
})
|
|
|
|
|
2020-10-02 12:40:15 -05:00
|
|
|
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")
|
2019-11-14 03:59:41 -06:00
|
|
|
})
|
|
|
|
})
|
2020-10-02 12:40:15 -05:00
|
|
|
|
|
|
|
t.Run("Match exact is", func(t *testing.T) {
|
|
|
|
query := &cloudWatchQuery{
|
|
|
|
RefId: "A",
|
|
|
|
Region: "us-east-1",
|
|
|
|
Expression: "",
|
2021-09-08 09:06:43 -05:00
|
|
|
Statistic: "Average",
|
2020-10-02 12:40:15 -05:00
|
|
|
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")
|
|
|
|
})
|
2019-11-14 03:59:41 -06:00
|
|
|
}
|