driftctl/pkg/remote/aws/vpc_security_group_supplier...

124 lines
4.0 KiB
Go
Raw Normal View History

package aws
import (
"context"
"testing"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/aws/aws-sdk-go/aws/awserr"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
2021-01-15 11:44:13 +00:00
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
2021-01-15 13:15:31 +00:00
"github.com/cloudskiff/driftctl/mocks"
2021-01-15 11:44:13 +00:00
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/remote/deserializer"
2021-01-15 11:44:13 +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"
2021-01-15 13:15:31 +00:00
mocks2 "github.com/cloudskiff/driftctl/test/mocks"
"github.com/stretchr/testify/assert"
2021-01-15 11:44:13 +00:00
"github.com/stretchr/testify/mock"
)
func TestVPCSecurityGroupSupplier_Resources(t *testing.T) {
tests := []struct {
2021-01-15 13:15:31 +00:00
test string
dirName string
mocks func(client *mocks.FakeEC2)
err error
}{
{
test: "no security groups",
dirName: "vpc_security_group_empty",
2021-01-15 13:15:31 +00:00
mocks: func(client *mocks.FakeEC2) {
client.On("DescribeSecurityGroupsPages",
&ec2.DescribeSecurityGroupsInput{},
mock.MatchedBy(func(callback func(res *ec2.DescribeSecurityGroupsOutput, lastPage bool) bool) bool {
callback(&ec2.DescribeSecurityGroupsOutput{}, true)
return true
})).Return(nil)
},
err: nil,
},
{
test: "with security groups",
dirName: "vpc_security_group_multiple",
2021-01-15 13:15:31 +00:00
mocks: func(client *mocks.FakeEC2) {
client.On("DescribeSecurityGroupsPages",
&ec2.DescribeSecurityGroupsInput{},
mock.MatchedBy(func(callback func(res *ec2.DescribeSecurityGroupsOutput, lastPage bool) bool) bool {
callback(&ec2.DescribeSecurityGroupsOutput{
SecurityGroups: []*ec2.SecurityGroup{
{
GroupId: aws.String("sg-0254c038e32f25530"),
GroupName: aws.String("foo"),
},
{
GroupId: aws.String("sg-9e0204ff"),
GroupName: aws.String("default"),
},
},
2021-01-15 13:15:31 +00:00
}, true)
return true
})).Return(nil)
},
err: nil,
},
{
test: "cannot list security groups",
dirName: "vpc_security_group_empty",
mocks: func(client *mocks.FakeEC2) {
client.On("DescribeSecurityGroupsPages",
&ec2.DescribeSecurityGroupsInput{},
mock.MatchedBy(func(callback func(res *ec2.DescribeSecurityGroupsOutput, lastPage bool) bool) bool {
return true
})).Return(awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsSecurityGroupResourceType),
},
}
for _, tt := range tests {
shouldUpdate := tt.dirName == *goldenfile.Update
providerLibrary := terraform.NewProviderLibrary()
supplierLibrary := resource.NewSupplierLibrary()
if shouldUpdate {
provider, err := InitTestAwsProvider(providerLibrary)
if err != nil {
t.Fatal(err)
}
supplierLibrary.AddSupplier(NewVPCSecurityGroupSupplier(provider))
}
t.Run(tt.test, func(t *testing.T) {
2021-01-15 13:15:31 +00:00
fakeEC2 := mocks.FakeEC2{}
tt.mocks(&fakeEC2)
provider := mocks2.NewMockedGoldenTFProvider(tt.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
2021-01-15 13:15:31 +00:00
securityGroupDeserializer := awsdeserializer.NewVPCSecurityGroupDeserializer()
defaultSecurityGroupDeserializer := awsdeserializer.NewDefaultSecurityGroupDeserializer()
s := &VPCSecurityGroupSupplier{
provider,
2021-01-15 13:15:31 +00:00
defaultSecurityGroupDeserializer,
securityGroupDeserializer,
&fakeEC2,
2021-01-15 11:44:13 +00:00
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
}
got, err := s.Resources()
assert.Equal(t, tt.err, err)
2021-01-15 13:15:31 +00:00
mock.AssertExpectationsForObjects(t)
deserializers := []deserializer.CTYDeserializer{securityGroupDeserializer, defaultSecurityGroupDeserializer}
test.CtyTestDiffMixed(got, tt.dirName, provider, deserializers, shouldUpdate, t)
})
}
}