Skip to content

Commit

Permalink
Move unique id function to separate go package file
Browse files Browse the repository at this point in the history
  • Loading branch information
tonerdo committed Feb 6, 2018
1 parent a4048ef commit e913e17
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 31 deletions.
5 changes: 3 additions & 2 deletions aws/asg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gruntwork-io/aws-nuke/util"
"github.com/gruntwork-io/gruntwork-cli/errors"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ func TestListAutoScalingGroups(t *testing.T) {
assert.Fail(t, errors.WithStackTrace(err).Error())
}

groupName := "aws-nuke-test-" + uniqueID()
groupName := "aws-nuke-test-" + util.UniqueID()
createTestAutoScalingGroup(t, session, groupName)

groupNames, err := getAllAutoScalingGroups(session, region)
Expand All @@ -86,7 +87,7 @@ func TestNukeAutoScalingGroups(t *testing.T) {
assert.Fail(t, errors.WithStackTrace(err).Error())
}

groupName := "aws-nuke-test-" + uniqueID()
groupName := "aws-nuke-test-" + util.UniqueID()
createTestAutoScalingGroup(t, session, groupName)

_, err = svc.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{
Expand Down
5 changes: 3 additions & 2 deletions aws/ebs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gruntwork-io/aws-nuke/util"
"github.com/gruntwork-io/gruntwork-cli/errors"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -73,7 +74,7 @@ func TestListEBSVolumes(t *testing.T) {
assert.Fail(t, errors.WithStackTrace(err).Error())
}

uniqueTestID := "aws-nuke-test-" + uniqueID()
uniqueTestID := "aws-nuke-test-" + util.UniqueID()
volume := createTestEBSVolume(t, session, uniqueTestID)

volumeIds, err := getAllEbsVolumes(session, region)
Expand All @@ -96,7 +97,7 @@ func TestNukeEBSVolumes(t *testing.T) {
assert.Fail(t, errors.WithStackTrace(err).Error())
}

uniqueTestID := "aws-nuke-test-" + uniqueID()
uniqueTestID := "aws-nuke-test-" + util.UniqueID()
createTestEC2Instance(t, session, uniqueTestID)

output, err := ec2.New(session).DescribeVolumes(&ec2.DescribeVolumesInput{})
Expand Down
26 changes: 3 additions & 23 deletions aws/ec2_test.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
package aws

import (
"bytes"
"math/rand"
"testing"
"time"

awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gruntwork-io/aws-nuke/util"
"github.com/gruntwork-io/gruntwork-cli/errors"
"github.com/stretchr/testify/assert"
)

// Returns a unique (ish) id we can attach to resources and tfstate files so they don't conflict with each other
// Uses base 62 to generate a 6 character string that's unlikely to collide with the handful of tests we run in
// parallel. Based on code here: http://stackoverflow.com/a/9543797/483528
func uniqueID() string {

const BASE_62_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const UNIQUE_ID_LENGTH = 6 // Should be good for 62^6 = 56+ billion combinations

var out bytes.Buffer

rand := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < UNIQUE_ID_LENGTH; i++ {
out.WriteByte(BASE_62_CHARS[rand.Intn(len(BASE_62_CHARS))])
}

return out.String()
}

func getLinuxAmiIDForRegion(region string) string {
switch region {
case "us-east-1":
Expand Down Expand Up @@ -147,7 +127,7 @@ func TestListInstances(t *testing.T) {
assert.Fail(t, errors.WithStackTrace(err).Error())
}

uniqueTestID := "aws-nuke-test-" + uniqueID()
uniqueTestID := "aws-nuke-test-" + util.UniqueID()
instance := createTestEC2Instance(t, session, uniqueTestID)
instanceIds, err := getAllEc2Instances(session, region)

Expand All @@ -170,7 +150,7 @@ func TestNukeInstances(t *testing.T) {
assert.Fail(t, errors.WithStackTrace(err).Error())
}

uniqueTestID := "aws-nuke-test-" + uniqueID()
uniqueTestID := "aws-nuke-test-" + util.UniqueID()
createTestEC2Instance(t, session, uniqueTestID)

output, err := ec2.New(session).DescribeInstances(&ec2.DescribeInstancesInput{})
Expand Down
5 changes: 3 additions & 2 deletions aws/elb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/gruntwork-io/aws-nuke/util"
"github.com/gruntwork-io/gruntwork-cli/errors"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -45,7 +46,7 @@ func TestListELBs(t *testing.T) {
assert.Fail(t, errors.WithStackTrace(err).Error())
}

elbName := "aws-nuke-test-" + uniqueID()
elbName := "aws-nuke-test-" + util.UniqueID()
createTestELB(t, session, elbName)

elbNames, err := getAllElbInstances(session, region)
Expand All @@ -69,7 +70,7 @@ func TestNukeELBs(t *testing.T) {
assert.Fail(t, errors.WithStackTrace(err).Error())
}

elbName := "aws-nuke-test-" + uniqueID()
elbName := "aws-nuke-test-" + util.UniqueID()
createTestELB(t, session, elbName)

_, err = svc.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{
Expand Down
5 changes: 3 additions & 2 deletions aws/elbv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/gruntwork-io/aws-nuke/util"
"github.com/gruntwork-io/gruntwork-cli/errors"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestListELBv2(t *testing.T) {
assert.Fail(t, errors.WithStackTrace(err).Error())
}

elbName := "aws-nuke-test-" + uniqueID()
elbName := "aws-nuke-test-" + util.UniqueID()
balancer := createTestELBv2(t, session, elbName)

arns, err := getAllElbv2Instances(session, region)
Expand All @@ -93,7 +94,7 @@ func TestNukeELBv2(t *testing.T) {
assert.Fail(t, errors.WithStackTrace(err).Error())
}

elbName := "aws-nuke-test-" + uniqueID()
elbName := "aws-nuke-test-" + util.UniqueID()
balancer := createTestELBv2(t, session, elbName)

_, err = svc.DescribeLoadBalancers(&elbv2.DescribeLoadBalancersInput{
Expand Down
25 changes: 25 additions & 0 deletions util/unique_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package util

import (
"bytes"
"math/rand"
"time"
)

// Returns a unique (ish) id we can attach to resources and tfstate files so they don't conflict with each other
// Uses base 62 to generate a 6 character string that's unlikely to collide with the handful of tests we run in
// parallel. Based on code here: http://stackoverflow.com/a/9543797/483528
func UniqueID() string {

const BASE_62_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const UNIQUE_ID_LENGTH = 6 // Should be good for 62^6 = 56+ billion combinations

var out bytes.Buffer

rand := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < UNIQUE_ID_LENGTH; i++ {
out.WriteByte(BASE_62_CHARS[rand.Intn(len(BASE_62_CHARS))])
}

return out.String()
}

0 comments on commit e913e17

Please sign in to comment.