github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/amazon/common/block_device_test.go (about) 1 package common 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/ec2" 9 ) 10 11 func TestBlockDevice(t *testing.T) { 12 cases := []struct { 13 Config *BlockDevice 14 Result *ec2.BlockDeviceMapping 15 }{ 16 { 17 Config: &BlockDevice{ 18 DeviceName: "/dev/sdb", 19 SnapshotId: "snap-1234", 20 VolumeType: "standard", 21 VolumeSize: 8, 22 DeleteOnTermination: true, 23 }, 24 25 Result: &ec2.BlockDeviceMapping{ 26 DeviceName: aws.String("/dev/sdb"), 27 Ebs: &ec2.EbsBlockDevice{ 28 SnapshotId: aws.String("snap-1234"), 29 VolumeType: aws.String("standard"), 30 VolumeSize: aws.Int64(8), 31 DeleteOnTermination: aws.Bool(true), 32 }, 33 }, 34 }, 35 { 36 Config: &BlockDevice{ 37 DeviceName: "/dev/sdb", 38 VolumeSize: 8, 39 }, 40 41 Result: &ec2.BlockDeviceMapping{ 42 DeviceName: aws.String("/dev/sdb"), 43 Ebs: &ec2.EbsBlockDevice{ 44 VolumeSize: aws.Int64(8), 45 DeleteOnTermination: aws.Bool(false), 46 }, 47 }, 48 }, 49 { 50 Config: &BlockDevice{ 51 DeviceName: "/dev/sdb", 52 VolumeType: "io1", 53 VolumeSize: 8, 54 DeleteOnTermination: true, 55 IOPS: 1000, 56 }, 57 58 Result: &ec2.BlockDeviceMapping{ 59 DeviceName: aws.String("/dev/sdb"), 60 Ebs: &ec2.EbsBlockDevice{ 61 VolumeType: aws.String("io1"), 62 VolumeSize: aws.Int64(8), 63 DeleteOnTermination: aws.Bool(true), 64 Iops: aws.Int64(1000), 65 }, 66 }, 67 }, 68 { 69 Config: &BlockDevice{ 70 DeviceName: "/dev/sdb", 71 VolumeType: "gp2", 72 VolumeSize: 8, 73 DeleteOnTermination: true, 74 Encrypted: true, 75 }, 76 77 Result: &ec2.BlockDeviceMapping{ 78 DeviceName: aws.String("/dev/sdb"), 79 Ebs: &ec2.EbsBlockDevice{ 80 VolumeType: aws.String("gp2"), 81 VolumeSize: aws.Int64(8), 82 DeleteOnTermination: aws.Bool(true), 83 Encrypted: aws.Bool(true), 84 }, 85 }, 86 }, 87 { 88 Config: &BlockDevice{ 89 DeviceName: "/dev/sdb", 90 VolumeType: "standard", 91 DeleteOnTermination: true, 92 }, 93 94 Result: &ec2.BlockDeviceMapping{ 95 DeviceName: aws.String("/dev/sdb"), 96 Ebs: &ec2.EbsBlockDevice{ 97 VolumeType: aws.String("standard"), 98 DeleteOnTermination: aws.Bool(true), 99 }, 100 }, 101 }, 102 { 103 Config: &BlockDevice{ 104 DeviceName: "/dev/sdb", 105 VirtualName: "ephemeral0", 106 }, 107 108 Result: &ec2.BlockDeviceMapping{ 109 DeviceName: aws.String("/dev/sdb"), 110 VirtualName: aws.String("ephemeral0"), 111 }, 112 }, 113 { 114 Config: &BlockDevice{ 115 DeviceName: "/dev/sdb", 116 NoDevice: true, 117 }, 118 119 Result: &ec2.BlockDeviceMapping{ 120 DeviceName: aws.String("/dev/sdb"), 121 NoDevice: aws.String(""), 122 }, 123 }, 124 } 125 126 for _, tc := range cases { 127 amiBlockDevices := AMIBlockDevices{ 128 AMIMappings: []BlockDevice{*tc.Config}, 129 } 130 131 launchBlockDevices := LaunchBlockDevices{ 132 LaunchMappings: []BlockDevice{*tc.Config}, 133 } 134 135 expected := []*ec2.BlockDeviceMapping{tc.Result} 136 137 amiResults := amiBlockDevices.BuildAMIDevices() 138 if !reflect.DeepEqual(expected, amiResults) { 139 t.Fatalf("Bad block device, \nexpected: %#v\n\ngot: %#v", 140 expected, amiResults) 141 } 142 143 launchResults := launchBlockDevices.BuildLaunchDevices() 144 if !reflect.DeepEqual(expected, launchResults) { 145 t.Fatalf("Bad block device, \nexpected: %#v\n\ngot: %#v", 146 expected, launchResults) 147 } 148 } 149 }