9dc6cac1f2
* Use context in aws DescribeRegionsWithContext In the current way, DescribeRegions is used which doesn't allow cancelling the request if the context changes. Using DescribeRegionsWithContext is the preferred way. * Fix context variable * Revert GetRegionsWithContext to GetRegions GetRegions is not an AWS SDK method. Hence, GetRegions should be enough as the name change is not needed for context implementation.
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/mocks"
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/utils"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
var testLogger = log.New("test logger")
|
|
|
|
func TestRegions(t *testing.T) {
|
|
t.Run("returns regions from the api and merges them with default regions", func(t *testing.T) {
|
|
mockRegions := &ec2.DescribeRegionsOutput{
|
|
Regions: []*ec2.Region{
|
|
{
|
|
RegionName: utils.Pointer("earth-1"),
|
|
},
|
|
},
|
|
}
|
|
ec2Mock := &mocks.EC2Mock{}
|
|
ec2Mock.On("DescribeRegionsWithContext").Return(mockRegions, nil)
|
|
regions, err := NewRegionsService(ec2Mock, testLogger).GetRegions(context.Background())
|
|
assert.NoError(t, err)
|
|
assert.Contains(t, regions, resources.ResourceResponse[resources.Region]{
|
|
Value: resources.Region{
|
|
Name: "us-east-2",
|
|
},
|
|
})
|
|
assert.Contains(t, regions, resources.ResourceResponse[resources.Region]{
|
|
Value: resources.Region{
|
|
Name: "earth-1",
|
|
},
|
|
})
|
|
})
|
|
|
|
t.Run("always returns default regions, even if fetch fails", func(t *testing.T) {
|
|
ec2Mock := &mocks.EC2Mock{}
|
|
mockRegions := &ec2.DescribeRegionsOutput{
|
|
Regions: []*ec2.Region{},
|
|
}
|
|
ec2Mock.On("DescribeRegionsWithContext").Return(mockRegions, assert.AnError)
|
|
regions, err := NewRegionsService(ec2Mock, testLogger).GetRegions(context.Background())
|
|
assert.NoError(t, err)
|
|
assert.Contains(t, regions, resources.ResourceResponse[resources.Region]{
|
|
Value: resources.Region{
|
|
Name: "us-east-2",
|
|
},
|
|
})
|
|
})
|
|
}
|