CloudWatch: Handle new error codes for MetricInsights (#47033)
* CloudWatch: Handle new error codes for MetricInsights * Changes/test to support case where only the first GetMetricDataOutput returns errors and refactoring * Fix Potential file inclusion via variable * Fix gosec 304 by assigning a new variable * fix goimports issue
This commit is contained in:
@@ -3,6 +3,7 @@ package cloudwatch
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -13,9 +14,10 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func loadGetMetricDataOutputsFromFile() ([]*cloudwatch.GetMetricDataOutput, error) {
|
||||
func loadGetMetricDataOutputsFromFile(filePath string) ([]*cloudwatch.GetMetricDataOutput, error) {
|
||||
var getMetricDataOutputs []*cloudwatch.GetMetricDataOutput
|
||||
jsonBody, err := ioutil.ReadFile("./test-data/multiple-outputs.json")
|
||||
cleanFilePath := filepath.Clean(filePath)
|
||||
jsonBody, err := ioutil.ReadFile(cleanFilePath)
|
||||
if err != nil {
|
||||
return getMetricDataOutputs, err
|
||||
}
|
||||
@@ -27,7 +29,7 @@ func TestCloudWatchResponseParser(t *testing.T) {
|
||||
startTime := time.Now()
|
||||
endTime := startTime.Add(2 * time.Hour)
|
||||
t.Run("when aggregating response", func(t *testing.T) {
|
||||
getMetricDataOutputs, err := loadGetMetricDataOutputsFromFile()
|
||||
getMetricDataOutputs, err := loadGetMetricDataOutputsFromFile("./test-data/multiple-outputs.json")
|
||||
require.NoError(t, err)
|
||||
aggregatedResponse := aggregateResponse(getMetricDataOutputs)
|
||||
t.Run("response for id a", func(t *testing.T) {
|
||||
@@ -43,7 +45,16 @@ func TestCloudWatchResponseParser(t *testing.T) {
|
||||
assert.Equal(t, "Complete", aggregatedResponse[idA].StatusCode)
|
||||
})
|
||||
t.Run("should have exceeded request limit", func(t *testing.T) {
|
||||
assert.True(t, aggregatedResponse[idA].RequestExceededMaxLimit)
|
||||
assert.True(t, aggregatedResponse[idA].ErrorCodes["MaxMetricsExceeded"])
|
||||
})
|
||||
t.Run("should have exceeded query time range", func(t *testing.T) {
|
||||
assert.True(t, aggregatedResponse[idA].ErrorCodes["MaxQueryTimeRangeExceeded"])
|
||||
})
|
||||
t.Run("should have exceeded max query results", func(t *testing.T) {
|
||||
assert.True(t, aggregatedResponse[idA].ErrorCodes["MaxQueryResultsExceeded"])
|
||||
})
|
||||
t.Run("should have exceeded max matching results", func(t *testing.T) {
|
||||
assert.True(t, aggregatedResponse[idA].ErrorCodes["MaxMatchingResultsExceeded"])
|
||||
})
|
||||
})
|
||||
t.Run("response for id b", func(t *testing.T) {
|
||||
@@ -58,6 +69,27 @@ func TestCloudWatchResponseParser(t *testing.T) {
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("when aggregating response and error codes are in first GetMetricDataOutput", func(t *testing.T) {
|
||||
getMetricDataOutputs, err := loadGetMetricDataOutputsFromFile("./test-data/multiple-outputs2.json")
|
||||
require.NoError(t, err)
|
||||
aggregatedResponse := aggregateResponse(getMetricDataOutputs)
|
||||
t.Run("response for id a", func(t *testing.T) {
|
||||
idA := "a"
|
||||
t.Run("should have exceeded request limit", func(t *testing.T) {
|
||||
assert.True(t, aggregatedResponse[idA].ErrorCodes["MaxMetricsExceeded"])
|
||||
})
|
||||
t.Run("should have exceeded query time range", func(t *testing.T) {
|
||||
assert.True(t, aggregatedResponse[idA].ErrorCodes["MaxQueryTimeRangeExceeded"])
|
||||
})
|
||||
t.Run("should have exceeded max query results", func(t *testing.T) {
|
||||
assert.True(t, aggregatedResponse[idA].ErrorCodes["MaxQueryResultsExceeded"])
|
||||
})
|
||||
t.Run("should have exceeded max matching results", func(t *testing.T) {
|
||||
assert.True(t, aggregatedResponse[idA].ErrorCodes["MaxMatchingResultsExceeded"])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Expand dimension value using exact match", func(t *testing.T) {
|
||||
timestamp := time.Unix(0, 0)
|
||||
response := &queryRowResponse{
|
||||
|
||||
Reference in New Issue
Block a user