Skip to content

Commit

Permalink
Merge pull request #3545 from terraform-providers/t-aws_nat_gateway-tags
Browse files Browse the repository at this point in the history
tests: Add sweepers for aws_internet_gateway and aws_nat_gateway
  • Loading branch information
radeksimko authored Feb 28, 2018
2 parents 7a3a64b + fc31786 commit fefe347
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
4 changes: 4 additions & 0 deletions aws/data_source_aws_nat_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ resource "aws_nat_gateway" "test" {
subnet_id = "${aws_subnet.test.id}"
allocation_id = "${aws_eip.test.id}"
tags {
Name = "terraform-testacc-nat-gw-data-source"
}
depends_on = ["aws_internet_gateway.test"]
}
Expand Down
4 changes: 4 additions & 0 deletions aws/import_aws_route_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ resource "aws_nat_gateway" "nat" {
count = "${length(split(",", var.private_subnet_cidrs))}"
allocation_id = "${element(aws_eip.nat.*.id, count.index)}"
subnet_id = "${aws_subnet.tf_test_subnet.id}"
tags {
Name = "terraform-testacc-route-table-import-complex-default"
}
}
resource "aws_route_table" "mod" {
Expand Down
59 changes: 59 additions & 0 deletions aws/resource_aws_internet_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"log"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -11,6 +12,54 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func init() {
resource.AddTestSweepers("aws_internet_gateway", &resource.Sweeper{
Name: "aws_internet_gateway",
F: testSweepInternetGateways,
})
}

func testSweepInternetGateways(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
conn := client.(*AWSClient).ec2conn

req := &ec2.DescribeInternetGatewaysInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag-value"),
Values: []*string{
aws.String("terraform-testacc-*"),
},
},
},
}
resp, err := conn.DescribeInternetGateways(req)
if err != nil {
return fmt.Errorf("Error describing Internet Gateways: %s", err)
}

if len(resp.InternetGateways) == 0 {
log.Print("[DEBUG] No AWS Internet Gateways to sweep")
return nil
}

for _, internetGateway := range resp.InternetGateways {
_, err := conn.DeleteInternetGateway(&ec2.DeleteInternetGatewayInput{
InternetGatewayId: internetGateway.InternetGatewayId,
})
if err != nil {
return fmt.Errorf(
"Error deleting Internet Gateway (%s): %s",
*internetGateway.InternetGatewayId, err)
}
}

return nil
}

