117 lines
2.7 KiB
Go
117 lines
2.7 KiB
Go
package middlewares
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awsutil"
|
|
"github.com/snyk/driftctl/pkg/resource"
|
|
"github.com/snyk/driftctl/pkg/resource/aws"
|
|
"github.com/snyk/driftctl/pkg/terraform"
|
|
|
|
"github.com/r3labs/diff/v2"
|
|
)
|
|
|
|
func TestAwsApiGatewayResourceExpander_Execute(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
resourcesFromState []*resource.Resource
|
|
mocks func(*terraform.MockResourceFactory)
|
|
expected []*resource.Resource
|
|
}{
|
|
{
|
|
name: "create api gateway root resource from rest api",
|
|
mocks: func(factory *terraform.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 := &terraform.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))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|