driftctl/pkg/remote/aws/ec2_ebs_volume_supplier_tes...

106 lines
3.0 KiB
Go
Raw Normal View History

package aws
import (
"context"
"testing"
2021-02-23 17:16:10 +00:00
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
2021-05-21 14:09:45 +00:00
testresource "github.com/cloudskiff/driftctl/test/resource"
2021-02-23 17:16:10 +00:00
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
2021-01-15 11:44:13 +00:00
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/stretchr/testify/assert"
"github.com/cloudskiff/driftctl/test/goldenfile"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/mocks"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
func TestEC2EbsVolumeSupplier_Resources(t *testing.T) {
tests := []struct {
2021-02-23 17:16:10 +00:00
test string
dirName string
mock func(mock *repository.MockEC2Repository)
err error
}{
{
test: "no volumes",
dirName: "ec2_ebs_volume_empty",
2021-02-23 17:16:10 +00:00
mock: func(mock *repository.MockEC2Repository) {
mock.On("ListAllVolumes").Return([]*ec2.Volume{}, nil)
},
err: nil,
},
{
test: "with volumes",
dirName: "ec2_ebs_volume_multiple",
2021-02-23 17:16:10 +00:00
mock: func(mock *repository.MockEC2Repository) {
mock.On("ListAllVolumes").Return([]*ec2.Volume{
{
VolumeId: aws.String("vol-081c7272a57a09db1"),
},
2021-02-23 17:16:10 +00:00
{
VolumeId: aws.String("vol-01ddc91d3d9d1318b"),
},
2021-02-23 17:16:10 +00:00
}, nil)
},
err: nil,
},
{
2021-02-23 17:16:10 +00:00
test: "cannot list volumes",
dirName: "ec2_ebs_volume_empty",
mock: func(mock *repository.MockEC2Repository) {
mock.On("ListAllVolumes").Return([]*ec2.Volume{}, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsEbsVolumeResourceType),
},
}
for _, tt := range tests {
shouldUpdate := tt.dirName == *goldenfile.Update
providerLibrary := terraform.NewProviderLibrary()
supplierLibrary := resource.NewSupplierLibrary()
2021-05-21 14:09:45 +00:00
repo := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(repo)
factory := terraform.NewTerraformResourceFactory(repo)
deserializer := resource.NewDeserializer(factory)
if shouldUpdate {
provider, err := InitTestAwsProvider(providerLibrary)
if err != nil {
t.Fatal(err)
}
2021-06-03 10:39:33 +00:00
supplierLibrary.AddSupplier(NewEC2EbsVolumeSupplier(provider, deserializer, repository.NewEC2Repository(provider.session, cache.New(0))))
}
t.Run(tt.test, func(t *testing.T) {
provider := mocks.NewMockedGoldenTFProvider(tt.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
2021-02-23 17:16:10 +00:00
client := &repository.MockEC2Repository{}
tt.mock(client)
s := &EC2EbsVolumeSupplier{
provider,
deserializer,
client,
2021-01-15 11:44:13 +00:00
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
}
got, err := s.Resources()
assert.Equal(t, tt.err, err)
test.CtyTestDiff(got, tt.dirName, provider, deserializer, shouldUpdate, t)
})
}
}