grafana/pkg/tsdb/cloudwatch/models/request/dimension_keys_request_test.go
Erik Sundell 017da781cf
Cloudwatch: Refactor dimension values resource request (#57346)
* refactor dimension values backend

* fix test file name
2022-10-24 15:04:25 +02:00

73 lines
2.8 KiB
Go

package request
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDimensionKeyRequest(t *testing.T) {
t.Run("Should parse parameters without dimension filter", func(t *testing.T) {
request, err := GetDimensionKeysRequest(map[string][]string{
"region": {"us-east-1"},
"namespace": {"AWS/EC2"},
"metricName": {"CPUUtilization"}},
)
require.NoError(t, err)
assert.Equal(t, "us-east-1", request.Region)
assert.Equal(t, "AWS/EC2", request.Namespace)
assert.Equal(t, "CPUUtilization", request.MetricName)
})
t.Run("Should parse parameters with single valued dimension filter", func(t *testing.T) {
request, err := GetDimensionKeysRequest(map[string][]string{
"region": {"us-east-1"},
"namespace": {"AWS/EC2"},
"metricName": {"CPUUtilization"},
"dimensionFilters": {"{\"InstanceId\": \"i-1234567890abcdef0\"}"},
})
require.NoError(t, err)
assert.Equal(t, "us-east-1", request.Region)
assert.Equal(t, "AWS/EC2", request.Namespace)
assert.Equal(t, "CPUUtilization", request.MetricName)
assert.Equal(t, 1, len(request.DimensionFilter))
assert.Equal(t, "InstanceId", request.DimensionFilter[0].Name)
assert.Equal(t, "i-1234567890abcdef0", request.DimensionFilter[0].Value)
})
t.Run("Should parse parameters with multi-valued dimension filter", func(t *testing.T) {
request, err := GetDimensionKeysRequest(map[string][]string{
"region": {"us-east-1"},
"namespace": {"AWS/EC2"},
"metricName": {"CPUUtilization"},
"dimensionFilters": {"{\"InstanceId\": [\"i-1234567890abcdef0\", \"i-1234567890abcdef1\"]}"},
})
require.NoError(t, err)
assert.Equal(t, "us-east-1", request.Region)
assert.Equal(t, "AWS/EC2", request.Namespace)
assert.Equal(t, "CPUUtilization", request.MetricName)
assert.Equal(t, 2, len(request.DimensionFilter))
assert.Equal(t, "InstanceId", request.DimensionFilter[0].Name)
assert.Equal(t, "i-1234567890abcdef0", request.DimensionFilter[0].Value)
assert.Equal(t, "InstanceId", request.DimensionFilter[1].Name)
assert.Equal(t, "i-1234567890abcdef1", request.DimensionFilter[1].Value)
})
t.Run("Should parse parameters with wildcard dimension filter", func(t *testing.T) {
request, err := GetDimensionKeysRequest(map[string][]string{
"region": {"us-east-1"},
"namespace": {"AWS/EC2"},
"metricName": {"CPUUtilization"},
"dimensionFilters": {"{\"InstanceId\": [\"*\"]}"},
})
require.NoError(t, err)
assert.Equal(t, "us-east-1", request.Region)
assert.Equal(t, "AWS/EC2", request.Namespace)
assert.Equal(t, "CPUUtilization", request.MetricName)
assert.Equal(t, 1, len(request.DimensionFilter))
assert.Equal(t, "InstanceId", request.DimensionFilter[0].Name)
assert.Equal(t, "", request.DimensionFilter[0].Value)
})
}