github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/fastly/resource_fastly_service_v1_request_setting_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 TestAccFastlyServiceV1RequestSetting_basic(t *testing.T) { 15 var service gofastly.ServiceDetail 16 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 17 domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10)) 18 19 rq1 := gofastly.RequestSetting{ 20 Name: "alt_backend", 21 RequestCondition: "serve_alt_backend", 22 DefaultHost: "tftestingother.tftesting.net.s3-website-us-west-2.amazonaws.com", 23 XForwardedFor: "append", 24 MaxStaleAge: uint(90), 25 } 26 27 resource.Test(t, resource.TestCase{ 28 PreCheck: func() { testAccPreCheck(t) }, 29 Providers: testAccProviders, 30 CheckDestroy: testAccCheckServiceV1Destroy, 31 Steps: []resource.TestStep{ 32 resource.TestStep{ 33 Config: testAccServiceV1RequestSetting(name, domainName1), 34 Check: resource.ComposeTestCheckFunc( 35 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 36 testAccCheckFastlyServiceV1RequestSettingsAttributes(&service, []*gofastly.RequestSetting{&rq1}), 37 resource.TestCheckResourceAttr( 38 "fastly_service_v1.foo", "name", name), 39 resource.TestCheckResourceAttr( 40 "fastly_service_v1.foo", "request_setting.#", "1"), 41 resource.TestCheckResourceAttr( 42 "fastly_service_v1.foo", "condition.#", "1"), 43 ), 44 }, 45 }, 46 }) 47 } 48 49 func testAccCheckFastlyServiceV1RequestSettingsAttributes(service *gofastly.ServiceDetail, rqs []*gofastly.RequestSetting) resource.TestCheckFunc { 50 return func(s *terraform.State) error { 51 52 conn := testAccProvider.Meta().(*FastlyClient).conn 53 rqList, err := conn.ListRequestSettings(&gofastly.ListRequestSettingsInput{ 54 Service: service.ID, 55 Version: service.ActiveVersion.Number, 56 }) 57 58 if err != nil { 59 return fmt.Errorf("[ERR] Error looking up Request Setting for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err) 60 } 61 62 if len(rqList) != len(rqs) { 63 return fmt.Errorf("Request Setting List count mismatch, expected (%d), got (%d)", len(rqs), len(rqList)) 64 } 65 66 var found int 67 for _, r := range rqs { 68 for _, lr := range rqList { 69 if r.Name == lr.Name { 70 // we don't know these things ahead of time, so populate them now 71 r.ServiceID = service.ID 72 r.Version = service.ActiveVersion.Number 73 if !reflect.DeepEqual(r, lr) { 74 return fmt.Errorf("Bad match Request Setting match, expected (%#v), got (%#v)", r, lr) 75 } 76 found++ 77 } 78 } 79 } 80 81 if found != len(rqs) { 82 return fmt.Errorf("Error matching Request Setting rules (%d/%d)", found, len(rqs)) 83 } 84 85 return nil 86 } 87 } 88 89 func testAccServiceV1RequestSetting(name, domain string) string { 90 return fmt.Sprintf(` 91 resource "fastly_service_v1" "foo" { 92 name = "%s" 93 94 domain { 95 name = "%s" 96 comment = "demo" 97 } 98 99 backend { 100 address = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com" 101 name = "AWS S3 hosting" 102 port = 80 103 } 104 105 backend { 106 address = "tftestingother.tftesting.net.s3-website-us-west-2.amazonaws.com" 107 name = "OtherAWSS3hosting" 108 port = 80 109 } 110 111 condition { 112 name = "serve_alt_backend" 113 type = "REQUEST" 114 priority = 10 115 statement = "req.url ~ \"^/alt/\"" 116 } 117 118 request_setting { 119 default_host = "tftestingother.tftesting.net.s3-website-us-west-2.amazonaws.com" 120 name = "alt_backend" 121 request_condition = "serve_alt_backend" 122 max_stale_age = 90 123 } 124 125 default_host = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com" 126 127 force_destroy = true 128 }`, name, domain) 129 }