2021-01-14 17:11:02 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awsutil"
|
|
|
|
"github.com/r3labs/diff/v2"
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/resource"
|
|
|
|
"github.com/snyk/driftctl/enumeration/resource/aws"
|
2021-01-14 17:11:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAwsDefaultRoute_Execute(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources []*resource.Resource
|
|
|
|
resourcesFromState []*resource.Resource
|
|
|
|
expected []*resource.Resource
|
2021-01-14 17:11:02 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"test that default routes are not ignored when managed by IaC",
|
2021-08-09 14:03:04 +00:00
|
|
|
[]*resource.Resource{
|
|
|
|
{
|
2021-01-14 17:11:02 +00:00
|
|
|
Id: "fake",
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-05-07 18:55:49 +00:00
|
|
|
Id: "a-dummy-route",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "fake-table-id",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-05-07 18:55:49 +00:00
|
|
|
Id: "default-managed-by-IaC",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "fake-table-id",
|
|
|
|
"origin": "CreateRouteTable",
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
[]*resource.Resource{
|
|
|
|
{
|
2021-05-07 18:55:49 +00:00
|
|
|
Id: "default-managed-by-IaC",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "fake-table-id",
|
|
|
|
"origin": "CreateRouteTable",
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
[]*resource.Resource{
|
|
|
|
{
|
2021-01-14 17:11:02 +00:00
|
|
|
Id: "fake",
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-05-07 18:55:49 +00:00
|
|
|
Id: "a-dummy-route",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "fake-table-id",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-05-07 18:55:49 +00:00
|
|
|
Id: "default-managed-by-IaC",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "fake-table-id",
|
|
|
|
"origin": "CreateRouteTable",
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test that default routes are ignored when not managed by IaC",
|
2021-08-09 14:03:04 +00:00
|
|
|
[]*resource.Resource{
|
|
|
|
{
|
2021-01-14 17:11:02 +00:00
|
|
|
Id: "fake",
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-05-07 18:55:49 +00:00
|
|
|
Id: "a-dummy-route",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "fake-table-id",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-05-07 18:55:49 +00:00
|
|
|
Id: "default-managed-by-IaC",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "fake-table-id",
|
|
|
|
"origin": "CreateRouteTable",
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
[]*resource.Resource{},
|
|
|
|
[]*resource.Resource{
|
|
|
|
{
|
2021-01-14 17:11:02 +00:00
|
|
|
Id: "fake",
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-05-07 18:55:49 +00:00
|
|
|
Id: "a-dummy-route",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "fake-table-id",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
m := NewAwsDefaultRoute()
|
|
|
|
err := m.Execute(&tt.remoteResources, &tt.resourcesFromState)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
changelog, err := diff.Diff(tt.expected, tt.remoteResources)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|