From 67ace0894b803b06419c42c48e320cb6ac2d8f1f Mon Sep 17 00:00:00 2001 From: Yevgeniy Brikman Date: Fri, 1 Apr 2016 11:41:14 +0200 Subject: [PATCH 1/2] Fetch the default VPC instead of a random one We should not be using VPCs at random. Our tests create and destroy VPCs all the time, so it's possible that one test will accidentally try to use a VPC that another test is trying to destroy. This changes terratest to return the default VPC instead of a random one. --- aws/vpc.go | 22 +++++++++++++--------- rand_resources.go | 2 +- rand_resources_test.go | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/aws/vpc.go b/aws/vpc.go index f95930a4c..6e2489f17 100644 --- a/aws/vpc.go +++ b/aws/vpc.go @@ -1,10 +1,11 @@ package aws + import ( "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws" "errors" - "github.com/gruntwork-io/terratest/util" + "strconv" ) var VpcIdFilterName = "vpc-id" @@ -15,25 +16,28 @@ type Vpc struct { Subnets []ec2.Subnet // A list of subnets in the VPC } -func GetRandomVpc(awsRegion string) (Vpc, error) { +var IS_DEFAULT_FILTER_NAME = "isDefault" +var IS_DEFAULT_FILTER_VALUE = "true" + +func GetDefaultVpc(awsRegion string) (Vpc, error) { vpc := Vpc{} svc := ec2.New(session.New(), aws.NewConfig().WithRegion(awsRegion)) - vpcs, err := svc.DescribeVpcs(&ec2.DescribeVpcsInput{}) + defaultVpcFilter := ec2.Filter{Name: &IS_DEFAULT_FILTER_NAME, Values: []*string{&IS_DEFAULT_FILTER_VALUE}} + vpcs, err := svc.DescribeVpcs(&ec2.DescribeVpcsInput{Filters: []*ec2.Filter{&defaultVpcFilter}}) if err != nil { return vpc, err } numVpcs := len(vpcs.Vpcs) - if numVpcs == 0 { - return vpc, errors.New("No VPCs found in region " + awsRegion) + if numVpcs != 1 { + return vpc, errors.New("Expected to find one default VPC in region " + awsRegion + " but found " + strconv.Itoa(numVpcs)) } - randomIndex := util.Random(0, numVpcs) - randomVpc := vpcs.Vpcs[randomIndex] + defaultVpc := vpcs.Vpcs[0] - vpc.Id = *randomVpc.VpcId - vpc.Name = FindVpcName(randomVpc) + vpc.Id = *defaultVpc.VpcId + vpc.Name = FindVpcName(defaultVpc) vpc.Subnets, err = GetSubnetsForVpc(vpc.Id, awsRegion) return vpc, err diff --git a/rand_resources.go b/rand_resources.go index e07cb59ca..c4636d73a 100644 --- a/rand_resources.go +++ b/rand_resources.go @@ -101,5 +101,5 @@ func (r *RandomResourceCollection) GetRandomPrivateCidrBlock(prefix int) string } func (r *RandomResourceCollection) GetRandomVpc() (aws.Vpc, error) { - return aws.GetRandomVpc(r.AwsRegion) + return aws.GetDefaultVpc(r.AwsRegion) } diff --git a/rand_resources_test.go b/rand_resources_test.go index d174cef14..219a969d9 100644 --- a/rand_resources_test.go +++ b/rand_resources_test.go @@ -121,7 +121,7 @@ func TestAllParametersSet(t *testing.T) { } } -func TestGetRandomVpc(t *testing.T) { +func TestGetDefaultVpc(t *testing.T) { t.Parallel() ro := NewRandomResourceCollectionOptions() From 00e565fb03ef3934784a199000cb261e3921a1a5 Mon Sep 17 00:00:00 2001 From: Yevgeniy Brikman Date: Fri, 1 Apr 2016 11:48:08 +0200 Subject: [PATCH 2/2] Rename API in terratest package too --- rand_resources.go | 2 +- rand_resources_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rand_resources.go b/rand_resources.go index c4636d73a..fb0dede1c 100644 --- a/rand_resources.go +++ b/rand_resources.go @@ -100,6 +100,6 @@ func (r *RandomResourceCollection) GetRandomPrivateCidrBlock(prefix int) string return util.GetRandomPrivateCidrBlock(prefix) } -func (r *RandomResourceCollection) GetRandomVpc() (aws.Vpc, error) { +func (r *RandomResourceCollection) GetDefaultVpc() (aws.Vpc, error) { return aws.GetDefaultVpc(r.AwsRegion) } diff --git a/rand_resources_test.go b/rand_resources_test.go index 219a969d9..14ee00ca3 100644 --- a/rand_resources_test.go +++ b/rand_resources_test.go @@ -130,7 +130,7 @@ func TestGetDefaultVpc(t *testing.T) { t.Fatalf("Failed to create RandomResourceCollection: %s", err.Error()) } - vpc, err := rand.GetRandomVpc() + vpc, err := rand.GetDefaultVpc() if err != nil { t.Fatalf("Failed to get random VPC: %s", err.Error()) }