2016-11-18 17:08:26 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-08 22:35:57 +01:00
|
|
|
"regexp"
|
2016-11-18 17:08:26 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
)
|
|
|
|
|
2016-11-30 01:08:44 +01:00
|
|
|
func TestAccComputeHealthCheck_tcp(t *testing.T) {
|
2016-11-18 17:08:26 +01:00
|
|
|
var healthCheck compute.HealthCheck
|
|
|
|
|
2017-06-06 14:11:19 +02:00
|
|
|
hckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
|
2016-11-18 17:08:26 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeHealthCheckDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-06-06 14:11:19 +02:00
|
|
|
Config: testAccComputeHealthCheck_tcp(hckName),
|
2016-11-18 17:08:26 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeHealthCheckExists(
|
|
|
|
"google_compute_health_check.foobar", &healthCheck),
|
|
|
|
testAccCheckComputeHealthCheckThresholds(
|
|
|
|
3, 3, &healthCheck),
|
2016-11-30 01:08:44 +01:00
|
|
|
testAccCheckComputeHealthCheckTcpPort(80, &healthCheck),
|
2016-11-18 17:08:26 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-01 01:52:34 +01:00
|
|
|
func TestAccComputeHealthCheck_tcp_update(t *testing.T) {
|
|
|
|
var healthCheck compute.HealthCheck
|
|
|
|
|
2017-06-06 14:11:19 +02:00
|
|
|
hckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
|
2016-12-01 01:52:34 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeHealthCheckDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-06-06 14:11:19 +02:00
|
|
|
Config: testAccComputeHealthCheck_tcp(hckName),
|
2016-12-01 01:52:34 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeHealthCheckExists(
|
|
|
|
"google_compute_health_check.foobar", &healthCheck),
|
|
|
|
testAccCheckComputeHealthCheckThresholds(
|
|
|
|
3, 3, &healthCheck),
|
|
|
|
testAccCheckComputeHealthCheckTcpPort(80, &healthCheck),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2017-06-06 14:11:19 +02:00
|
|
|
Config: testAccComputeHealthCheck_tcp_update(hckName),
|
2016-12-01 01:52:34 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeHealthCheckExists(
|
|
|
|
"google_compute_health_check.foobar", &healthCheck),
|
|
|
|
testAccCheckComputeHealthCheckThresholds(
|
|
|
|
10, 10, &healthCheck),
|
|
|
|
testAccCheckComputeHealthCheckTcpPort(8080, &healthCheck),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-30 01:08:44 +01:00
|
|
|
func TestAccComputeHealthCheck_ssl(t *testing.T) {
|
|
|
|
var healthCheck compute.HealthCheck
|
|
|
|
|
2017-06-06 14:11:19 +02:00
|
|
|
hckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
|
2016-11-30 01:08:44 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeHealthCheckDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-06-06 14:11:19 +02:00
|
|
|
Config: testAccComputeHealthCheck_ssl(hckName),
|
2016-11-18 17:08:26 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeHealthCheckExists(
|
|
|
|
"google_compute_health_check.foobar", &healthCheck),
|
|
|
|
testAccCheckComputeHealthCheckThresholds(
|
2016-11-30 01:08:44 +01:00
|
|
|
3, 3, &healthCheck),
|
2016-11-18 17:08:26 +01:00
|
|
|
),
|
|
|
|
},
|
2016-11-30 01:08:44 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccComputeHealthCheck_http(t *testing.T) {
|
|
|
|
var healthCheck compute.HealthCheck
|
|
|
|
|
2017-06-06 14:11:19 +02:00
|
|
|
hckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
|
2016-11-30 01:08:44 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeHealthCheckDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-11-18 17:08:26 +01:00
|
|
|
resource.TestStep{
|
2017-06-06 14:11:19 +02:00
|
|
|
Config: testAccComputeHealthCheck_http(hckName),
|
2016-11-18 17:08:26 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeHealthCheckExists(
|
|
|
|
"google_compute_health_check.foobar", &healthCheck),
|
|
|
|
testAccCheckComputeHealthCheckThresholds(
|
2016-11-30 01:08:44 +01:00
|
|
|
3, 3, &healthCheck),
|
2016-11-18 17:08:26 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-30 01:08:44 +01:00
|
|
|
func TestAccComputeHealthCheck_https(t *testing.T) {
|
|
|
|
var healthCheck compute.HealthCheck
|
|
|
|
|
2017-06-06 14:11:19 +02:00
|
|
|
hckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
|
2016-11-30 01:08:44 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeHealthCheckDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-06-06 14:11:19 +02:00
|
|
|
Config: testAccComputeHealthCheck_https(hckName),
|
2016-11-30 01:08:44 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeHealthCheckExists(
|
|
|
|
"google_compute_health_check.foobar", &healthCheck),
|
|
|
|
testAccCheckComputeHealthCheckThresholds(
|
|
|
|
3, 3, &healthCheck),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-08 22:35:57 +01:00
|
|
|
func TestAccComputeHealthCheck_tcpAndSsl_shouldFail(t *testing.T) {
|
2017-06-06 14:11:19 +02:00
|
|
|
hckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
|
2016-12-08 22:35:57 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeHealthCheckDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-06-06 14:11:19 +02:00
|
|
|
Config: testAccComputeHealthCheck_tcpAndSsl_shouldFail(hckName),
|
2016-12-08 22:35:57 +01:00
|
|
|
ExpectError: regexp.MustCompile("conflicts with tcp_health_check"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-18 17:08:26 +01:00
|
|
|
func testAccCheckComputeHealthCheckDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "google_compute_health_check" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := config.clientCompute.HealthChecks.Get(
|
|
|
|
config.Project, rs.Primary.ID).Do()
|
|
|
|
if err == nil {
|
2016-12-08 19:40:09 +01:00
|
|
|
return fmt.Errorf("HealthCheck %s still exists", rs.Primary.ID)
|
2016-11-18 17:08:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeHealthCheckExists(n string, healthCheck *compute.HealthCheck) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
found, err := config.clientCompute.HealthChecks.Get(
|
|
|
|
config.Project, rs.Primary.ID).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.Name != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("HealthCheck not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*healthCheck = *found
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 22:35:57 +01:00
|
|
|
func testAccCheckErrorCreating(n string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
_, ok := s.RootModule().Resources[n]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("HealthCheck %s created successfully with bad config", n)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 17:08:26 +01:00
|
|
|
func testAccCheckComputeHealthCheckThresholds(healthy, unhealthy int64, healthCheck *compute.HealthCheck) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if healthCheck.HealthyThreshold != healthy {
|
|
|
|
return fmt.Errorf("HealthyThreshold doesn't match: expected %d, got %d", healthy, healthCheck.HealthyThreshold)
|
|
|
|
}
|
|
|
|
|
|
|
|
if healthCheck.UnhealthyThreshold != unhealthy {
|
|
|
|
return fmt.Errorf("UnhealthyThreshold doesn't match: expected %d, got %d", unhealthy, healthCheck.UnhealthyThreshold)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 01:08:44 +01:00
|
|
|
func testAccCheckComputeHealthCheckTcpPort(port int64, healthCheck *compute.HealthCheck) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if healthCheck.TcpHealthCheck.Port != port {
|
|
|
|
return fmt.Errorf("Port doesn't match: expected %v, got %v", port, healthCheck.TcpHealthCheck.Port)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 14:11:19 +02:00
|
|
|
func testAccComputeHealthCheck_tcp(hckName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-11-18 17:08:26 +01:00
|
|
|
resource "google_compute_health_check" "foobar" {
|
|
|
|
check_interval_sec = 3
|
|
|
|
description = "Resource created for Terraform acceptance testing"
|
|
|
|
healthy_threshold = 3
|
|
|
|
name = "health-test-%s"
|
|
|
|
timeout_sec = 2
|
|
|
|
unhealthy_threshold = 3
|
|
|
|
tcp_health_check {
|
|
|
|
}
|
|
|
|
}
|
2017-06-06 14:11:19 +02:00
|
|
|
`, hckName)
|
|
|
|
}
|
2016-11-18 17:08:26 +01:00
|
|
|
|
2017-06-06 14:11:19 +02:00
|
|
|
func testAccComputeHealthCheck_tcp_update(hckName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-12-01 01:52:34 +01:00
|
|
|
resource "google_compute_health_check" "foobar" {
|
|
|
|
check_interval_sec = 3
|
|
|
|
description = "Resource updated for Terraform acceptance testing"
|
|
|
|
healthy_threshold = 10
|
|
|
|
name = "health-test-%s"
|
|
|
|
timeout_sec = 2
|
|
|
|
unhealthy_threshold = 10
|
|
|
|
tcp_health_check {
|
|
|
|
port = "8080"
|
|
|
|
}
|
|
|
|
}
|
2017-06-06 14:11:19 +02:00
|
|
|
`, hckName)
|
|
|
|
}
|
2016-12-01 01:52:34 +01:00
|
|
|
|
2017-06-06 14:11:19 +02:00
|
|
|
func testAccComputeHealthCheck_ssl(hckName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-11-30 01:08:44 +01:00
|
|
|
resource "google_compute_health_check" "foobar" {
|
|
|
|
check_interval_sec = 3
|
|
|
|
description = "Resource created for Terraform acceptance testing"
|
|
|
|
healthy_threshold = 3
|
|
|
|
name = "health-test-%s"
|
|
|
|
timeout_sec = 2
|
|
|
|
unhealthy_threshold = 3
|
|
|
|
ssl_health_check {
|
|
|
|
port = "443"
|
|
|
|
}
|
2016-11-18 17:08:26 +01:00
|
|
|
}
|
2017-06-06 14:11:19 +02:00
|
|
|
`, hckName)
|
|
|
|
}
|
2016-11-18 17:08:26 +01:00
|
|
|
|
2017-06-06 14:11:19 +02:00
|
|
|
func testAccComputeHealthCheck_http(hckName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-11-18 17:08:26 +01:00
|
|
|
resource "google_compute_health_check" "foobar" {
|
2016-11-30 01:08:44 +01:00
|
|
|
check_interval_sec = 3
|
|
|
|
description = "Resource created for Terraform acceptance testing"
|
|
|
|
healthy_threshold = 3
|
|
|
|
name = "health-test-%s"
|
|
|
|
timeout_sec = 2
|
|
|
|
unhealthy_threshold = 3
|
|
|
|
http_health_check {
|
|
|
|
port = "80"
|
|
|
|
}
|
|
|
|
}
|
2017-06-06 14:11:19 +02:00
|
|
|
`, hckName)
|
|
|
|
}
|
2016-11-30 01:08:44 +01:00
|
|
|
|
2017-06-06 14:11:19 +02:00
|
|
|
func testAccComputeHealthCheck_https(hckName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-11-30 01:08:44 +01:00
|
|
|
resource "google_compute_health_check" "foobar" {
|
|
|
|
check_interval_sec = 3
|
|
|
|
description = "Resource created for Terraform acceptance testing"
|
|
|
|
healthy_threshold = 3
|
|
|
|
name = "health-test-%s"
|
|
|
|
timeout_sec = 2
|
|
|
|
unhealthy_threshold = 3
|
|
|
|
https_health_check {
|
|
|
|
port = "443"
|
|
|
|
}
|
2016-11-18 17:08:26 +01:00
|
|
|
}
|
2017-06-06 14:11:19 +02:00
|
|
|
`, hckName)
|
|
|
|
}
|
2016-12-08 22:35:57 +01:00
|
|
|
|
2017-06-06 14:11:19 +02:00
|
|
|
func testAccComputeHealthCheck_tcpAndSsl_shouldFail(hckName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-12-08 22:35:57 +01:00
|
|
|
resource "google_compute_health_check" "foobar" {
|
|
|
|
check_interval_sec = 3
|
|
|
|
description = "Resource created for Terraform acceptance testing"
|
|
|
|
healthy_threshold = 3
|
|
|
|
name = "health-test-%s"
|
|
|
|
timeout_sec = 2
|
|
|
|
unhealthy_threshold = 3
|
|
|
|
|
|
|
|
tcp_health_check {
|
|
|
|
}
|
|
|
|
ssl_health_check {
|
|
|
|
}
|
|
|
|
}
|
2017-06-06 14:11:19 +02:00
|
|
|
`, hckName)
|
|
|
|
}
|