driftctl/pkg/middlewares/aws_api_gateway_resource_ex...

116 lines
2.7 KiB
Go

package middlewares
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/r3labs/diff/v2"
"github.com/snyk/driftctl/enumeration/resource"
dctlresource "github.com/snyk/driftctl/pkg/resource"
"github.com/snyk/driftctl/pkg/resource/aws"
)
func TestAwsApiGatewayResourceExpander_Execute(t *testing.T) {
tests := []struct {
name string
resourcesFromState []*resource.Resource
mocks func(*dctlresource.MockResourceFactory)
expected []*resource.Resource
}{
{
name: "create api gateway root resource from rest api",
mocks: func(factory *dctlresource.MockResourceFactory) {
factory.On(
"CreateAbstractResource",
aws.AwsApiGatewayResourceResourceType,
"bar",
map[string]interface{}{
"rest_api_id": "foo",
"path": "/",
},
).Once().Return(&resource.Resource{
Id: "bar",
Type: aws.AwsApiGatewayResourceResourceType,
})
},
resourcesFromState: []*resource.Resource{
{
Id: "foo",
Type: aws.AwsApiGatewayRestApiResourceType,
Attrs: &resource.Attributes{
"root_resource_id": "bar",
},
},
},
expected: []*resource.Resource{
{
Id: "foo",
Type: aws.AwsApiGatewayRestApiResourceType,
Attrs: &resource.Attributes{
"root_resource_id": "bar",
},
},
{
Id: "bar",
Type: aws.AwsApiGatewayResourceResourceType,
},
},
},
{
name: "empty or unknown root_resource_id",
resourcesFromState: []*resource.Resource{
{
Id: "foo",
Type: aws.AwsApiGatewayRestApiResourceType,
Attrs: &resource.Attributes{
"root_resource_id": "",
},
},
{
Id: "bar",
Type: aws.AwsApiGatewayRestApiResourceType,
Attrs: &resource.Attributes{},
},
},
expected: []*resource.Resource{
{
Id: "foo",
Type: aws.AwsApiGatewayRestApiResourceType,
Attrs: &resource.Attributes{
"root_resource_id": "",
},
},
{
Id: "bar",
Type: aws.AwsApiGatewayRestApiResourceType,
Attrs: &resource.Attributes{},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
factory := &dctlresource.MockResourceFactory{}
if tt.mocks != nil {
tt.mocks(factory)
}
m := NewAwsApiGatewayResourceExpander(factory)
err := m.Execute(&[]*resource.Resource{}, &tt.resourcesFromState)
if err != nil {
t.Fatal(err)
}
changelog, err := diff.Diff(tt.expected, tt.resourcesFromState)
if err != nil {
t.Fatal(err)
}
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s got = %v, want %v", strings.Join(change.Path, "."), awsutil.Prettify(change.From), awsutil.Prettify(change.To))
}
}
})
}
}