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