github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/fastly/resource_fastly_service_v1_test.go (about) 1 package fastly 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 gofastly "github.com/sethvargo/go-fastly" 12 ) 13 14 func TestResourceFastlyFlattenDomains(t *testing.T) { 15 cases := []struct { 16 remote []*gofastly.Domain 17 local []map[string]interface{} 18 }{ 19 { 20 remote: []*gofastly.Domain{ 21 &gofastly.Domain{ 22 Name: "test.notexample.com", 23 Comment: "not comment", 24 }, 25 }, 26 local: []map[string]interface{}{ 27 map[string]interface{}{ 28 "name": "test.notexample.com", 29 "comment": "not comment", 30 }, 31 }, 32 }, 33 { 34 remote: []*gofastly.Domain{ 35 &gofastly.Domain{ 36 Name: "test.notexample.com", 37 }, 38 }, 39 local: []map[string]interface{}{ 40 map[string]interface{}{ 41 "name": "test.notexample.com", 42 "comment": "", 43 }, 44 }, 45 }, 46 } 47 48 for _, c := range cases { 49 out := flattenDomains(c.remote) 50 if !reflect.DeepEqual(out, c.local) { 51 t.Fatalf("Error matching:\nexpected: %#v\ngot: %#v", c.local, out) 52 } 53 } 54 } 55 56 func TestResourceFastlyFlattenBackend(t *testing.T) { 57 cases := []struct { 58 remote []*gofastly.Backend 59 local []map[string]interface{} 60 }{ 61 { 62 remote: []*gofastly.Backend{ 63 &gofastly.Backend{ 64 Name: "test.notexample.com", 65 Address: "www.notexample.com", 66 Port: uint(80), 67 AutoLoadbalance: true, 68 BetweenBytesTimeout: uint(10000), 69 ConnectTimeout: uint(1000), 70 ErrorThreshold: uint(0), 71 FirstByteTimeout: uint(15000), 72 MaxConn: uint(200), 73 RequestCondition: "", 74 HealthCheck: "", 75 SSLCheckCert: true, 76 SSLHostname: "", 77 SSLCertHostname: "", 78 SSLSNIHostname: "", 79 Shield: "New York", 80 Weight: uint(100), 81 }, 82 }, 83 local: []map[string]interface{}{ 84 map[string]interface{}{ 85 "name": "test.notexample.com", 86 "address": "www.notexample.com", 87 "port": 80, 88 "auto_loadbalance": true, 89 "between_bytes_timeout": 10000, 90 "connect_timeout": 1000, 91 "error_threshold": 0, 92 "first_byte_timeout": 15000, 93 "max_conn": 200, 94 "request_condition": "", 95 "healthcheck": "", 96 "ssl_check_cert": true, 97 "ssl_hostname": "", 98 "ssl_cert_hostname": "", 99 "ssl_sni_hostname": "", 100 "shield": "New York", 101 "weight": 100, 102 }, 103 }, 104 }, 105 } 106 107 for _, c := range cases { 108 out := flattenBackends(c.remote) 109 if !reflect.DeepEqual(out, c.local) { 110 t.Fatalf("Error matching:\nexpected: %#v\ngot: %#v", c.local, out) 111 } 112 } 113 } 114 115 func TestAccFastlyServiceV1_updateDomain(t *testing.T) { 116 var service gofastly.ServiceDetail 117 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 118 nameUpdate := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 119 domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10)) 120 domainName2 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10)) 121 122 resource.Test(t, resource.TestCase{ 123 PreCheck: func() { testAccPreCheck(t) }, 124 Providers: testAccProviders, 125 CheckDestroy: testAccCheckServiceV1Destroy, 126 Steps: []resource.TestStep{ 127 resource.TestStep{ 128 Config: testAccServiceV1Config(name, domainName1), 129 Check: resource.ComposeTestCheckFunc( 130 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 131 testAccCheckFastlyServiceV1Attributes(&service, name, []string{domainName1}), 132 resource.TestCheckResourceAttr( 133 "fastly_service_v1.foo", "name", name), 134 resource.TestCheckResourceAttr( 135 "fastly_service_v1.foo", "active_version", "1"), 136 resource.TestCheckResourceAttr( 137 "fastly_service_v1.foo", "domain.#", "1"), 138 ), 139 }, 140 141 resource.TestStep{ 142 Config: testAccServiceV1Config_domainUpdate(nameUpdate, domainName1, domainName2), 143 Check: resource.ComposeTestCheckFunc( 144 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 145 testAccCheckFastlyServiceV1Attributes(&service, nameUpdate, []string{domainName1, domainName2}), 146 resource.TestCheckResourceAttr( 147 "fastly_service_v1.foo", "name", nameUpdate), 148 resource.TestCheckResourceAttr( 149 "fastly_service_v1.foo", "active_version", "2"), 150 resource.TestCheckResourceAttr( 151 "fastly_service_v1.foo", "domain.#", "2"), 152 ), 153 }, 154 }, 155 }) 156 } 157 158 func TestAccFastlyServiceV1_updateBackend(t *testing.T) { 159 var service gofastly.ServiceDetail 160 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 161 backendName := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3)) 162 backendName2 := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3)) 163 164 resource.Test(t, resource.TestCase{ 165 PreCheck: func() { testAccPreCheck(t) }, 166 Providers: testAccProviders, 167 CheckDestroy: testAccCheckServiceV1Destroy, 168 Steps: []resource.TestStep{ 169 resource.TestStep{ 170 Config: testAccServiceV1Config_backend(name, backendName), 171 Check: resource.ComposeTestCheckFunc( 172 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 173 testAccCheckFastlyServiceV1Attributes_backends(&service, name, []string{backendName}), 174 ), 175 }, 176 177 resource.TestStep{ 178 Config: testAccServiceV1Config_backend_update(name, backendName, backendName2, 3400), 179 Check: resource.ComposeTestCheckFunc( 180 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 181 testAccCheckFastlyServiceV1Attributes_backends(&service, name, []string{backendName, backendName2}), 182 resource.TestCheckResourceAttr( 183 "fastly_service_v1.foo", "active_version", "2"), 184 resource.TestCheckResourceAttr( 185 "fastly_service_v1.foo", "backend.#", "2"), 186 ), 187 }, 188 }, 189 }) 190 } 191 192 func TestAccFastlyServiceV1_basic(t *testing.T) { 193 var service gofastly.ServiceDetail 194 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 195 domainName := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10)) 196 197 resource.Test(t, resource.TestCase{ 198 PreCheck: func() { testAccPreCheck(t) }, 199 Providers: testAccProviders, 200 CheckDestroy: testAccCheckServiceV1Destroy, 201 Steps: []resource.TestStep{ 202 resource.TestStep{ 203 Config: testAccServiceV1Config(name, domainName), 204 Check: resource.ComposeTestCheckFunc( 205 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 206 testAccCheckFastlyServiceV1Attributes(&service, name, []string{domainName}), 207 resource.TestCheckResourceAttr( 208 "fastly_service_v1.foo", "name", name), 209 resource.TestCheckResourceAttr( 210 "fastly_service_v1.foo", "active_version", "1"), 211 resource.TestCheckResourceAttr( 212 "fastly_service_v1.foo", "domain.#", "1"), 213 ), 214 }, 215 }, 216 }) 217 } 218 219 // ServiceV1_disappears – test that a non-empty plan is returned when a Fastly 220 // Service is destroyed outside of Terraform, and can no longer be found, 221 // correctly clearing the ID field and generating a new plan 222 func TestAccFastlyServiceV1_disappears(t *testing.T) { 223 var service gofastly.ServiceDetail 224 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 225 domainName := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10)) 226 227 testDestroy := func(*terraform.State) error { 228 // reach out and DELETE the service 229 conn := testAccProvider.Meta().(*FastlyClient).conn 230 // deactivate active version to destoy 231 _, err := conn.DeactivateVersion(&gofastly.DeactivateVersionInput{ 232 Service: service.ID, 233 Version: service.ActiveVersion.Number, 234 }) 235 if err != nil { 236 return err 237 } 238 239 // delete service 240 err = conn.DeleteService(&gofastly.DeleteServiceInput{ 241 ID: service.ID, 242 }) 243 244 if err != nil { 245 return err 246 } 247 248 return nil 249 } 250 251 resource.Test(t, resource.TestCase{ 252 PreCheck: func() { testAccPreCheck(t) }, 253 Providers: testAccProviders, 254 CheckDestroy: testAccCheckServiceV1Destroy, 255 Steps: []resource.TestStep{ 256 resource.TestStep{ 257 Config: testAccServiceV1Config(name, domainName), 258 Check: resource.ComposeTestCheckFunc( 259 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 260 testDestroy, 261 ), 262 ExpectNonEmptyPlan: true, 263 }, 264 }, 265 }) 266 } 267 268 func testAccCheckServiceV1Exists(n string, service *gofastly.ServiceDetail) resource.TestCheckFunc { 269 return func(s *terraform.State) error { 270 rs, ok := s.RootModule().Resources[n] 271 if !ok { 272 return fmt.Errorf("Not found: %s", n) 273 } 274 275 if rs.Primary.ID == "" { 276 return fmt.Errorf("No Service ID is set") 277 } 278 279 conn := testAccProvider.Meta().(*FastlyClient).conn 280 latest, err := conn.GetServiceDetails(&gofastly.GetServiceInput{ 281 ID: rs.Primary.ID, 282 }) 283 284 if err != nil { 285 return err 286 } 287 288 *service = *latest 289 290 return nil 291 } 292 } 293 294 func testAccCheckFastlyServiceV1Attributes(service *gofastly.ServiceDetail, name string, domains []string) resource.TestCheckFunc { 295 return func(s *terraform.State) error { 296 297 if service.Name != name { 298 return fmt.Errorf("Bad name, expected (%s), got (%s)", name, service.Name) 299 } 300 301 conn := testAccProvider.Meta().(*FastlyClient).conn 302 domainList, err := conn.ListDomains(&gofastly.ListDomainsInput{ 303 Service: service.ID, 304 Version: service.ActiveVersion.Number, 305 }) 306 307 if err != nil { 308 return fmt.Errorf("[ERR] Error looking up Domains for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err) 309 } 310 311 expected := len(domains) 312 for _, d := range domainList { 313 for _, e := range domains { 314 if d.Name == e { 315 expected-- 316 } 317 } 318 } 319 320 if expected > 0 { 321 return fmt.Errorf("Domain count mismatch, expected: %#v, got: %#v", domains, domainList) 322 } 323 324 return nil 325 } 326 } 327 328 func testAccCheckFastlyServiceV1Attributes_backends(service *gofastly.ServiceDetail, name string, backends []string) resource.TestCheckFunc { 329 return func(s *terraform.State) error { 330 331 if service.Name != name { 332 return fmt.Errorf("Bad name, expected (%s), got (%s)", name, service.Name) 333 } 334 335 conn := testAccProvider.Meta().(*FastlyClient).conn 336 backendList, err := conn.ListBackends(&gofastly.ListBackendsInput{ 337 Service: service.ID, 338 Version: service.ActiveVersion.Number, 339 }) 340 341 if err != nil { 342 return fmt.Errorf("[ERR] Error looking up Backends for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err) 343 } 344 345 expected := len(backendList) 346 for _, b := range backendList { 347 for _, e := range backends { 348 if b.Address == e { 349 expected-- 350 } 351 } 352 } 353 354 if expected > 0 { 355 return fmt.Errorf("Backend count mismatch, expected: %#v, got: %#v", backends, backendList) 356 } 357 358 return nil 359 } 360 } 361 362 func TestAccFastlyServiceV1_defaultTTL(t *testing.T) { 363 var service gofastly.ServiceDetail 364 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 365 backendName := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3)) 366 backendName2 := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3)) 367 368 resource.Test(t, resource.TestCase{ 369 PreCheck: func() { testAccPreCheck(t) }, 370 Providers: testAccProviders, 371 CheckDestroy: testAccCheckServiceV1Destroy, 372 Steps: []resource.TestStep{ 373 resource.TestStep{ 374 Config: testAccServiceV1Config_backend(name, backendName), 375 Check: resource.ComposeTestCheckFunc( 376 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 377 testAccCheckFastlyServiceV1Attributes_backends(&service, name, []string{backendName}), 378 ), 379 }, 380 381 resource.TestStep{ 382 Config: testAccServiceV1Config_backend_update(name, backendName, backendName2, 3400), 383 Check: resource.ComposeTestCheckFunc( 384 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 385 testAccCheckFastlyServiceV1Attributes_backends(&service, name, []string{backendName, backendName2}), 386 resource.TestCheckResourceAttr( 387 "fastly_service_v1.foo", "default_ttl", "3400"), 388 resource.TestCheckResourceAttr( 389 "fastly_service_v1.foo", "active_version", "2"), 390 ), 391 }, 392 // Now update the default_ttl to 0 and encounter the issue https://github.com/hashicorp/terraform/issues/12910 393 resource.TestStep{ 394 Config: testAccServiceV1Config_backend_update(name, backendName, backendName2, 0), 395 Check: resource.ComposeTestCheckFunc( 396 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 397 testAccCheckFastlyServiceV1Attributes_backends(&service, name, []string{backendName, backendName2}), 398 resource.TestCheckResourceAttr( 399 "fastly_service_v1.foo", "default_ttl", "0"), 400 resource.TestCheckResourceAttr( 401 "fastly_service_v1.foo", "active_version", "3"), 402 ), 403 }, 404 }, 405 }) 406 } 407 408 func testAccCheckServiceV1Destroy(s *terraform.State) error { 409 for _, rs := range s.RootModule().Resources { 410 if rs.Type != "fastly_service_v1" { 411 continue 412 } 413 414 conn := testAccProvider.Meta().(*FastlyClient).conn 415 l, err := conn.ListServices(&gofastly.ListServicesInput{}) 416 if err != nil { 417 return fmt.Errorf("[WARN] Error listing servcies when deleting Fastly Service (%s): %s", rs.Primary.ID, err) 418 } 419 420 for _, s := range l { 421 if s.ID == rs.Primary.ID { 422 // service still found 423 return fmt.Errorf("[WARN] Tried deleting Service (%s), but was still found", rs.Primary.ID) 424 } 425 } 426 } 427 return nil 428 } 429 430 func testAccServiceV1Config(name, domain string) string { 431 return fmt.Sprintf(` 432 resource "fastly_service_v1" "foo" { 433 name = "%s" 434 435 domain { 436 name = "%s" 437 comment = "tf-testing-domain" 438 } 439 440 backend { 441 address = "aws.amazon.com" 442 name = "amazon docs" 443 } 444 445 force_destroy = true 446 }`, name, domain) 447 } 448 449 func testAccServiceV1Config_domainUpdate(name, domain1, domain2 string) string { 450 return fmt.Sprintf(` 451 resource "fastly_service_v1" "foo" { 452 name = "%s" 453 454 domain { 455 name = "%s" 456 comment = "tf-testing-domain" 457 } 458 459 domain { 460 name = "%s" 461 comment = "tf-testing-other-domain" 462 } 463 464 backend { 465 address = "aws.amazon.com" 466 name = "amazon docs" 467 } 468 469 force_destroy = true 470 }`, name, domain1, domain2) 471 } 472 473 func testAccServiceV1Config_backend(name, backend string) string { 474 return fmt.Sprintf(` 475 resource "fastly_service_v1" "foo" { 476 name = "%s" 477 478 domain { 479 name = "test.notadomain.com" 480 comment = "tf-testing-domain" 481 } 482 483 backend { 484 address = "%s" 485 name = "tf -test backend" 486 } 487 488 force_destroy = true 489 }`, name, backend) 490 } 491 492 func testAccServiceV1Config_backend_update(name, backend, backend2 string, ttl uint) string { 493 return fmt.Sprintf(` 494 resource "fastly_service_v1" "foo" { 495 name = "%s" 496 497 default_ttl = %d 498 499 domain { 500 name = "test.notadomain.com" 501 comment = "tf-testing-domain" 502 } 503 504 backend { 505 address = "%s" 506 name = "tf-test-backend" 507 } 508 509 backend { 510 address = "%s" 511 name = "tf-test-backend-other" 512 } 513 514 force_destroy = true 515 }`, name, ttl, backend, backend2) 516 }