driftctl/enumeration/remote/aws_cloudformation_scanner_...

126 lines
4.7 KiB
Go
Raw Permalink Normal View History

2021-09-08 09:16:44 +00:00
package remote
import (
"errors"
"testing"
"github.com/snyk/driftctl/enumeration"
"github.com/snyk/driftctl/enumeration/remote/alerts"
"github.com/snyk/driftctl/enumeration/remote/aws"
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
"github.com/snyk/driftctl/enumeration/remote/cache"
2022-07-06 08:44:42 +00:00
"github.com/snyk/driftctl/enumeration/remote/common"
remoteerr "github.com/snyk/driftctl/enumeration/remote/error"
"github.com/snyk/driftctl/enumeration/terraform"
2021-09-08 09:16:44 +00:00
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/snyk/driftctl/enumeration/resource"
resourceaws "github.com/snyk/driftctl/enumeration/resource/aws"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/mocks"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/test"
"github.com/snyk/driftctl/test/goldenfile"
terraform2 "github.com/snyk/driftctl/test/terraform"
2021-09-08 09:16:44 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestCloudformationStack(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(*repository.MockCloudformationRepository, *mocks.AlerterInterface)
2021-09-08 09:16:44 +00:00
wantErr error
}{
{
test: "no cloudformation stacks",
dirName: "aws_cloudformation_stack_empty",
mocks: func(repository *repository.MockCloudformationRepository, alerter *mocks.AlerterInterface) {
2021-09-08 09:16:44 +00:00
repository.On("ListAllStacks").Return([]*cloudformation.Stack{}, nil)
},
},
{
test: "multiple cloudformation stacks",
dirName: "aws_cloudformation_stack_multiple",
mocks: func(repository *repository.MockCloudformationRepository, alerter *mocks.AlerterInterface) {
2021-09-08 09:16:44 +00:00
repository.On("ListAllStacks").Return([]*cloudformation.Stack{
{StackId: awssdk.String("arn:aws:cloudformation:us-east-1:047081014315:stack/bar-stack/c7a96e70-0f21-11ec-bd2a-0a2d95c2b2ab")},
{StackId: awssdk.String("arn:aws:cloudformation:us-east-1:047081014315:stack/foo-stack/c7aa0ab0-0f21-11ec-ba25-129d8c0b3757")},
}, nil)
},
},
{
test: "cannot list cloudformation stacks",
dirName: "aws_cloudformation_stack_list",
mocks: func(repository *repository.MockCloudformationRepository, alerter *mocks.AlerterInterface) {
2021-09-08 09:16:44 +00:00
awsError := awserr.NewRequestFailure(awserr.New("AccessDeniedException", "", errors.New("")), 400, "")
repository.On("ListAllStacks").Return(nil, awsError)
2022-07-06 08:44:42 +00:00
alerter.On("SendAlert", resourceaws.AwsCloudformationStackResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(awsError, resourceaws.AwsCloudformationStackResourceType, resourceaws.AwsCloudformationStackResourceType), alerts.EnumerationPhase)).Return()
2021-09-08 09:16:44 +00:00
},
wantErr: nil,
},
}
2022-07-21 08:37:03 +00:00
factory := terraform.NewTerraformResourceFactory()
2021-09-08 09:16:44 +00:00
deserializer := resource.NewDeserializer(factory)
for _, c := range tests {
t.Run(c.test, func(tt *testing.T) {
shouldUpdate := c.dirName == *goldenfile.Update
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform.NewProviderLibrary()
2022-07-06 08:44:42 +00:00
remoteLibrary := common.NewRemoteLibrary()
2021-09-08 09:16:44 +00:00
// Initialize mocks
alerter := &mocks.AlerterInterface{}
fakeRepo := &repository.MockCloudformationRepository{}
2021-09-08 09:16:44 +00:00
c.mocks(fakeRepo, alerter)
var repo repository.CloudformationRepository = fakeRepo
2021-09-08 09:16:44 +00:00
providerVersion := "3.19.0"
realProvider, err := terraform2.InitTestAwsProvider(providerLibrary, providerVersion)
if err != nil {
t.Fatal(err)
}
provider := terraform2.NewFakeTerraformProvider(realProvider)
provider.WithResponse(c.dirName)
// Replace mock by real resources if we are in update mode
if shouldUpdate {
err := realProvider.Init()
if err != nil {
t.Fatal(err)
}
provider.ShouldUpdate()
repo = repository.NewCloudformationRepository(sess, cache.New(0))
2021-09-08 09:16:44 +00:00
}
remoteLibrary.AddEnumerator(aws.NewCloudformationStackEnumerator(repo, factory))
2022-07-06 08:44:42 +00:00
remoteLibrary.AddDetailsFetcher(resourceaws.AwsCloudformationStackResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsCloudformationStackResourceType, provider, deserializer))
2021-09-08 09:16:44 +00:00
testFilter := &enumeration.MockFilter{}
2021-09-08 09:16:44 +00:00
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
got, err := s.Resources()
assert.Equal(tt, err, c.wantErr)
if err != nil {
return
}
test.TestAgainstGoldenFile(got, resourceaws.AwsCloudformationStackResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
testFilter.AssertExpectations(tt)
})
}
}