Subnet information is only really needed when the specified `vpc_id` is not the default VPC for the region where the builder is being executed. This change uses the AWS API to determine if the VPC provided is a non-default VPC and only validates the existence of a `subnet_id` if a user has provided a non-default `vpc_id`. Tests after change ``` > make test TEST=./builder/amazon/... TESTARGS='-count=1 -v -run=TestStepPreValidate_checkVpc' ... === RUN TestStepPreValidate_checkVpc === RUN TestStepPreValidate_checkVpc/DefaultVpc === RUN TestStepPreValidate_checkVpc/NonDefaultVpcNoSubnet === RUN TestStepPreValidate_checkVpc/NonDefaultVpcWithSubnet === RUN TestStepPreValidate_checkVpc/SubnetWithNoVpc === RUN TestStepPreValidate_checkVpc/NoVpcInformation --- PASS: TestStepPreValidate_checkVpc (0.00s) --- PASS: TestStepPreValidate_checkVpc/DefaultVpc (0.00s) --- PASS: TestStepPreValidate_checkVpc/NonDefaultVpcNoSubnet (0.00s) --- PASS: TestStepPreValidate_checkVpc/NonDefaultVpcWithSubnet (0.00s) --- PASS: TestStepPreValidate_checkVpc/SubnetWithNoVpc (0.00s) --- PASS: TestStepPreValidate_checkVpc/NoVpcInformation (0.00s) PASS ... ```
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
)
|
|
|
|
//DescribeVpcs mocks an ec2.DescribeVpcsOutput for a given input
|
|
func (m *mockEC2Conn) DescribeVpcs(input *ec2.DescribeVpcsInput) (*ec2.DescribeVpcsOutput, error) {
|
|
m.lock.Lock()
|
|
m.copyImageCount++
|
|
m.lock.Unlock()
|
|
|
|
if input == nil || len(input.VpcIds) == 0 {
|
|
return nil, fmt.Errorf("oops looks like we need more input")
|
|
}
|
|
|
|
var isDefault bool
|
|
vpcID := aws.StringValue(input.VpcIds[0])
|
|
|
|
//only one default VPC per region
|
|
if strings.Contains("vpc-default-id", vpcID) {
|
|
isDefault = true
|
|
}
|
|
|
|
output := &ec2.DescribeVpcsOutput{
|
|
Vpcs: []*ec2.Vpc{
|
|
&ec2.Vpc{IsDefault: aws.Bool(isDefault),
|
|
VpcId: aws.String(vpcID),
|
|
},
|
|
},
|
|
}
|
|
return output, nil
|
|
}
|
|
|
|
func TestStepPreValidate_checkVpc(t *testing.T) {
|
|
tt := []struct {
|
|
name string
|
|
step StepPreValidate
|
|
errorExpected bool
|
|
}{
|
|
{"DefaultVpc", StepPreValidate{VpcId: "vpc-default-id"}, false},
|
|
{"NonDefaultVpcNoSubnet", StepPreValidate{VpcId: "vpc-1234567890"}, true},
|
|
{"NonDefaultVpcWithSubnet", StepPreValidate{VpcId: "vpc-1234567890", SubnetId: "subnet-1234567890"}, false},
|
|
{"SubnetWithNoVpc", StepPreValidate{SubnetId: "subnet-1234567890"}, false},
|
|
{"NoVpcInformation", StepPreValidate{}, false},
|
|
}
|
|
|
|
mockConn, err := getMockConn(nil, "")
|
|
if err != nil {
|
|
t.Fatal("unable to get a mock connection")
|
|
}
|
|
|
|
for _, tc := range tt {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := tc.step.checkVpc(mockConn)
|
|
|
|
if tc.errorExpected && err == nil {
|
|
t.Errorf("expected a validation error for %q but got %q", tc.name, err)
|
|
}
|
|
|
|
if !tc.errorExpected && err != nil {
|
|
t.Errorf("expected a validation to pass for %q but got %q", tc.name, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|