github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/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 var zone route53.HostedZone 67 var td route53.ResourceTagSet 68 69 resource.Test(t, resource.TestCase{ 70 PreCheck: func() { testAccPreCheck(t) }, 71 Providers: testAccProviders, 72 CheckDestroy: testAccCheckRoute53ZoneDestroy, 73 Steps: []resource.TestStep{ 74 resource.TestStep{ 75 Config: testAccRoute53ZoneConfig, 76 Check: resource.ComposeTestCheckFunc( 77 testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone), 78 testAccLoadTagsR53(&zone, &td), 79 testAccCheckTagsR53(&td.Tags, "foo", "bar"), 80 ), 81 }, 82 }, 83 }) 84 } 85 86 func testAccCheckRoute53ZoneDestroy(s *terraform.State) error { 87 conn := testAccProvider.Meta().(*AWSClient).r53conn 88 for _, rs := range s.RootModule().Resources { 89 if rs.Type != "aws_route53_zone" { 90 continue 91 } 92 93 _, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(rs.Primary.ID)}) 94 if err == nil { 95 return fmt.Errorf("Hosted zone still exists") 96 } 97 } 98 return nil 99 } 100 101 func testAccCheckRoute53ZoneExists(n string, zone *route53.HostedZone) resource.TestCheckFunc { 102 return func(s *terraform.State) error { 103 rs, ok := s.RootModule().Resources[n] 104 if !ok { 105 return fmt.Errorf("Not found: %s", n) 106 } 107 108 if rs.Primary.ID == "" { 109 return fmt.Errorf("No hosted zone ID is set") 110 } 111 112 conn := testAccProvider.Meta().(*AWSClient).r53conn 113 resp, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(rs.Primary.ID)}) 114 if err != nil { 115 return fmt.Errorf("Hosted zone err: %v", err) 116 } 117 *zone = *resp.HostedZone 118 return nil 119 } 120 } 121 122 func testAccLoadTagsR53(zone *route53.HostedZone, td *route53.ResourceTagSet) resource.TestCheckFunc { 123 return func(s *terraform.State) error { 124 conn := testAccProvider.Meta().(*AWSClient).r53conn 125 126 zone := cleanZoneID(*zone.ID) 127 req := &route53.ListTagsForResourceRequest{ 128 ResourceID: aws.String(zone), 129 ResourceType: aws.String("hostedzone"), 130 } 131 132 resp, err := conn.ListTagsForResource(req) 133 if err != nil { 134 return err 135 } 136 137 if resp.ResourceTagSet != nil { 138 *td = *resp.ResourceTagSet 139 } 140 141 return nil 142 } 143 } 144 145 const testAccRoute53ZoneConfig = ` 146 resource "aws_route53_zone" "main" { 147 name = "hashicorp.com" 148 149 tags { 150 foo = "bar" 151 Name = "tf-route53-tag-test" 152 } 153 } 154 `