github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 SSLCheckCert: true, 74 Weight: uint(100), 75 }, 76 }, 77 local: []map[string]interface{}{ 78 map[string]interface{}{ 79 "name": "test.notexample.com", 80 "address": "www.notexample.com", 81 "port": 80, 82 "auto_loadbalance": true, 83 "between_bytes_timeout": 10000, 84 "connect_timeout": 1000, 85 "error_threshold": 0, 86 "first_byte_timeout": 15000, 87 "max_conn": 200, 88 "ssl_check_cert": true, 89 "weight": 100, 90 }, 91 }, 92 }, 93 } 94 95 for _, c := range cases { 96 out := flattenBackends(c.remote) 97 if !reflect.DeepEqual(out, c.local) { 98 t.Fatalf("Error matching:\nexpected: %#v\ngot: %#v", c.local, out) 99 } 100 } 101 } 102 103 func TestAccFastlyServiceV1_updateDomain(t *testing.T) { 104 var service gofastly.ServiceDetail 105 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 106 nameUpdate := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 107 domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10)) 108 domainName2 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10)) 109 110 resource.Test(t, resource.TestCase{ 111 PreCheck: func() { testAccPreCheck(t) }, 112 Providers: testAccProviders, 113 CheckDestroy: testAccCheckServiceV1Destroy, 114 Steps: []resource.TestStep{ 115 resource.TestStep{ 116 Config: testAccServiceV1Config(name, domainName1), 117 Check: resource.ComposeTestCheckFunc( 118 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 119 testAccCheckFastlyServiceV1Attributes(&service, name, []string{domainName1}), 120 resource.TestCheckResourceAttr( 121 "fastly_service_v1.foo", "name", name), 122 resource.TestCheckResourceAttr( 123 "fastly_service_v1.foo", "active_version", "1"), 124 resource.TestCheckResourceAttr( 125 "fastly_service_v1.foo", "domain.#", "1"), 126 ), 127 }, 128 129 resource.TestStep{ 130 Config: testAccServiceV1Config_domainUpdate(nameUpdate, domainName1, domainName2), 131 Check: resource.ComposeTestCheckFunc( 132 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 133 testAccCheckFastlyServiceV1Attributes(&service, nameUpdate, []string{domainName1, domainName2}), 134 resource.TestCheckResourceAttr( 135 "fastly_service_v1.foo", "name", nameUpdate), 136 resource.TestCheckResourceAttr( 137 "fastly_service_v1.foo", "active_version", "2"), 138 resource.TestCheckResourceAttr( 139 "fastly_service_v1.foo", "domain.#", "2"), 140 ), 141 }, 142 }, 143 }) 144 } 145 146 func TestAccFastlyServiceV1_updateBackend(t *testing.T) { 147 var service gofastly.ServiceDetail 148 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 149 backendName := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3)) 150 backendName2 := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3)) 151 152 resource.Test(t, resource.TestCase{ 153 PreCheck: func() { testAccPreCheck(t) }, 154 Providers: testAccProviders, 155 CheckDestroy: testAccCheckServiceV1Destroy, 156 Steps: []resource.TestStep{ 157 resource.TestStep{ 158 Config: testAccServiceV1Config_backend(name, backendName), 159 Check: resource.ComposeTestCheckFunc( 160 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 161 testAccCheckFastlyServiceV1Attributes_backends(&service, name, []string{backendName}), 162 ), 163 }, 164 165 resource.TestStep{ 166 Config: testAccServiceV1Config_backend_update(name, backendName, backendName2), 167 Check: resource.ComposeTestCheckFunc( 168 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 169 testAccCheckFastlyServiceV1Attributes_backends(&service, name, []string{backendName, backendName2}), 170 resource.TestCheckResourceAttr( 171 "fastly_service_v1.foo", "active_version", "2"), 172 resource.TestCheckResourceAttr( 173 "fastly_service_v1.foo", "backend.#", "2"), 174 ), 175 }, 176 }, 177 }) 178 } 179 180 func TestAccFastlyServiceV1_basic(t *testing.T) { 181 var service gofastly.ServiceDetail 182 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 183 domainName := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10)) 184 185 resource.Test(t, resource.TestCase{ 186 PreCheck: func() { testAccPreCheck(t) }, 187 Providers: testAccProviders, 188 CheckDestroy: testAccCheckServiceV1Destroy, 189 Steps: []resource.TestStep{ 190 resource.TestStep{ 191 Config: testAccServiceV1Config(name, domainName), 192 Check: resource.ComposeTestCheckFunc( 193 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 194 testAccCheckFastlyServiceV1Attributes(&service, name, []string{domainName}), 195 resource.TestCheckResourceAttr( 196 "fastly_service_v1.foo", "name", name), 197 resource.TestCheckResourceAttr( 198 "fastly_service_v1.foo", "active_version", "1"), 199 resource.TestCheckResourceAttr( 200 "fastly_service_v1.foo", "domain.#", "1"), 201 ), 202 }, 203 }, 204 }) 205 } 206 207 // ServiceV1_disappears – test that a non-empty plan is returned when a Fastly 208 // Service is destroyed outside of Terraform, and can no longer be found, 209 // correctly clearing the ID field and generating a new plan 210 func TestAccFastlyServiceV1_disappears(t *testing.T) { 211 var service gofastly.ServiceDetail 212 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 213 domainName := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10)) 214 215 testDestroy := func(*terraform.State) error { 216 // reach out and DELETE the service 217 conn := testAccProvider.Meta().(*FastlyClient).conn 218 // deactivate active version to destoy 219 _, err := conn.DeactivateVersion(&gofastly.DeactivateVersionInput{ 220 Service: service.ID, 221 Version: service.ActiveVersion.Number, 222 }) 223 if err != nil { 224 return err 225 } 226 227 // delete service 228 err = conn.DeleteService(&gofastly.DeleteServiceInput{ 229 ID: service.ID, 230 }) 231 232 if err != nil { 233 return err 234 } 235 236 return nil 237 } 238 239 resource.Test(t, resource.TestCase{ 240 PreCheck: func() { testAccPreCheck(t) }, 241 Providers: testAccProviders, 242 CheckDestroy: testAccCheckServiceV1Destroy, 243 Steps: []resource.TestStep{ 244 resource.TestStep{ 245 Config: testAccServiceV1Config(name, domainName), 246 Check: resource.ComposeTestCheckFunc( 247 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 248 testDestroy, 249 ), 250 ExpectNonEmptyPlan: true, 251 }, 252 }, 253 }) 254 } 255 256 func testAccCheckServiceV1Exists(n string, service *gofastly.ServiceDetail) resource.TestCheckFunc { 257 return func(s *terraform.State) error { 258 rs, ok := s.RootModule().Resources[n] 259 if !ok { 260 return fmt.Errorf("Not found: %s", n) 261 } 262 263 if rs.Primary.ID == "" { 264 return fmt.Errorf("No Service ID is set") 265 } 266 267 conn := testAccProvider.Meta().(*FastlyClient).conn 268 latest, err := conn.GetServiceDetails(&gofastly.GetServiceInput{ 269 ID: rs.Primary.ID, 270 }) 271 272 if err != nil { 273 return err 274 } 275 276 *service = *latest 277 278 return nil 279 } 280 } 281 282 func testAccCheckFastlyServiceV1Attributes(service *gofastly.ServiceDetail, name string, domains []string) resource.TestCheckFunc { 283 return func(s *terraform.State) error { 284 285 if service.Name != name { 286 return fmt.Errorf("Bad name, expected (%s), got (%s)", name, service.Name) 287 } 288 289 conn := testAccProvider.Meta().(*FastlyClient).conn 290 domainList, err := conn.ListDomains(&gofastly.ListDomainsInput{ 291 Service: service.ID, 292 Version: service.ActiveVersion.Number, 293 }) 294 295 if err != nil { 296 return fmt.Errorf("[ERR] Error looking up Domains for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err) 297 } 298 299 expected := len(domains) 300 for _, d := range domainList { 301 for _, e := range domains { 302 if d.Name == e { 303 expected-- 304 } 305 } 306 } 307 308 if expected > 0 { 309 return fmt.Errorf("Domain count mismatch, expected: %#v, got: %#v", domains, domainList) 310 } 311 312 return nil 313 } 314 } 315 316 func testAccCheckFastlyServiceV1Attributes_backends(service *gofastly.ServiceDetail, name string, backends []string) resource.TestCheckFunc { 317 return func(s *terraform.State) error { 318 319 if service.Name != name { 320 return fmt.Errorf("Bad name, expected (%s), got (%s)", name, service.Name) 321 } 322 323 conn := testAccProvider.Meta().(*FastlyClient).conn 324 backendList, err := conn.ListBackends(&gofastly.ListBackendsInput{ 325 Service: service.ID, 326 Version: service.ActiveVersion.Number, 327 }) 328 329 if err != nil { 330 return fmt.Errorf("[ERR] Error looking up Backends for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err) 331 } 332 333 expected := len(backendList) 334 for _, b := range backendList { 335 for _, e := range backends { 336 if b.Address == e { 337 expected-- 338 } 339 } 340 } 341 342 if expected > 0 { 343 return fmt.Errorf("Backend count mismatch, expected: %#v, got: %#v", backends, backendList) 344 } 345 346 return nil 347 } 348 } 349 350 func testAccCheckServiceV1Destroy(s *terraform.State) error { 351 for _, rs := range s.RootModule().Resources { 352 if rs.Type != "fastly_service_v1" { 353 continue 354 } 355 356 conn := testAccProvider.Meta().(*FastlyClient).conn 357 l, err := conn.ListServices(&gofastly.ListServicesInput{}) 358 if err != nil { 359 return fmt.Errorf("[WARN] Error listing servcies when deleting Fastly Service (%s): %s", rs.Primary.ID, err) 360 } 361 362 for _, s := range l { 363 if s.ID == rs.Primary.ID { 364 // service still found 365 return fmt.Errorf("[WARN] Tried deleting Service (%s), but was still found", rs.Primary.ID) 366 } 367 } 368 } 369 return nil 370 } 371 372 func testAccServiceV1Config(name, domain string) string { 373 return fmt.Sprintf(` 374 resource "fastly_service_v1" "foo" { 375 name = "%s" 376 377 domain { 378 name = "%s" 379 comment = "tf-testing-domain" 380 } 381 382 backend { 383 address = "aws.amazon.com" 384 name = "amazon docs" 385 } 386 387 force_destroy = true 388 }`, name, domain) 389 } 390 391 func testAccServiceV1Config_domainUpdate(name, domain1, domain2 string) string { 392 return fmt.Sprintf(` 393 resource "fastly_service_v1" "foo" { 394 name = "%s" 395 396 domain { 397 name = "%s" 398 comment = "tf-testing-domain" 399 } 400 401 domain { 402 name = "%s" 403 comment = "tf-testing-other-domain" 404 } 405 406 backend { 407 address = "aws.amazon.com" 408 name = "amazon docs" 409 } 410 411 force_destroy = true 412 }`, name, domain1, domain2) 413 } 414 415 func testAccServiceV1Config_backend(name, backend string) string { 416 return fmt.Sprintf(` 417 resource "fastly_service_v1" "foo" { 418 name = "%s" 419 420 domain { 421 name = "test.notadomain.com" 422 comment = "tf-testing-domain" 423 } 424 425 backend { 426 address = "%s" 427 name = "tf -test backend" 428 } 429 430 force_destroy = true 431 }`, name, backend) 432 } 433 434 func testAccServiceV1Config_backend_update(name, backend, backend2 string) string { 435 return fmt.Sprintf(` 436 resource "fastly_service_v1" "foo" { 437 name = "%s" 438 439 default_ttl = 3400 440 441 domain { 442 name = "test.notadomain.com" 443 comment = "tf-testing-domain" 444 } 445 446 backend { 447 address = "%s" 448 name = "tf-test-backend" 449 } 450 451 backend { 452 address = "%s" 453 name = "tf-test-backend-other" 454 } 455 456 force_destroy = true 457 }`, name, backend, backend2) 458 }