github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/fastly/resource_fastly_service_v1_cache_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 TestAccFastlyServiceV1CacheSetting_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  	cq1 := gofastly.CacheSetting{
    20  		Name:           "alt_backend",
    21  		Action:         "pass",
    22  		StaleTTL:       uint(3600),
    23  		CacheCondition: "serve_alt_backend",
    24  	}
    25  
    26  	cq2 := gofastly.CacheSetting{
    27  		Name:           "cache_backend",
    28  		Action:         "restart",
    29  		StaleTTL:       uint(1600),
    30  		CacheCondition: "cache_alt_backend",
    31  		TTL:            uint(300),
    32  	}
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckServiceV1Destroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccServiceV1CacheSetting(name, domainName1),
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    43  					testAccCheckFastlyServiceV1CacheSettingsAttributes(&service, []*gofastly.CacheSetting{&cq1}),
    44  					resource.TestCheckResourceAttr(
    45  						"fastly_service_v1.foo", "name", name),
    46  					resource.TestCheckResourceAttr(
    47  						"fastly_service_v1.foo", "cache_setting.#", "1"),
    48  					resource.TestCheckResourceAttr(
    49  						"fastly_service_v1.foo", "condition.#", "1"),
    50  				),
    51  			},
    52  
    53  			resource.TestStep{
    54  				Config: testAccServiceV1CacheSetting_update(name, domainName1),
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    57  					testAccCheckFastlyServiceV1CacheSettingsAttributes(&service, []*gofastly.CacheSetting{&cq1, &cq2}),
    58  					resource.TestCheckResourceAttr(
    59  						"fastly_service_v1.foo", "cache_setting.#", "2"),
    60  					resource.TestCheckResourceAttr(
    61  						"fastly_service_v1.foo", "condition.#", "2"),
    62  				),
    63  			},
    64  		},
    65  	})
    66  }
    67  
    68  func testAccCheckFastlyServiceV1CacheSettingsAttributes(service *gofastly.ServiceDetail, rqs []*gofastly.CacheSetting) resource.TestCheckFunc {
    69  	return func(s *terraform.State) error {
    70  
    71  		conn := testAccProvider.Meta().(*FastlyClient).conn
    72  		rqList, err := conn.ListCacheSettings(&gofastly.ListCacheSettingsInput{
    73  			Service: service.ID,
    74  			Version: service.ActiveVersion.Number,
    75  		})
    76  
    77  		if err != nil {
    78  			return fmt.Errorf("[ERR] Error looking up Request Setting for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err)
    79  		}
    80  
    81  		if len(rqList) != len(rqs) {
    82  			return fmt.Errorf("Request Setting List count mismatch, expected (%d), got (%d)", len(rqs), len(rqList))
    83  		}
    84  
    85  		var found int
    86  		for _, r := range rqs {
    87  			for _, lr := range rqList {
    88  				if r.Name == lr.Name {
    89  					// we don't know these things ahead of time, so populate them now
    90  					r.ServiceID = service.ID
    91  					r.Version = service.ActiveVersion.Number
    92  					if !reflect.DeepEqual(r, lr) {
    93  						return fmt.Errorf("Bad match Request Setting match, expected (%#v), got (%#v)", r, lr)
    94  					}
    95  					found++
    96  				}
    97  			}
    98  		}
    99  
   100  		if found != len(rqs) {
   101  			return fmt.Errorf("Error matching Request Setting rules (%d/%d)", found, len(rqs))
   102  		}
   103  
   104  		return nil
   105  	}
   106  }
   107  
   108  func testAccServiceV1CacheSetting(name, domain string) string {
   109  	return fmt.Sprintf(`
   110  resource "fastly_service_v1" "foo" {
   111    name = "%s"
   112  
   113    domain {
   114      name    = "%s"
   115      comment = "demo"
   116    }
   117  
   118    backend {
   119      address = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com"
   120      name    = "AWS S3 hosting"
   121      port    = 80
   122    }
   123  
   124    backend {
   125      address = "tftestingother.tftesting.net.s3-website-us-west-2.amazonaws.com"
   126      name    = "OtherAWSS3hosting"
   127      port    = 80
   128    }
   129  
   130    condition {
   131      name      = "serve_alt_backend"
   132      type      = "CACHE"
   133      priority  = 10
   134      statement = "req.url ~ \"^/alt/\""
   135    }
   136  
   137    cache_setting {
   138      name            = "alt_backend"
   139      stale_ttl       = 3600
   140      cache_condition = "serve_alt_backend"
   141      action          = "pass"
   142    }
   143  
   144    default_host = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com"
   145  
   146    force_destroy = true
   147  }`, name, domain)
   148  }
   149  
   150  func testAccServiceV1CacheSetting_update(name, domain string) string {
   151  	return fmt.Sprintf(`
   152  resource "fastly_service_v1" "foo" {
   153    name = "%s"
   154  
   155    domain {
   156      name    = "%s"
   157      comment = "demo"
   158    }
   159  
   160    backend {
   161      address = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com"
   162      name    = "AWS S3 hosting"
   163      port    = 80
   164    }
   165  
   166    backend {
   167      address = "tftestingother.tftesting.net.s3-website-us-west-2.amazonaws.com"
   168      name    = "OtherAWSS3hosting"
   169      port    = 80
   170    }
   171  
   172    condition {
   173      name      = "serve_alt_backend"
   174      type      = "CACHE"
   175      priority  = 10
   176      statement = "req.url ~ \"^/alt/\""
   177    }
   178  
   179    condition {
   180      name      = "cache_alt_backend"
   181      type      = "CACHE"
   182      priority  = 20
   183      statement = "req.url ~ \"^/cache/\""
   184    }
   185  
   186    cache_setting {
   187      name            = "alt_backend"
   188      stale_ttl       = 3600
   189      cache_condition = "serve_alt_backend"
   190      action          = "pass"
   191    }
   192  
   193    cache_setting {
   194      name            = "cache_backend"
   195      stale_ttl       = 1600
   196      cache_condition = "cache_alt_backend"
   197      action          = "restart"
   198      ttl             = 300
   199    }
   200  
   201    default_host = "tftesting.tftesting.net.s3-website-us-west-2.amazonaws.com"
   202  
   203    force_destroy = true
   204  }`, name, domain)
   205  }