diff --git a/builder/amazon/common/ami_config_test.go b/builder/amazon/common/ami_config_test.go index 8430c964c..2448965db 100644 --- a/builder/amazon/common/ami_config_test.go +++ b/builder/amazon/common/ami_config_test.go @@ -69,7 +69,6 @@ func TestAMIConfigPrepare_regions(t *testing.T) { if errs = c.prepareRegions(accessConf); len(errs) > 0 { t.Fatalf("shouldn't have err: %#v", errs) } - errs = errs[:0] c.AMIRegions = []string{"us-east-1", "us-west-1", "us-east-1"} if errs = c.prepareRegions(accessConf); len(errs) > 0 { diff --git a/builder/amazon/common/errors.go b/builder/amazon/common/errors.go index f12f58e7d..bca247bfa 100644 --- a/builder/amazon/common/errors.go +++ b/builder/amazon/common/errors.go @@ -24,7 +24,7 @@ type stsDecoder interface { func decodeAWSError(decoder stsDecoder, err error) error { groups := encodedFailureMessagePattern.FindStringSubmatch(err.Error()) - if groups != nil && len(groups) > 1 { + if len(groups) > 1 { result, decodeErr := decoder.DecodeAuthorizationMessage(&sts.DecodeAuthorizationMessageInput{ EncodedMessage: aws.String(groups[2]), }) diff --git a/builder/amazon/common/step_modify_ebs_instance_test.go b/builder/amazon/common/step_modify_ebs_instance_test.go deleted file mode 100644 index cb8a4b020..000000000 --- a/builder/amazon/common/step_modify_ebs_instance_test.go +++ /dev/null @@ -1,105 +0,0 @@ -package common - -import ( - "bytes" - "context" - "fmt" - "testing" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" - "github.com/aws/aws-sdk-go/service/ec2/ec2iface" - helperconfig "github.com/hashicorp/packer/helper/config" - "github.com/hashicorp/packer/helper/multistep" - "github.com/hashicorp/packer/packer" -) - -// Define a mock struct to be used in unit tests for common aws steps. -type mockEC2Conn_ModifyEBS struct { - ec2iface.EC2API - Config *aws.Config - - // Counters to figure out what code path was taken - shouldError bool - modifyImageAttrCount int -} - -func (m *mockEC2Conn_ModifyEBS) ModifyInstanceAttribute(modifyInput *ec2.ModifyInstanceAttributeInput) (*ec2.ModifyInstanceAttributeOutput, error) { - m.modifyImageAttrCount++ - // don't need to define output since we always discard it anyway. - output := &ec2.ModifyInstanceAttributeOutput{} - if m.shouldError { - return output, fmt.Errorf("fake ModifyInstanceAttribute error") - } - return output, nil -} - -// Create statebag for running test -func fakeModifyEBSBackedInstanceState() multistep.StateBag { - state := new(multistep.BasicStateBag) - state.Put("ui", &packer.BasicUi{ - Reader: new(bytes.Buffer), - Writer: new(bytes.Buffer), - }) - state.Put("instance", "i-12345") - return state -} - -func StepModifyEBSBackedInstance_EnableAMIENASupport(t *testing.T) { - // Value is unset, so we shouldn't modify - stepModifyEBSBackedInstance := StepModifyEBSBackedInstance{ - EnableAMIENASupport: helperconfig.TriUnset, - EnableAMISriovNetSupport: false, - } - - // mock out the region connection code - mockConn := &mockEC2Conn_ModifyEBS{ - Config: aws.NewConfig(), - } - - state := fakeModifyEBSBackedInstanceState() - state.Put("ec2", mockConn) - stepModifyEBSBackedInstance.Run(context.Background(), state) - - if mockConn.modifyImageAttrCount > 0 { - t.Fatalf("Should not have modified image since EnableAMIENASupport is unset") - } - - // Value is true, so we should modify - stepModifyEBSBackedInstance = StepModifyEBSBackedInstance{ - EnableAMIENASupport: helperconfig.TriTrue, - EnableAMISriovNetSupport: false, - } - - // mock out the region connection code - mockConn = &mockEC2Conn_ModifyEBS{ - Config: aws.NewConfig(), - } - - state = fakeModifyEBSBackedInstanceState() - state.Put("ec2", mockConn) - stepModifyEBSBackedInstance.Run(context.Background(), state) - - if mockConn.modifyImageAttrCount != 1 { - t.Fatalf("Should have modified image, since EnableAMIENASupport is true") - } - - // Value is false, so we should modify - stepModifyEBSBackedInstance = StepModifyEBSBackedInstance{ - EnableAMIENASupport: helperconfig.TriFalse, - EnableAMISriovNetSupport: false, - } - - // mock out the region connection code - mockConn = &mockEC2Conn_ModifyEBS{ - Config: aws.NewConfig(), - } - - state = fakeModifyEBSBackedInstanceState() - state.Put("ec2", mockConn) - stepModifyEBSBackedInstance.Run(context.Background(), state) - - if mockConn.modifyImageAttrCount != 1 { - t.Fatalf("Should have modified image, since EnableAMIENASupport is true") - } -} diff --git a/builder/amazon/common/step_run_spot_instance_test.go b/builder/amazon/common/step_run_spot_instance_test.go index e39e05db9..3122ef9d9 100644 --- a/builder/amazon/common/step_run_spot_instance_test.go +++ b/builder/amazon/common/step_run_spot_instance_test.go @@ -4,67 +4,14 @@ import ( "bytes" "fmt" "testing" - "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/aws/aws-sdk-go/service/ec2/ec2iface" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) -// Define a mock struct to be used in unit tests for common aws steps. -type mockEC2ConnSpot struct { - ec2iface.EC2API - Config *aws.Config - - // Counters to figure out what code path was taken - describeSpotPriceHistoryCount int -} - -// Generates fake SpotPriceHistory data and returns it in the expected output -// format. Also increments a -func (m *mockEC2ConnSpot) DescribeSpotPriceHistory(copyInput *ec2.DescribeSpotPriceHistoryInput) (*ec2.DescribeSpotPriceHistoryOutput, error) { - m.describeSpotPriceHistoryCount++ - testTime := time.Now().Add(-1 * time.Hour) - sp := []*ec2.SpotPrice{ - { - AvailabilityZone: aws.String("us-east-1c"), - InstanceType: aws.String("t2.micro"), - ProductDescription: aws.String("Linux/UNIX"), - SpotPrice: aws.String("0.003500"), - Timestamp: &testTime, - }, - { - AvailabilityZone: aws.String("us-east-1f"), - InstanceType: aws.String("t2.micro"), - ProductDescription: aws.String("Linux/UNIX"), - SpotPrice: aws.String("0.003500"), - Timestamp: &testTime, - }, - { - AvailabilityZone: aws.String("us-east-1b"), - InstanceType: aws.String("t2.micro"), - ProductDescription: aws.String("Linux/UNIX"), - SpotPrice: aws.String("0.003500"), - Timestamp: &testTime, - }, - } - output := &ec2.DescribeSpotPriceHistoryOutput{SpotPriceHistory: sp} - - return output, nil - -} - -func getMockConnSpot() ec2iface.EC2API { - mockConn := &mockEC2ConnSpot{ - Config: aws.NewConfig(), - } - - return mockConn -} - // Create statebag for running test func tStateSpot() multistep.StateBag { state := new(multistep.BasicStateBag)