2014-07-22 23:55:19 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-02-12 17:48:48 +01:00
|
|
|
|
2015-02-25 22:29:11 +01:00
|
|
|
"github.com/hashicorp/aws-sdk-go/aws"
|
|
|
|
"github.com/hashicorp/aws-sdk-go/gen/route53"
|
2014-07-22 23:55:19 +02:00
|
|
|
)
|
|
|
|
|
2015-02-12 17:48:48 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-22 23:55:19 +02:00
|
|
|
func TestAccRoute53Zone(t *testing.T) {
|
|
|
|
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"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckRoute53ZoneDestroy(s *terraform.State) error {
|
2015-02-12 17:48:48 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).r53conn
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-22 23:55:19 +02:00
|
|
|
if rs.Type != "aws_route53_zone" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-02-12 17:48:48 +01:00
|
|
|
_, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(rs.Primary.ID)})
|
2014-07-22 23:55:19 +02:00
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Hosted zone still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckRoute53ZoneExists(n string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-22 23:55:19 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-22 23:55:19 +02:00
|
|
|
return fmt.Errorf("No hosted zone ID is set")
|
|
|
|
}
|
|
|
|
|
2015-02-12 17:48:48 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).r53conn
|
|
|
|
_, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(rs.Primary.ID)})
|
2014-07-22 23:55:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Hosted zone err: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccRoute53ZoneConfig = `
|
|
|
|
resource "aws_route53_zone" "main" {
|
|
|
|
name = "hashicorp.com"
|
|
|
|
}
|
|
|
|
`
|