2020-12-14 11:18:14 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/resource"
|
|
|
|
"github.com/snyk/driftctl/enumeration/resource/aws"
|
2020-12-14 11:18:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAwsInstanceEIP_Execute(t *testing.T) {
|
|
|
|
type args struct {
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources *[]*resource.Resource
|
|
|
|
resourcesFromState *[]*resource.Resource
|
2020-12-14 11:18:14 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
2020-12-23 09:56:56 +00:00
|
|
|
name string
|
|
|
|
args args
|
2020-12-14 11:18:14 +00:00
|
|
|
expected args
|
|
|
|
}{
|
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
name: "test that public ip and dns are nilled with eip",
|
2020-12-23 09:56:56 +00:00
|
|
|
args: args{
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources: &[]*resource.Resource{
|
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
Id: "instance1",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"public_ip": "1.2.3.4",
|
|
|
|
"public_dns": "dns-of-eip.com",
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
Id: "instance2",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"public_ip": "1.2.3.4",
|
|
|
|
"public_dns": "dns-of-eip.com",
|
|
|
|
},
|
2020-12-14 11:18:14 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
resourcesFromState: &[]*resource.Resource{
|
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
Id: "instance1",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"public_ip": "5.6.7.8",
|
|
|
|
"public_dns": "example.com",
|
|
|
|
},
|
2020-12-14 11:18:14 +00:00
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-05-17 09:38:08 +00:00
|
|
|
Type: aws.AwsEipResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"instance": "instance1",
|
|
|
|
},
|
2020-12-14 11:18:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: args{
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources: &[]*resource.Resource{
|
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
Id: "instance1",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
Id: "instance2",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"public_ip": "1.2.3.4",
|
|
|
|
"public_dns": "dns-of-eip.com",
|
|
|
|
},
|
2020-12-14 11:18:14 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
resourcesFromState: &[]*resource.Resource{
|
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
Id: "instance1",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{},
|
2020-12-14 11:18:14 +00:00
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-05-17 09:38:08 +00:00
|
|
|
Type: aws.AwsEipResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"instance": "instance1",
|
|
|
|
},
|
2020-12-14 11:18:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
name: "test that public ip and dns are nilled when eip association",
|
2020-12-23 09:56:56 +00:00
|
|
|
args: args{
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources: &[]*resource.Resource{
|
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
Id: "instance1",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"public_ip": "1.2.3.4",
|
|
|
|
"public_dns": "dns-of-eip.com",
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
Id: "instance2",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"public_ip": "1.2.3.4",
|
|
|
|
"public_dns": "dns-of-eip.com",
|
|
|
|
},
|
2020-12-14 11:18:14 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
resourcesFromState: &[]*resource.Resource{
|
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
Id: "instance1",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"public_ip": "5.6.7.8",
|
|
|
|
"public_dns": "example.com",
|
|
|
|
},
|
2020-12-14 11:18:14 +00:00
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-05-17 09:38:08 +00:00
|
|
|
Type: aws.AwsEipAssociationResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"instance_id": "instance1",
|
|
|
|
},
|
2020-12-14 11:18:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: args{
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources: &[]*resource.Resource{
|
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
Id: "instance1",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
Id: "instance2",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"public_ip": "1.2.3.4",
|
|
|
|
"public_dns": "dns-of-eip.com",
|
|
|
|
},
|
2020-12-14 11:18:14 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
resourcesFromState: &[]*resource.Resource{
|
|
|
|
{
|
2021-04-29 10:32:02 +00:00
|
|
|
Id: "instance1",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{},
|
2020-12-14 11:18:14 +00:00
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-05-17 09:38:08 +00:00
|
|
|
Type: aws.AwsEipAssociationResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"instance_id": "instance1",
|
|
|
|
},
|
2020-12-14 11:18:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
a := AwsInstanceEIP{}
|
|
|
|
if err := a.Execute(tt.args.remoteResources, tt.args.resourcesFromState); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-12-23 09:56:56 +00:00
|
|
|
if !reflect.DeepEqual(tt.args, tt.expected) {
|
2020-12-14 11:18:14 +00:00
|
|
|
t.Fatalf("Expected results mismatch")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|