github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_https_health_check_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "google.golang.org/api/compute/v1" 10 ) 11 12 func TestAccComputeHttpsHealthCheck_basic(t *testing.T) { 13 var healthCheck compute.HttpsHealthCheck 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckComputeHttpsHealthCheckDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccComputeHttpsHealthCheck_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckComputeHttpsHealthCheckExists( 24 "google_compute_https_health_check.foobar", &healthCheck), 25 testAccCheckComputeHttpsHealthCheckRequestPath( 26 "/health_check", &healthCheck), 27 testAccCheckComputeHttpsHealthCheckThresholds( 28 3, 3, &healthCheck), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func TestAccComputeHttpsHealthCheck_update(t *testing.T) { 36 var healthCheck compute.HttpsHealthCheck 37 38 resource.Test(t, resource.TestCase{ 39 PreCheck: func() { testAccPreCheck(t) }, 40 Providers: testAccProviders, 41 CheckDestroy: testAccCheckComputeHttpsHealthCheckDestroy, 42 Steps: []resource.TestStep{ 43 resource.TestStep{ 44 Config: testAccComputeHttpsHealthCheck_update1, 45 Check: resource.ComposeTestCheckFunc( 46 testAccCheckComputeHttpsHealthCheckExists( 47 "google_compute_https_health_check.foobar", &healthCheck), 48 testAccCheckComputeHttpsHealthCheckRequestPath( 49 "/not_default", &healthCheck), 50 testAccCheckComputeHttpsHealthCheckThresholds( 51 2, 2, &healthCheck), 52 ), 53 }, 54 resource.TestStep{ 55 Config: testAccComputeHttpsHealthCheck_update2, 56 Check: resource.ComposeTestCheckFunc( 57 testAccCheckComputeHttpsHealthCheckExists( 58 "google_compute_https_health_check.foobar", &healthCheck), 59 testAccCheckComputeHttpsHealthCheckRequestPath( 60 "/", &healthCheck), 61 testAccCheckComputeHttpsHealthCheckThresholds( 62 10, 10, &healthCheck), 63 ), 64 }, 65 }, 66 }) 67 } 68 69 func testAccCheckComputeHttpsHealthCheckDestroy(s *terraform.State) error { 70 config := testAccProvider.Meta().(*Config) 71 72 for _, rs := range s.RootModule().Resources { 73 if rs.Type != "google_compute_https_health_check" { 74 continue 75 } 76 77 _, err := config.clientCompute.HttpsHealthChecks.Get( 78 config.Project, rs.Primary.ID).Do() 79 if err == nil { 80 return fmt.Errorf("HttpsHealthCheck still exists") 81 } 82 } 83 84 return nil 85 } 86 87 func testAccCheckComputeHttpsHealthCheckExists(n string, healthCheck *compute.HttpsHealthCheck) resource.TestCheckFunc { 88 return func(s *terraform.State) error { 89 rs, ok := s.RootModule().Resources[n] 90 if !ok { 91 return fmt.Errorf("Not found: %s", n) 92 } 93 94 if rs.Primary.ID == "" { 95 return fmt.Errorf("No ID is set") 96 } 97 98 config := testAccProvider.Meta().(*Config) 99 100 found, err := config.clientCompute.HttpsHealthChecks.Get( 101 config.Project, rs.Primary.ID).Do() 102 if err != nil { 103 return err 104 } 105 106 if found.Name != rs.Primary.ID { 107 return fmt.Errorf("HttpsHealthCheck not found") 108 } 109 110 *healthCheck = *found 111 112 return nil 113 } 114 } 115 116 func testAccCheckComputeHttpsHealthCheckRequestPath(path string, healthCheck *compute.HttpsHealthCheck) resource.TestCheckFunc { 117 return func(s *terraform.State) error { 118 if healthCheck.RequestPath != path { 119 return fmt.Errorf("RequestPath doesn't match: expected %s, got %s", path, healthCheck.RequestPath) 120 } 121 122 return nil 123 } 124 } 125 126 func testAccCheckComputeHttpsHealthCheckThresholds(healthy, unhealthy int64, healthCheck *compute.HttpsHealthCheck) resource.TestCheckFunc { 127 return func(s *terraform.State) error { 128 if healthCheck.HealthyThreshold != healthy { 129 return fmt.Errorf("HealthyThreshold doesn't match: expected %d, got %d", healthy, healthCheck.HealthyThreshold) 130 } 131 132 if healthCheck.UnhealthyThreshold != unhealthy { 133 return fmt.Errorf("UnhealthyThreshold doesn't match: expected %d, got %d", unhealthy, healthCheck.UnhealthyThreshold) 134 } 135 136 return nil 137 } 138 } 139 140 const testAccComputeHttpsHealthCheck_basic = ` 141 resource "google_compute_https_health_check" "foobar" { 142 check_interval_sec = 3 143 description = "Resource created for Terraform acceptance testing" 144 healthy_threshold = 3 145 host = "foobar" 146 name = "terraform-test" 147 port = "80" 148 request_path = "/health_check" 149 timeout_sec = 2 150 unhealthy_threshold = 3 151 } 152 ` 153 154 const testAccComputeHttpsHealthCheck_update1 = ` 155 resource "google_compute_https_health_check" "foobar" { 156 name = "terraform-test" 157 description = "Resource created for Terraform acceptance testing" 158 request_path = "/not_default" 159 } 160 ` 161 162 /* Change description, restore request_path to default, and change 163 * thresholds from defaults */ 164 const testAccComputeHttpsHealthCheck_update2 = ` 165 resource "google_compute_https_health_check" "foobar" { 166 name = "terraform-test" 167 description = "Resource updated for Terraform acceptance testing" 168 healthy_threshold = 10 169 unhealthy_threshold = 10 170 } 171 `