github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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_updateComment(t *testing.T) { 89 var zone route53.GetHostedZoneOutput 90 var td route53.ResourceTagSet 91 92 resource.Test(t, resource.TestCase{ 93 PreCheck: func() { testAccPreCheck(t) }, 94 Providers: testAccProviders, 95 CheckDestroy: testAccCheckRoute53ZoneDestroy, 96 Steps: []resource.TestStep{ 97 resource.TestStep{ 98 Config: testAccRoute53ZoneConfig, 99 Check: resource.ComposeTestCheckFunc( 100 testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone), 101 testAccLoadTagsR53(&zone, &td), 102 testAccCheckTagsR53(&td.Tags, "foo", "bar"), 103 resource.TestCheckResourceAttr( 104 "aws_route53_zone.main", "comment", "Custom comment"), 105 ), 106 }, 107 108 resource.TestStep{ 109 Config: testAccRoute53ZoneConfigUpdateComment, 110 Check: resource.ComposeTestCheckFunc( 111 testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone), 112 testAccLoadTagsR53(&zone, &td), 113 resource.TestCheckResourceAttr( 114 "aws_route53_zone.main", "comment", "Change Custom Comment"), 115 ), 116 }, 117 }, 118 }) 119 } 120 121 func TestAccAWSRoute53Zone_private_basic(t *testing.T) { 122 var zone route53.GetHostedZoneOutput 123 124 resource.Test(t, resource.TestCase{ 125 PreCheck: func() { testAccPreCheck(t) }, 126 Providers: testAccProviders, 127 CheckDestroy: testAccCheckRoute53ZoneDestroy, 128 Steps: []resource.TestStep{ 129 resource.TestStep{ 130 Config: testAccRoute53PrivateZoneConfig, 131 Check: resource.ComposeTestCheckFunc( 132 testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone), 133 testAccCheckRoute53ZoneAssociatesWithVpc("aws_vpc.main", &zone), 134 ), 135 }, 136 }, 137 }) 138 } 139 140 func TestAccAWSRoute53Zone_private_region(t *testing.T) { 141 var zone route53.GetHostedZoneOutput 142 143 // record the initialized providers so that we can use them to 144 // check for the instances in each region 145 var providers []*schema.Provider 146 providerFactories := map[string]terraform.ResourceProviderFactory{ 147 "aws": func() (terraform.ResourceProvider, error) { 148 p := Provider() 149 providers = append(providers, p.(*schema.Provider)) 150 return p, nil 151 }, 152 } 153 154 resource.Test(t, resource.TestCase{ 155 PreCheck: func() { testAccPreCheck(t) }, 156 ProviderFactories: providerFactories, 157 CheckDestroy: testAccCheckRoute53ZoneDestroyWithProviders(&providers), 158 Steps: []resource.TestStep{ 159 resource.TestStep{ 160 Config: testAccRoute53PrivateZoneRegionConfig, 161 Check: resource.ComposeTestCheckFunc( 162 testAccCheckRoute53ZoneExistsWithProviders("aws_route53_zone.main", &zone, &providers), 163 testAccCheckRoute53ZoneAssociatesWithVpc("aws_vpc.main", &zone), 164 ), 165 }, 166 }, 167 }) 168 } 169 170 func testAccCheckRoute53ZoneDestroy(s *terraform.State) error { 171 return testAccCheckRoute53ZoneDestroyWithProvider(s, testAccProvider) 172 } 173 174 func testAccCheckRoute53ZoneDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 175 return func(s *terraform.State) error { 176 for _, provider := range *providers { 177 if provider.Meta() == nil { 178 continue 179 } 180 if err := testAccCheckRoute53ZoneDestroyWithProvider(s, provider); err != nil { 181 return err 182 } 183 } 184 return nil 185 } 186 } 187 188 func testAccCheckRoute53ZoneDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 189 conn := provider.Meta().(*AWSClient).r53conn 190 for _, rs := range s.RootModule().Resources { 191 if rs.Type != "aws_route53_zone" { 192 continue 193 } 194 195 _, err := conn.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(rs.Primary.ID)}) 196 if err == nil { 197 return fmt.Errorf("Hosted zone still exists") 198 } 199 } 200 return nil 201 } 202 203 func testAccCheckRoute53ZoneExists(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc { 204 return func(s *terraform.State) error { 205 return testAccCheckRoute53ZoneExistsWithProvider(s, n, zone, testAccProvider) 206 } 207 } 208 209 func testAccCheckRoute53ZoneExistsWithProviders(n string, zone *route53.GetHostedZoneOutput, providers *[]*schema.Provider) resource.TestCheckFunc { 210 return func(s *terraform.State) error { 211 for _, provider := range *providers { 212 if provider.Meta() == nil { 213 continue 214 } 215 if err := testAccCheckRoute53ZoneExistsWithProvider(s, n, zone, provider); err != nil { 216 return err 217 } 218 } 219 return nil 220 } 221 } 222 223 func testAccCheckRoute53ZoneExistsWithProvider(s *terraform.State, n string, zone *route53.GetHostedZoneOutput, provider *schema.Provider) error { 224 rs, ok := s.RootModule().Resources[n] 225 if !ok { 226 return fmt.Errorf("Not found: %s", n) 227 } 228 229 if rs.Primary.ID == "" { 230 return fmt.Errorf("No hosted zone ID is set") 231 } 232 233 conn := provider.Meta().(*AWSClient).r53conn 234 resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(rs.Primary.ID)}) 235 if err != nil { 236 return fmt.Errorf("Hosted zone err: %v", err) 237 } 238 239 aws_comment := *resp.HostedZone.Config.Comment 240 rs_comment := rs.Primary.Attributes["comment"] 241 if rs_comment != "" && rs_comment != aws_comment { 242 return fmt.Errorf("Hosted zone with comment '%s' found but does not match '%s'", aws_comment, rs_comment) 243 } 244 245 if !*resp.HostedZone.Config.PrivateZone { 246 sorted_ns := make([]string, len(resp.DelegationSet.NameServers)) 247 for i, ns := range resp.DelegationSet.NameServers { 248 sorted_ns[i] = *ns 249 } 250 sort.Strings(sorted_ns) 251 for idx, ns := range sorted_ns { 252 attribute := fmt.Sprintf("name_servers.%d", idx) 253 dsns := rs.Primary.Attributes[attribute] 254 if dsns != ns { 255 return fmt.Errorf("Got: %v for %v, Expected: %v", dsns, attribute, ns) 256 } 257 } 258 } 259 260 *zone = *resp 261 return nil 262 } 263 264 func testAccCheckRoute53ZoneAssociatesWithVpc(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc { 265 return func(s *terraform.State) error { 266 rs, ok := s.RootModule().Resources[n] 267 if !ok { 268 return fmt.Errorf("Not found: %s", n) 269 } 270 271 if rs.Primary.ID == "" { 272 return fmt.Errorf("No VPC ID is set") 273 } 274 275 var associatedVPC *route53.VPC 276 for _, vpc := range zone.VPCs { 277 if *vpc.VPCId == rs.Primary.ID { 278 associatedVPC = vpc 279 } 280 } 281 if associatedVPC == nil { 282 return fmt.Errorf("VPC: %v is not associated to Zone: %v", n, cleanZoneID(*zone.HostedZone.Id)) 283 } 284 return nil 285 } 286 } 287 288 func testAccLoadTagsR53(zone *route53.GetHostedZoneOutput, td *route53.ResourceTagSet) resource.TestCheckFunc { 289 return func(s *terraform.State) error { 290 conn := testAccProvider.Meta().(*AWSClient).r53conn 291 292 zone := cleanZoneID(*zone.HostedZone.Id) 293 req := &route53.ListTagsForResourceInput{ 294 ResourceId: aws.String(zone), 295 ResourceType: aws.String("hostedzone"), 296 } 297 298 resp, err := conn.ListTagsForResource(req) 299 if err != nil { 300 return err 301 } 302 303 if resp.ResourceTagSet != nil { 304 *td = *resp.ResourceTagSet 305 } 306 307 return nil 308 } 309 } 310 311 const testAccRoute53ZoneConfig = ` 312 resource "aws_route53_zone" "main" { 313 name = "hashicorp.com" 314 comment = "Custom comment" 315 316 tags { 317 foo = "bar" 318 Name = "tf-route53-tag-test" 319 } 320 } 321 ` 322 323 const testAccRoute53ZoneConfigUpdateComment = ` 324 resource "aws_route53_zone" "main" { 325 name = "hashicorp.com" 326 comment = "Change Custom Comment" 327 328 tags { 329 foo = "bar" 330 Name = "tf-route53-tag-test" 331 } 332 } 333 ` 334 335 const testAccRoute53PrivateZoneConfig = ` 336 resource "aws_vpc" "main" { 337 cidr_block = "172.29.0.0/24" 338 instance_tenancy = "default" 339 enable_dns_support = true 340 enable_dns_hostnames = true 341 } 342 343 resource "aws_route53_zone" "main" { 344 name = "hashicorp.com" 345 vpc_id = "${aws_vpc.main.id}" 346 } 347 ` 348 349 const testAccRoute53PrivateZoneRegionConfig = ` 350 provider "aws" { 351 alias = "west" 352 region = "us-west-2" 353 } 354 355 provider "aws" { 356 alias = "east" 357 region = "us-east-1" 358 } 359 360 resource "aws_vpc" "main" { 361 provider = "aws.east" 362 cidr_block = "172.29.0.0/24" 363 instance_tenancy = "default" 364 enable_dns_support = true 365 enable_dns_hostnames = true 366 } 367 368 resource "aws_route53_zone" "main" { 369 provider = "aws.west" 370 name = "hashicorp.com" 371 vpc_id = "${aws_vpc.main.id}" 372 vpc_region = "us-east-1" 373 } 374 `