139 lines
4.2 KiB
Go
139 lines
4.2 KiB
Go
|
package remote
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
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/s3"
|
||
|
"github.com/cloudskiff/driftctl/mocks"
|
||
|
"github.com/cloudskiff/driftctl/pkg/remote/aws"
|
||
|
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||
|
"github.com/cloudskiff/driftctl/pkg/remote/common"
|
||
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
||
|
tf "github.com/cloudskiff/driftctl/pkg/remote/terraform"
|
||
|
testresource "github.com/cloudskiff/driftctl/test/resource"
|
||
|
terraform2 "github.com/cloudskiff/driftctl/test/terraform"
|
||
|
|
||
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/client"
|
||
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
||
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
||
|
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
||
|
|
||
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
||
|
"github.com/cloudskiff/driftctl/test"
|
||
|
"github.com/cloudskiff/driftctl/test/goldenfile"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestS3Bucket(t *testing.T) {
|
||
|
|
||
|
tests := []struct {
|
||
|
test string
|
||
|
dirName string
|
||
|
mocks func(repository *repository.MockS3Repository)
|
||
|
wantErr error
|
||
|
}{
|
||
|
{
|
||
|
test: "multiple bucket", dirName: "aws_s3_bucket_multiple",
|
||
|
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",
|
||
|
"bucket-martin-test-drift",
|
||
|
).Return(
|
||
|
"eu-west-1",
|
||
|
nil,
|
||
|
)
|
||
|
|
||
|
repository.On(
|
||
|
"GetBucketLocation",
|
||
|
"bucket-martin-test-drift2",
|
||
|
).Return(
|
||
|
"eu-west-3",
|
||
|
nil,
|
||
|
)
|
||
|
|
||
|
repository.On(
|
||
|
"GetBucketLocation",
|
||
|
"bucket-martin-test-drift3",
|
||
|
).Return(
|
||
|
"ap-northeast-1",
|
||
|
nil,
|
||
|
)
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
test: "cannot list bucket", dirName: "s3_bucket_list",
|
||
|
mocks: func(repository *repository.MockS3Repository) {
|
||
|
repository.On("ListAllBuckets").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
||
|
},
|
||
|
wantErr: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsS3BucketResourceType),
|
||
|
},
|
||
|
}
|
||
|
|
||
|
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
|
||
|
resourceaws.InitResourcesMetadata(schemaRepository)
|
||
|
factory := terraform.NewTerraformResourceFactory(schemaRepository)
|
||
|
deserializer := resource.NewDeserializer(factory)
|
||
|
alerter := &mocks.AlerterInterface{}
|
||
|
|
||
|
for _, c := range tests {
|
||
|
t.Run(c.test, func(tt *testing.T) {
|
||
|
shouldUpdate := c.dirName == *goldenfile.Update
|
||
|
|
||
|
session := session.Must(session.NewSessionWithOptions(session.Options{
|
||
|
SharedConfigState: session.SharedConfigEnable,
|
||
|
}))
|
||
|
|
||
|
scanOptions := ScannerOptions{Deep: true}
|
||
|
providerLibrary := terraform.NewProviderLibrary()
|
||
|
remoteLibrary := common.NewRemoteLibrary()
|
||
|
|
||
|
// Initialize mocks
|
||
|
fakeRepo := &repository.MockS3Repository{}
|
||
|
c.mocks(fakeRepo)
|
||
|
var repo repository.S3Repository = fakeRepo
|
||
|
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.NewS3Repository(client.NewAWSClientFactory(session), cache.New(0))
|
||
|
}
|
||
|
|
||
|
remoteLibrary.AddEnumerator(aws.NewS3BucketEnumerator(repo, factory, tf.TerraformProviderConfig{
|
||
|
Name: "test",
|
||
|
DefaultAlias: "eu-west-3",
|
||
|
}))
|
||
|
remoteLibrary.AddDetailsFetcher(resourceaws.AwsS3BucketResourceType, aws.NewS3BucketDetailsFetcher(provider, deserializer))
|
||
|
|
||
|
s := NewScanner(nil, remoteLibrary, alerter, scanOptions)
|
||
|
got, err := s.Resources()
|
||
|
assert.Equal(tt, err, c.wantErr)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
test.TestAgainstGoldenFile(got, resourceaws.AwsS3BucketResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
||
|
})
|
||
|
}
|
||
|
}
|