diff --git a/builder/amazon/chroot/step_register_ami.go b/builder/amazon/chroot/step_register_ami.go index 69ec6dbb6..62e6a3ff0 100644 --- a/builder/amazon/chroot/step_register_ami.go +++ b/builder/amazon/chroot/step_register_ami.go @@ -30,14 +30,7 @@ func (s *StepRegisterAMI) Run(state multistep.StateBag) multistep.StepAction { blockDevices[i] = newDevice } - registerOpts := &ec2.RegisterImage{ - Name: config.AMIName, - Architecture: image.Architecture, - KernelId: image.KernelId, - RamdiskId: image.RamdiskId, - RootDeviceName: image.RootDeviceName, - BlockDevices: blockDevices, - } + registerOpts := buildRegisterOpts(config, image, blockDevices) // Set SriovNetSupport to "simple". See http://goo.gl/icuXh5 if config.AMIEnhancedNetworking { @@ -77,3 +70,20 @@ func (s *StepRegisterAMI) Run(state multistep.StateBag) multistep.StepAction { } func (s *StepRegisterAMI) Cleanup(state multistep.StateBag) {} + +func buildRegisterOpts(config *Config, image *ec2.Image, blockDevices []ec2.BlockDeviceMapping) *ec2.RegisterImage { + registerOpts := &ec2.RegisterImage{ + Name: config.AMIName, + Architecture: image.Architecture, + RootDeviceName: image.RootDeviceName, + BlockDevices: blockDevices, + VirtType: config.AMIVirtType, + } + + if config.AMIVirtType != "hvm" { + registerOpts.KernelId = image.KernelId + registerOpts.RamdiskId = image.RamdiskId + } + + return registerOpts +} diff --git a/builder/amazon/chroot/step_register_ami_test.go b/builder/amazon/chroot/step_register_ami_test.go new file mode 100644 index 000000000..7d75687bc --- /dev/null +++ b/builder/amazon/chroot/step_register_ami_test.go @@ -0,0 +1,83 @@ +package chroot + +import ( + "fmt" + "testing" + + "github.com/kr/pretty" + "github.com/mitchellh/goamz/ec2" + // awscommon "github.com/mitchellh/packer/builder/amazon/common" +) + +func testImage() ec2.Image { + return ec2.Image{ + Id: "ami-abcd1234", + Name: "ami_test_name", + Architecture: "x86_64", + KernelId: "aki-abcd1234", + } +} + +func TestStepRegisterAmi_buildRegisterOpts_pv(t *testing.T) { + config := Config{} + config.AMIName = "test_ami_name" + config.AMIDescription = "test_ami_description" + config.AMIVirtType = "paravirtual" + + image := testImage() + + blockDevices := []ec2.BlockDeviceMapping{} + + opts := buildRegisterOpts(&config, &image, blockDevices) + + fmt.Println("******** PAS ********") + fmt.Printf("%# v", pretty.Formatter(opts)) + + expected := config.AMIVirtType + if opts.VirtType != expected { + t.Fatalf("Unexpected VirtType value: expected %s got %s\n", expected, opts.VirtType) + } + + expected = config.AMIName + if opts.Name != expected { + t.Fatalf("Unexpected Name value: expected %s got %s\n", expected, opts.Name) + } + + expected = image.KernelId + if opts.KernelId != expected { + t.Fatalf("Unexpected KernelId value: expected %s got %s\n", expected, opts.KernelId) + } + +} + +func TestStepRegisterAmi_buildRegisterOpts_hvm(t *testing.T) { + config := Config{} + config.AMIName = "test_ami_name" + config.AMIDescription = "test_ami_description" + config.AMIVirtType = "hvm" + + image := testImage() + + blockDevices := []ec2.BlockDeviceMapping{} + + opts := buildRegisterOpts(&config, &image, blockDevices) + + fmt.Println("******** PAS ********") + fmt.Printf("%# v", pretty.Formatter(opts)) + + expected := config.AMIVirtType + if opts.VirtType != expected { + t.Fatalf("Unexpected VirtType value: expected %s got %s\n", expected, opts.VirtType) + } + + expected = config.AMIName + if opts.Name != expected { + t.Fatalf("Unexpected Name value: expected %s got %s\n", expected, opts.Name) + } + + expected = "" + if opts.KernelId != expected { + t.Fatalf("Unexpected KernelId value: expected %s got %s\n", expected, opts.KernelId) + } + +}