github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/resource_compute_region_backend_service_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 TestAccComputeRegionBackendService_basic(t *testing.T) { 14 serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 15 checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 16 extraCheckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 17 var svc compute.BackendService 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckComputeRegionBackendServiceDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccComputeRegionBackendService_basic(serviceName, checkName), 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckComputeRegionBackendServiceExists( 28 "google_compute_region_backend_service.foobar", &svc), 29 ), 30 }, 31 resource.TestStep{ 32 Config: testAccComputeRegionBackendService_basicModified( 33 serviceName, checkName, extraCheckName), 34 Check: resource.ComposeTestCheckFunc( 35 testAccCheckComputeRegionBackendServiceExists( 36 "google_compute_region_backend_service.foobar", &svc), 37 ), 38 }, 39 }, 40 }) 41 } 42 43 func TestAccComputeRegionBackendService_withBackend(t *testing.T) { 44 serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 45 igName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 46 itName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 47 checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 48 var svc compute.BackendService 49 resource.Test(t, resource.TestCase{ 50 PreCheck: func() { testAccPreCheck(t) }, 51 Providers: testAccProviders, 52 CheckDestroy: testAccCheckComputeRegionBackendServiceDestroy, 53 Steps: []resource.TestStep{ 54 resource.TestStep{ 55 Config: testAccComputeRegionBackendService_withBackend( 56 serviceName, igName, itName, checkName, 10), 57 Check: resource.ComposeTestCheckFunc( 58 testAccCheckComputeRegionBackendServiceExists( 59 "google_compute_region_backend_service.lipsum", &svc), 60 ), 61 }, 62 }, 63 }) 64 65 if svc.TimeoutSec != 10 { 66 t.Errorf("Expected TimeoutSec == 10, got %d", svc.TimeoutSec) 67 } 68 if svc.Protocol != "TCP" { 69 t.Errorf("Expected Protocol to be TCP, got %q", svc.Protocol) 70 } 71 if len(svc.Backends) != 1 { 72 t.Errorf("Expected 1 backend, got %d", len(svc.Backends)) 73 } 74 } 75 76 func TestAccComputeRegionBackendService_withBackendAndUpdate(t *testing.T) { 77 serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 78 igName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 79 itName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 80 checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 81 var svc compute.BackendService 82 resource.Test(t, resource.TestCase{ 83 PreCheck: func() { testAccPreCheck(t) }, 84 Providers: testAccProviders, 85 CheckDestroy: testAccCheckComputeRegionBackendServiceDestroy, 86 Steps: []resource.TestStep{ 87 resource.TestStep{ 88 Config: testAccComputeRegionBackendService_withBackend( 89 serviceName, igName, itName, checkName, 10), 90 Check: resource.ComposeTestCheckFunc( 91 testAccCheckComputeRegionBackendServiceExists( 92 "google_compute_region_backend_service.lipsum", &svc), 93 ), 94 }, 95 resource.TestStep{ 96 Config: testAccComputeRegionBackendService_withBackend( 97 serviceName, igName, itName, checkName, 20), 98 Check: resource.ComposeTestCheckFunc( 99 testAccCheckComputeRegionBackendServiceExists( 100 "google_compute_region_backend_service.lipsum", &svc), 101 ), 102 }, 103 }, 104 }) 105 106 if svc.TimeoutSec != 20 { 107 t.Errorf("Expected TimeoutSec == 20, got %d", svc.TimeoutSec) 108 } 109 if svc.Protocol != "TCP" { 110 t.Errorf("Expected Protocol to be TCP, got %q", svc.Protocol) 111 } 112 if len(svc.Backends) != 1 { 113 t.Errorf("Expected 1 backend, got %d", len(svc.Backends)) 114 } 115 } 116 117 func TestAccComputeRegionBackendService_withSessionAffinity(t *testing.T) { 118 serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 119 checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 120 var svc compute.BackendService 121 122 resource.Test(t, resource.TestCase{ 123 PreCheck: func() { testAccPreCheck(t) }, 124 Providers: testAccProviders, 125 CheckDestroy: testAccCheckComputeRegionBackendServiceDestroy, 126 Steps: []resource.TestStep{ 127 resource.TestStep{ 128 Config: testAccComputeRegionBackendService_withSessionAffinity( 129 serviceName, checkName), 130 Check: resource.ComposeTestCheckFunc( 131 testAccCheckComputeRegionBackendServiceExists( 132 "google_compute_region_backend_service.foobar", &svc), 133 ), 134 }, 135 }, 136 }) 137 138 if svc.SessionAffinity != "CLIENT_IP" { 139 t.Errorf("Expected Protocol to be CLIENT_IP, got %q", svc.SessionAffinity) 140 } 141 } 142 143 func testAccCheckComputeRegionBackendServiceDestroy(s *terraform.State) error { 144 config := testAccProvider.Meta().(*Config) 145 146 for _, rs := range s.RootModule().Resources { 147 if rs.Type != "google_compute_region_backend_service" { 148 continue 149 } 150 151 _, err := config.clientCompute.RegionBackendServices.Get( 152 config.Project, config.Region, rs.Primary.ID).Do() 153 if err == nil { 154 return fmt.Errorf("Backend service still exists") 155 } 156 } 157 158 return nil 159 } 160 161 func testAccCheckComputeRegionBackendServiceExists(n string, svc *compute.BackendService) resource.TestCheckFunc { 162 return func(s *terraform.State) error { 163 rs, ok := s.RootModule().Resources[n] 164 if !ok { 165 return fmt.Errorf("Not found: %s", n) 166 } 167 168 if rs.Primary.ID == "" { 169 return fmt.Errorf("No ID is set") 170 } 171 172 config := testAccProvider.Meta().(*Config) 173 174 found, err := config.clientCompute.RegionBackendServices.Get( 175 config.Project, config.Region, rs.Primary.ID).Do() 176 if err != nil { 177 return err 178 } 179 180 if found.Name != rs.Primary.ID { 181 return fmt.Errorf("Backend service not found") 182 } 183 184 *svc = *found 185 186 return nil 187 } 188 } 189 190 func testAccComputeRegionBackendService_basic(serviceName, checkName string) string { 191 return fmt.Sprintf(` 192 resource "google_compute_region_backend_service" "foobar" { 193 name = "%s" 194 health_checks = ["${google_compute_health_check.zero.self_link}"] 195 region = "us-central1" 196 } 197 198 resource "google_compute_health_check" "zero" { 199 name = "%s" 200 check_interval_sec = 1 201 timeout_sec = 1 202 203 tcp_health_check { 204 port = "80" 205 } 206 } 207 `, serviceName, checkName) 208 } 209 210 func testAccComputeRegionBackendService_basicModified(serviceName, checkOne, checkTwo string) string { 211 return fmt.Sprintf(` 212 resource "google_compute_region_backend_service" "foobar" { 213 name = "%s" 214 health_checks = ["${google_compute_health_check.one.self_link}"] 215 region = "us-central1" 216 } 217 218 resource "google_compute_health_check" "zero" { 219 name = "%s" 220 check_interval_sec = 1 221 timeout_sec = 1 222 223 tcp_health_check { 224 } 225 } 226 227 resource "google_compute_health_check" "one" { 228 name = "%s" 229 check_interval_sec = 30 230 timeout_sec = 30 231 232 tcp_health_check { 233 } 234 } 235 `, serviceName, checkOne, checkTwo) 236 } 237 238 func testAccComputeRegionBackendService_withBackend( 239 serviceName, igName, itName, checkName string, timeout int64) string { 240 return fmt.Sprintf(` 241 resource "google_compute_region_backend_service" "lipsum" { 242 name = "%s" 243 description = "Hello World 1234" 244 protocol = "TCP" 245 region = "us-central1" 246 timeout_sec = %v 247 248 backend { 249 group = "${google_compute_instance_group_manager.foobar.instance_group}" 250 } 251 252 health_checks = ["${google_compute_health_check.default.self_link}"] 253 } 254 255 resource "google_compute_instance_group_manager" "foobar" { 256 name = "%s" 257 instance_template = "${google_compute_instance_template.foobar.self_link}" 258 base_instance_name = "foobar" 259 zone = "us-central1-f" 260 target_size = 1 261 } 262 263 resource "google_compute_instance_template" "foobar" { 264 name = "%s" 265 machine_type = "n1-standard-1" 266 267 network_interface { 268 network = "default" 269 } 270 271 disk { 272 source_image = "debian-8-jessie-v20160803" 273 auto_delete = true 274 boot = true 275 } 276 } 277 278 resource "google_compute_health_check" "default" { 279 name = "%s" 280 check_interval_sec = 1 281 timeout_sec = 1 282 283 tcp_health_check { 284 285 } 286 } 287 `, serviceName, timeout, igName, itName, checkName) 288 } 289 290 func testAccComputeRegionBackendService_withSessionAffinity(serviceName, checkName string) string { 291 return fmt.Sprintf(` 292 resource "google_compute_region_backend_service" "foobar" { 293 name = "%s" 294 health_checks = ["${google_compute_health_check.zero.self_link}"] 295 region = "us-central1" 296 session_affinity = "CLIENT_IP" 297 298 } 299 300 resource "google_compute_health_check" "zero" { 301 name = "%s" 302 check_interval_sec = 1 303 timeout_sec = 1 304 305 tcp_health_check { 306 port = "80" 307 } 308 } 309 `, serviceName, checkName) 310 }