github.phpd.cn/hashicorp/packer@v1.3.2/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: "gp2", 91 VolumeSize: 8, 92 DeleteOnTermination: true, 93 Encrypted: true, 94 KmsKeyId: "2Fa48a521f-3aff-4b34-a159-376ac5d37812", 95 }, 96 97 Result: &ec2.BlockDeviceMapping{ 98 DeviceName: aws.String("/dev/sdb"), 99 Ebs: &ec2.EbsBlockDevice{ 100 VolumeType: aws.String("gp2"), 101 VolumeSize: aws.Int64(8), 102 DeleteOnTermination: aws.Bool(true), 103 Encrypted: aws.Bool(true), 104 KmsKeyId: aws.String("2Fa48a521f-3aff-4b34-a159-376ac5d37812"), 105 }, 106 }, 107 }, 108 { 109 Config: &BlockDevice{ 110 DeviceName: "/dev/sdb", 111 VolumeType: "standard", 112 DeleteOnTermination: true, 113 }, 114 115 Result: &ec2.BlockDeviceMapping{ 116 DeviceName: aws.String("/dev/sdb"), 117 Ebs: &ec2.EbsBlockDevice{ 118 VolumeType: aws.String("standard"), 119 DeleteOnTermination: aws.Bool(true), 120 }, 121 }, 122 }, 123 { 124 Config: &BlockDevice{ 125 DeviceName: "/dev/sdb", 126 VirtualName: "ephemeral0", 127 }, 128 129 Result: &ec2.BlockDeviceMapping{ 130 DeviceName: aws.String("/dev/sdb"), 131 VirtualName: aws.String("ephemeral0"), 132 }, 133 }, 134 { 135 Config: &BlockDevice{ 136 DeviceName: "/dev/sdb", 137 NoDevice: true, 138 }, 139 140 Result: &ec2.BlockDeviceMapping{ 141 DeviceName: aws.String("/dev/sdb"), 142 NoDevice: aws.String(""), 143 }, 144 }, 145 } 146 147 for _, tc := range cases { 148 amiBlockDevices := AMIBlockDevices{ 149 AMIMappings: []BlockDevice{*tc.Config}, 150 } 151 152 launchBlockDevices := LaunchBlockDevices{ 153 LaunchMappings: []BlockDevice{*tc.Config}, 154 } 155 156 expected := []*ec2.BlockDeviceMapping{tc.Result} 157 158 amiResults := amiBlockDevices.BuildAMIDevices() 159 if !reflect.DeepEqual(expected, amiResults) { 160 t.Fatalf("Bad block device, \nexpected: %#v\n\ngot: %#v", 161 expected, amiResults) 162 } 163 164 launchResults := launchBlockDevices.BuildLaunchDevices() 165 if !reflect.DeepEqual(expected, launchResults) { 166 t.Fatalf("Bad block device, \nexpected: %#v\n\ngot: %#v", 167 expected, launchResults) 168 } 169 } 170 }