github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_route53_zone_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "sort" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/hashicorp/terraform/terraform" 11 12 "github.com/aws/aws-sdk-go/aws" 13 "github.com/aws/aws-sdk-go/service/route53" 14 ) 15 16 func TestCleanPrefix(t *testing.T) { 17 cases := []struct { 18 Input, Prefix, Output string 19 }{ 20 {"/hostedzone/foo", "/hostedzone/", "foo"}, 21 {"/change/foo", "/change/", "foo"}, 22 {"/bar", "/test", "/bar"}, 23 } 24 25 for _, tc := range cases { 26 actual := cleanPrefix(tc.Input, tc.Prefix) 27 if actual != tc.Output { 28 t.Fatalf("input: %s\noutput: %s", tc.Input, actual) 29 } 30 } 31 } 32 33 func TestCleanZoneID(t *testing.T) { 34 cases := []struct { 35 Input, Output string 36 }{ 37 {"/hostedzone/foo", "foo"}, 38 {"/change/foo", "/change/foo"}, 39 {"/bar", "/bar"}, 40 } 41 42 for _, tc := range cases { 43 actual := cleanZoneID(tc.Input) 44 if actual != tc.Output { 45 t.Fatalf("input: %s\noutput: %s", tc.Input, actual) 46 } 47 } 48 } 49 50 func TestCleanChangeID(t *testing.T) { 51 cases := []struct { 52 Input, Output string 53 }{ 54 {"/hostedzone/foo", "/hostedzone/foo"}, 55 {"/change/foo", "foo"}, 56 {"/bar", "/bar"}, 57 } 58 59 for _, tc := range cases { 60 actual := cleanChangeID(tc.Input) 61 if actual != tc.Output { 62 t.Fatalf("input: %s\noutput: %s", tc.Input, actual) 63 } 64 } 65 } 66 67 func TestAccAWSRoute53Zone_basic(t *testing.T) { 68 var zone route53.GetHostedZoneOutput 69 var td route53.ResourceTagSet 70 71 resource.Test(t, resource.TestCase{ 72 PreCheck: func() { testAccPreCheck(t) }, 73 Providers: testAccProviders, 74 CheckDestroy: testAccCheckRoute53ZoneDestroy, 75 Steps: []resource.TestStep{ 76 resource.TestStep{ 77 Config: testAccRoute53ZoneConfig, 78 Check: resource.ComposeTestCheckFunc( 79 testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone), 80 testAccLoadTagsR53(&zone, &td), 81 testAccCheckTagsR53(&td.Tags, "foo", "bar"), 82 ), 83 }, 84 }, 85 }) 86 } 87 88 func TestAccAWSRoute53Zone_private_basic(t *testing.T) { 89 var zone route53.GetHostedZoneOutput 90 91 resource.Test(t, resource.TestCase{ 92 PreCheck: func() { testAccPreCheck(t) }, 93 Providers: testAccProviders, 94 CheckDestroy: testAccCheckRoute53ZoneDestroy, 95 Steps: []resource.TestStep{ 96 resource.TestStep{ 97 Config: testAccRoute53PrivateZoneConfig, 98 Check: resource.ComposeTestCheckFunc( 99 testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone), 100 testAccCheckRoute53ZoneAssociatesWithVpc("aws_vpc.main", &zone), 101 ), 102 }, 103 }, 104 }) 105 } 106 107 func TestAccAWSRoute53Zone_private_region(t *testing.T) { 108 var zone route53.GetHostedZoneOutput 109 110 // record the initialized providers so that we can use them to 111 // check for the instances in each region 112 var providers []*schema.Provider 113 providerFactories := map[string]terraform.ResourceProviderFactory{ 114 "aws": func() (terraform.ResourceProvider, error) { 115 p := Provider() 116 providers = append(providers, p.(*schema.Provider)) 117 return p, nil 118 }, 119 } 120 121 resource.Test(t, resource.TestCase{ 122 PreCheck: func() { testAccPreCheck(t) }, 123 ProviderFactories: providerFactories, 124 CheckDestroy: testAccCheckRoute53ZoneDestroyWithProviders(&providers), 125 Steps: []resource.TestStep{ 126 resource.TestStep{ 127 Config: testAccRoute53PrivateZoneRegionConfig, 128 Check: resource.ComposeTestCheckFunc( 129 testAccCheckRoute53ZoneExistsWithProviders("aws_route53_zone.main", &zone, &providers), 130 testAccCheckRoute53ZoneAssociatesWithVpc("aws_vpc.main", &zone), 131 ), 132 }, 133 }, 134 }) 135 } 136 137 func testAccCheckRoute53ZoneDestroy(s *terraform.State) error { 138 return testAccCheckRoute53ZoneDestroyWithProvider(s, testAccProvider) 139 } 140 141 func testAccCheckRoute53ZoneDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 142 return func(s *terraform.State) error { 143 for _, provider := range *providers { 144 if provider.Meta() == nil { 145 continue 146 } 147 if err := testAccCheckRoute53ZoneDestroyWithProvider(s, provider); err != nil { 148 return err 149 } 150 } 151 return nil 152 } 153 } 154 155 func testAccCheckRoute53ZoneDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 156 conn := provider.Meta().(*AWSClient).r53conn 157 for _, rs := range s.RootModule().Resources { 158 if rs.Type != "aws_route53_zone" { 159 continue 160 } 161 162 _, err := conn.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(rs.Primary.ID)}) 163 if err == nil { 164 return fmt.Errorf("Hosted zone still exists") 165 } 166 } 167 return nil 168 } 169 170 func testAccCheckRoute53ZoneExists(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc { 171 return func(s *terraform.State) error { 172 return testAccCheckRoute53ZoneExistsWithProvider(s, n, zone, testAccProvider) 173 } 174 } 175 176 func testAccCheckRoute53ZoneExistsWithProviders(n string, zone *route53.GetHostedZoneOutput, providers *[]*schema.Provider) resource.TestCheckFunc { 177 return func(s *terraform.State) error { 178 for _, provider := range *providers { 179 if provider.Meta() == nil { 180 continue 181 } 182 if err := testAccCheckRoute53ZoneExistsWithProvider(s, n, zone, provider); err != nil { 183 return err 184 } 185 } 186 return nil 187 } 188 } 189 190 func testAccCheckRoute53ZoneExistsWithProvider(s *terraform.State, n string, zone *route53.GetHostedZoneOutput, provider *schema.Provider) error { 191 rs, ok := s.RootModule().Resources[n] 192 if !ok { 193 return fmt.Errorf("Not found: %s", n) 194 } 195 196 if rs.Primary.ID == "" { 197 return fmt.Errorf("No hosted zone ID is set") 198 } 199 200 conn := provider.Meta().(*AWSClient).r53conn 201 resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(rs.Primary.ID)}) 202 if err != nil { 203 return fmt.Errorf("Hosted zone err: %v", err) 204 } 205 206 aws_comment := *resp.HostedZone.Config.Comment 207 rs_comment := rs.Primary.Attributes["comment"] 208 if rs_comment != "" && rs_comment != aws_comment { 209 return fmt.Errorf("Hosted zone with comment '%s' found but does not match '%s'", aws_comment, rs_comment) 210 } 211 212 if !*resp.HostedZone.Config.PrivateZone { 213 sorted_ns := make([]string, len(resp.DelegationSet.NameServers)) 214 for i, ns := range resp.DelegationSet.NameServers { 215 sorted_ns[i] = *ns 216 } 217 sort.Strings(sorted_ns) 218 for idx, ns := range sorted_ns { 219 attribute := fmt.Sprintf("name_servers.%d", idx) 220 dsns := rs.Primary.Attributes[attribute] 221 if dsns != ns { 222 return fmt.Errorf("Got: %v for %v, Expected: %v", dsns, attribute, ns) 223 } 224 } 225 } 226 227 *zone = *resp 228 return nil 229 } 230 231 func testAccCheckRoute53ZoneAssociatesWithVpc(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc { 232 return func(s *terraform.State) error { 233 rs, ok := s.RootModule().Resources[n] 234 if !ok { 235 return fmt.Errorf("Not found: %s", n) 236 } 237 238 if rs.Primary.ID == "" { 239 return fmt.Errorf("No VPC ID is set") 240 } 241 242 var associatedVPC *route53.VPC 243 for _, vpc := range zone.VPCs { 244 if *vpc.VPCId == rs.Primary.ID { 245 associatedVPC = vpc 246 } 247 } 248 if associatedVPC == nil { 249 return fmt.Errorf("VPC: %v is not associated to Zone: %v", n, cleanZoneID(*zone.HostedZone.Id)) 250 } 251 return nil 252 } 253 } 254 255 func testAccLoadTagsR53(zone *route53.GetHostedZoneOutput, td *route53.ResourceTagSet) resource.TestCheckFunc { 256 return func(s *terraform.State) error { 257 conn := testAccProvider.Meta().(*AWSClient).r53conn 258 259 zone := cleanZoneID(*zone.HostedZone.Id) 260 req := &route53.ListTagsForResourceInput{ 261 ResourceId: aws.String(zone), 262 ResourceType: aws.String("hostedzone"), 263 } 264 265 resp, err := conn.ListTagsForResource(req) 266 if err != nil { 267 return err 268 } 269 270 if resp.ResourceTagSet != nil { 271 *td = *resp.ResourceTagSet 272 } 273 274 return nil 275 } 276 } 277 278 const testAccRoute53ZoneConfig = ` 279 resource "aws_route53_zone" "main" { 280 name = "hashicorp.com" 281 comment = "Custom comment" 282 283 tags { 284 foo = "bar" 285 Name = "tf-route53-tag-test" 286 } 287 } 288 ` 289 290 const testAccRoute53PrivateZoneConfig = ` 291 resource "aws_vpc" "main" { 292 cidr_block = "172.29.0.0/24" 293 instance_tenancy = "default" 294 enable_dns_support = true 295 enable_dns_hostnames = true 296 } 297 298 resource "aws_route53_zone" "main" { 299 name = "hashicorp.com" 300 vpc_id = "${aws_vpc.main.id}" 301 } 302 ` 303 304 const testAccRoute53PrivateZoneRegionConfig = ` 305 provider "aws" { 306 alias = "west" 307 region = "us-west-2" 308 } 309 310 provider "aws" { 311 alias = "east" 312 region = "us-east-1" 313 } 314 315 resource "aws_vpc" "main" { 316 provider = "aws.east" 317 cidr_block = "172.29.0.0/24" 318 instance_tenancy = "default" 319 enable_dns_support = true 320 enable_dns_hostnames = true 321 } 322 323 resource "aws_route53_zone" "main" { 324 provider = "aws.west" 325 name = "hashicorp.com" 326 vpc_id = "${aws_vpc.main.id}" 327 vpc_region = "us-east-1" 328 } 329 `