github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/fastly/resource_fastly_service_v1_headers_test.go (about) 1 package fastly 2 3 import ( 4 "fmt" 5 "reflect" 6 "sort" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 gofastly "github.com/sethvargo/go-fastly" 13 ) 14 15 func TestFastlyServiceV1_BuildHeaders(t *testing.T) { 16 cases := []struct { 17 remote *gofastly.CreateHeaderInput 18 local map[string]interface{} 19 }{ 20 { 21 remote: &gofastly.CreateHeaderInput{ 22 Name: "someheadder", 23 Action: gofastly.HeaderActionDelete, 24 IgnoreIfSet: true, 25 Type: gofastly.HeaderTypeCache, 26 Destination: "http.aws-id", 27 Priority: uint(100), 28 }, 29 local: map[string]interface{}{ 30 "name": "someheadder", 31 "action": "delete", 32 "ignore_if_set": true, 33 "destination": "http.aws-id", 34 "priority": 100, 35 "source": "", 36 "regex": "", 37 "substitution": "", 38 "request_condition": "", 39 "cache_condition": "", 40 "response_condition": "", 41 "type": "cache", 42 }, 43 }, 44 { 45 remote: &gofastly.CreateHeaderInput{ 46 Name: "someheadder", 47 Action: gofastly.HeaderActionSet, 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 resource.Test(t, resource.TestCase{ 84 PreCheck: func() { testAccPreCheck(t) }, 85 Providers: testAccProviders, 86 CheckDestroy: testAccCheckServiceV1Destroy, 87 Steps: []resource.TestStep{ 88 resource.TestStep{ 89 Config: testAccServiceV1HeadersConfig(name, domainName1), 90 Check: resource.ComposeTestCheckFunc( 91 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 92 testAccCheckFastlyServiceV1HeaderAttributes(&service, name, []string{"http.x-amz-request-id", "http.Server"}, nil), 93 resource.TestCheckResourceAttr( 94 "fastly_service_v1.foo", "name", name), 95 resource.TestCheckResourceAttr( 96 "fastly_service_v1.foo", "header.#", "2"), 97 ), 98 }, 99 100 resource.TestStep{ 101 Config: testAccServiceV1HeadersConfig_update(name, domainName1), 102 Check: resource.ComposeTestCheckFunc( 103 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 104 testAccCheckFastlyServiceV1HeaderAttributes(&service, name, []string{"http.x-amz-request-id", "http.Server"}, []string{"http.server-name"}), 105 resource.TestCheckResourceAttr( 106 "fastly_service_v1.foo", "name", name), 107 resource.TestCheckResourceAttr( 108 "fastly_service_v1.foo", "header.#", "3"), 109 resource.TestCheckResourceAttr( 110 "fastly_service_v1.foo", "header.1147514417.source", "server.identity"), 111 ), 112 }, 113 }, 114 }) 115 } 116 117 func testAccCheckFastlyServiceV1HeaderAttributes(service *gofastly.ServiceDetail, name string, headersDeleted, headersAdded []string) resource.TestCheckFunc { 118 return func(s *terraform.State) error { 119 120 if service.Name != name { 121 return fmt.Errorf("Bad name, expected (%s), got (%s)", name, service.Name) 122 } 123 124 conn := testAccProvider.Meta().(*FastlyClient).conn 125 headersList, err := conn.ListHeaders(&gofastly.ListHeadersInput{ 126 Service: service.ID, 127 Version: service.ActiveVersion.Number, 128 }) 129 130 if err != nil { 131 return fmt.Errorf("[ERR] Error looking up Headers for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err) 132 } 133 134 var deleted []string 135 var added []string 136 for _, h := range headersList { 137 if h.Action == gofastly.HeaderActionDelete { 138 deleted = append(deleted, h.Destination) 139 } 140 if h.Action == gofastly.HeaderActionSet { 141 added = append(added, h.Destination) 142 } 143 } 144 145 sort.Strings(headersAdded) 146 sort.Strings(headersDeleted) 147 sort.Strings(deleted) 148 sort.Strings(added) 149 150 if !reflect.DeepEqual(headersDeleted, deleted) { 151 return fmt.Errorf("Deleted Headers did not match.\n\tExpected: (%#v)\n\tGot: (%#v)", headersDeleted, deleted) 152 } 153 if !reflect.DeepEqual(headersAdded, added) { 154 return fmt.Errorf("Added Headers did not match.\n\tExpected: (%#v)\n\tGot: (%#v)", headersAdded, added) 155 } 156 157 return nil 158 } 159 } 160 161 func testAccServiceV1HeadersConfig(name, domain string) string { 162 return fmt.Sprintf(` 163 resource "fastly_service_v1" "foo" { 164 name = "%s" 165 166 domain { 167 name = "%s" 168 comment = "tf-testing-domain" 169 } 170 171 backend { 172 address = "aws.amazon.com" 173 name = "amazon docs" 174 } 175 176 header { 177 destination = "http.x-amz-request-id" 178 type = "cache" 179 action = "delete" 180 name = "remove x-amz-request-id" 181 } 182 183 header { 184 destination = "http.Server" 185 type = "cache" 186 action = "delete" 187 name = "remove s3 server" 188 ignore_if_set = "true" 189 } 190 191 force_destroy = true 192 }`, name, domain) 193 } 194 195 func testAccServiceV1HeadersConfig_update(name, domain string) string { 196 return fmt.Sprintf(` 197 resource "fastly_service_v1" "foo" { 198 name = "%s" 199 200 domain { 201 name = "%s" 202 comment = "tf-testing-domain" 203 } 204 205 backend { 206 address = "aws.amazon.com" 207 name = "amazon docs" 208 } 209 210 header { 211 destination = "http.x-amz-request-id" 212 type = "cache" 213 action = "delete" 214 name = "remove x-amz-request-id" 215 } 216 217 header { 218 destination = "http.Server" 219 type = "cache" 220 action = "delete" 221 name = "DESTROY S3" 222 } 223 224 header { 225 destination = "http.server-name" 226 type = "request" 227 action = "set" 228 source = "server.identity" 229 name = "Add server name" 230 } 231 232 force_destroy = true 233 }`, name, domain) 234 }