2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-01-20 13:01:57 +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"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-01-15 11:44:13 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/parallel"
|
2021-01-20 13:01:57 +00:00
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
awsdeserializer "github.com/cloudskiff/driftctl/pkg/resource/aws/deserializer"
|
|
|
|
|
|
|
|
"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 TestEC2EipAssociationSupplier_Resources(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
|
|
|
dirName string
|
|
|
|
addresses []*ec2.Address
|
2021-01-20 13:01:57 +00:00
|
|
|
listError error
|
2020-12-09 15:31:34 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "no eip associations",
|
|
|
|
dirName: "ec2_eip_association_empty",
|
|
|
|
addresses: []*ec2.Address{},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "with eip associations",
|
|
|
|
dirName: "ec2_eip_association_single",
|
|
|
|
addresses: []*ec2.Address{
|
|
|
|
{
|
|
|
|
AssociationId: aws.String("eipassoc-0e9a7356e30f0c3d1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
2021-01-20 13:01:57 +00:00
|
|
|
{
|
|
|
|
test: "Cannot list eip associations",
|
|
|
|
dirName: "ec2_eip_association_empty",
|
|
|
|
listError: awserr.NewRequestFailure(nil, 403, ""),
|
|
|
|
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsEipAssociationResourceType),
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
shouldUpdate := tt.dirName == *goldenfile.Update
|
|
|
|
if shouldUpdate {
|
|
|
|
provider, err := NewTerraFormProvider()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
terraform.AddProvider(terraform.AWS, provider)
|
|
|
|
resource.AddSupplier(NewEC2EipAssociationSupplier(provider.Runner(), ec2.New(provider.session)))
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(tt.test, func(t *testing.T) {
|
|
|
|
provider := mocks.NewMockedGoldenTFProvider(tt.dirName, terraform.Provider(terraform.AWS), shouldUpdate)
|
|
|
|
deserializer := awsdeserializer.NewEC2EipAssociationDeserializer()
|
2021-01-20 13:01:57 +00:00
|
|
|
client := mocks.NewMockAWSEC2EipClient(tt.addresses)
|
|
|
|
if tt.listError != nil {
|
|
|
|
client = mocks.NewMockAWSEC2ErrorClient(tt.listError)
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
s := &EC2EipAssociationSupplier{
|
|
|
|
provider,
|
|
|
|
deserializer,
|
2021-01-20 13:01:57 +00:00
|
|
|
client,
|
2021-01-15 11:44:13 +00:00
|
|
|
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
got, err := s.Resources()
|
2021-01-20 13:01:57 +00:00
|
|
|
assert.Equal(t, tt.err, err)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
test.CtyTestDiff(got, tt.dirName, provider, deserializer, shouldUpdate, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|