github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 testAccCheckComputeRegionBackendServiceDestroy(s *terraform.State) error { 118 config := testAccProvider.Meta().(*Config) 119 120 for _, rs := range s.RootModule().Resources { 121 if rs.Type != "google_compute_region_backend_service" { 122 continue 123 } 124 125 _, err := config.clientCompute.RegionBackendServices.Get( 126 config.Project, config.Region, rs.Primary.ID).Do() 127 if err == nil { 128 return fmt.Errorf("Backend service still exists") 129 } 130 } 131 132 return nil 133 } 134 135 func testAccCheckComputeRegionBackendServiceExists(n string, svc *compute.BackendService) resource.TestCheckFunc { 136 return func(s *terraform.State) error { 137 rs, ok := s.RootModule().Resources[n] 138 if !ok { 139 return fmt.Errorf("Not found: %s", n) 140 } 141 142 if rs.Primary.ID == "" { 143 return fmt.Errorf("No ID is set") 144 } 145 146 config := testAccProvider.Meta().(*Config) 147 148 found, err := config.clientCompute.RegionBackendServices.Get( 149 config.Project, config.Region, rs.Primary.ID).Do() 150 if err != nil { 151 return err 152 } 153 154 if found.Name != rs.Primary.ID { 155 return fmt.Errorf("Backend service not found") 156 } 157 158 *svc = *found 159 160 return nil 161 } 162 } 163 164 func testAccComputeRegionBackendService_basic(serviceName, checkName string) string { 165 return fmt.Sprintf(` 166 resource "google_compute_region_backend_service" "foobar" { 167 name = "%s" 168 health_checks = ["${google_compute_health_check.zero.self_link}"] 169 region = "us-central1" 170 } 171 172 resource "google_compute_health_check" "zero" { 173 name = "%s" 174 check_interval_sec = 1 175 timeout_sec = 1 176 177 tcp_health_check { 178 port = "80" 179 } 180 } 181 `, serviceName, checkName) 182 } 183 184 func testAccComputeRegionBackendService_basicModified(serviceName, checkOne, checkTwo string) string { 185 return fmt.Sprintf(` 186 resource "google_compute_region_backend_service" "foobar" { 187 name = "%s" 188 health_checks = ["${google_compute_health_check.one.self_link}"] 189 region = "us-central1" 190 } 191 192 resource "google_compute_health_check" "zero" { 193 name = "%s" 194 check_interval_sec = 1 195 timeout_sec = 1 196 197 tcp_health_check { 198 } 199 } 200 201 resource "google_compute_health_check" "one" { 202 name = "%s" 203 check_interval_sec = 30 204 timeout_sec = 30 205 206 tcp_health_check { 207 } 208 } 209 `, serviceName, checkOne, checkTwo) 210 } 211 212 func testAccComputeRegionBackendService_withBackend( 213 serviceName, igName, itName, checkName string, timeout int64) string { 214 return fmt.Sprintf(` 215 resource "google_compute_region_backend_service" "lipsum" { 216 name = "%s" 217 description = "Hello World 1234" 218 protocol = "TCP" 219 region = "us-central1" 220 timeout_sec = %v 221 222 backend { 223 group = "${google_compute_instance_group_manager.foobar.instance_group}" 224 } 225 226 health_checks = ["${google_compute_health_check.default.self_link}"] 227 } 228 229 resource "google_compute_instance_group_manager" "foobar" { 230 name = "%s" 231 instance_template = "${google_compute_instance_template.foobar.self_link}" 232 base_instance_name = "foobar" 233 zone = "us-central1-f" 234 target_size = 1 235 } 236 237 resource "google_compute_instance_template" "foobar" { 238 name = "%s" 239 machine_type = "n1-standard-1" 240 241 network_interface { 242 network = "default" 243 } 244 245 disk { 246 source_image = "debian-8-jessie-v20160803" 247 auto_delete = true 248 boot = true 249 } 250 } 251 252 resource "google_compute_health_check" "default" { 253 name = "%s" 254 check_interval_sec = 1 255 timeout_sec = 1 256 257 tcp_health_check { 258 259 } 260 } 261 `, serviceName, timeout, igName, itName, checkName) 262 }