driftctl/pkg/middlewares/aws_instance_block_device_t...

370 lines
11 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 {
2021-08-09 14:03:04 +00:00
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 {
2021-08-09 14:03:04 +00:00
expectedResource *[]*resource.Resource
resourcesFromState *[]*resource.Resource
}{
2021-08-09 14:03:04 +00:00
expectedResource: &[]*resource.Resource{
{
2021-04-29 10:32:02 +00:00
Id: "dummy-instance",
Type: "aws_instance",
Attrs: &resource.Attributes{
"availability_zone": "eu-west-3",
},
},
2021-08-09 14:03:04 +00:00
{
2021-04-29 15:17:55 +00:00
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",
"throughput": 125,
2021-04-29 15:17:55 +00:00
"tags": map[string]interface{}{
"Name": "rootVol",
},
},
},
2021-08-09 14:03:04 +00:00
{
2021-04-29 15:17:55 +00:00
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",
"throughput": 125,
2021-04-29 15:17:55 +00:00
"tags": map[string]interface{}{
"Name": "rootVol",
},
},
},
2021-08-09 14:03:04 +00:00
{
2021-04-29 15:17:55 +00:00
Id: "vol-foobar",
Type: "aws_ebs_volume",
Attrs: &resource.Attributes{},
},
},
2021-08-09 14:03:04 +00:00
resourcesFromState: &[]*resource.Resource{
{
2021-04-29 15:17:55 +00:00
Id: "vol-foobar",
Type: "aws_ebs_volume",
Attrs: &resource.Attributes{},
},
2021-08-09 14:03:04 +00:00
{
2021-04-29 10:32:02 +00:00
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,
"throughput": 125,
2021-04-29 15:17:55 +00:00
"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,
"throughput": 125,
2021-04-29 10:32:02 +00:00
},
},
},
},
},
},
2021-03-29 16:10:50 +00:00
func(factory *terraform.MockResourceFactory) {
2021-08-09 14:03:04 +00:00
foo := resource.Resource{
2021-04-29 15:17:55 +00:00
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",
"throughput": 125,
2021-04-29 15:17:55 +00:00
"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
2021-08-09 14:03:04 +00:00
bar := resource.Resource{
2021-04-29 15:17:55 +00:00
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",
"throughput": 125,
2021-04-29 15:17:55 +00:00
"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,
},
2021-06-22 09:26:39 +00:00
{
"Test with tags inside root/ebs block device",
struct {
2021-08-09 14:03:04 +00:00
expectedResource *[]*resource.Resource
resourcesFromState *[]*resource.Resource
2021-06-22 09:26:39 +00:00
}{
2021-08-09 14:03:04 +00:00
expectedResource: &[]*resource.Resource{
&resource.Resource{
2021-06-22 09:26:39 +00:00
Id: "dummy-instance",
Type: "aws_instance",
Attrs: &resource.Attributes{
"availability_zone": "eu-west-3",
},
},
2021-08-09 14:03:04 +00:00
&resource.Resource{
2021-06-22 09:26:39 +00:00
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",
"throughput": 125,
"tags": map[string]interface{}{
"Name": "rootVol",
},
},
},
2021-08-09 14:03:04 +00:00
&resource.Resource{
2021-06-22 09:26:39 +00:00
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",
"throughput": 125,
"tags": map[string]interface{}{
"Name": "ebsVol",
},
},
},
},
2021-08-09 14:03:04 +00:00
resourcesFromState: &[]*resource.Resource{
&resource.Resource{
2021-06-22 09:26:39 +00:00
Id: "dummy-instance",
Type: "aws_instance",
Attrs: &resource.Attributes{
"availability_zone": "eu-west-3",
"root_block_device": []interface{}{
map[string]interface{}{
"volume_id": "vol-02862d9b39045a3a4",
"volume_type": "gp2",
"device_name": "/dev/sda1",
"encrypted": true,
"kms_key_id": "kms",
"volume_size": 8,
"throughput": 125,
"iops": 1234,
"tags": map[string]interface{}{
"Name": "rootVol",
},
},
},
"ebs_block_device": []interface{}{
map[string]interface{}{
"volume_id": "vol-018c5ae89895aca4c",
"volume_type": "gp2",
"device_name": "/dev/sdb",
"encrypted": true,
"delete_on_termination": true,
"volume_size": 23,
"throughput": 125,
"tags": map[string]interface{}{
"Name": "ebsVol",
},
},
},
},
},
},
},
func(factory *terraform.MockResourceFactory) {
2021-08-09 14:03:04 +00:00
foo := resource.Resource{
2021-06-22 09:26:39 +00:00
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",
"throughput": 125,
"tags": map[string]interface{}{
"Name": "rootVol",
},
},
}
factory.On("CreateAbstractResource", "aws_ebs_volume", mock.Anything, mock.MatchedBy(func(input map[string]interface{}) bool {
return input["id"] == "vol-02862d9b39045a3a4" && len(input["tags"].(map[string]interface{})) == 1
})).Times(1).Return(&foo, nil)
2021-08-09 14:03:04 +00:00
bar := resource.Resource{
2021-06-22 09:26:39 +00:00
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",
"throughput": 125,
"tags": map[string]interface{}{
"Name": "ebsVol",
},
},
}
factory.On("CreateAbstractResource", "aws_ebs_volume", mock.Anything, mock.MatchedBy(func(input map[string]interface{}) bool {
return input["id"] == "vol-018c5ae89895aca4c" && len(input["tags"].(map[string]interface{})) == 1
})).Times(1).Return(&bar, nil)
},
false,
},
2021-06-22 13:40:33 +00:00
{
"Should not create ebs volume if there is already one (e.g. inline ebs_block_device)",
struct {
2021-08-09 14:03:04 +00:00
expectedResource *[]*resource.Resource
resourcesFromState *[]*resource.Resource
2021-06-22 13:40:33 +00:00
}{
2021-08-09 14:03:04 +00:00
expectedResource: &[]*resource.Resource{
&resource.Resource{
2021-06-22 13:40:33 +00:00
Id: "dummy-instance",
Type: "aws_instance",
Attrs: &resource.Attributes{
"availability_zone": "eu-west-3",
},
},
2021-08-09 14:03:04 +00:00
&resource.Resource{
2021-06-22 13:40:33 +00:00
Id: "vol-02862d9b39045a3a4",
Type: "aws_ebs_volume",
Attrs: &resource.Attributes{
"id": "vol-02862d9b39045a3a4",
},
},
},
2021-08-09 14:03:04 +00:00
resourcesFromState: &[]*resource.Resource{
&resource.Resource{
2021-06-22 13:40:33 +00:00
Id: "vol-02862d9b39045a3a4",
Type: "aws_ebs_volume",
Attrs: &resource.Attributes{
"id": "vol-02862d9b39045a3a4",
},
},
2021-08-09 14:03:04 +00:00
&resource.Resource{
2021-06-22 13:40:33 +00:00
Id: "dummy-instance",
Type: "aws_instance",
Attrs: &resource.Attributes{
"availability_zone": "eu-west-3",
"ebs_block_device": []interface{}{
map[string]interface{}{
"volume_id": "vol-02862d9b39045a3a4",
},
},
},
},
},
},
func(factory *terraform.MockResourceFactory) {},
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)
2021-08-09 14:03:04 +00:00
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))
}
}
})
}
}