func TestAccAWSInternetGateway_basic(t *testing.T) {
var v, v2 ec2.InternetGateway

Expand Down Expand Up @@ -102,6 +151,7 @@ func TestAccAWSInternetGateway_tags(t *testing.T) {
Config: testAccCheckInternetGatewayConfigTags,
Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
testAccCheckTags(&v.Tags, "Name", "terraform-testacc-internet-gateway-tags"),
testAccCheckTags(&v.Tags, "foo", "bar"),
),
},
Expand All @@ -110,6 +160,7 @@ func TestAccAWSInternetGateway_tags(t *testing.T) {
Config: testAccCheckInternetGatewayConfigTagsUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
testAccCheckTags(&v.Tags, "Name", "terraform-testacc-internet-gateway-tags"),
testAccCheckTags(&v.Tags, "foo", ""),
testAccCheckTags(&v.Tags, "bar", "baz"),
),
Expand Down Expand Up @@ -198,6 +249,9 @@ resource "aws_vpc" "foo" {
resource "aws_internet_gateway" "foo" {
vpc_id = "${aws_vpc.foo.id}"
tags {
Name = "terraform-testacc-internet-gateway"
}
}
`

Expand All @@ -218,6 +272,9 @@ resource "aws_vpc" "bar" {
resource "aws_internet_gateway" "foo" {
vpc_id = "${aws_vpc.bar.id}"
tags {
Name = "terraform-testacc-internet-gateway-change-vpc-other"
}
}
`

Expand All @@ -232,6 +289,7 @@ resource "aws_vpc" "foo" {
resource "aws_internet_gateway" "foo" {
vpc_id = "${aws_vpc.foo.id}"
tags {
Name = "terraform-testacc-internet-gateway-tags"
foo = "bar"
}
}
Expand All @@ -248,6 +306,7 @@ resource "aws_vpc" "foo" {
resource "aws_internet_gateway" "foo" {
vpc_id = "${aws_vpc.foo.id}"
tags {
Name = "terraform-testacc-internet-gateway-tags"
bar = "baz"
}
}
Expand Down
57 changes: 57 additions & 0 deletions aws/resource_aws_nat_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"log"
"strings"
"testing"

Expand All @@ -12,6 +13,54 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func init() {
resource.AddTestSweepers("aws_nat_gateway", &resource.Sweeper{
Name: "aws_nat_gateway",
F: testSweepNatGateways,
})
}

func testSweepNatGateways(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
conn := client.(*AWSClient).ec2conn

req := &ec2.DescribeNatGatewaysInput{
Filter: []*ec2.Filter{
{
Name: aws.String("tag-value"),
Values: []*string{
aws.String("terraform-testacc-*"),
},
},
},
}
resp, err := conn.DescribeNatGateways(req)
if err != nil {
return fmt.Errorf("Error describing NAT Gateways: %s", err)
}

if len(resp.NatGateways) == 0 {
log.Print("[DEBUG] No AWS NAT Gateways to sweep")
return nil
}

for _, natGateway := range resp.NatGateways {
_, err := conn.DeleteNatGateway(&ec2.DeleteNatGatewayInput{
NatGatewayId: natGateway.NatGatewayId,
})
if err != nil {
return fmt.Errorf(
"Error deleting NAT Gateway (%s): %s",
*natGateway.NatGatewayId, err)
}
}

return nil
}

func TestAccAWSNatGateway_basic(t *testing.T) {
var natGateway ec2.NatGateway

Expand Down Expand Up @@ -43,6 +92,7 @@ func TestAccAWSNatGateway_tags(t *testing.T) {
Config: testAccNatGatewayConfigTags,
Check: resource.ComposeTestCheckFunc(
testAccCheckNatGatewayExists("aws_nat_gateway.gateway", &natGateway),
testAccCheckTags(&natGateway.Tags, "Name", "terraform-testacc-nat-gw-tags"),
testAccCheckTags(&natGateway.Tags, "foo", "bar"),
),
},
Expand All @@ -51,6 +101,7 @@ func TestAccAWSNatGateway_tags(t *testing.T) {
Config: testAccNatGatewayConfigTagsUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckNatGatewayExists("aws_nat_gateway.gateway", &natGateway),
testAccCheckTags(&natGateway.Tags, "Name", "terraform-testacc-nat-gw-tags"),
testAccCheckTags(&natGateway.Tags, "foo", ""),
testAccCheckTags(&natGateway.Tags, "bar", "baz"),
),
Expand Down Expand Up @@ -158,6 +209,10 @@ resource "aws_nat_gateway" "gateway" {
allocation_id = "${aws_eip.nat_gateway.id}"
subnet_id = "${aws_subnet.public.id}"
tags {
Name = "terraform-testacc-nat-gw-basic"
}
depends_on = ["aws_internet_gateway.gw"]
}
Expand Down Expand Up @@ -224,6 +279,7 @@ resource "aws_nat_gateway" "gateway" {
subnet_id = "${aws_subnet.public.id}"
tags {
Name = "terraform-testacc-nat-gw-tags"
foo = "bar"
}
Expand Down Expand Up @@ -293,6 +349,7 @@ resource "aws_nat_gateway" "gateway" {
subnet_id = "${aws_subnet.public.id}"
tags {
Name = "terraform-testacc-nat-gw-tags"
bar = "baz"
}
Expand Down
2 changes: 2 additions & 0 deletions aws/resource_aws_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func init() {
resource.AddTestSweepers("aws_vpc", &resource.Sweeper{
Name: "aws_vpc",
Dependencies: []string{
"aws_internet_gateway",
"aws_nat_gateway",
"aws_network_acl",
"aws_security_group",
"aws_subnet",
Expand Down

0 comments on commit fefe347

Please sign in to comment.