Make concurrent AWS test runs safer

Instead of cleaning up all resources that might have been
created by the test suite, we now only cleanup resources
created by this specific run of the tests.  That makes
concurrent test execution safer, and the tests more self-
contained.  However, it does increase the chances of
accidentially leaving behind resources when tests fail.

Fixes #265.
This commit is contained in:
Luke Hoban 2017-06-23 15:50:58 -07:00
parent 3c1041af49
commit e663b90ce7
3 changed files with 43 additions and 43 deletions

View file

@ -19,27 +19,28 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/aws/aws-sdk-go/aws"
awsdynamodb "github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/pulumi/lumi/lib/aws/provider/awsctx"
"github.com/pulumi/lumi/lib/aws/provider/testutil"
"github.com/pulumi/lumi/lib/aws/rpc/dynamodb"
"github.com/pulumi/lumi/pkg/resource"
"github.com/stretchr/testify/assert"
)
const RESOURCEPREFIX = "lumitest"
func Test(t *testing.T) {
t.Parallel()
prefix := resource.NewUniqueHex("lumitest", 20, 20)
ctx := testutil.CreateContext(t)
err := cleanup(ctx)
assert.Nil(t, err)
defer func() {
err := cleanup(prefix, ctx)
assert.Nil(t, err)
}()
testutil.ProviderTestSimple(t, NewTableProvider(ctx), TableToken, []interface{}{
&dynamodb.Table{
Name: aws.String(RESOURCEPREFIX),
Name: aws.String(prefix),
Attributes: []dynamodb.Attribute{
{Name: "Album", Type: "S"},
{Name: "Artist", Type: "S"},
@ -62,7 +63,7 @@ func Test(t *testing.T) {
},
},
&dynamodb.Table{
Name: aws.String(RESOURCEPREFIX),
Name: aws.String(prefix),
Attributes: []dynamodb.Attribute{
{Name: "Album", Type: "S"},
{Name: "Artist", Type: "S"},
@ -97,15 +98,15 @@ func Test(t *testing.T) {
})
}
func cleanup(ctx *awsctx.Context) error {
fmt.Printf("Cleaning up tables with prefix: %v\n", RESOURCEPREFIX)
func cleanup(prefix string, ctx *awsctx.Context) error {
fmt.Printf("Cleaning up tables with prefix: %v\n", prefix)
list, err := ctx.DynamoDB().ListTables(&awsdynamodb.ListTablesInput{})
if err != nil {
return err
}
cleaned := 0
for _, table := range list.TableNames {
if strings.HasPrefix(aws.StringValue(table), RESOURCEPREFIX) {
if strings.HasPrefix(aws.StringValue(table), prefix) {
if _, delerr := ctx.DynamoDB().DeleteTable(&awsdynamodb.DeleteTableInput{
TableName: table,
}); delerr != nil {

View file

@ -23,10 +23,9 @@ import (
"github.com/pulumi/lumi/lib/aws/provider/awsctx"
"github.com/pulumi/lumi/lib/aws/provider/testutil"
"github.com/pulumi/lumi/lib/aws/rpc/ec2"
"github.com/pulumi/lumi/pkg/resource"
)
const RESOURCEPREFIX = "lumitest"
var amis = map[string]string{
"us-east-1": "ami-6869aa05",
"us-west-2": "ami-7172b611",
@ -48,28 +47,28 @@ var amis = map[string]string{
func Test(t *testing.T) {
t.Parallel()
prefix := resource.NewUniqueHex("lumitest", 20, 20)
ctx := testutil.CreateContext(t)
cleanup(ctx)
defer cleanup(prefix, ctx)
instanceType := ec2.InstanceType("t2.nano")
testutil.ProviderTestSimple(t, NewInstanceProvider(ctx), InstanceToken, []interface{}{
&ec2.Instance{
Name: aws.String(RESOURCEPREFIX),
Name: aws.String(prefix),
InstanceType: &instanceType,
ImageID: amis[ctx.Region()],
Tags: &[]ec2.Tag{{
Key: RESOURCEPREFIX,
Value: RESOURCEPREFIX,
Key: prefix,
Value: prefix,
}},
},
&ec2.Instance{
Name: aws.String(RESOURCEPREFIX),
Name: aws.String(prefix),
InstanceType: &instanceType,
ImageID: amis[ctx.Region()],
Tags: &[]ec2.Tag{{
Key: RESOURCEPREFIX,
Value: RESOURCEPREFIX,
Key: prefix,
Value: prefix,
}, {
Key: "Hello",
Value: "World",
@ -79,12 +78,12 @@ func Test(t *testing.T) {
}
func cleanup(ctx *awsctx.Context) {
fmt.Printf("Cleaning up instances with tag:%v=%v\n", RESOURCEPREFIX, RESOURCEPREFIX)
func cleanup(prefix string, ctx *awsctx.Context) {
fmt.Printf("Cleaning up instances with tag:%v=%v\n", prefix, prefix)
list, err := ctx.EC2().DescribeInstances(&awsec2.DescribeInstancesInput{
Filters: []*awsec2.Filter{{
Name: aws.String("tag:" + RESOURCEPREFIX),
Values: []*string{aws.String(RESOURCEPREFIX)},
Name: aws.String("tag:" + prefix),
Values: []*string{aws.String(prefix)},
}},
})
if err != nil {

View file

@ -19,8 +19,6 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/aws/aws-sdk-go/aws"
awsiam "github.com/aws/aws-sdk-go/service/iam"
awslambda "github.com/aws/aws-sdk-go/service/lambda"
@ -31,18 +29,20 @@ import (
"github.com/pulumi/lumi/lib/aws/rpc/iam"
"github.com/pulumi/lumi/lib/aws/rpc/lambda"
"github.com/pulumi/lumi/pkg/resource"
"github.com/stretchr/testify/assert"
)
const RESOURCEPREFIX = "lumitest"
func Test(t *testing.T) {
t.Parallel()
prefix := resource.NewUniqueHex("lumitest", 20, 20)
ctx := testutil.CreateContext(t)
funcerr := cleanupFunctions(ctx)
assert.Nil(t, funcerr)
roleerr := cleanupRoles(ctx)
assert.Nil(t, roleerr)
defer func() {
funcerr := cleanupFunctions(prefix, ctx)
assert.Nil(t, funcerr)
roleerr := cleanupRoles(prefix, ctx)
assert.Nil(t, roleerr)
}()
sourceARN := rpc.ARN("arn:aws:s3:::elasticbeanstalk-us-east-1-111111111111")
@ -57,7 +57,7 @@ func Test(t *testing.T) {
Name: "role",
Creator: func(ctx testutil.Context) interface{} {
return &iam.Role{
Name: aws.String(RESOURCEPREFIX),
Name: aws.String(prefix),
ManagedPolicyARNs: &[]rpc.ARN{
rpc.ARN("arn:aws:iam::aws:policy/AWSLambdaFullAccess"),
},
@ -81,7 +81,7 @@ func Test(t *testing.T) {
Name: "f",
Creator: func(ctx testutil.Context) interface{} {
return &lambda.Function{
Name: aws.String(RESOURCEPREFIX),
Name: aws.String(prefix),
Code: resource.Archive{
Assets: &map[string]*resource.Asset{
"index.js": {
@ -99,7 +99,7 @@ func Test(t *testing.T) {
Name: "permission",
Creator: func(ctx testutil.Context) interface{} {
return &lambda.Permission{
Name: aws.String(RESOURCEPREFIX),
Name: aws.String(prefix),
Function: ctx.GetResourceID("f"),
Action: "lambda:InvokeFunction",
Principal: "s3.amazonaws.com",
@ -115,19 +115,19 @@ func Test(t *testing.T) {
}
func cleanupFunctions(ctx *awsctx.Context) error {
fmt.Printf("Cleaning up function with name:%v\n", RESOURCEPREFIX)
func cleanupFunctions(prefix string, ctx *awsctx.Context) error {
fmt.Printf("Cleaning up function with name:%v\n", prefix)
list, err := ctx.Lambda().ListFunctions(&awslambda.ListFunctionsInput{})
if err != nil {
return err
}
cleaned := 0
for _, fnc := range list.Functions {
if strings.HasPrefix(aws.StringValue(fnc.FunctionName), RESOURCEPREFIX) {
if strings.HasPrefix(aws.StringValue(fnc.FunctionName), prefix) {
if _, delerr := ctx.Lambda().DeleteFunction(&awslambda.DeleteFunctionInput{
FunctionName: fnc.FunctionName,
}); delerr != nil {
fmt.Printf("Unable to cleanip function %v: %v\n", fnc.FunctionName, delerr)
fmt.Printf("Unable to cleanup function %v: %v\n", fnc.FunctionName, delerr)
return delerr
}
cleaned++
@ -137,15 +137,15 @@ func cleanupFunctions(ctx *awsctx.Context) error {
return nil
}
func cleanupRoles(ctx *awsctx.Context) error {
fmt.Printf("Cleaning up roles with name:%v\n", RESOURCEPREFIX)
func cleanupRoles(prefix string, ctx *awsctx.Context) error {
fmt.Printf("Cleaning up roles with name:%v\n", prefix)
list, err := ctx.IAM().ListRoles(&awsiam.ListRolesInput{})
if err != nil {
return err
}
cleaned := 0
for _, role := range list.Roles {
if strings.HasPrefix(aws.StringValue(role.RoleName), RESOURCEPREFIX) {
if strings.HasPrefix(aws.StringValue(role.RoleName), prefix) {
policies, err := ctx.IAM().ListAttachedRolePolicies(&awsiam.ListAttachedRolePoliciesInput{
RoleName: role.RoleName,
})