Adding the conditional necessary to construct the right options for both PV and HVM images. Also adding a test to make sure it is doing the right thing

This commit is contained in:
Peter Sankauskas 2014-07-29 21:56:37 -07:00
parent 460e2da248
commit a2c0b104f0
2 changed files with 101 additions and 8 deletions

View File

@ -30,14 +30,7 @@ func (s *StepRegisterAMI) Run(state multistep.StateBag) multistep.StepAction {
blockDevices[i] = newDevice blockDevices[i] = newDevice
} }
registerOpts := &ec2.RegisterImage{ registerOpts := buildRegisterOpts(config, image, blockDevices)
Name: config.AMIName,
Architecture: image.Architecture,
KernelId: image.KernelId,
RamdiskId: image.RamdiskId,
RootDeviceName: image.RootDeviceName,
BlockDevices: blockDevices,
}
// Set SriovNetSupport to "simple". See http://goo.gl/icuXh5 // Set SriovNetSupport to "simple". See http://goo.gl/icuXh5
if config.AMIEnhancedNetworking { if config.AMIEnhancedNetworking {
@ -77,3 +70,20 @@ func (s *StepRegisterAMI) Run(state multistep.StateBag) multistep.StepAction {
} }
func (s *StepRegisterAMI) Cleanup(state multistep.StateBag) {} 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
}

View File

@ -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)
}
}