driftctl/pkg/middlewares/aws_instance_block_device_t...

184 lines
5.4 KiB
Go
Raw Normal View History

package middlewares
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/cloudskiff/driftctl/pkg/resource"
2021-03-29 16:10:50 +00:00
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/r3labs/diff/v2"
2021-04-29 15:17:55 +00:00
"github.com/stretchr/testify/mock"
)
func TestAwsInstanceBlockDeviceResourceMapper_Execute(t *testing.T) {
type args struct {
expectedResource *[]resource.Resource
resourcesFromState *[]resource.Resource
}
tests := []struct {
name string
args args
2021-03-29 16:10:50 +00:00
mocks func(factory *terraform.MockResourceFactory)
wantErr bool
}{
{
"Test with root block device and ebs block device",
struct {
expectedResource *[]resource.Resource
resourcesFromState *[]resource.Resource
}{
expectedResource: &[]resource.Resource{
2021-04-29 10:32:02 +00:00
&resource.AbstractResource{
Id: "dummy-instance",
Type: "aws_instance",
Attrs: &resource.Attributes{
"availability_zone": "eu-west-3",
},
},
2021-04-29 15:17:55 +00:00
&resource.AbstractResource{
Id: "vol-02862d9b39045a3a4",
Type: "aws_ebs_volume",
Attrs: &resource.Attributes{
"id": "vol-02862d9b39045a3a4",
"encrypted": true,
"multi_attach_enabled": false,
"availability_zone": "eu-west-3",
"iops": 1234,
"kms_key_id": "kms",
"size": 8,
"type": "gp2",
"tags": map[string]interface{}{
"Name": "rootVol",
},
},
},
2021-04-29 15:17:55 +00:00
&resource.AbstractResource{
Id: "vol-018c5ae89895aca4c",
Type: "aws_ebs_volume",
Attrs: &resource.Attributes{
"id": "vol-018c5ae89895aca4c",
"encrypted": true,
"multi_attach_enabled": false,
"availability_zone": "eu-west-3",
"size": 23,
"type": "gp2",
"tags": map[string]interface{}{
"Name": "rootVol",
},
},
},
2021-04-29 15:17:55 +00:00
&resource.AbstractResource{
Id: "vol-foobar",
Type: "aws_ebs_volume",
Attrs: &resource.Attributes{},
},
},
resourcesFromState: &[]resource.Resource{
2021-04-29 15:17:55 +00:00
&resource.AbstractResource{
Id: "vol-foobar",
Type: "aws_ebs_volume",
Attrs: &resource.Attributes{},
},
2021-04-29 10:32:02 +00:00
&resource.AbstractResource{
Id: "dummy-instance",
Type: "aws_instance",
Attrs: &resource.Attributes{
"availability_zone": "eu-west-3",
"volume_tags": map[string]string{
"Name": "rootVol",
},
2021-05-06 10:55:19 +00:00
"root_block_device": []interface{}{
map[string]interface{}{
2021-04-29 10:32:02 +00:00
"volume_id": "vol-02862d9b39045a3a4",
"volume_type": "gp2",
"device_name": "/dev/sda1",
"encrypted": true,
"kms_key_id": "kms",
2021-04-29 15:17:55 +00:00
"volume_size": 8,
"iops": 1234,
2021-04-29 10:32:02 +00:00
},
},
2021-05-06 10:55:19 +00:00
"ebs_block_device": []interface{}{
map[string]interface{}{
2021-04-29 10:32:02 +00:00
"volume_id": "vol-018c5ae89895aca4c",
"volume_type": "gp2",
"device_name": "/dev/sdb",
"encrypted": true,
"delete_on_termination": true,
2021-04-29 15:17:55 +00:00
"volume_size": 23,
2021-04-29 10:32:02 +00:00
},
},
},
},
},
},
2021-03-29 16:10:50 +00:00
func(factory *terraform.MockResourceFactory) {
2021-04-29 15:17:55 +00:00
foo := resource.AbstractResource{
Id: "vol-02862d9b39045a3a4",
Type: "aws_ebs_volume",
Attrs: &resource.Attributes{
"id": "vol-02862d9b39045a3a4",
"encrypted": true,
"multi_attach_enabled": false,
"availability_zone": "eu-west-3",
"iops": 1234,
"kms_key_id": "kms",
"size": 8,
"type": "gp2",
"tags": map[string]interface{}{
"Name": "rootVol",
},
},
}
2021-05-04 13:03:17 +00:00
factory.On("CreateAbstractResource", "aws_ebs_volume", mock.Anything, mock.MatchedBy(func(input map[string]interface{}) bool {
2021-04-29 15:17:55 +00:00
return input["id"] == "vol-02862d9b39045a3a4"
2021-05-04 13:03:17 +00:00
})).Times(1).Return(&foo, nil)
2021-04-29 15:17:55 +00:00
bar := resource.AbstractResource{
Id: "vol-018c5ae89895aca4c",
Type: "aws_ebs_volume",
Attrs: &resource.Attributes{
"id": "vol-018c5ae89895aca4c",
"encrypted": true,
"multi_attach_enabled": false,
"availability_zone": "eu-west-3",
"size": 23,
"type": "gp2",
"tags": map[string]interface{}{
"Name": "rootVol",
},
},
}
2021-05-04 13:03:17 +00:00
factory.On("CreateAbstractResource", "aws_ebs_volume", mock.Anything, mock.MatchedBy(func(input map[string]interface{}) bool {
2021-04-29 15:17:55 +00:00
return input["id"] == "vol-018c5ae89895aca4c"
2021-05-04 13:03:17 +00:00
})).Times(1).Return(&bar, nil)
2021-03-29 16:10:50 +00:00
},
false,
},
}
for _, c := range tests {
t.Run(c.name, func(tt *testing.T) {
2021-03-29 16:10:50 +00:00
factory := &terraform.MockResourceFactory{}
if c.mocks != nil {
c.mocks(factory)
}
2021-04-29 15:17:55 +00:00
a := NewAwsInstanceBlockDeviceResourceMapper(factory)
if err := a.Execute(&[]resource.Resource{}, c.args.resourcesFromState); (err != nil) != c.wantErr {
t.Errorf("Execute() error = %v, wantErr %v", err, c.wantErr)
}
changelog, err := diff.Diff(c.args.resourcesFromState, c.args.expectedResource)
if err != nil {
tt.Error(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))
}
}
})
}
}