2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
awssdk "github.com/aws/aws-sdk-go/aws"
|
2021-01-20 13:01:57 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2021-02-16 17:42:46 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
2021-01-15 11:44:13 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/parallel"
|
2021-02-16 17:42:46 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/client"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
2021-06-04 10:22:17 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
2021-02-16 17:42:46 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
2021-03-18 11:12:34 +00:00
|
|
|
tf "github.com/cloudskiff/driftctl/pkg/remote/terraform"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
2021-02-16 17:42:46 +00:00
|
|
|
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-05-21 14:09:45 +00:00
|
|
|
testresource "github.com/cloudskiff/driftctl/test/resource"
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
"github.com/cloudskiff/driftctl/test"
|
2021-02-16 17:42:46 +00:00
|
|
|
"github.com/cloudskiff/driftctl/test/goldenfile"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/test/mocks"
|
2021-02-16 17:42:46 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-09 15:31:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestS3BucketNotificationSupplier_Resources(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-02-16 17:42:46 +00:00
|
|
|
test string
|
|
|
|
dirName string
|
|
|
|
mocks func(repository *repository.MockS3Repository)
|
|
|
|
wantErr error
|
2020-12-09 15:31:34 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "single bucket without notifications",
|
|
|
|
dirName: "s3_bucket_notifications_no_notif",
|
2021-02-16 17:42:46 +00:00
|
|
|
mocks: func(repository *repository.MockS3Repository) {
|
|
|
|
repository.On(
|
|
|
|
"ListAllBuckets",
|
|
|
|
).Return([]*s3.Bucket{
|
|
|
|
{Name: awssdk.String("dritftctl-test-no-notifications")},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
repository.On(
|
|
|
|
"GetBucketLocation",
|
|
|
|
&s3.Bucket{Name: awssdk.String("dritftctl-test-no-notifications")},
|
|
|
|
).Return(
|
|
|
|
"eu-west-3",
|
|
|
|
nil,
|
|
|
|
)
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "multiple bucket with notifications", dirName: "s3_bucket_notifications_multiple",
|
2021-02-16 17:42:46 +00:00
|
|
|
mocks: func(repository *repository.MockS3Repository) {
|
|
|
|
repository.On(
|
|
|
|
"ListAllBuckets",
|
|
|
|
).Return([]*s3.Bucket{
|
|
|
|
{Name: awssdk.String("bucket-martin-test-drift")},
|
|
|
|
{Name: awssdk.String("bucket-martin-test-drift2")},
|
|
|
|
{Name: awssdk.String("bucket-martin-test-drift3")},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
repository.On(
|
|
|
|
"GetBucketLocation",
|
|
|
|
&s3.Bucket{Name: awssdk.String("bucket-martin-test-drift")},
|
|
|
|
).Return(
|
|
|
|
"eu-west-1",
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
repository.On(
|
|
|
|
"GetBucketLocation",
|
|
|
|
&s3.Bucket{Name: awssdk.String("bucket-martin-test-drift2")},
|
|
|
|
).Return(
|
|
|
|
"eu-west-3",
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
repository.On(
|
|
|
|
"GetBucketLocation",
|
|
|
|
&s3.Bucket{Name: awssdk.String("bucket-martin-test-drift3")},
|
|
|
|
).Return(
|
|
|
|
"ap-northeast-1",
|
|
|
|
nil,
|
|
|
|
)
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-01-20 13:01:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "Cannot list bucket", dirName: "s3_bucket_notifications_list_bucket",
|
2021-02-16 17:42:46 +00:00
|
|
|
mocks: func(repository *repository.MockS3Repository) {
|
|
|
|
repository.On("ListAllBuckets").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
2021-01-20 13:01:57 +00:00
|
|
|
},
|
|
|
|
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
|
2021-01-22 17:06:17 +00:00
|
|
|
|
|
|
|
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)
|
2020-12-09 15:31:34 +00:00
|
|
|
if shouldUpdate {
|
2021-02-09 13:56:11 +00:00
|
|
|
provider, err := InitTestAwsProvider(providerLibrary)
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-06-04 10:22:17 +00:00
|
|
|
repository := repository.NewS3Repository(client.NewAWSClientFactory(provider.session), cache.New(0))
|
2021-05-21 14:09:45 +00:00
|
|
|
supplierLibrary.AddSupplier(NewS3BucketNotificationSupplier(provider, repository, deserializer))
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(tt.test, func(t *testing.T) {
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
mock := repository.MockS3Repository{}
|
|
|
|
tt.mocks(&mock)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2021-01-22 17:06:17 +00:00
|
|
|
provider := mocks.NewMockedGoldenTFProvider(tt.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
|
2020-12-09 15:31:34 +00:00
|
|
|
s := &S3BucketNotificationSupplier{
|
|
|
|
provider,
|
|
|
|
deserializer,
|
2021-02-16 17:42:46 +00:00
|
|
|
&mock,
|
2021-01-15 11:44:13 +00:00
|
|
|
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
|
2021-03-18 11:12:34 +00:00
|
|
|
tf.TerraformProviderConfig{
|
|
|
|
Name: "test",
|
|
|
|
DefaultAlias: "eu-west-3",
|
|
|
|
},
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|