Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds TestListNatGatewaysWithConfigFile
Browse files Browse the repository at this point in the history
Giampaolo Falqui authored and Giampaolo Falqui committed Jun 8, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 4d808ef commit 86ac910
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions aws/nat_gateway_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package aws

import (
"regexp"
"testing"
"time"

"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/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/util"
"github.com/gruntwork-io/go-commons/errors"
terraws "github.com/gruntwork-io/terratest/modules/aws"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -31,6 +34,51 @@ func TestListNatGateways(t *testing.T) {
assert.Contains(t, aws.StringValueSlice(natGatewayIDs), aws.StringValue(ngwID))
}

func createNatGatewayWithName(t *testing.T, svc *ec2.EC2, region string, name string) *string {
ngwID := createNatGateway(t, svc, region)

err := setTagsToResource(t, svc, ngwID, []*ec2.Tag{
{
Key: aws.String("Name"),
Value: aws.String(name),
},
})
require.NoError(t, err)

return ngwID
}

func TestListNatGatewaysWithConfigFile(t *testing.T) {
t.Parallel()

region, err := getRandomRegion()
require.NoError(t, err)

session, err := session.NewSession(&aws.Config{Region: aws.String(region)})
require.NoError(t, err)
svc := ec2.New(session)

includedNatGatewayName := "cloud-nuke-test-include-" + util.UniqueID()
excludedNatGatewayName := "cloud-nuke-test-" + util.UniqueID()
includedNatGatewayID := createNatGatewayWithName(t, svc, region, includedNatGatewayName)
excludedNatGatewayID := createNatGatewayWithName(t, svc, region, excludedNatGatewayName)
defer nukeAllNatGateways(session, []*string{includedNatGatewayID, excludedNatGatewayID})

nateGatewayIds, err := getAllNatGateways(session, time.Now().Add(1*time.Hour), config.Config{
NatGateway: config.ResourceType{
IncludeRule: config.FilterRule{
NamesRegExp: []config.Expression{
{RE: *regexp.MustCompile("^cloud-nuke-test-include-.*")},
},
},
},
})

require.NoError(t, err)
require.Equal(t, 1, len(nateGatewayIds))
require.Equal(t, aws.StringValue(includedNatGatewayID), aws.StringValue(nateGatewayIds[0]))
}

func TestTimeFilterExclusionNewlyCreatedNatGateway(t *testing.T) {
t.Parallel()

@@ -108,21 +156,31 @@ func TestNukeNatGatewayMoreThanOne(t *testing.T) {
assertNatGatewaysDeleted(t, svc, natGateways)
}

// Helper functions for driving the NAT gateway tests
// Helper functions for driving the NAT gateway testsŚ
func setTagsToResource(t *testing.T, svc *ec2.EC2, resourceId *string, tags []*ec2.Tag) error {
_, err := svc.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{resourceId},
Tags: tags,
})
return err
}

// createNatGateway will create a new NAT gateway in the default VPC
func createNatGateway(t *testing.T, svc *ec2.EC2, region string) *string {
defaultVpc := terraws.GetDefaultVpc(t, region)
subnet := defaultVpc.Subnets[0]

resp, err := svc.CreateNatGateway(&ec2.CreateNatGatewayInput{
SubnetId: aws.String(subnet.Id),
ConnectivityType: aws.String(ec2.ConnectivityTypePrivate),
SubnetId: aws.String(subnet.Id),
ConnectivityType: aws.String(ec2.ConnectivityTypePrivate),
})
require.NoError(t, err)
if err != nil {
assert.Failf(t, "Could not create test EBS volume", errors.WithStackTrace(err).Error())
}
if resp.NatGateway == nil {
t.Fatalf("Impossible error: AWS returned nil NAT gateway")
}

return resp.NatGateway.NatGatewayId
}

0 comments on commit 86ac910

Please sign in to comment.