github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/google/resource_compute_health_check_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 "google.golang.org/api/compute/v1" 12 ) 13 14 func TestAccComputeHealthCheck_tcp(t *testing.T) { 15 var healthCheck compute.HealthCheck 16 17 hckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckComputeHealthCheckDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccComputeHealthCheck_tcp(hckName), 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckComputeHealthCheckExists( 28 "google_compute_health_check.foobar", &healthCheck), 29 testAccCheckComputeHealthCheckThresholds( 30 3, 3, &healthCheck), 31 testAccCheckComputeHealthCheckTcpPort(80, &healthCheck), 32 ), 33 }, 34 }, 35 }) 36 } 37 38 func TestAccComputeHealthCheck_tcp_update(t *testing.T) { 39 var healthCheck compute.HealthCheck 40 41 hckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 42 43 resource.Test(t, resource.TestCase{ 44 PreCheck: func() { testAccPreCheck(t) }, 45 Providers: testAccProviders, 46 CheckDestroy: testAccCheckComputeHealthCheckDestroy, 47 Steps: []resource.TestStep{ 48 resource.TestStep{ 49 Config: testAccComputeHealthCheck_tcp(hckName), 50 Check: resource.ComposeTestCheckFunc( 51 testAccCheckComputeHealthCheckExists( 52 "google_compute_health_check.foobar", &healthCheck), 53 testAccCheckComputeHealthCheckThresholds( 54 3, 3, &healthCheck), 55 testAccCheckComputeHealthCheckTcpPort(80, &healthCheck), 56 ), 57 }, 58 resource.TestStep{ 59 Config: testAccComputeHealthCheck_tcp_update(hckName), 60 Check: resource.ComposeTestCheckFunc( 61 testAccCheckComputeHealthCheckExists( 62 "google_compute_health_check.foobar", &healthCheck), 63 testAccCheckComputeHealthCheckThresholds( 64 10, 10, &healthCheck), 65 testAccCheckComputeHealthCheckTcpPort(8080, &healthCheck), 66 ), 67 }, 68 }, 69 }) 70 } 71 72 func TestAccComputeHealthCheck_ssl(t *testing.T) { 73 var healthCheck compute.HealthCheck 74 75 hckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 76 77 resource.Test(t, resource.TestCase{ 78 PreCheck: func() { testAccPreCheck(t) }, 79 Providers: testAccProviders, 80 CheckDestroy: testAccCheckComputeHealthCheckDestroy, 81 Steps: []resource.TestStep{ 82 resource.TestStep{ 83 Config: testAccComputeHealthCheck_ssl(hckName), 84 Check: resource.ComposeTestCheckFunc( 85 testAccCheckComputeHealthCheckExists( 86 "google_compute_health_check.foobar", &healthCheck), 87 testAccCheckComputeHealthCheckThresholds( 88 3, 3, &healthCheck), 89 ), 90 }, 91 }, 92 }) 93 } 94 95 func TestAccComputeHealthCheck_http(t *testing.T) { 96 var healthCheck compute.HealthCheck 97 98 hckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 99 100 resource.Test(t, resource.TestCase{ 101 PreCheck: func() { testAccPreCheck(t) }, 102 Providers: testAccProviders, 103 CheckDestroy: testAccCheckComputeHealthCheckDestroy, 104 Steps: []resource.TestStep{ 105 resource.TestStep{ 106 Config: testAccComputeHealthCheck_http(hckName), 107 Check: resource.ComposeTestCheckFunc( 108 testAccCheckComputeHealthCheckExists( 109 "google_compute_health_check.foobar", &healthCheck), 110 testAccCheckComputeHealthCheckThresholds( 111 3, 3, &healthCheck), 112 ), 113 }, 114 }, 115 }) 116 } 117 118 func TestAccComputeHealthCheck_https(t *testing.T) { 119 var healthCheck compute.HealthCheck 120 121 hckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 122 123 resource.Test(t, resource.TestCase{ 124 PreCheck: func() { testAccPreCheck(t) }, 125 Providers: testAccProviders, 126 CheckDestroy: testAccCheckComputeHealthCheckDestroy, 127 Steps: []resource.TestStep{ 128 resource.TestStep{ 129 Config: testAccComputeHealthCheck_https(hckName), 130 Check: resource.ComposeTestCheckFunc( 131 testAccCheckComputeHealthCheckExists( 132 "google_compute_health_check.foobar", &healthCheck), 133 testAccCheckComputeHealthCheckThresholds( 134 3, 3, &healthCheck), 135 ), 136 }, 137 }, 138 }) 139 } 140 141 func TestAccComputeHealthCheck_tcpAndSsl_shouldFail(t *testing.T) { 142 hckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 143 144 resource.Test(t, resource.TestCase{ 145 PreCheck: func() { testAccPreCheck(t) }, 146 Providers: testAccProviders, 147 CheckDestroy: testAccCheckComputeHealthCheckDestroy, 148 Steps: []resource.TestStep{ 149 resource.TestStep{ 150 Config: testAccComputeHealthCheck_tcpAndSsl_shouldFail(hckName), 151 ExpectError: regexp.MustCompile("conflicts with tcp_health_check"), 152 }, 153 }, 154 }) 155 } 156 157 func testAccCheckComputeHealthCheckDestroy(s *terraform.State) error { 158 config := testAccProvider.Meta().(*Config) 159 160 for _, rs := range s.RootModule().Resources { 161 if rs.Type != "google_compute_health_check" { 162 continue 163 } 164 165 _, err := config.clientCompute.HealthChecks.Get( 166 config.Project, rs.Primary.ID).Do() 167 if err == nil { 168 return fmt.Errorf("HealthCheck %s still exists", rs.Primary.ID) 169 } 170 } 171 172 return nil 173 } 174 175 func testAccCheckComputeHealthCheckExists(n string, healthCheck *compute.HealthCheck) resource.TestCheckFunc { 176 return func(s *terraform.State) error { 177 rs, ok := s.RootModule().Resources[n] 178 if !ok { 179 return fmt.Errorf("Not found: %s", n) 180 } 181 182 if rs.Primary.ID == "" { 183 return fmt.Errorf("No ID is set") 184 } 185 186 config := testAccProvider.Meta().(*Config) 187 188 found, err := config.clientCompute.HealthChecks.Get( 189 config.Project, rs.Primary.ID).Do() 190 if err != nil { 191 return err 192 } 193 194 if found.Name != rs.Primary.ID { 195 return fmt.Errorf("HealthCheck not found") 196 } 197 198 *healthCheck = *found 199 200 return nil 201 } 202 } 203 204 func testAccCheckErrorCreating(n string) resource.TestCheckFunc { 205 return func(s *terraform.State) error { 206 _, ok := s.RootModule().Resources[n] 207 if ok { 208 return fmt.Errorf("HealthCheck %s created successfully with bad config", n) 209 } 210 return nil 211 } 212 } 213 214 func testAccCheckComputeHealthCheckThresholds(healthy, unhealthy int64, healthCheck *compute.HealthCheck) resource.TestCheckFunc { 215 return func(s *terraform.State) error { 216 if healthCheck.HealthyThreshold != healthy { 217 return fmt.Errorf("HealthyThreshold doesn't match: expected %d, got %d", healthy, healthCheck.HealthyThreshold) 218 } 219 220 if healthCheck.UnhealthyThreshold != unhealthy { 221 return fmt.Errorf("UnhealthyThreshold doesn't match: expected %d, got %d", unhealthy, healthCheck.UnhealthyThreshold) 222 } 223 224 return nil 225 } 226 } 227 228 func testAccCheckComputeHealthCheckTcpPort(port int64, healthCheck *compute.HealthCheck) resource.TestCheckFunc { 229 return func(s *terraform.State) error { 230 if healthCheck.TcpHealthCheck.Port != port { 231 return fmt.Errorf("Port doesn't match: expected %v, got %v", port, healthCheck.TcpHealthCheck.Port) 232 } 233 return nil 234 } 235 } 236 237 func testAccComputeHealthCheck_tcp(hckName string) string { 238 return fmt.Sprintf(` 239 resource "google_compute_health_check" "foobar" { 240 check_interval_sec = 3 241 description = "Resource created for Terraform acceptance testing" 242 healthy_threshold = 3 243 name = "health-test-%s" 244 timeout_sec = 2 245 unhealthy_threshold = 3 246 tcp_health_check { 247 } 248 } 249 `, hckName) 250 } 251 252 func testAccComputeHealthCheck_tcp_update(hckName string) string { 253 return fmt.Sprintf(` 254 resource "google_compute_health_check" "foobar" { 255 check_interval_sec = 3 256 description = "Resource updated for Terraform acceptance testing" 257 healthy_threshold = 10 258 name = "health-test-%s" 259 timeout_sec = 2 260 unhealthy_threshold = 10 261 tcp_health_check { 262 port = "8080" 263 } 264 } 265 `, hckName) 266 } 267 268 func testAccComputeHealthCheck_ssl(hckName string) string { 269 return fmt.Sprintf(` 270 resource "google_compute_health_check" "foobar" { 271 check_interval_sec = 3 272 description = "Resource created for Terraform acceptance testing" 273 healthy_threshold = 3 274 name = "health-test-%s" 275 timeout_sec = 2 276 unhealthy_threshold = 3 277 ssl_health_check { 278 port = "443" 279 } 280 } 281 `, hckName) 282 } 283 284 func testAccComputeHealthCheck_http(hckName string) string { 285 return fmt.Sprintf(` 286 resource "google_compute_health_check" "foobar" { 287 check_interval_sec = 3 288 description = "Resource created for Terraform acceptance testing" 289 healthy_threshold = 3 290 name = "health-test-%s" 291 timeout_sec = 2 292 unhealthy_threshold = 3 293 http_health_check { 294 port = "80" 295 } 296 } 297 `, hckName) 298 } 299 300 func testAccComputeHealthCheck_https(hckName string) string { 301 return fmt.Sprintf(` 302 resource "google_compute_health_check" "foobar" { 303 check_interval_sec = 3 304 description = "Resource created for Terraform acceptance testing" 305 healthy_threshold = 3 306 name = "health-test-%s" 307 timeout_sec = 2 308 unhealthy_threshold = 3 309 https_health_check { 310 port = "443" 311 } 312 } 313 `, hckName) 314 } 315 316 func testAccComputeHealthCheck_tcpAndSsl_shouldFail(hckName string) string { 317 return fmt.Sprintf(` 318 resource "google_compute_health_check" "foobar" { 319 check_interval_sec = 3 320 description = "Resource created for Terraform acceptance testing" 321 healthy_threshold = 3 322 name = "health-test-%s" 323 timeout_sec = 2 324 unhealthy_threshold = 3 325 326 tcp_health_check { 327 } 328 ssl_health_check { 329 } 330 } 331 `, hckName) 332 }