@ -23,12 +23,13 @@ func TestStepRegisterAmi_buildRegisterOpts_pv(t *testing.T) {
config . AMIName = "test_ami_name"
config . AMIDescription = "test_ami_description"
config . AMIVirtType = "paravirtual"
rootDeviceName := "foo"
image := testImage ( )
blockDevices := [ ] * ec2 . BlockDeviceMapping { }
opts := buildRegisterOpts ( & config , & image , blockDevices )
opts := buildRegisterOpts FromExistingImage ( & config , & image , blockDevices , rootDeviceName )
expected := config . AMIVirtType
if * opts . VirtualizationType != expected {
@ -45,6 +46,10 @@ func TestStepRegisterAmi_buildRegisterOpts_pv(t *testing.T) {
t . Fatalf ( "Unexpected KernelId value: expected %s got %s\n" , expected , * opts . KernelId )
}
expected = rootDeviceName
if * opts . RootDeviceName != expected {
t . Fatalf ( "Unexpected RootDeviceName value: expected %s got %s\n" , expected , * opts . RootDeviceName )
}
}
func TestStepRegisterAmi_buildRegisterOpts_hvm ( t * testing . T ) {
@ -52,12 +57,13 @@ func TestStepRegisterAmi_buildRegisterOpts_hvm(t *testing.T) {
config . AMIName = "test_ami_name"
config . AMIDescription = "test_ami_description"
config . AMIVirtType = "hvm"
rootDeviceName := "foo"
image := testImage ( )
blockDevices := [ ] * ec2 . BlockDeviceMapping { }
opts := buildRegisterOpts ( & config , & image , blockDevices )
opts := buildRegisterOpts FromExistingImage ( & config , & image , blockDevices , rootDeviceName )
expected := config . AMIVirtType
if * opts . VirtualizationType != expected {
@ -72,6 +78,11 @@ func TestStepRegisterAmi_buildRegisterOpts_hvm(t *testing.T) {
if opts . KernelId != nil {
t . Fatalf ( "Unexpected KernelId value: expected nil got %s\n" , * opts . KernelId )
}
expected = rootDeviceName
if * opts . RootDeviceName != expected {
t . Fatalf ( "Unexpected RootDeviceName value: expected %s got %s\n" , expected , * opts . RootDeviceName )
}
}
func TestStepRegisterAmi_buildRegisterOptsFromScratch ( t * testing . T ) {
@ -146,3 +157,63 @@ func TestStepRegisterAmi_buildRegisterOptFromExistingImage(t *testing.T) {
}
t . Fatalf ( "Could not find device with snapshot ID %s" , snapshotID )
}
func TestStepRegisterAmi_buildRegisterOptFromExistingImageWithBlockDeviceMappings ( t * testing . T ) {
const (
rootDeviceName = "/dev/xvda"
oldRootDevice = "/dev/sda1"
)
snapshotId := "foo"
config := Config {
FromScratch : false ,
PackerConfig : common . PackerConfig { } ,
AMIBlockDevices : amazon . AMIBlockDevices {
AMIMappings : [ ] amazon . BlockDevice {
{
DeviceName : rootDeviceName ,
} ,
} ,
} ,
RootDeviceName : rootDeviceName ,
}
// Intentionally try to use a different root devicename
sourceImage := ec2 . Image {
RootDeviceName : aws . String ( oldRootDevice ) ,
BlockDeviceMappings : [ ] * ec2 . BlockDeviceMapping {
{
DeviceName : aws . String ( oldRootDevice ) ,
Ebs : & ec2 . EbsBlockDevice {
VolumeSize : aws . Int64 ( 10 ) ,
} ,
} ,
// Throw in an ephemeral device, it seems like all devices in the return struct in a source AMI have
// a size, even if it's for ephemeral
{
DeviceName : aws . String ( "/dev/sdb" ) ,
VirtualName : aws . String ( "ephemeral0" ) ,
Ebs : & ec2 . EbsBlockDevice {
VolumeSize : aws . Int64 ( 0 ) ,
} ,
} ,
} ,
}
registerOpts := buildBaseRegisterOpts ( & config , & sourceImage , 15 , snapshotId )
if len ( registerOpts . BlockDeviceMappings ) != 1 {
t . Fatal ( "Expected block device mapping of length 1" )
}
if * registerOpts . BlockDeviceMappings [ 0 ] . Ebs . SnapshotId != snapshotId {
t . Fatalf ( "Snapshot ID of root disk set to '%s' expected '%s'" , * registerOpts . BlockDeviceMappings [ 0 ] . Ebs . SnapshotId , rootDeviceName )
}
if * registerOpts . RootDeviceName != rootDeviceName {
t . Fatalf ( "Root device set to '%s' expected %s" , * registerOpts . RootDeviceName , rootDeviceName )
}
if * registerOpts . BlockDeviceMappings [ 0 ] . Ebs . VolumeSize != 15 {
t . Fatalf ( "Size of root disk not set to 15 GB, instead %d" , * registerOpts . BlockDeviceMappings [ 0 ] . Ebs . VolumeSize )
}
}