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