github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/fastly/resource_fastly_service_v1_headers_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 TestFastlyServiceV1_BuildHeaders(t *testing.T) { 15 cases := []struct { 16 remote *gofastly.CreateHeaderInput 17 local map[string]interface{} 18 }{ 19 { 20 remote: &gofastly.CreateHeaderInput{ 21 Name: "someheadder", 22 Action: gofastly.HeaderActionDelete, 23 IgnoreIfSet: gofastly.CBool(true), 24 Type: gofastly.HeaderTypeCache, 25 Destination: "http.aws-id", 26 Priority: uint(100), 27 }, 28 local: map[string]interface{}{ 29 "name": "someheadder", 30 "action": "delete", 31 "ignore_if_set": true, 32 "destination": "http.aws-id", 33 "priority": 100, 34 "source": "", 35 "regex": "", 36 "substitution": "", 37 "request_condition": "", 38 "cache_condition": "", 39 "response_condition": "", 40 "type": "cache", 41 }, 42 }, 43 { 44 remote: &gofastly.CreateHeaderInput{ 45 Name: "someheadder", 46 Action: gofastly.HeaderActionSet, 47 IgnoreIfSet: gofastly.CBool(false), 48 Type: gofastly.HeaderTypeCache, 49 Destination: "http.aws-id", 50 Priority: uint(100), 51 Source: "http.server-name", 52 }, 53 local: map[string]interface{}{ 54 "name": "someheadder", 55 "action": "set", 56 "ignore_if_set": false, 57 "destination": "http.aws-id", 58 "priority": 100, 59 "source": "http.server-name", 60 "regex": "", 61 "substitution": "", 62 "request_condition": "", 63 "cache_condition": "", 64 "response_condition": "", 65 "type": "cache", 66 }, 67 }, 68 } 69 70 for _, c := range cases { 71 out, _ := buildHeader(c.local) 72 if !reflect.DeepEqual(out, c.remote) { 73 t.Fatalf("Error matching:\nexpected: %#v\ngot: %#v", c.remote, out) 74 } 75 } 76 } 77 78 func TestAccFastlyServiceV1_headers_basic(t *testing.T) { 79 var service gofastly.ServiceDetail 80 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 81 domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10)) 82 83 log1 := gofastly.Header{ 84 Version: 1, 85 Name: "remove x-amz-request-id", 86 Destination: "http.x-amz-request-id", 87 Type: "cache", 88 Action: "delete", 89 Priority: uint(100), 90 } 91 92 log2 := gofastly.Header{ 93 Version: 1, 94 Name: "remove s3 server", 95 Destination: "http.Server", 96 Type: "cache", 97 Action: "delete", 98 IgnoreIfSet: true, 99 Priority: uint(100), 100 } 101 102 log3 := gofastly.Header{ 103 Version: 1, 104 Name: "DESTROY S3", 105 Destination: "http.Server", 106 Type: "cache", 107 Action: "delete", 108 Priority: uint(100), 109 } 110 111 log4 := gofastly.Header{ 112 Version: 1, 113 Name: "Add server name", 114 Destination: "http.server-name", 115 Type: "request", 116 Action: "set", 117 Source: "server.identity", 118 Priority: uint(100), 119 RequestCondition: "test_req_condition", 120 CacheCondition: "test_cache_condition", 121 ResponseCondition: "test_res_condition", 122 } 123 124 resource.Test(t, resource.TestCase{ 125 PreCheck: func() { testAccPreCheck(t) }, 126 Providers: testAccProviders, 127 CheckDestroy: testAccCheckServiceV1Destroy, 128 Steps: []resource.TestStep{ 129 resource.TestStep{ 130 Config: testAccServiceV1HeadersConfig(name, domainName1), 131 Check: resource.ComposeTestCheckFunc( 132 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 133 testAccCheckFastlyServiceV1HeaderAttributes(&service, []*gofastly.Header{&log1, &log2}), 134 resource.TestCheckResourceAttr( 135 "fastly_service_v1.foo", "name", name), 136 resource.TestCheckResourceAttr( 137 "fastly_service_v1.foo", "header.#", "2"), 138 ), 139 }, 140 141 resource.TestStep{ 142 Config: testAccServiceV1HeadersConfig_update(name, domainName1), 143 Check: resource.ComposeTestCheckFunc( 144 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 145 testAccCheckFastlyServiceV1HeaderAttributes(&service, []*gofastly.Header{&log1, &log3, &log4}), 146 resource.TestCheckResourceAttr( 147 "fastly_service_v1.foo", "name", name), 148 resource.TestCheckResourceAttr( 149 "fastly_service_v1.foo", "header.#", "3"), 150 ), 151 }, 152 }, 153 }) 154 } 155 156 func testAccCheckFastlyServiceV1HeaderAttributes(service *gofastly.ServiceDetail, headers []*gofastly.Header) resource.TestCheckFunc { 157 return func(s *terraform.State) error { 158 159 conn := testAccProvider.Meta().(*FastlyClient).conn 160 headersList, err := conn.ListHeaders(&gofastly.ListHeadersInput{ 161 Service: service.ID, 162 Version: service.ActiveVersion.Number, 163 }) 164 165 if err != nil { 166 return fmt.Errorf("[ERR] Error looking up Headers for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err) 167 } 168 169 if len(headersList) != len(headers) { 170 return fmt.Errorf("Healthcheck List count mismatch, expected (%d), got (%d)", len(headers), len(headersList)) 171 } 172 173 var found int 174 for _, h := range headers { 175 for _, lh := range headersList { 176 if h.Name == lh.Name { 177 // we don't know these things ahead of time, so populate them now 178 h.ServiceID = service.ID 179 h.Version = service.ActiveVersion.Number 180 if !reflect.DeepEqual(h, lh) { 181 return fmt.Errorf("Bad match Header match, expected (%#v), got (%#v)", h, lh) 182 } 183 found++ 184 } 185 } 186 } 187 188 if found != len(headers) { 189 return fmt.Errorf("Error matching Header rules") 190 } 191 192 return nil 193 } 194 } 195 196 func testAccServiceV1HeadersConfig(name, domain string) string { 197 return fmt.Sprintf(` 198 resource "fastly_service_v1" "foo" { 199 name = "%s" 200 201 domain { 202 name = "%s" 203 comment = "tf-testing-domain" 204 } 205 206 backend { 207 address = "aws.amazon.com" 208 name = "amazon docs" 209 } 210 211 header { 212 destination = "http.x-amz-request-id" 213 type = "cache" 214 action = "delete" 215 name = "remove x-amz-request-id" 216 } 217 218 header { 219 destination = "http.Server" 220 type = "cache" 221 action = "delete" 222 name = "remove s3 server" 223 ignore_if_set = "true" 224 } 225 226 force_destroy = true 227 }`, name, domain) 228 } 229 230 func testAccServiceV1HeadersConfig_update(name, domain string) string { 231 return fmt.Sprintf(` 232 resource "fastly_service_v1" "foo" { 233 name = "%s" 234 235 domain { 236 name = "%s" 237 comment = "tf-testing-domain" 238 } 239 240 backend { 241 address = "aws.amazon.com" 242 name = "amazon docs" 243 } 244 245 header { 246 destination = "http.x-amz-request-id" 247 type = "cache" 248 action = "delete" 249 name = "remove x-amz-request-id" 250 } 251 252 header { 253 destination = "http.Server" 254 type = "cache" 255 action = "delete" 256 name = "DESTROY S3" 257 } 258 259 condition { 260 name = "test_req_condition" 261 type = "REQUEST" 262 priority = 5 263 statement = "req.url ~ \"^/foo/bar$\"" 264 } 265 266 condition { 267 name = "test_cache_condition" 268 type = "CACHE" 269 priority = 9 270 statement = "req.url ~ \"^/articles/\"" 271 } 272 273 condition { 274 name = "test_res_condition" 275 type = "RESPONSE" 276 priority = 10 277 statement = "resp.status == 404" 278 } 279 280 header { 281 destination = "http.server-name" 282 type = "request" 283 action = "set" 284 source = "server.identity" 285 name = "Add server name" 286 request_condition = "test_req_condition" 287 cache_condition = "test_cache_condition" 288 response_condition = "test_res_condition" 289 } 290 291 force_destroy = true 292 }`, name, domain) 293 }