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"
|
|
|
|
|
2021-01-15 11:44:13 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/parallel"
|
2021-01-20 13:01:57 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestS3BucketNotificationSupplier_Resources(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
|
|
|
dirName string
|
|
|
|
bucketsIDs []string
|
|
|
|
bucketLocation map[string]string
|
2021-01-20 13:01:57 +00:00
|
|
|
listError error
|
|
|
|
wantErr error
|
2020-12-09 15:31:34 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "single bucket without notifications",
|
|
|
|
dirName: "s3_bucket_notifications_no_notif",
|
|
|
|
bucketsIDs: []string{
|
|
|
|
"dritftctl-test-no-notifications",
|
|
|
|
},
|
|
|
|
bucketLocation: map[string]string{
|
|
|
|
"dritftctl-test-no-notifications": "eu-west-3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "multiple bucket with notifications", dirName: "s3_bucket_notifications_multiple",
|
|
|
|
bucketsIDs: []string{
|
|
|
|
"bucket-martin-test-drift",
|
|
|
|
"bucket-martin-test-drift2",
|
|
|
|
"bucket-martin-test-drift3",
|
|
|
|
},
|
|
|
|
bucketLocation: map[string]string{
|
|
|
|
"bucket-martin-test-drift": "eu-west-1",
|
|
|
|
"bucket-martin-test-drift2": "eu-west-3",
|
|
|
|
"bucket-martin-test-drift3": "ap-northeast-1",
|
|
|
|
},
|
2021-01-20 13:01:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "Cannot list bucket", dirName: "s3_bucket_notifications_list_bucket",
|
|
|
|
listError: awserr.NewRequestFailure(nil, 403, ""),
|
|
|
|
bucketLocation: map[string]string{
|
|
|
|
"bucket-martin-test-drift": "eu-west-1",
|
|
|
|
"bucket-martin-test-drift2": "eu-west-3",
|
|
|
|
"bucket-martin-test-drift3": "ap-northeast-1",
|
|
|
|
},
|
|
|
|
wantErr: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsS3BucketNotificationResourceType, resourceaws.AwsS3BucketResourceType),
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
factory := AwsClientFactory{config: provider.session}
|
|
|
|
|
|
|
|
terraform.AddProvider(terraform.AWS, provider)
|
|
|
|
resource.AddSupplier(NewS3BucketNotificationSupplier(provider.Runner().SubRunner(), factory))
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(tt.test, func(t *testing.T) {
|
|
|
|
|
2021-01-20 13:01:57 +00:00
|
|
|
mock := mocks.NewMockAWSS3Client(tt.bucketsIDs, nil, nil, nil, tt.bucketLocation, tt.listError)
|
2020-12-09 15:31:34 +00:00
|
|
|
factory := mocks.NewMockAwsClientFactory(mock)
|
|
|
|
|
|
|
|
provider := mocks.NewMockedGoldenTFProvider(tt.dirName, terraform.Provider(terraform.AWS), shouldUpdate)
|
|
|
|
deserializer := awsdeserializer.NewS3BucketNotificationDeserializer()
|
|
|
|
s := &S3BucketNotificationSupplier{
|
|
|
|
provider,
|
|
|
|
deserializer,
|
|
|
|
factory,
|
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, err, tt.wantErr)
|
2020-12-09 15:31:34 +00:00
|
|
|
test.CtyTestDiff(got, tt.dirName, provider, deserializer, shouldUpdate, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|