93 lines
2.5 KiB
Go
93 lines
2.5 KiB
Go
package aws
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/parallel"
|
|
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 TestS3BucketMetricSupplier_Resources(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
test string
|
|
dirName string
|
|
bucketsIDs []string
|
|
bucketLocation map[string]string
|
|
metricsIDs map[string][]string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
test: "multiple bucket with multiple metrics", dirName: "s3_bucket_metrics_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",
|
|
},
|
|
metricsIDs: map[string][]string{
|
|
"bucket-martin-test-drift": {
|
|
"Metrics_Bucket1",
|
|
"Metrics2_Bucket1",
|
|
},
|
|
"bucket-martin-test-drift2": {
|
|
"Metrics_Bucket2",
|
|
"Metrics2_Bucket2",
|
|
},
|
|
"bucket-martin-test-drift3": {
|
|
"Metrics_Bucket3",
|
|
"Metrics2_Bucket3",
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
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(NewS3BucketMetricSupplier(provider.Runner().SubRunner(), factory))
|
|
}
|
|
|
|
t.Run(tt.test, func(t *testing.T) {
|
|
|
|
mock := mocks.NewMockAWSS3Client(tt.bucketsIDs, nil, nil, tt.metricsIDs, tt.bucketLocation)
|
|
factory := mocks.NewMockAwsClientFactory(mock)
|
|
|
|
provider := mocks.NewMockedGoldenTFProvider(tt.dirName, terraform.Provider(terraform.AWS), shouldUpdate)
|
|
deserializer := awsdeserializer.NewS3BucketMetricDeserializer()
|
|
s := &S3BucketMetricSupplier{
|
|
provider,
|
|
deserializer,
|
|
factory,
|
|
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
|
|
}
|
|
got, err := s.Resources()
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("Resources() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
test.CtyTestDiff(got, tt.dirName, provider, deserializer, shouldUpdate, t)
|
|
})
|
|
}
|
|
}
|