2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-24 15:26:12 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
2021-06-04 14:16:35 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
2021-05-21 14:09:45 +00:00
|
|
|
testresource "github.com/cloudskiff/driftctl/test/resource"
|
2021-02-24 15:26:12 +00:00
|
|
|
|
2021-01-20 13:01:57 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
|
|
|
|
|
|
|
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
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/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"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/lambda"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLambdaFunctionSupplier_Resources(t *testing.T) {
|
|
|
|
tests := []struct {
|
2021-02-24 15:26:12 +00:00
|
|
|
test string
|
|
|
|
dirName string
|
|
|
|
mocks func(repo *repository.MockLambdaRepository)
|
|
|
|
err error
|
2020-12-09 15:31:34 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "no lambda functions",
|
|
|
|
dirName: "lambda_function_empty",
|
2021-02-24 15:26:12 +00:00
|
|
|
mocks: func(repo *repository.MockLambdaRepository) {
|
|
|
|
repo.On("ListAllLambdaFunctions").Return([]*lambda.FunctionConfiguration{}, nil)
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "with lambda functions",
|
|
|
|
dirName: "lambda_function_multiple",
|
2021-02-24 15:26:12 +00:00
|
|
|
mocks: func(repo *repository.MockLambdaRepository) {
|
|
|
|
repo.On("ListAllLambdaFunctions").Return([]*lambda.FunctionConfiguration{
|
|
|
|
{
|
|
|
|
FunctionName: aws.String("foo"),
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-02-24 15:26:12 +00:00
|
|
|
{
|
|
|
|
FunctionName: aws.String("bar"),
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-02-24 15:26:12 +00:00
|
|
|
}, nil)
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "One lambda with signing",
|
|
|
|
dirName: "lambda_function_signed",
|
2021-02-24 15:26:12 +00:00
|
|
|
mocks: func(repo *repository.MockLambdaRepository) {
|
|
|
|
repo.On("ListAllLambdaFunctions").Return([]*lambda.FunctionConfiguration{
|
|
|
|
{
|
|
|
|
FunctionName: aws.String("foo"),
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-02-24 15:26:12 +00:00
|
|
|
}, nil)
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
2021-01-20 13:01:57 +00:00
|
|
|
{
|
2021-02-24 15:26:12 +00:00
|
|
|
test: "cannot list lambda functions",
|
|
|
|
dirName: "lambda_function_empty",
|
|
|
|
mocks: func(repo *repository.MockLambdaRepository) {
|
|
|
|
repo.On("ListAllLambdaFunctions").Return([]*lambda.FunctionConfiguration{}, awserr.NewRequestFailure(nil, 403, ""))
|
|
|
|
},
|
|
|
|
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsLambdaFunctionResourceType),
|
2021-01-20 13:01:57 +00:00
|
|
|
},
|
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 14:16:35 +00:00
|
|
|
supplierLibrary.AddSupplier(NewLambdaFunctionSupplier(provider, deserializer, repository.NewLambdaRepository(provider.session, cache.New(0))))
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(tt.test, func(t *testing.T) {
|
2021-01-22 17:06:17 +00:00
|
|
|
provider := mocks.NewMockedGoldenTFProvider(tt.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
|
2021-02-24 15:26:12 +00:00
|
|
|
client := &repository.MockLambdaRepository{}
|
|
|
|
tt.mocks(client)
|
2020-12-09 15:31:34 +00:00
|
|
|
s := &LambdaFunctionSupplier{
|
|
|
|
provider,
|
|
|
|
deserializer,
|
2021-01-20 13:01:57 +00:00
|
|
|
client,
|
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(t, tt.err, err)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
test.CtyTestDiff(got, tt.dirName, provider, deserializer, shouldUpdate, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|