terraform/builtin/providers/aws/resource_aws_route53_zone_t...

170 lines
3.8 KiB
Go
Raw Normal View History

package aws
import (
"fmt"
2015-04-23 15:48:19 +02:00
"sort"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
2015-04-16 20:42:16 +02:00
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/route53"
)
func TestCleanPrefix(t *testing.T) {
cases := []struct {
Input, Prefix, Output string
}{
{"/hostedzone/foo", "/hostedzone/", "foo"},
{"/change/foo", "/change/", "foo"},
{"/bar", "/test", "/bar"},
}
for _, tc := range cases {
actual := cleanPrefix(tc.Input, tc.Prefix)
if actual != tc.Output {
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
}
}
}
func TestCleanZoneID(t *testing.T) {
cases := []struct {
Input, Output string
}{
{"/hostedzone/foo", "foo"},
{"/change/foo", "/change/foo"},
{"/bar", "/bar"},
}
for _, tc := range cases {
actual := cleanZoneID(tc.Input)
if actual != tc.Output {
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
}
}
}
func TestCleanChangeID(t *testing.T) {
cases := []struct {
Input, Output string
}{
{"/hostedzone/foo", "/hostedzone/foo"},
{"/change/foo", "foo"},
{"/bar", "/bar"},
}
for _, tc := range cases {
actual := cleanChangeID(tc.Input)
if actual != tc.Output {
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
}
}
}
func TestAccRoute53Zone(t *testing.T) {
var zone route53.HostedZone
var td route53.ResourceTagSet
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRoute53ZoneDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRoute53ZoneConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone),
testAccLoadTagsR53(&zone, &td),
testAccCheckTagsR53(&td.Tags, "foo", "bar"),
),
},
},
})
}
func testAccCheckRoute53ZoneDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).r53conn
2014-09-17 02:44:42 +02:00
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_route53_zone" {
continue
}
2015-04-16 20:42:16 +02:00
_, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)})
if err == nil {
return fmt.Errorf("Hosted zone still exists")
}
}
return nil
}
func testAccCheckRoute53ZoneExists(n string, zone *route53.HostedZone) resource.TestCheckFunc {
return func(s *terraform.State) error {
2014-09-17 02:44:42 +02:00
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
2014-09-17 02:44:42 +02:00
if rs.Primary.ID == "" {
return fmt.Errorf("No hosted zone ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).r53conn
2015-04-16 20:42:16 +02:00
resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)})
if err != nil {
return fmt.Errorf("Hosted zone err: %v", err)
}
2015-04-14 21:17:01 +02:00
2015-04-23 15:48:19 +02:00
sorted_ns := make([]string, len(resp.DelegationSet.NameServers))
for i, ns := range resp.DelegationSet.NameServers {
sorted_ns[i] = *ns
}
sort.Strings(sorted_ns)
for idx, ns := range sorted_ns {
attribute := fmt.Sprintf("name_servers.%d", idx)
2015-04-14 21:17:01 +02:00
dsns := rs.Primary.Attributes[attribute]
2015-04-23 15:48:19 +02:00
if dsns != ns {
2015-04-14 21:17:01 +02:00
return fmt.Errorf("Got: %v for %v, Expected: %v", dsns, attribute, ns)
}
}
*zone = *resp.HostedZone
return nil
}
}
func testAccLoadTagsR53(zone *route53.HostedZone, td *route53.ResourceTagSet) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).r53conn
zone := cleanZoneID(*zone.ID)
2015-04-16 20:42:16 +02:00
req := &route53.ListTagsForResourceInput{
ResourceID: aws.String(zone),
ResourceType: aws.String("hostedzone"),
}
resp, err := conn.ListTagsForResource(req)
if err != nil {
return err
}
if resp.ResourceTagSet != nil {
2015-03-27 22:05:54 +01:00
*td = *resp.ResourceTagSet
}
return nil
}
}
const testAccRoute53ZoneConfig = `
resource "aws_route53_zone" "main" {
name = "hashicorp.com"
tags {
foo = "bar"
Name = "tf-route53-tag-test"
}
}
`