Skip to content

Commit

Permalink
Add aws_route53_zone acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jckuester committed Mar 28, 2018
1 parent 469ad6c commit 575bd40
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 10 deletions.
200 changes: 200 additions & 0 deletions test/route53_zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package test

import (
"fmt"
"os"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/cloudetc/awsweeper/command"
res "github.com/cloudetc/awsweeper/resource"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/spf13/afero"
)

func TestAccRoute53Zone_deleteByTags(t *testing.T) {
var zone1, zone2 route53.HostedZone

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccRoute53ZoneConfig,
ExpectNonEmptyPlan: true,
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53ZoneExists("aws_vpc.foo", &zone1),
testAccCheckRoute53ZoneExists("aws_vpc.bar", &zone2),
testMainTags(argsDryRun, testAccRoute53ZoneAWSweeperTagsConfig),
testRoute53ZoneExists(&zone1),
testRoute53ZoneExists(&zone2),
testMainTags(argsForceDelete, testAccRoute53ZoneAWSweeperTagsConfig),
testRoute53ZoneDeleted(&zone1),
testRoute53ZoneExists(&zone2),
),
},
},
})
}

func TestAccRoute53Zone_deleteByIds(t *testing.T) {
var zone1, zone2 route53.HostedZone

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccRoute53ZoneConfig,
ExpectNonEmptyPlan: true,
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53ZoneExists("aws_vpc.foo", &zone1),
testAccCheckRoute53ZoneExists("aws_vpc.bar", &zone2),
testMainRoute53ZoneIds(argsDryRun, &zone1),
testRoute53ZoneExists(&zone1),
testRoute53ZoneExists(&zone2),
testMainRoute53ZoneIds(argsForceDelete, &zone1),
testRoute53ZoneDeleted(&zone1),
testRoute53ZoneExists(&zone2),
),
},
},
})
}

func testAccCheckRoute53ZoneExists(n string, vpc *route53.HostedZone) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

conn := client.r53conn
desc := &route53.GetHostedZoneInput{
Id: aws.String(rs.Primary.ID),
}
resp, err := conn.GetHostedZone(desc)
if err != nil {
return err
}
if len(resp.HostedZone.Id) == 0 {
return fmt.Errorf("VPC not found")
}

*vpc = *resp.HostedZone

return nil
}
}

func testMainRoute53ZoneIds(args []string, z *route53.HostedZone) resource.TestCheckFunc {
return func(s *terraform.State) error {
res.AppFs = afero.NewMemMapFs()
afero.WriteFile(res.AppFs, "config.yml", []byte(testAccRoute53ZoneAWSweeperIdsConfig(z)), 0644)
os.Args = args

command.WrappedMain()
return nil
}
}

func testRoute53ZoneExists(z *route53.HostedZone) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := client.r53conn
desc := &route53.DescribeRoute53ZonesInput{
Route53ZoneIds: []*string{z.Route53ZoneId},
}
resp, err := conn.DescribeRoute53Zones(desc)
if err != nil {
return err
}
if len(resp.Route53Zones) == 0 {
return fmt.Errorf("VPC has been deleted")
}

return nil
}
}

func testRoute53ZoneDeleted(z *route53.HostedZone) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := client.r53conn
desc := &route53.DescribeRoute53ZonesInput{
Route53ZoneIds: []*string{z.Route53ZoneId},
}
resp, err := conn.DescribeRoute53Zones(desc)
if err != nil {
route53err, ok := err.(awserr.Error)
if !ok {
return err
}
if route53err.Code() == "InvalidRoute53ZoneID.NotFound" {
return nil
}
return err
}

if len(resp.Route53Zones) != 0 {
return fmt.Errorf("VPC hasn't been deleted")

}

return nil
}
}

const testAccRoute53ZoneConfig = `
resource "aws_route53_zone" "foo" {
name = "foo.com"
tags {
foo = "bar"
Name = "awsweeper-testacc"
}
}
resource "aws_route53_zone" "bar" {
name = "bar.com"
tags {
bar = "baz"
Name = "awsweeper-testacc"
}
}
resource "aws_route53_record" "foo" {
zone_id = "${aws_route53_zone.foo.zone_id}"
name = "bar".com"
type = "NS"
ttl = "30"
records = [
"${aws_route53_zone.bar.name_servers.0}",
"${aws_route53_zone.bar.name_servers.1}",
"${aws_route53_zone.bar.name_servers.2}",
"${aws_route53_zone.bar.name_servers.3}",
]
}
`

const testAccRoute53ZoneAWSweeperTagsConfig = `
aws_route53_zone:
tags:
foo: bar
`

func testAccRoute53ZoneAWSweeperIdsConfig(z *route53.HostedZone) string {
id := z.Id
return fmt.Sprintf(`
aws_route53_zone:
ids:
- %s
`, *id)
}
20 changes: 10 additions & 10 deletions test/vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,22 @@ func TestAccVpc_deleteByIds(t *testing.T) {
})
}

func testAccCheckVpcExists(n string, vpc *ec2.Vpc) resource.TestCheckFunc {
func testAccCheckVpcExists(name string, vpc *ec2.Vpc) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", n)
return fmt.Errorf("Not found: %s", name)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No VPC ID is set")
return fmt.Errorf("No ID is set")
}

conn := client.ec2conn
DescribeVpcOpts := &ec2.DescribeVpcsInput{
desc := &ec2.DescribeVpcsInput{
VpcIds: []*string{aws.String(rs.Primary.ID)},
}
resp, err := conn.DescribeVpcs(DescribeVpcOpts)
resp, err := conn.DescribeVpcs(desc)
if err != nil {
return err
}
Expand Down Expand Up @@ -108,10 +108,10 @@ func testMainVpcIds(args []string, vpc *ec2.Vpc) resource.TestCheckFunc {
func testVpcExists(vpc *ec2.Vpc) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := client.ec2conn
DescribeVpcOpts := &ec2.DescribeVpcsInput{
desc := &ec2.DescribeVpcsInput{
VpcIds: []*string{vpc.VpcId},
}
resp, err := conn.DescribeVpcs(DescribeVpcOpts)
resp, err := conn.DescribeVpcs(desc)
if err != nil {
return err
}
Expand All @@ -126,10 +126,10 @@ func testVpcExists(vpc *ec2.Vpc) resource.TestCheckFunc {
func testVpcDeleted(vpc *ec2.Vpc) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := client.ec2conn
DescribeVpcOpts := &ec2.DescribeVpcsInput{
desc := &ec2.DescribeVpcsInput{
VpcIds: []*string{vpc.VpcId},
}
resp, err := conn.DescribeVpcs(DescribeVpcOpts)
resp, err := conn.DescribeVpcs(desc)
if err != nil {
ec2err, ok := err.(awserr.Error)
if !ok {
Expand Down

0 comments on commit 575bd40

Please sign in to comment.