2013-08-15 17:05:08 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2019-09-20 09:28:43 -04:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-11-18 13:34:59 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/config"
|
2020-11-20 18:24:43 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2013-08-15 17:05:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBlockDevice(t *testing.T) {
|
2014-09-04 00:27:43 -04:00
|
|
|
cases := []struct {
|
|
|
|
Config *BlockDevice
|
|
|
|
Result *ec2.BlockDeviceMapping
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Config: &BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
SnapshotId: "snap-1234",
|
|
|
|
VolumeType: "standard",
|
|
|
|
VolumeSize: 8,
|
|
|
|
DeleteOnTermination: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
Result: &ec2.BlockDeviceMapping{
|
2015-10-18 14:00:05 -04:00
|
|
|
DeviceName: aws.String("/dev/sdb"),
|
2015-08-17 20:44:01 -04:00
|
|
|
Ebs: &ec2.EbsBlockDevice{
|
|
|
|
SnapshotId: aws.String("snap-1234"),
|
2015-04-05 17:58:48 -04:00
|
|
|
VolumeType: aws.String("standard"),
|
2015-07-28 20:10:21 -04:00
|
|
|
VolumeSize: aws.Int64(8),
|
|
|
|
DeleteOnTermination: aws.Bool(true),
|
2015-06-09 12:56:40 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-06-10 11:20:41 -04:00
|
|
|
{
|
|
|
|
Config: &BlockDevice{
|
2015-06-17 23:23:04 -04:00
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeSize: 8,
|
2015-06-10 11:20:41 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
Result: &ec2.BlockDeviceMapping{
|
2015-10-18 14:00:05 -04:00
|
|
|
DeviceName: aws.String("/dev/sdb"),
|
2015-08-17 20:44:01 -04:00
|
|
|
Ebs: &ec2.EbsBlockDevice{
|
2015-07-28 20:10:21 -04:00
|
|
|
VolumeSize: aws.Int64(8),
|
|
|
|
DeleteOnTermination: aws.Bool(false),
|
2015-06-10 11:20:41 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-06-09 12:56:40 -04:00
|
|
|
{
|
|
|
|
Config: &BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "io1",
|
|
|
|
VolumeSize: 8,
|
|
|
|
DeleteOnTermination: true,
|
|
|
|
IOPS: 1000,
|
|
|
|
},
|
|
|
|
|
|
|
|
Result: &ec2.BlockDeviceMapping{
|
2015-10-18 14:00:05 -04:00
|
|
|
DeviceName: aws.String("/dev/sdb"),
|
2015-08-17 20:44:01 -04:00
|
|
|
Ebs: &ec2.EbsBlockDevice{
|
2015-06-09 12:56:40 -04:00
|
|
|
VolumeType: aws.String("io1"),
|
2015-07-28 20:10:21 -04:00
|
|
|
VolumeSize: aws.Int64(8),
|
|
|
|
DeleteOnTermination: aws.Bool(true),
|
2015-08-17 20:44:01 -04:00
|
|
|
Iops: aws.Int64(1000),
|
2015-04-05 17:58:48 -04:00
|
|
|
},
|
2014-09-04 00:27:43 -04:00
|
|
|
},
|
2013-08-15 17:05:08 -04:00
|
|
|
},
|
2020-10-14 10:01:17 -04:00
|
|
|
{
|
|
|
|
Config: &BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "io2",
|
|
|
|
VolumeSize: 8,
|
|
|
|
DeleteOnTermination: true,
|
|
|
|
IOPS: 1000,
|
|
|
|
},
|
|
|
|
|
|
|
|
Result: &ec2.BlockDeviceMapping{
|
|
|
|
DeviceName: aws.String("/dev/sdb"),
|
|
|
|
Ebs: &ec2.EbsBlockDevice{
|
|
|
|
VolumeType: aws.String("io2"),
|
|
|
|
VolumeSize: aws.Int64(8),
|
|
|
|
DeleteOnTermination: aws.Bool(true),
|
|
|
|
Iops: aws.Int64(1000),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-24 17:19:11 -04:00
|
|
|
{
|
|
|
|
Config: &BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "gp2",
|
|
|
|
VolumeSize: 8,
|
|
|
|
DeleteOnTermination: true,
|
2019-08-23 05:17:45 -04:00
|
|
|
Encrypted: config.TriTrue,
|
2015-08-24 17:19:11 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
Result: &ec2.BlockDeviceMapping{
|
2015-10-18 14:00:05 -04:00
|
|
|
DeviceName: aws.String("/dev/sdb"),
|
2015-08-24 17:19:11 -04:00
|
|
|
Ebs: &ec2.EbsBlockDevice{
|
|
|
|
VolumeType: aws.String("gp2"),
|
|
|
|
VolumeSize: aws.Int64(8),
|
|
|
|
DeleteOnTermination: aws.Bool(true),
|
|
|
|
Encrypted: aws.Bool(true),
|
2015-10-18 16:04:41 -04:00
|
|
|
},
|
2015-07-01 18:02:31 -04:00
|
|
|
},
|
|
|
|
},
|
2018-01-08 23:30:29 -05:00
|
|
|
{
|
|
|
|
Config: &BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "gp2",
|
|
|
|
VolumeSize: 8,
|
|
|
|
DeleteOnTermination: true,
|
2019-08-23 05:17:45 -04:00
|
|
|
Encrypted: config.TriTrue,
|
2018-01-08 23:30:29 -05:00
|
|
|
KmsKeyId: "2Fa48a521f-3aff-4b34-a159-376ac5d37812",
|
|
|
|
},
|
|
|
|
|
|
|
|
Result: &ec2.BlockDeviceMapping{
|
|
|
|
DeviceName: aws.String("/dev/sdb"),
|
|
|
|
Ebs: &ec2.EbsBlockDevice{
|
|
|
|
VolumeType: aws.String("gp2"),
|
|
|
|
VolumeSize: aws.Int64(8),
|
|
|
|
DeleteOnTermination: aws.Bool(true),
|
|
|
|
Encrypted: aws.Bool(true),
|
2019-09-20 09:28:43 -04:00
|
|
|
KmsKeyId: aws.String("2Fa48a521f-3aff-4b34-a159-376ac5d37812"),
|
2018-01-08 23:30:29 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-07-21 01:07:30 -04:00
|
|
|
{
|
|
|
|
Config: &BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "standard",
|
|
|
|
DeleteOnTermination: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
Result: &ec2.BlockDeviceMapping{
|
2015-10-18 16:04:41 -04:00
|
|
|
DeviceName: aws.String("/dev/sdb"),
|
|
|
|
Ebs: &ec2.EbsBlockDevice{
|
2015-07-21 01:07:30 -04:00
|
|
|
VolumeType: aws.String("standard"),
|
2015-10-18 16:04:41 -04:00
|
|
|
DeleteOnTermination: aws.Bool(true),
|
2015-08-24 17:19:11 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-07-01 18:02:31 -04:00
|
|
|
{
|
|
|
|
Config: &BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VirtualName: "ephemeral0",
|
|
|
|
},
|
|
|
|
|
|
|
|
Result: &ec2.BlockDeviceMapping{
|
|
|
|
DeviceName: aws.String("/dev/sdb"),
|
|
|
|
VirtualName: aws.String("ephemeral0"),
|
|
|
|
},
|
|
|
|
},
|
2015-08-24 17:19:11 -04:00
|
|
|
{
|
|
|
|
Config: &BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
NoDevice: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
Result: &ec2.BlockDeviceMapping{
|
2015-10-18 14:00:05 -04:00
|
|
|
DeviceName: aws.String("/dev/sdb"),
|
|
|
|
NoDevice: aws.String(""),
|
2015-08-24 17:19:11 -04:00
|
|
|
},
|
|
|
|
},
|
2013-08-15 17:05:08 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 00:27:43 -04:00
|
|
|
for _, tc := range cases {
|
2019-06-18 06:37:47 -04:00
|
|
|
var amiBlockDevices BlockDevices = []BlockDevice{*tc.Config}
|
2016-08-12 19:29:55 -04:00
|
|
|
|
2019-06-18 06:37:47 -04:00
|
|
|
var launchBlockDevices BlockDevices = []BlockDevice{*tc.Config}
|
2013-08-15 17:05:08 -04:00
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
expected := []*ec2.BlockDeviceMapping{tc.Result}
|
2016-08-12 19:29:55 -04:00
|
|
|
|
2019-06-18 06:44:24 -04:00
|
|
|
amiResults := amiBlockDevices.BuildEC2BlockDeviceMappings()
|
2019-09-20 09:28:43 -04:00
|
|
|
if diff := cmp.Diff(expected, amiResults); diff != "" {
|
|
|
|
t.Fatalf("Bad block device: %s", diff)
|
2014-09-04 00:27:43 -04:00
|
|
|
}
|
2013-10-16 22:29:53 -04:00
|
|
|
|
2019-06-18 06:44:24 -04:00
|
|
|
launchResults := launchBlockDevices.BuildEC2BlockDeviceMappings()
|
2019-09-20 09:28:43 -04:00
|
|
|
if diff := cmp.Diff(expected, launchResults); diff != "" {
|
|
|
|
t.Fatalf("Bad block device: %s", diff)
|
2014-09-04 00:27:43 -04:00
|
|
|
}
|
2013-10-16 22:29:53 -04:00
|
|
|
}
|
2013-08-15 17:05:08 -04:00
|
|
|
}
|
2020-10-31 08:10:51 -04:00
|
|
|
|
|
|
|
func TestIOPSValidation(t *testing.T) {
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
device BlockDevice
|
|
|
|
ok bool
|
|
|
|
msg string
|
|
|
|
}{
|
|
|
|
// volume size unknown
|
|
|
|
{
|
|
|
|
device: BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "io1",
|
|
|
|
IOPS: 1000,
|
|
|
|
},
|
|
|
|
ok: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
device: BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "io2",
|
|
|
|
IOPS: 1000,
|
|
|
|
},
|
|
|
|
ok: true,
|
|
|
|
},
|
|
|
|
// ratio requirement satisfied
|
|
|
|
{
|
|
|
|
device: BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "io1",
|
|
|
|
VolumeSize: 50,
|
|
|
|
IOPS: 1000,
|
|
|
|
},
|
|
|
|
ok: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
device: BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "io2",
|
|
|
|
VolumeSize: 100,
|
|
|
|
IOPS: 1000,
|
|
|
|
},
|
|
|
|
ok: true,
|
|
|
|
},
|
|
|
|
// ratio requirement not satisfied
|
|
|
|
{
|
|
|
|
device: BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "io1",
|
|
|
|
VolumeSize: 10,
|
|
|
|
IOPS: 2000,
|
|
|
|
},
|
|
|
|
ok: false,
|
2020-11-04 14:29:09 -05:00
|
|
|
msg: "/dev/sdb: the maximum ratio of provisioned IOPS to requested volume size (in GiB) is 50:1 for io1 volumes",
|
2020-10-31 08:10:51 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
device: BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "io2",
|
|
|
|
VolumeSize: 50,
|
|
|
|
IOPS: 30000,
|
|
|
|
},
|
|
|
|
ok: false,
|
2020-11-04 14:29:09 -05:00
|
|
|
msg: "/dev/sdb: the maximum ratio of provisioned IOPS to requested volume size (in GiB) is 500:1 for io2 volumes",
|
|
|
|
},
|
|
|
|
// exceed max iops
|
|
|
|
{
|
|
|
|
device: BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "io2",
|
|
|
|
VolumeSize: 500,
|
|
|
|
IOPS: 99999,
|
|
|
|
},
|
|
|
|
ok: false,
|
|
|
|
msg: "IOPS must be between 100 and 64000 for device /dev/sdb",
|
|
|
|
},
|
|
|
|
// lower than min iops
|
|
|
|
{
|
|
|
|
device: BlockDevice{
|
|
|
|
DeviceName: "/dev/sdb",
|
|
|
|
VolumeType: "io2",
|
|
|
|
VolumeSize: 50,
|
|
|
|
IOPS: 10,
|
|
|
|
},
|
|
|
|
ok: false,
|
|
|
|
msg: "IOPS must be between 100 and 64000 for device /dev/sdb",
|
2020-10-31 08:10:51 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := interpolate.Context{}
|
|
|
|
for _, testCase := range cases {
|
|
|
|
err := testCase.device.Prepare(&ctx)
|
|
|
|
if testCase.ok && err != nil {
|
|
|
|
t.Fatalf("should not error, but: %v", err)
|
|
|
|
}
|
|
|
|
if !testCase.ok {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should error")
|
|
|
|
} else if err.Error() != testCase.msg {
|
|
|
|
t.Fatalf("wrong error: expected %s, found: %v", testCase.msg, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|