driftctl/pkg/remote/aws/route53_health_check_suppli...

94 lines
3.0 KiB
Go
Raw Normal View History

2021-02-15 20:00:16 +00:00
package aws
import (
"context"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
2021-02-15 20:00:16 +00:00
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/goldenfile"
testmocks "github.com/cloudskiff/driftctl/test/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/cloudskiff/driftctl/pkg/resource"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
awsdeserializer "github.com/cloudskiff/driftctl/pkg/resource/aws/deserializer"
"github.com/cloudskiff/driftctl/pkg/terraform"
)
func TestRoute53HealthCheckSupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *repository.MockRoute53Repository)
2021-02-15 20:00:16 +00:00
err error
}{
{
test: "no health check",
dirName: "route53_health_check_empty",
mocks: func(client *repository.MockRoute53Repository) {
2021-02-15 20:00:16 +00:00
client.On("ListAllHealthChecks").Return([]*route53.HealthCheck{}, nil)
},
err: nil,
},
{
test: "Multiple health check",
dirName: "route53_health_check_multiple",
mocks: func(client *repository.MockRoute53Repository) {
2021-02-15 20:00:16 +00:00
client.On("ListAllHealthChecks").Return([]*route53.HealthCheck{
{Id: aws.String("7001a9df-ded4-4802-9909-668eb80b972b")},
{Id: aws.String("84fc318a-2e0d-41d6-b638-280e2f0f4e26")},
}, nil)
},
err: nil,
},
{
test: "cannot list health check",
dirName: "route53_health_check_empty",
mocks: func(client *repository.MockRoute53Repository) {
2021-02-15 20:00:16 +00:00
client.On("ListAllHealthChecks").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsRoute53HealthCheckResourceType),
},
}
for _, c := range cases {
shouldUpdate := c.dirName == *goldenfile.Update
providerLibrary := terraform.NewProviderLibrary()
supplierLibrary := resource.NewSupplierLibrary()
if shouldUpdate {
provider, err := InitTestAwsProvider(providerLibrary)
if err != nil {
t.Fatal(err)
}
supplierLibrary.AddSupplier(NewRoute53HealthCheckSupplier(provider))
}
t.Run(c.test, func(tt *testing.T) {
fakeClient := repository.MockRoute53Repository{}
2021-02-15 20:00:16 +00:00
c.mocks(&fakeClient)
provider := testmocks.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
deserializer := awsdeserializer.NewRoute53HealthCheckDeserializer()
s := &Route53HealthCheckSupplier{
provider,
deserializer,
&fakeClient,
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
}
got, err := s.Resources()
assert.Equal(tt, c.err, err)
mock.AssertExpectationsForObjects(tt)
test.CtyTestDiff(got, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}