github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/fastly/resource_fastly_service_v1_gzip_test.go (about) 1 package fastly 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/acctest" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/hashicorp/terraform/terraform" 11 gofastly "github.com/sethvargo/go-fastly" 12 ) 13 14 func TestFastlyServiceV1_FlattenGzips(t *testing.T) { 15 cases := []struct { 16 remote []*gofastly.Gzip 17 local []map[string]interface{} 18 }{ 19 { 20 remote: []*gofastly.Gzip{ 21 &gofastly.Gzip{ 22 Name: "somegzip", 23 Extensions: "css", 24 }, 25 }, 26 local: []map[string]interface{}{ 27 map[string]interface{}{ 28 "name": "somegzip", 29 "extensions": schema.NewSet(schema.HashString, []interface{}{"css"}), 30 }, 31 }, 32 }, 33 { 34 remote: []*gofastly.Gzip{ 35 &gofastly.Gzip{ 36 Name: "somegzip", 37 Extensions: "css json js", 38 ContentTypes: "text/html", 39 }, 40 &gofastly.Gzip{ 41 Name: "someothergzip", 42 Extensions: "css js", 43 ContentTypes: "text/html text/xml", 44 }, 45 }, 46 local: []map[string]interface{}{ 47 map[string]interface{}{ 48 "name": "somegzip", 49 "extensions": schema.NewSet(schema.HashString, []interface{}{"css", "json", "js"}), 50 "content_types": schema.NewSet(schema.HashString, []interface{}{"text/html"}), 51 }, 52 map[string]interface{}{ 53 "name": "someothergzip", 54 "extensions": schema.NewSet(schema.HashString, []interface{}{"css", "js"}), 55 "content_types": schema.NewSet(schema.HashString, []interface{}{"text/html", "text/xml"}), 56 }, 57 }, 58 }, 59 } 60 61 for _, c := range cases { 62 out := flattenGzips(c.remote) 63 // loop, because deepequal wont work with our sets 64 expectedCount := len(c.local) 65 var found int 66 for _, o := range out { 67 for _, l := range c.local { 68 if o["name"].(string) == l["name"].(string) { 69 found++ 70 if o["extensions"] == nil && l["extensions"] != nil { 71 t.Fatalf("output extensions are nil, local are not") 72 } 73 74 if o["extensions"] != nil { 75 oex := o["extensions"].(*schema.Set) 76 lex := l["extensions"].(*schema.Set) 77 if !oex.Equal(lex) { 78 t.Fatalf("Extensions don't match, expected: %#v, got: %#v", lex, oex) 79 } 80 } 81 82 if o["content_types"] == nil && l["content_types"] != nil { 83 t.Fatalf("output content types are nil, local are not") 84 } 85 86 if o["content_types"] != nil { 87 oct := o["content_types"].(*schema.Set) 88 lct := l["content_types"].(*schema.Set) 89 if !oct.Equal(lct) { 90 t.Fatalf("ContentTypes don't match, expected: %#v, got: %#v", lct, oct) 91 } 92 } 93 94 } 95 } 96 } 97 98 if found != expectedCount { 99 t.Fatalf("Found and expected mismatch: %d / %d", found, expectedCount) 100 } 101 } 102 } 103 104 func TestAccFastlyServiceV1_gzips_basic(t *testing.T) { 105 var service gofastly.ServiceDetail 106 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 107 domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10)) 108 109 resource.Test(t, resource.TestCase{ 110 PreCheck: func() { testAccPreCheck(t) }, 111 Providers: testAccProviders, 112 CheckDestroy: testAccCheckServiceV1Destroy, 113 Steps: []resource.TestStep{ 114 resource.TestStep{ 115 Config: testAccServiceV1GzipsConfig(name, domainName1), 116 Check: resource.ComposeTestCheckFunc( 117 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 118 testAccCheckFastlyServiceV1GzipsAttributes(&service, name, 2), 119 resource.TestCheckResourceAttr( 120 "fastly_service_v1.foo", "name", name), 121 resource.TestCheckResourceAttr( 122 "fastly_service_v1.foo", "gzip.#", "2"), 123 resource.TestCheckResourceAttr( 124 "fastly_service_v1.foo", "gzip.3704620722.extensions.#", "2"), 125 resource.TestCheckResourceAttr( 126 "fastly_service_v1.foo", "gzip.3704620722.content_types.#", "0"), 127 resource.TestCheckResourceAttr( 128 "fastly_service_v1.foo", "gzip.3820313126.content_types.#", "2"), 129 resource.TestCheckResourceAttr( 130 "fastly_service_v1.foo", "gzip.3820313126.extensions.#", "0"), 131 ), 132 }, 133 134 resource.TestStep{ 135 Config: testAccServiceV1GzipsConfig_update(name, domainName1), 136 Check: resource.ComposeTestCheckFunc( 137 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 138 testAccCheckFastlyServiceV1GzipsAttributes(&service, name, 1), 139 resource.TestCheckResourceAttr( 140 "fastly_service_v1.foo", "name", name), 141 resource.TestCheckResourceAttr( 142 "fastly_service_v1.foo", "gzip.#", "1"), 143 resource.TestCheckResourceAttr( 144 "fastly_service_v1.foo", "gzip.3694165387.extensions.#", "3"), 145 resource.TestCheckResourceAttr( 146 "fastly_service_v1.foo", "gzip.3694165387.content_types.#", "5"), 147 ), 148 }, 149 }, 150 }) 151 } 152 153 func testAccCheckFastlyServiceV1GzipsAttributes(service *gofastly.ServiceDetail, name string, gzipCount int) resource.TestCheckFunc { 154 return func(s *terraform.State) error { 155 156 if service.Name != name { 157 return fmt.Errorf("Bad name, expected (%s), got (%s)", name, service.Name) 158 } 159 160 conn := testAccProvider.Meta().(*FastlyClient).conn 161 gzipsList, err := conn.ListGzips(&gofastly.ListGzipsInput{ 162 Service: service.ID, 163 Version: service.ActiveVersion.Number, 164 }) 165 166 if err != nil { 167 return fmt.Errorf("[ERR] Error looking up Gzips for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err) 168 } 169 170 if len(gzipsList) != gzipCount { 171 return fmt.Errorf("Gzip count mismatch, expected (%d), got (%d)", gzipCount, len(gzipsList)) 172 } 173 174 return nil 175 } 176 } 177 178 func testAccServiceV1GzipsConfig(name, domain string) string { 179 return fmt.Sprintf(` 180 resource "fastly_service_v1" "foo" { 181 name = "%s" 182 183 domain { 184 name = "%s" 185 comment = "tf-testing-domain" 186 } 187 188 backend { 189 address = "aws.amazon.com" 190 name = "amazon docs" 191 } 192 193 gzip { 194 name = "gzip file types" 195 extensions = ["css", "js"] 196 } 197 198 gzip { 199 name = "gzip extensions" 200 content_types = ["text/html", "text/css"] 201 } 202 203 force_destroy = true 204 }`, name, domain) 205 } 206 207 func testAccServiceV1GzipsConfig_update(name, domain string) string { 208 return fmt.Sprintf(` 209 resource "fastly_service_v1" "foo" { 210 name = "%s" 211 212 domain { 213 name = "%s" 214 comment = "tf-testing-domain" 215 } 216 217 backend { 218 address = "aws.amazon.com" 219 name = "amazon docs" 220 } 221 222 gzip { 223 name = "all" 224 extensions = ["css", "js", "html"] 225 226 content_types = [ 227 "text/html", 228 "text/css", 229 "application/x-javascript", 230 "text/css", 231 "application/javascript", 232 "text/javascript", 233 ] 234 } 235 236 force_destroy = true 237 }`, name, domain) 238 }