github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/builtin/providers/aws/resource_aws_route53_health_check_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/aws/aws-sdk-go/service/route53" 11 ) 12 13 func TestAccAWSRoute53HealthCheck_basic(t *testing.T) { 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 IDRefreshName: "aws_route53_health_check.foo", 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckRoute53HealthCheckDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccRoute53HealthCheckConfig, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"), 24 resource.TestCheckResourceAttr( 25 "aws_route53_health_check.foo", "measure_latency", "true"), 26 resource.TestCheckResourceAttr( 27 "aws_route53_health_check.foo", "invert_healthcheck", "true"), 28 ), 29 }, 30 resource.TestStep{ 31 Config: testAccRoute53HealthCheckConfigUpdate, 32 Check: resource.ComposeTestCheckFunc( 33 testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"), 34 resource.TestCheckResourceAttr( 35 "aws_route53_health_check.foo", "failure_threshold", "5"), 36 resource.TestCheckResourceAttr( 37 "aws_route53_health_check.foo", "invert_healthcheck", "false"), 38 ), 39 }, 40 }, 41 }) 42 } 43 44 func TestAccAWSRoute53HealthCheck_withChildHealthChecks(t *testing.T) { 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccCheckRoute53HealthCheckDestroy, 49 Steps: []resource.TestStep{ 50 resource.TestStep{ 51 Config: testAccRoute53HealthCheckConfig_withChildHealthChecks, 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"), 54 ), 55 }, 56 }, 57 }) 58 } 59 60 func TestAccAWSRoute53HealthCheck_IpConfig(t *testing.T) { 61 resource.Test(t, resource.TestCase{ 62 PreCheck: func() { testAccPreCheck(t) }, 63 Providers: testAccProviders, 64 CheckDestroy: testAccCheckRoute53HealthCheckDestroy, 65 Steps: []resource.TestStep{ 66 resource.TestStep{ 67 Config: testAccRoute53HealthCheckIpConfig, 68 Check: resource.ComposeTestCheckFunc( 69 testAccCheckRoute53HealthCheckExists("aws_route53_health_check.bar"), 70 ), 71 }, 72 }, 73 }) 74 } 75 76 func testAccCheckRoute53HealthCheckDestroy(s *terraform.State) error { 77 conn := testAccProvider.Meta().(*AWSClient).r53conn 78 79 for _, rs := range s.RootModule().Resources { 80 if rs.Type != "aws_route53_health_check" { 81 continue 82 } 83 84 lopts := &route53.ListHealthChecksInput{} 85 resp, err := conn.ListHealthChecks(lopts) 86 if err != nil { 87 return err 88 } 89 if len(resp.HealthChecks) == 0 { 90 return nil 91 } 92 93 for _, check := range resp.HealthChecks { 94 if *check.Id == rs.Primary.ID { 95 return fmt.Errorf("Record still exists: %#v", check) 96 } 97 98 } 99 100 } 101 return nil 102 } 103 104 func testAccCheckRoute53HealthCheckExists(n string) resource.TestCheckFunc { 105 return func(s *terraform.State) error { 106 conn := testAccProvider.Meta().(*AWSClient).r53conn 107 108 rs, ok := s.RootModule().Resources[n] 109 if !ok { 110 return fmt.Errorf("Not found: %s", n) 111 } 112 113 fmt.Print(rs.Primary.ID) 114 115 if rs.Primary.ID == "" { 116 return fmt.Errorf("No health check ID is set") 117 } 118 119 lopts := &route53.ListHealthChecksInput{} 120 resp, err := conn.ListHealthChecks(lopts) 121 if err != nil { 122 return err 123 } 124 if len(resp.HealthChecks) == 0 { 125 return fmt.Errorf("Health Check does not exist") 126 } 127 128 for _, check := range resp.HealthChecks { 129 if *check.Id == rs.Primary.ID { 130 return nil 131 } 132 133 } 134 return fmt.Errorf("Health Check does not exist") 135 } 136 } 137 138 func testUpdateHappened(n string) resource.TestCheckFunc { 139 return nil 140 } 141 142 const testAccRoute53HealthCheckConfig = ` 143 resource "aws_route53_health_check" "foo" { 144 fqdn = "dev.notexample.com" 145 port = 80 146 type = "HTTP" 147 resource_path = "/" 148 failure_threshold = "2" 149 request_interval = "30" 150 measure_latency = true 151 invert_healthcheck = true 152 153 tags = { 154 Name = "tf-test-health-check" 155 } 156 } 157 ` 158 159 const testAccRoute53HealthCheckConfigUpdate = ` 160 resource "aws_route53_health_check" "foo" { 161 fqdn = "dev.notexample.com" 162 port = 80 163 type = "HTTP" 164 resource_path = "/" 165 failure_threshold = "5" 166 request_interval = "30" 167 measure_latency = true 168 invert_healthcheck = false 169 170 tags = { 171 Name = "tf-test-health-check" 172 } 173 } 174 ` 175 176 const testAccRoute53HealthCheckIpConfig = ` 177 resource "aws_route53_health_check" "bar" { 178 ip_address = "1.2.3.4" 179 port = 80 180 type = "HTTP" 181 resource_path = "/" 182 failure_threshold = "2" 183 request_interval = "30" 184 185 tags = { 186 Name = "tf-test-health-check" 187 } 188 } 189 ` 190 191 const testAccRoute53HealthCheckConfig_withChildHealthChecks = ` 192 resource "aws_route53_health_check" "child1" { 193 fqdn = "child1.notexample.com" 194 port = 80 195 type = "HTTP" 196 resource_path = "/" 197 failure_threshold = "2" 198 request_interval = "30" 199 } 200 201 resource "aws_route53_health_check" "foo" { 202 type = "CALCULATED" 203 child_health_threshold = 1 204 child_healthchecks = ["${aws_route53_health_check.child1.id}"] 205 206 tags = { 207 Name = "tf-test-calculated-health-check" 208 } 209 } 210 `