Unverified Commit 1b7ef252 authored by Adrien Delorme's avatar Adrien Delorme Committed by GitHub
Browse files

Merge pull request #8303 from alrs/amazon-builder-cleanup

Cleanup builder/amazon/common
parents 10ae0baa d0720798
Showing with 1 addition and 160 deletions
+1 -160
......@@ -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 {
......
......@@ -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]),
})
......
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")
}
}
......@@ -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)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment