2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-19 15:09:11 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
2021-01-20 13:01:57 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
2021-05-21 14:09:45 +00:00
|
|
|
testresource "github.com/cloudskiff/driftctl/test/resource"
|
2021-01-20 13:01:57 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
|
|
|
|
2021-01-20 13:01:57 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
2021-06-07 13:02:12 +00:00
|
|
|
"github.com/cloudskiff/driftctl/test/mocks"
|
2021-05-19 15:09:11 +00:00
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
"github.com/cloudskiff/driftctl/test"
|
|
|
|
"github.com/cloudskiff/driftctl/test/goldenfile"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIamPolicySupplier_Resources(t *testing.T) {
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
test string
|
|
|
|
dirName string
|
2021-05-19 15:09:11 +00:00
|
|
|
mocks func(repo *repository.MockIAMRepository)
|
2020-12-09 15:31:34 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "no iam custom policies",
|
|
|
|
dirName: "iam_policy_empty",
|
2021-05-19 15:09:11 +00:00
|
|
|
mocks: func(repo *repository.MockIAMRepository) {
|
|
|
|
repo.On("ListAllPolicies").Once().Return([]*iam.Policy{}, nil)
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "iam multiples custom policies",
|
|
|
|
dirName: "iam_policy_multiple",
|
2021-05-19 15:09:11 +00:00
|
|
|
mocks: func(repo *repository.MockIAMRepository) {
|
|
|
|
repo.On("ListAllPolicies").Once().Return([]*iam.Policy{
|
|
|
|
{
|
|
|
|
Arn: aws.String("arn:aws:iam::929327065333:policy/policy-0"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Arn: aws.String("arn:aws:iam::929327065333:policy/policy-1"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Arn: aws.String("arn:aws:iam::929327065333:policy/policy-2"),
|
|
|
|
},
|
|
|
|
}, nil)
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
2021-01-20 13:01:57 +00:00
|
|
|
{
|
|
|
|
test: "cannot list iam custom policies",
|
|
|
|
dirName: "iam_policy_empty",
|
2021-05-19 15:09:11 +00:00
|
|
|
mocks: func(repo *repository.MockIAMRepository) {
|
|
|
|
repo.On("ListAllPolicies").Once().Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
2021-01-20 13:01:57 +00:00
|
|
|
},
|
|
|
|
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsIamPolicyResourceType),
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
shouldUpdate := c.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-07 14:59:54 +00:00
|
|
|
supplierLibrary.AddSupplier(NewIamPolicySupplier(provider, deserializer, repository.NewIAMRepository(provider.session)))
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(c.test, func(tt *testing.T) {
|
2021-05-19 15:09:11 +00:00
|
|
|
fakeIam := repository.MockIAMRepository{}
|
2020-12-09 15:31:34 +00:00
|
|
|
c.mocks(&fakeIam)
|
|
|
|
|
2021-06-07 13:02:12 +00:00
|
|
|
provider := mocks.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
|
2020-12-09 15:31:34 +00:00
|
|
|
s := &IamPolicySupplier{
|
|
|
|
provider,
|
|
|
|
deserializer,
|
|
|
|
&fakeIam,
|
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(tt, c.err, err)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
mock.AssertExpectationsForObjects(tt)
|
|
|
|
test.CtyTestDiff(got, c.dirName, provider, deserializer, shouldUpdate, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|