2021-09-03 14:22:43 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/terraform"
|
2021-09-03 14:22:43 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/resource"
|
|
|
|
"github.com/snyk/driftctl/enumeration/resource/aws"
|
2021-09-03 14:22:43 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAwsRDSClusterInstanceExpander_Execute(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
remoteResources []*resource.Resource
|
|
|
|
stateResources []*resource.Resource
|
|
|
|
expectedRemoteResources []*resource.Resource
|
|
|
|
expectedStateResources []*resource.Resource
|
|
|
|
mock func(factory *terraform.MockResourceFactory)
|
|
|
|
}{
|
2021-09-20 12:44:57 +00:00
|
|
|
{
|
|
|
|
name: "should not map any rds cluster instance into db instances",
|
|
|
|
remoteResources: []*resource.Resource{
|
|
|
|
{
|
|
|
|
Id: "db-0",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "db-1",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stateResources: []*resource.Resource{},
|
|
|
|
expectedRemoteResources: []*resource.Resource{
|
|
|
|
{
|
|
|
|
Id: "db-0",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "db-1",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedStateResources: []*resource.Resource{},
|
|
|
|
},
|
2021-09-03 14:22:43 +00:00
|
|
|
{
|
|
|
|
name: "should import db instances in state",
|
|
|
|
remoteResources: []*resource.Resource{
|
|
|
|
{
|
|
|
|
Id: "bucket89713",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "bucket01",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
2021-10-19 14:54:45 +00:00
|
|
|
Id: "aurora-cluster-demo-0",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"field": "test",
|
|
|
|
},
|
2021-09-03 14:22:43 +00:00
|
|
|
},
|
|
|
|
{
|
2021-10-19 14:54:45 +00:00
|
|
|
Id: "aurora-cluster-demo-1",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"field": "test",
|
|
|
|
},
|
2021-09-03 14:22:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
stateResources: []*resource.Resource{
|
|
|
|
{
|
|
|
|
Id: "aurora-cluster-demo-0",
|
|
|
|
Type: aws.AwsRDSClusterInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "aurora-cluster-demo-1",
|
|
|
|
Type: aws.AwsRDSClusterInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedRemoteResources: []*resource.Resource{
|
|
|
|
{
|
|
|
|
Id: "bucket89713",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "bucket01",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
2021-10-19 14:54:45 +00:00
|
|
|
Id: "aurora-cluster-demo-0",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"field": "test",
|
|
|
|
},
|
2021-09-03 14:22:43 +00:00
|
|
|
},
|
|
|
|
{
|
2021-10-19 14:54:45 +00:00
|
|
|
Id: "aurora-cluster-demo-1",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"field": "test",
|
|
|
|
},
|
2021-09-03 14:22:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedStateResources: []*resource.Resource{
|
|
|
|
{
|
2021-10-19 14:54:45 +00:00
|
|
|
Id: "aurora-cluster-demo-0",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"field": "test",
|
|
|
|
},
|
2021-09-03 14:22:43 +00:00
|
|
|
},
|
|
|
|
{
|
2021-10-19 14:54:45 +00:00
|
|
|
Id: "aurora-cluster-demo-1",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"field": "test",
|
|
|
|
},
|
2021-09-03 14:22:43 +00:00
|
|
|
},
|
|
|
|
},
|
2021-09-20 12:44:57 +00:00
|
|
|
mock: func(factory *terraform.MockResourceFactory) {
|
2021-10-19 14:54:45 +00:00
|
|
|
factory.On("CreateAbstractResource", aws.AwsDbInstanceResourceType, "aurora-cluster-demo-0", map[string]interface{}{"field": "test"}).
|
2021-09-20 12:44:57 +00:00
|
|
|
Return(&resource.Resource{
|
|
|
|
Id: "aurora-cluster-demo-0",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
2021-10-19 14:54:45 +00:00
|
|
|
Attrs: &resource.Attributes{"field": "test"},
|
2021-09-20 12:44:57 +00:00
|
|
|
}).
|
|
|
|
Once()
|
|
|
|
|
2021-10-19 14:54:45 +00:00
|
|
|
factory.On("CreateAbstractResource", aws.AwsDbInstanceResourceType, "aurora-cluster-demo-1", map[string]interface{}{"field": "test"}).
|
2021-09-20 12:44:57 +00:00
|
|
|
Return(&resource.Resource{
|
|
|
|
Id: "aurora-cluster-demo-1",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
2021-10-19 14:54:45 +00:00
|
|
|
Attrs: &resource.Attributes{"field": "test"},
|
2021-09-20 12:44:57 +00:00
|
|
|
}).
|
|
|
|
Once()
|
|
|
|
},
|
2021-09-03 14:22:43 +00:00
|
|
|
},
|
|
|
|
{
|
2021-09-20 12:44:57 +00:00
|
|
|
name: "should find only one db instances in remote",
|
2021-09-03 14:22:43 +00:00
|
|
|
remoteResources: []*resource.Resource{
|
|
|
|
{
|
|
|
|
Id: "bucket89713",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "bucket01",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "aurora-cluster-demo-0",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stateResources: []*resource.Resource{
|
|
|
|
{
|
|
|
|
Id: "bucket01",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "aurora-cluster-demo-0",
|
|
|
|
Type: aws.AwsRDSClusterInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "aurora-cluster-demo-1",
|
|
|
|
Type: aws.AwsRDSClusterInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedRemoteResources: []*resource.Resource{
|
|
|
|
{
|
|
|
|
Id: "bucket89713",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "bucket01",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "aurora-cluster-demo-0",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedStateResources: []*resource.Resource{
|
|
|
|
{
|
|
|
|
Id: "bucket01",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "aurora-cluster-demo-0",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "aurora-cluster-demo-1",
|
|
|
|
Type: aws.AwsRDSClusterInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
},
|
2021-09-20 12:44:57 +00:00
|
|
|
mock: func(factory *terraform.MockResourceFactory) {
|
|
|
|
factory.On("CreateAbstractResource", aws.AwsDbInstanceResourceType, "aurora-cluster-demo-0", map[string]interface{}{}).
|
|
|
|
Return(&resource.Resource{
|
|
|
|
Id: "aurora-cluster-demo-0",
|
|
|
|
Type: aws.AwsDbInstanceResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
}).
|
|
|
|
Once()
|
|
|
|
},
|
2021-09-03 14:22:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
factory := &terraform.MockResourceFactory{}
|
|
|
|
if tt.mock != nil {
|
|
|
|
tt.mock(factory)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := NewRDSClusterInstanceExpander(factory)
|
|
|
|
err := m.Execute(&tt.remoteResources, &tt.stateResources)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, tt.expectedRemoteResources, tt.remoteResources, "Unexpected remote resources")
|
|
|
|
assert.Equal(t, tt.expectedStateResources, tt.stateResources, "Unexpected state resources")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|