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