From 88f4df28ac661a59e3200c64fd1d65b78c6f190a Mon Sep 17 00:00:00 2001 From: Dan Carley Date: Wed, 6 May 2015 22:11:14 +0100 Subject: [PATCH 1/3] provider/gce: Fix whitespace in test fixture Mixture of hard and soft tabs, which isn't picked up by `go fmt` because it's inside a string. Standardise on hard-tabs since that is what's used in the rest of the code. --- .../google/resource_compute_http_health_check_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/builtin/providers/google/resource_compute_http_health_check_test.go b/builtin/providers/google/resource_compute_http_health_check_test.go index 1797e9831209..ac98540dda8d 100644 --- a/builtin/providers/google/resource_compute_http_health_check_test.go +++ b/builtin/providers/google/resource_compute_http_health_check_test.go @@ -72,14 +72,14 @@ func testAccCheckComputeHttpHealthCheckExists(n string) resource.TestCheckFunc { const testAccComputeHttpHealthCheck_basic = ` resource "google_compute_http_health_check" "foobar" { - check_interval_sec = 3 + check_interval_sec = 3 description = "Resource created for Terraform acceptance testing" healthy_threshold = 3 host = "foobar" - name = "terraform-test" + name = "terraform-test" port = "80" - request_path = "/health_check" - timeout_sec = 2 + request_path = "/health_check" + timeout_sec = 2 unhealthy_threshold = 3 } ` From 13e9e6f51dc11b3d1963df0cd96a7a94f56a320c Mon Sep 17 00:00:00 2001 From: Dan Carley Date: Thu, 7 May 2015 08:13:06 +0100 Subject: [PATCH 2/3] provider/gce: Test updates to http_health_check By first creating a very simple resource that mostly uses the default values and then changing the two thresholds from their computed defaults. This currently fails with the following error and will be fixed in a subsequent commit: --- FAIL: TestAccComputeHttpHealthCheck_update (5.58s) testing.go:131: Step 1 error: Error applying: 1 error(s) occurred: * 1 error(s) occurred: * 1 error(s) occurred: * Error patching HttpHealthCheck: googleapi: Error 400: Invalid value for field 'resource.port': '0'. Must be greater than or equal to 1 More details: Reason: invalid, Message: Invalid value for field 'resource.port': '0'. Must be greater than or equal to 1 Reason: invalid, Message: Invalid value for field 'resource.checkIntervalSec': '0'. Must be greater than or equal to 1 Reason: invalid, Message: Invalid value for field 'resource.timeoutSec': '0'. Must be greater than or equal to 1 --- ...resource_compute_http_health_check_test.go | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/builtin/providers/google/resource_compute_http_health_check_test.go b/builtin/providers/google/resource_compute_http_health_check_test.go index ac98540dda8d..bc987af19bca 100644 --- a/builtin/providers/google/resource_compute_http_health_check_test.go +++ b/builtin/providers/google/resource_compute_http_health_check_test.go @@ -25,6 +25,32 @@ func TestAccComputeHttpHealthCheck_basic(t *testing.T) { }) } +func TestAccComputeHttpHealthCheck_update(t *testing.T) { + var healthCheck compute.HttpHealthCheck + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeHttpHealthCheckDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeHttpHealthCheck_update1, + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeHttpHealthCheckExists( + "google_compute_http_health_check.foobar", &healthCheck), + ), + }, + resource.TestStep{ + Config: testAccComputeHttpHealthCheck_update2, + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeHttpHealthCheckExists( + "google_compute_http_health_check.foobar", &healthCheck), + ), + }, + }, + }) +} + func testAccCheckComputeHttpHealthCheckDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -83,3 +109,22 @@ resource "google_compute_http_health_check" "foobar" { unhealthy_threshold = 3 } ` + +const testAccComputeHttpHealthCheck_update1 = ` +resource "google_compute_http_health_check" "foobar" { + name = "terraform-test" + description = "Resource created for Terraform acceptance testing" + request_path = "/not_default" +} +` + +/* Change description, restore request_path to default, and change +* thresholds from defaults */ +const testAccComputeHttpHealthCheck_update2 = ` +resource "google_compute_http_health_check" "foobar" { + name = "terraform-test" + description = "Resource updated for Terraform acceptance testing" + healthy_threshold = 10 + unhealthy_threshold = 10 +} +` From 579c37cd740b7b1027bf8a69f9a0417faca499dd Mon Sep 17 00:00:00 2001 From: Dan Carley Date: Thu, 7 May 2015 10:21:21 +0100 Subject: [PATCH 3/3] provider/gce: Set defaults for http_health_check In order to fix the failing test in the preceding commit when optional params are changed from their default "computed" values. These weren't working well with `HttpHealthCheck.Patch()` because it was attempting to set all unspecified params to Go's type defaults (eg. 0 for int64) which the API rejected. Changing the call to `HttpHealthCheck.Update()` seemed to fix this but it still didn't allow you to reset a param back to it's default by no longer specifying it. Settings defaults like this, which match the Terraform docs, seems like the best all round solution. Includes two additional tests for the acceptance tests which verify the params are really getting set correctly. --- .../resource_compute_http_health_check.go | 12 ++--- ...resource_compute_http_health_check_test.go | 45 ++++++++++++++++++- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/builtin/providers/google/resource_compute_http_health_check.go b/builtin/providers/google/resource_compute_http_health_check.go index 752302326832..4dfe3a03d16c 100644 --- a/builtin/providers/google/resource_compute_http_health_check.go +++ b/builtin/providers/google/resource_compute_http_health_check.go @@ -21,7 +21,7 @@ func resourceComputeHttpHealthCheck() *schema.Resource { "check_interval_sec": &schema.Schema{ Type: schema.TypeInt, Optional: true, - Computed: true, + Default: 5, }, "description": &schema.Schema{ @@ -32,7 +32,7 @@ func resourceComputeHttpHealthCheck() *schema.Resource { "healthy_threshold": &schema.Schema{ Type: schema.TypeInt, Optional: true, - Computed: true, + Default: 2, }, "host": &schema.Schema{ @@ -49,13 +49,13 @@ func resourceComputeHttpHealthCheck() *schema.Resource { "port": &schema.Schema{ Type: schema.TypeInt, Optional: true, - Computed: true, + Default: 80, }, "request_path": &schema.Schema{ Type: schema.TypeString, Optional: true, - Computed: true, + Default: "/", }, "self_link": &schema.Schema{ @@ -66,13 +66,13 @@ func resourceComputeHttpHealthCheck() *schema.Resource { "timeout_sec": &schema.Schema{ Type: schema.TypeInt, Optional: true, - Computed: true, + Default: 5, }, "unhealthy_threshold": &schema.Schema{ Type: schema.TypeInt, Optional: true, - Computed: true, + Default: 2, }, }, } diff --git a/builtin/providers/google/resource_compute_http_health_check_test.go b/builtin/providers/google/resource_compute_http_health_check_test.go index bc987af19bca..d071b5ae3f80 100644 --- a/builtin/providers/google/resource_compute_http_health_check_test.go +++ b/builtin/providers/google/resource_compute_http_health_check_test.go @@ -6,9 +6,12 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" + "google.golang.org/api/compute/v1" ) func TestAccComputeHttpHealthCheck_basic(t *testing.T) { + var healthCheck compute.HttpHealthCheck + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -18,7 +21,11 @@ func TestAccComputeHttpHealthCheck_basic(t *testing.T) { Config: testAccComputeHttpHealthCheck_basic, Check: resource.ComposeTestCheckFunc( testAccCheckComputeHttpHealthCheckExists( - "google_compute_http_health_check.foobar"), + "google_compute_http_health_check.foobar", &healthCheck), + testAccCheckComputeHttpHealthCheckRequestPath( + "/health_check", &healthCheck), + testAccCheckComputeHttpHealthCheckThresholds( + 3, 3, &healthCheck), ), }, }, @@ -38,6 +45,10 @@ func TestAccComputeHttpHealthCheck_update(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckComputeHttpHealthCheckExists( "google_compute_http_health_check.foobar", &healthCheck), + testAccCheckComputeHttpHealthCheckRequestPath( + "/not_default", &healthCheck), + testAccCheckComputeHttpHealthCheckThresholds( + 2, 2, &healthCheck), ), }, resource.TestStep{ @@ -45,6 +56,10 @@ func TestAccComputeHttpHealthCheck_update(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckComputeHttpHealthCheckExists( "google_compute_http_health_check.foobar", &healthCheck), + testAccCheckComputeHttpHealthCheckRequestPath( + "/", &healthCheck), + testAccCheckComputeHttpHealthCheckThresholds( + 10, 10, &healthCheck), ), }, }, @@ -69,7 +84,7 @@ func testAccCheckComputeHttpHealthCheckDestroy(s *terraform.State) error { return nil } -func testAccCheckComputeHttpHealthCheckExists(n string) resource.TestCheckFunc { +func testAccCheckComputeHttpHealthCheckExists(n string, healthCheck *compute.HttpHealthCheck) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -92,6 +107,32 @@ func testAccCheckComputeHttpHealthCheckExists(n string) resource.TestCheckFunc { return fmt.Errorf("HttpHealthCheck not found") } + *healthCheck = *found + + return nil + } +} + +func testAccCheckComputeHttpHealthCheckRequestPath(path string, healthCheck *compute.HttpHealthCheck) resource.TestCheckFunc { + return func(s *terraform.State) error { + if healthCheck.RequestPath != path { + return fmt.Errorf("RequestPath doesn't match: expected %d, got %d", path, healthCheck.RequestPath) + } + + return nil + } +} + +func testAccCheckComputeHttpHealthCheckThresholds(healthy, unhealthy int64, healthCheck *compute.HttpHealthCheck) 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 } }