2015-08-20 21:53:12 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-05 19:39:30 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-08-20 21:53:12 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccComputeBackendService_basic(t *testing.T) {
|
2016-01-05 19:39:30 +01:00
|
|
|
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
extraCheckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
2015-08-20 21:53:12 +02:00
|
|
|
var svc compute.BackendService
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeBackendServiceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-05 19:39:30 +01:00
|
|
|
Config: testAccComputeBackendService_basic(serviceName, checkName),
|
2015-08-20 21:53:12 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeBackendServiceExists(
|
|
|
|
"google_compute_backend_service.foobar", &svc),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-01-05 19:39:30 +01:00
|
|
|
Config: testAccComputeBackendService_basicModified(
|
|
|
|
serviceName, checkName, extraCheckName),
|
2015-08-20 21:53:12 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeBackendServiceExists(
|
|
|
|
"google_compute_backend_service.foobar", &svc),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccComputeBackendService_withBackend(t *testing.T) {
|
2016-01-05 19:39:30 +01:00
|
|
|
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
igName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
itName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
2015-08-20 21:53:12 +02:00
|
|
|
var svc compute.BackendService
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeBackendServiceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-05 19:39:30 +01:00
|
|
|
Config: testAccComputeBackendService_withBackend(
|
2016-08-08 02:28:43 +02:00
|
|
|
serviceName, igName, itName, checkName, 10),
|
2015-08-20 21:53:12 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeBackendServiceExists(
|
|
|
|
"google_compute_backend_service.lipsum", &svc),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if svc.TimeoutSec != 10 {
|
|
|
|
t.Errorf("Expected TimeoutSec == 10, got %d", svc.TimeoutSec)
|
|
|
|
}
|
|
|
|
if svc.Protocol != "HTTP" {
|
|
|
|
t.Errorf("Expected Protocol to be HTTP, got %q", svc.Protocol)
|
|
|
|
}
|
|
|
|
if len(svc.Backends) != 1 {
|
|
|
|
t.Errorf("Expected 1 backend, got %d", len(svc.Backends))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 02:28:43 +02:00
|
|
|
func TestAccComputeBackendService_withBackendAndUpdate(t *testing.T) {
|
|
|
|
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
igName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
itName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
var svc compute.BackendService
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeBackendServiceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeBackendService_withBackend(
|
|
|
|
serviceName, igName, itName, checkName, 10),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeBackendServiceExists(
|
|
|
|
"google_compute_backend_service.lipsum", &svc),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeBackendService_withBackend(
|
|
|
|
serviceName, igName, itName, checkName, 20),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeBackendServiceExists(
|
|
|
|
"google_compute_backend_service.lipsum", &svc),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if svc.TimeoutSec != 20 {
|
|
|
|
t.Errorf("Expected TimeoutSec == 20, got %d", svc.TimeoutSec)
|
|
|
|
}
|
|
|
|
if svc.Protocol != "HTTP" {
|
|
|
|
t.Errorf("Expected Protocol to be HTTP, got %q", svc.Protocol)
|
|
|
|
}
|
|
|
|
if len(svc.Backends) != 1 {
|
|
|
|
t.Errorf("Expected 1 backend, got %d", len(svc.Backends))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 00:17:49 +02:00
|
|
|
func TestAccComputeBackendService_withConnectionDraining(t *testing.T) {
|
|
|
|
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
var svc compute.BackendService
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeBackendServiceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeBackendService_withConnectionDraining(serviceName, checkName, 10),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeBackendServiceExists(
|
|
|
|
"google_compute_backend_service.foobar", &svc),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if svc.ConnectionDraining.DrainingTimeoutSec != 10 {
|
|
|
|
t.Errorf("Expected ConnectionDraining.DrainingTimeoutSec == 10, got %d", svc.ConnectionDraining.DrainingTimeoutSec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccComputeBackendService_withConnectionDrainingAndUpdate(t *testing.T) {
|
|
|
|
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
var svc compute.BackendService
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeBackendServiceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeBackendService_withConnectionDraining(serviceName, checkName, 10),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeBackendServiceExists(
|
|
|
|
"google_compute_backend_service.foobar", &svc),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeBackendService_basic(serviceName, checkName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeBackendServiceExists(
|
|
|
|
"google_compute_backend_service.foobar", &svc),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if svc.ConnectionDraining.DrainingTimeoutSec != 0 {
|
|
|
|
t.Errorf("Expected ConnectionDraining.DrainingTimeoutSec == 0, got %d", svc.ConnectionDraining.DrainingTimeoutSec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:53:12 +02:00
|
|
|
func testAccCheckComputeBackendServiceDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "google_compute_backend_service" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := config.clientCompute.BackendServices.Get(
|
|
|
|
config.Project, rs.Primary.ID).Do()
|
|
|
|
if err == nil {
|
2017-05-02 23:05:22 +02:00
|
|
|
return fmt.Errorf("Backend service %s still exists", rs.Primary.ID)
|
2015-08-20 21:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeBackendServiceExists(n string, svc *compute.BackendService) 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.BackendServices.Get(
|
|
|
|
config.Project, rs.Primary.ID).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.Name != rs.Primary.ID {
|
2017-05-02 23:05:22 +02:00
|
|
|
return fmt.Errorf("Backend service %s not found", rs.Primary.ID)
|
2015-08-20 21:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*svc = *found
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 23:57:33 +02:00
|
|
|
func TestAccComputeBackendService_withCDNEnabled(t *testing.T) {
|
|
|
|
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
var svc compute.BackendService
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeBackendServiceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeBackendService_withCDNEnabled(
|
|
|
|
serviceName, checkName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeBackendServiceExists(
|
|
|
|
"google_compute_backend_service.foobar", &svc),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if svc.EnableCDN != true {
|
|
|
|
t.Errorf("Expected EnableCDN == true, got %t", svc.EnableCDN)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:12:56 +01:00
|
|
|
func TestAccComputeBackendService_withSessionAffinity(t *testing.T) {
|
|
|
|
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
var svc compute.BackendService
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeBackendServiceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeBackendService_withSessionAffinity(
|
|
|
|
serviceName, checkName, "CLIENT_IP"),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeBackendServiceExists(
|
|
|
|
"google_compute_backend_service.foobar", &svc),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeBackendService_withSessionAffinity(
|
|
|
|
serviceName, checkName, "GENERATED_COOKIE"),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeBackendServiceExists(
|
|
|
|
"google_compute_backend_service.foobar", &svc),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if svc.SessionAffinity != "GENERATED_COOKIE" {
|
2016-12-12 21:03:21 +01:00
|
|
|
t.Errorf("Expected SessionAffinity == \"GENERATED_COOKIE\", got %s", svc.SessionAffinity)
|
2016-11-27 20:12:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 19:39:30 +01:00
|
|
|
func testAccComputeBackendService_basic(serviceName, checkName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-08-20 21:53:12 +02:00
|
|
|
resource "google_compute_backend_service" "foobar" {
|
2016-01-05 19:39:30 +01:00
|
|
|
name = "%s"
|
|
|
|
health_checks = ["${google_compute_http_health_check.zero.self_link}"]
|
2015-08-20 21:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_http_health_check" "zero" {
|
2016-01-05 19:39:30 +01:00
|
|
|
name = "%s"
|
|
|
|
request_path = "/"
|
|
|
|
check_interval_sec = 1
|
|
|
|
timeout_sec = 1
|
|
|
|
}
|
|
|
|
`, serviceName, checkName)
|
2015-08-20 21:53:12 +02:00
|
|
|
}
|
|
|
|
|
2016-07-28 23:57:33 +02:00
|
|
|
func testAccComputeBackendService_withCDNEnabled(serviceName, checkName string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_compute_backend_service" "foobar" {
|
|
|
|
name = "%s"
|
|
|
|
health_checks = ["${google_compute_http_health_check.zero.self_link}"]
|
|
|
|
enable_cdn = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_http_health_check" "zero" {
|
|
|
|
name = "%s"
|
|
|
|
request_path = "/"
|
|
|
|
check_interval_sec = 1
|
|
|
|
timeout_sec = 1
|
|
|
|
}
|
|
|
|
`, serviceName, checkName)
|
|
|
|
}
|
|
|
|
|
2016-01-05 19:39:30 +01:00
|
|
|
func testAccComputeBackendService_basicModified(serviceName, checkOne, checkTwo string) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-08-20 21:53:12 +02:00
|
|
|
resource "google_compute_backend_service" "foobar" {
|
2016-01-05 19:39:30 +01:00
|
|
|
name = "%s"
|
2015-08-20 21:53:12 +02:00
|
|
|
health_checks = ["${google_compute_http_health_check.one.self_link}"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_http_health_check" "zero" {
|
2016-01-05 19:39:30 +01:00
|
|
|
name = "%s"
|
2015-08-20 21:53:12 +02:00
|
|
|
request_path = "/"
|
|
|
|
check_interval_sec = 1
|
|
|
|
timeout_sec = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_http_health_check" "one" {
|
2016-01-05 19:39:30 +01:00
|
|
|
name = "%s"
|
2015-08-20 21:53:12 +02:00
|
|
|
request_path = "/one"
|
|
|
|
check_interval_sec = 30
|
|
|
|
timeout_sec = 30
|
|
|
|
}
|
2016-01-05 19:39:30 +01:00
|
|
|
`, serviceName, checkOne, checkTwo)
|
|
|
|
}
|
2015-08-20 21:53:12 +02:00
|
|
|
|
2016-01-05 19:39:30 +01:00
|
|
|
func testAccComputeBackendService_withBackend(
|
2016-08-08 02:28:43 +02:00
|
|
|
serviceName, igName, itName, checkName string, timeout int64) string {
|
2016-01-05 19:39:30 +01:00
|
|
|
return fmt.Sprintf(`
|
2015-08-20 21:53:12 +02:00
|
|
|
resource "google_compute_backend_service" "lipsum" {
|
2016-01-05 19:39:30 +01:00
|
|
|
name = "%s"
|
|
|
|
description = "Hello World 1234"
|
|
|
|
port_name = "http"
|
|
|
|
protocol = "HTTP"
|
2016-08-08 02:28:43 +02:00
|
|
|
timeout_sec = %v
|
2015-08-20 21:53:12 +02:00
|
|
|
|
2016-01-05 19:39:30 +01:00
|
|
|
backend {
|
|
|
|
group = "${google_compute_instance_group_manager.foobar.instance_group}"
|
|
|
|
}
|
2015-08-20 21:53:12 +02:00
|
|
|
|
2016-01-05 19:39:30 +01:00
|
|
|
health_checks = ["${google_compute_http_health_check.default.self_link}"]
|
2015-08-20 21:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_instance_group_manager" "foobar" {
|
2016-01-05 19:39:30 +01:00
|
|
|
name = "%s"
|
|
|
|
instance_template = "${google_compute_instance_template.foobar.self_link}"
|
|
|
|
base_instance_name = "foobar"
|
|
|
|
zone = "us-central1-f"
|
|
|
|
target_size = 1
|
2015-08-20 21:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_instance_template" "foobar" {
|
2016-01-05 19:39:30 +01:00
|
|
|
name = "%s"
|
|
|
|
machine_type = "n1-standard-1"
|
|
|
|
|
|
|
|
network_interface {
|
|
|
|
network = "default"
|
|
|
|
}
|
|
|
|
|
|
|
|
disk {
|
2016-08-19 04:31:45 +02:00
|
|
|
source_image = "debian-8-jessie-v20160803"
|
2016-01-05 19:39:30 +01:00
|
|
|
auto_delete = true
|
|
|
|
boot = true
|
|
|
|
}
|
2015-08-20 21:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_http_health_check" "default" {
|
2016-01-05 19:39:30 +01:00
|
|
|
name = "%s"
|
|
|
|
request_path = "/"
|
|
|
|
check_interval_sec = 1
|
|
|
|
timeout_sec = 1
|
|
|
|
}
|
2016-08-08 02:28:43 +02:00
|
|
|
`, serviceName, timeout, igName, itName, checkName)
|
2015-08-20 21:53:12 +02:00
|
|
|
}
|
2016-11-27 20:12:56 +01:00
|
|
|
|
|
|
|
func testAccComputeBackendService_withSessionAffinity(serviceName, checkName, affinityName string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_compute_backend_service" "foobar" {
|
|
|
|
name = "%s"
|
|
|
|
health_checks = ["${google_compute_http_health_check.zero.self_link}"]
|
|
|
|
session_affinity = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_http_health_check" "zero" {
|
|
|
|
name = "%s"
|
|
|
|
request_path = "/"
|
|
|
|
check_interval_sec = 1
|
|
|
|
timeout_sec = 1
|
|
|
|
}
|
|
|
|
`, serviceName, affinityName, checkName)
|
|
|
|
}
|
2017-06-08 00:17:49 +02:00
|
|
|
|
|
|
|
func testAccComputeBackendService_withConnectionDraining(serviceName, checkName string, drainingTimeout int64) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_compute_backend_service" "foobar" {
|
|
|
|
name = "%s"
|
|
|
|
health_checks = ["${google_compute_http_health_check.zero.self_link}"]
|
|
|
|
connection_draining_timeout_sec = %v
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_http_health_check" "zero" {
|
|
|
|
name = "%s"
|
|
|
|
request_path = "/"
|
|
|
|
check_interval_sec = 1
|
|
|
|
timeout_sec = 1
|
|
|
|
}
|
|
|
|
`, serviceName, drainingTimeout, checkName)
|
|
|
|
}
|