2021-09-14 14:48:29 +00:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration"
|
|
|
|
aws2 "github.com/snyk/driftctl/enumeration/remote/aws"
|
2022-07-06 08:39:33 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/remote/cache"
|
|
|
|
common2 "github.com/snyk/driftctl/enumeration/remote/common"
|
|
|
|
remoteerror "github.com/snyk/driftctl/enumeration/remote/error"
|
|
|
|
terraform3 "github.com/snyk/driftctl/enumeration/terraform"
|
|
|
|
|
2021-09-14 14:48:29 +00:00
|
|
|
awssdk "github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/applicationautoscaling"
|
|
|
|
"github.com/pkg/errors"
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/resource"
|
|
|
|
resourceaws "github.com/snyk/driftctl/enumeration/resource/aws"
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/mocks"
|
2022-06-28 07:23:29 +00:00
|
|
|
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/test"
|
|
|
|
"github.com/snyk/driftctl/test/goldenfile"
|
|
|
|
testresource "github.com/snyk/driftctl/test/resource"
|
|
|
|
terraform2 "github.com/snyk/driftctl/test/terraform"
|
2021-09-14 14:48:29 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAppAutoScalingTarget(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
|
|
|
dirName string
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks func(*repository.MockAppAutoScalingRepository, *mocks.AlerterInterface)
|
2021-09-14 14:48:29 +00:00
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "should return one target",
|
|
|
|
dirName: "aws_appautoscaling_target_single",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(client *repository.MockAppAutoScalingRepository, alerter *mocks.AlerterInterface) {
|
2021-09-14 14:48:29 +00:00
|
|
|
client.On("ServiceNamespaceValues").Return(applicationautoscaling.ServiceNamespace_Values()).Once()
|
|
|
|
|
|
|
|
client.On("DescribeScalableTargets", "dynamodb").Return([]*applicationautoscaling.ScalableTarget{
|
|
|
|
{
|
|
|
|
ResourceId: awssdk.String("table/GameScores"),
|
|
|
|
RoleARN: awssdk.String("arn:aws:iam::533948124879:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable"),
|
|
|
|
ScalableDimension: awssdk.String("dynamodb:table:ReadCapacityUnits"),
|
|
|
|
ServiceNamespace: awssdk.String("dynamodb"),
|
|
|
|
MaxCapacity: awssdk.Int64(100),
|
|
|
|
MinCapacity: awssdk.Int64(5),
|
|
|
|
},
|
|
|
|
}, nil).Once()
|
|
|
|
|
|
|
|
client.On("DescribeScalableTargets", mock.AnythingOfType("string")).Return([]*applicationautoscaling.ScalableTarget{}, nil).Times(len(applicationautoscaling.ServiceNamespace_Values()) - 1)
|
|
|
|
},
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "should return remote error",
|
|
|
|
dirName: "aws_appautoscaling_target_single",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(client *repository.MockAppAutoScalingRepository, alerter *mocks.AlerterInterface) {
|
2021-09-14 14:48:29 +00:00
|
|
|
client.On("ServiceNamespaceValues").Return(applicationautoscaling.ServiceNamespace_Values()).Once()
|
|
|
|
|
|
|
|
client.On("DescribeScalableTargets", mock.AnythingOfType("string")).Return(nil, errors.New("remote error")).Once()
|
|
|
|
},
|
|
|
|
wantErr: remoteerror.NewResourceListingError(errors.New("remote error"), resourceaws.AwsAppAutoscalingTargetResourceType),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
|
|
|
|
resourceaws.InitResourcesMetadata(schemaRepository)
|
2022-06-28 07:23:29 +00:00
|
|
|
factory := terraform3.NewTerraformResourceFactory(schemaRepository)
|
2021-09-14 14:48:29 +00:00
|
|
|
deserializer := resource.NewDeserializer(factory)
|
|
|
|
|
|
|
|
for _, c := range tests {
|
|
|
|
t.Run(c.test, func(tt *testing.T) {
|
|
|
|
shouldUpdate := c.dirName == *goldenfile.Update
|
|
|
|
|
|
|
|
sess := session.Must(session.NewSessionWithOptions(session.Options{
|
|
|
|
SharedConfigState: session.SharedConfigEnable,
|
|
|
|
}))
|
|
|
|
|
|
|
|
scanOptions := ScannerOptions{Deep: true}
|
2022-06-28 07:23:29 +00:00
|
|
|
providerLibrary := terraform3.NewProviderLibrary()
|
|
|
|
remoteLibrary := common2.NewRemoteLibrary()
|
2021-09-14 14:48:29 +00:00
|
|
|
|
|
|
|
// Initialize mocks
|
|
|
|
alerter := &mocks.AlerterInterface{}
|
2022-07-06 08:39:33 +00:00
|
|
|
fakeRepo := &repository.MockAppAutoScalingRepository{}
|
2021-09-14 14:48:29 +00:00
|
|
|
c.mocks(fakeRepo, alerter)
|
|
|
|
|
2022-07-06 08:39:33 +00:00
|
|
|
var repo repository.AppAutoScalingRepository = fakeRepo
|
2021-09-14 14:48:29 +00:00
|
|
|
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()
|
2022-07-06 08:39:33 +00:00
|
|
|
repo = repository.NewAppAutoScalingRepository(sess, cache.New(0))
|
2021-09-14 14:48:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
remoteLibrary.AddEnumerator(aws2.NewAppAutoscalingTargetEnumerator(repo, factory))
|
|
|
|
remoteLibrary.AddDetailsFetcher(resourceaws.AwsAppAutoscalingTargetResourceType, common2.NewGenericDetailsFetcher(resourceaws.AwsAppAutoscalingTargetResourceType, provider, deserializer))
|
2021-09-14 14:48:29 +00:00
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
testFilter := &enumeration.MockFilter{}
|
2021-09-14 14:48:29 +00:00
|
|
|
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
|
|
|
|
|
|
|
|
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
|
|
|
|
got, err := s.Resources()
|
|
|
|
if err != nil {
|
|
|
|
assert.EqualError(tt, c.wantErr, err.Error())
|
|
|
|
} else {
|
|
|
|
assert.Equal(tt, err, c.wantErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
test.TestAgainstGoldenFile(got, resourceaws.AwsAppAutoscalingTargetResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
|
|
|
alerter.AssertExpectations(tt)
|
|
|
|
fakeRepo.AssertExpectations(tt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 09:03:20 +00:00
|
|
|
|
|
|
|
func TestAppAutoScalingPolicy(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
test string
|
|
|
|
dirName string
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks func(*repository.MockAppAutoScalingRepository, *mocks.AlerterInterface)
|
2021-09-24 09:03:20 +00:00
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "should return one policy",
|
|
|
|
dirName: "aws_appautoscaling_policy_single",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(client *repository.MockAppAutoScalingRepository, alerter *mocks.AlerterInterface) {
|
2021-09-24 09:03:20 +00:00
|
|
|
client.On("ServiceNamespaceValues").Return(applicationautoscaling.ServiceNamespace_Values()).Once()
|
|
|
|
|
|
|
|
client.On("DescribeScalingPolicies", "dynamodb").Return([]*applicationautoscaling.ScalingPolicy{
|
|
|
|
{
|
|
|
|
PolicyName: awssdk.String("DynamoDBReadCapacityUtilization:table/GameScores"),
|
|
|
|
ResourceId: awssdk.String("table/GameScores"),
|
|
|
|
ScalableDimension: awssdk.String("dynamodb:table:ReadCapacityUnits"),
|
|
|
|
ServiceNamespace: awssdk.String("dynamodb"),
|
|
|
|
},
|
|
|
|
}, nil).Once()
|
|
|
|
|
|
|
|
client.On("DescribeScalingPolicies", mock.AnythingOfType("string")).Return([]*applicationautoscaling.ScalingPolicy{}, nil).Times(len(applicationautoscaling.ServiceNamespace_Values()) - 1)
|
|
|
|
},
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "should return remote error",
|
|
|
|
dirName: "aws_appautoscaling_policy_single",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(client *repository.MockAppAutoScalingRepository, alerter *mocks.AlerterInterface) {
|
2021-09-24 09:03:20 +00:00
|
|
|
client.On("ServiceNamespaceValues").Return(applicationautoscaling.ServiceNamespace_Values()).Once()
|
|
|
|
|
|
|
|
client.On("DescribeScalingPolicies", mock.AnythingOfType("string")).Return(nil, errors.New("remote error")).Once()
|
|
|
|
},
|
|
|
|
wantErr: remoteerror.NewResourceListingError(errors.New("remote error"), resourceaws.AwsAppAutoscalingPolicyResourceType),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
|
|
|
|
resourceaws.InitResourcesMetadata(schemaRepository)
|
2022-06-28 07:23:29 +00:00
|
|
|
factory := terraform3.NewTerraformResourceFactory(schemaRepository)
|
2021-09-24 09:03:20 +00:00
|
|
|
deserializer := resource.NewDeserializer(factory)
|
|
|
|
|
|
|
|
for _, c := range tests {
|
|
|
|
t.Run(c.test, func(tt *testing.T) {
|
|
|
|
shouldUpdate := c.dirName == *goldenfile.Update
|
|
|
|
|
|
|
|
sess := session.Must(session.NewSessionWithOptions(session.Options{
|
|
|
|
SharedConfigState: session.SharedConfigEnable,
|
|
|
|
}))
|
|
|
|
|
|
|
|
scanOptions := ScannerOptions{Deep: true}
|
2022-06-28 07:23:29 +00:00
|
|
|
providerLibrary := terraform3.NewProviderLibrary()
|
|
|
|
remoteLibrary := common2.NewRemoteLibrary()
|
2021-09-24 09:03:20 +00:00
|
|
|
|
|
|
|
// Initialize mocks
|
|
|
|
alerter := &mocks.AlerterInterface{}
|
2022-07-06 08:39:33 +00:00
|
|
|
fakeRepo := &repository.MockAppAutoScalingRepository{}
|
2021-09-24 09:03:20 +00:00
|
|
|
c.mocks(fakeRepo, alerter)
|
|
|
|
|
2022-07-06 08:39:33 +00:00
|
|
|
var repo repository.AppAutoScalingRepository = fakeRepo
|
2021-09-24 09:03:20 +00:00
|
|
|
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()
|
2022-07-06 08:39:33 +00:00
|
|
|
repo = repository.NewAppAutoScalingRepository(sess, cache.New(0))
|
2021-09-24 09:03:20 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
remoteLibrary.AddEnumerator(aws2.NewAppAutoscalingPolicyEnumerator(repo, factory))
|
|
|
|
remoteLibrary.AddDetailsFetcher(resourceaws.AwsAppAutoscalingPolicyResourceType, common2.NewGenericDetailsFetcher(resourceaws.AwsAppAutoscalingPolicyResourceType, provider, deserializer))
|
2021-09-24 09:03:20 +00:00
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
testFilter := &enumeration.MockFilter{}
|
2021-09-24 09:03:20 +00:00
|
|
|
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
|
|
|
|
|
|
|
|
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
|
|
|
|
got, err := s.Resources()
|
|
|
|
if err != nil {
|
|
|
|
assert.EqualError(tt, c.wantErr, err.Error())
|
|
|
|
} else {
|
|
|
|
assert.Equal(tt, err, c.wantErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
test.TestAgainstGoldenFile(got, resourceaws.AwsAppAutoscalingPolicyResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
|
|
|
alerter.AssertExpectations(tt)
|
|
|
|
fakeRepo.AssertExpectations(tt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 17:47:04 +00:00
|
|
|
|
|
|
|
func TestAppAutoScalingScheduledAction(t *testing.T) {
|
2021-10-04 09:38:22 +00:00
|
|
|
dummyError := errors.New("this is an error")
|
|
|
|
|
2021-09-22 17:47:04 +00:00
|
|
|
tests := []struct {
|
2021-10-04 09:38:22 +00:00
|
|
|
test string
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks func(*repository.MockAppAutoScalingRepository, *mocks.AlerterInterface)
|
2021-10-04 09:38:22 +00:00
|
|
|
assertExpected func(t *testing.T, got []*resource.Resource)
|
|
|
|
wantErr error
|
2021-09-22 17:47:04 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-10-04 09:38:22 +00:00
|
|
|
test: "should return one scheduled action",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(client *repository.MockAppAutoScalingRepository, alerter *mocks.AlerterInterface) {
|
2021-09-22 17:47:04 +00:00
|
|
|
matchServiceNamespaceFunc := func(ns string) bool {
|
|
|
|
for _, n := range applicationautoscaling.ServiceNamespace_Values() {
|
|
|
|
if n == ns {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
client.On("ServiceNamespaceValues").Return(applicationautoscaling.ServiceNamespace_Values()).Once()
|
|
|
|
|
|
|
|
client.On("DescribeScheduledActions", mock.MatchedBy(matchServiceNamespaceFunc)).Return([]*applicationautoscaling.ScheduledAction{
|
|
|
|
{
|
|
|
|
ScheduledActionName: awssdk.String("action"),
|
|
|
|
ResourceId: awssdk.String("table/GameScores"),
|
|
|
|
ScalableDimension: awssdk.String("dynamodb:table:ReadCapacityUnits"),
|
|
|
|
ServiceNamespace: awssdk.String("dynamodb"),
|
|
|
|
},
|
|
|
|
}, nil).Once()
|
|
|
|
|
|
|
|
client.On("DescribeScheduledActions", mock.MatchedBy(matchServiceNamespaceFunc)).Return([]*applicationautoscaling.ScheduledAction{}, nil).Times(len(applicationautoscaling.ServiceNamespace_Values()) - 1)
|
|
|
|
},
|
2021-10-04 09:38:22 +00:00
|
|
|
assertExpected: func(t *testing.T, got []*resource.Resource) {
|
|
|
|
assert.Len(t, got, 1)
|
|
|
|
assert.Equal(t, "action-dynamodb-table/GameScores", got[0].ResourceId())
|
|
|
|
assert.Equal(t, resourceaws.AwsAppAutoscalingScheduledActionResourceType, got[0].ResourceType())
|
|
|
|
},
|
2021-09-22 17:47:04 +00:00
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
2021-10-04 09:38:22 +00:00
|
|
|
test: "should return remote error",
|
2022-07-06 08:39:33 +00:00
|
|
|
mocks: func(client *repository.MockAppAutoScalingRepository, alerter *mocks.AlerterInterface) {
|
2021-09-22 17:47:04 +00:00
|
|
|
client.On("ServiceNamespaceValues").Return(applicationautoscaling.ServiceNamespace_Values()).Once()
|
|
|
|
|
2021-10-04 09:38:22 +00:00
|
|
|
client.On("DescribeScheduledActions", mock.AnythingOfType("string")).Return(nil, dummyError).Once()
|
|
|
|
},
|
|
|
|
assertExpected: func(t *testing.T, got []*resource.Resource) {
|
|
|
|
assert.Len(t, got, 0)
|
2021-09-22 17:47:04 +00:00
|
|
|
},
|
2021-10-04 09:38:22 +00:00
|
|
|
wantErr: remoteerror.NewResourceListingError(dummyError, resourceaws.AwsAppAutoscalingScheduledActionResourceType),
|
2021-09-22 17:47:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-04 09:38:22 +00:00
|
|
|
providerVersion := "3.19.0"
|
|
|
|
schemaRepository := testresource.InitFakeSchemaRepository("aws", providerVersion)
|
2021-09-22 17:47:04 +00:00
|
|
|
resourceaws.InitResourcesMetadata(schemaRepository)
|
2022-06-28 07:23:29 +00:00
|
|
|
factory := terraform3.NewTerraformResourceFactory(schemaRepository)
|
2021-09-22 17:47:04 +00:00
|
|
|
|
|
|
|
for _, c := range tests {
|
|
|
|
t.Run(c.test, func(tt *testing.T) {
|
2021-10-04 09:38:22 +00:00
|
|
|
scanOptions := ScannerOptions{}
|
2022-06-28 07:23:29 +00:00
|
|
|
remoteLibrary := common2.NewRemoteLibrary()
|
2021-09-22 17:47:04 +00:00
|
|
|
|
|
|
|
// Initialize mocks
|
|
|
|
alerter := &mocks.AlerterInterface{}
|
2022-07-06 08:39:33 +00:00
|
|
|
fakeRepo := &repository.MockAppAutoScalingRepository{}
|
2021-09-22 17:47:04 +00:00
|
|
|
c.mocks(fakeRepo, alerter)
|
|
|
|
|
2022-07-06 08:39:33 +00:00
|
|
|
var repo repository.AppAutoScalingRepository = fakeRepo
|
2021-09-22 17:47:04 +00:00
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
remoteLibrary.AddEnumerator(aws2.NewAppAutoscalingScheduledActionEnumerator(repo, factory))
|
2021-09-22 17:47:04 +00:00
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
testFilter := &enumeration.MockFilter{}
|
2021-09-22 17:47:04 +00:00
|
|
|
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
|
|
|
|
|
|
|
|
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
|
|
|
|
got, err := s.Resources()
|
2021-10-04 09:38:22 +00:00
|
|
|
assert.Equal(tt, err, c.wantErr)
|
2021-09-22 17:47:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-10-04 09:38:22 +00:00
|
|
|
|
|
|
|
c.assertExpected(tt, got)
|
2021-09-22 17:47:04 +00:00
|
|
|
alerter.AssertExpectations(tt)
|
|
|
|
fakeRepo.AssertExpectations(tt)
|
2021-10-04 09:38:22 +00:00
|
|
|
testFilter.AssertExpectations(tt)
|
2021-09-22 17:47:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|