github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/resource_aws_route53_zone_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 10 "github.com/hashicorp/aws-sdk-go/aws" 11 "github.com/hashicorp/aws-sdk-go/gen/route53" 12 ) 13 14 func TestCleanPrefix(t *testing.T) { 15 cases := []struct { 16 Input, Prefix, Output string 17 }{ 18 {"/hostedzone/foo", "/hostedzone/", "foo"}, 19 {"/change/foo", "/change/", "foo"}, 20 {"/bar", "/test", "/bar"}, 21 } 22 23 for _, tc := range cases { 24 actual := cleanPrefix(tc.Input, tc.Prefix) 25 if actual != tc.Output { 26 t.Fatalf("input: %s\noutput: %s", tc.Input, actual) 27 } 28 } 29 } 30 31 func TestCleanZoneID(t *testing.T) { 32 cases := []struct { 33 Input, Output string 34 }{ 35 {"/hostedzone/foo", "foo"}, 36 {"/change/foo", "/change/foo"}, 37 {"/bar", "/bar"}, 38 } 39 40 for _, tc := range cases { 41 actual := cleanZoneID(tc.Input) 42 if actual != tc.Output { 43 t.Fatalf("input: %s\noutput: %s", tc.Input, actual) 44 } 45 } 46 } 47 48 func TestCleanChangeID(t *testing.T) { 49 cases := []struct { 50 Input, Output string 51 }{ 52 {"/hostedzone/foo", "/hostedzone/foo"}, 53 {"/change/foo", "foo"}, 54 {"/bar", "/bar"}, 55 } 56 57 for _, tc := range cases { 58 actual := cleanChangeID(tc.Input) 59 if actual != tc.Output { 60 t.Fatalf("input: %s\noutput: %s", tc.Input, actual) 61 } 62 } 63 } 64 65 func TestAccRoute53Zone(t *testing.T) { 66 resource.Test(t, resource.TestCase{ 67 PreCheck: func() { testAccPreCheck(t) }, 68 Providers: testAccProviders, 69 CheckDestroy: testAccCheckRoute53ZoneDestroy, 70 Steps: []resource.TestStep{ 71 resource.TestStep{ 72 Config: testAccRoute53ZoneConfig, 73 Check: resource.ComposeTestCheckFunc( 74 testAccCheckRoute53ZoneExists("aws_route53_zone.main"), 75 ), 76 }, 77 }, 78 }) 79 } 80 81 func testAccCheckRoute53ZoneDestroy(s *terraform.State) error { 82 conn := testAccProvider.Meta().(*AWSClient).r53conn 83 for _, rs := range s.RootModule().Resources { 84 if rs.Type != "aws_route53_zone" { 85 continue 86 } 87 88 _, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(rs.Primary.ID)}) 89 if err == nil { 90 return fmt.Errorf("Hosted zone still exists") 91 } 92 } 93 return nil 94 } 95 96 func testAccCheckRoute53ZoneExists(n string) resource.TestCheckFunc { 97 return func(s *terraform.State) error { 98 rs, ok := s.RootModule().Resources[n] 99 if !ok { 100 return fmt.Errorf("Not found: %s", n) 101 } 102 103 if rs.Primary.ID == "" { 104 return fmt.Errorf("No hosted zone ID is set") 105 } 106 107 conn := testAccProvider.Meta().(*AWSClient).r53conn 108 _, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(rs.Primary.ID)}) 109 if err != nil { 110 return fmt.Errorf("Hosted zone err: %v", err) 111 } 112 return nil 113 } 114 } 115 116 const testAccRoute53ZoneConfig = ` 117 resource "aws_route53_zone" "main" { 118 name = "hashicorp.com" 119 } 120 `