github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/fastly/resource_fastly_service_v1_conditionals_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 TestAccFastlyServiceV1_conditional_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  	con1 := gofastly.Condition{
    20  		Name:      "some amz condition",
    21  		Priority:  10,
    22  		Type:      "REQUEST",
    23  		Statement: `req.url ~ "^/yolo/"`,
    24  	}
    25  
    26  	resource.Test(t, resource.TestCase{
    27  		PreCheck:     func() { testAccPreCheck(t) },
    28  		Providers:    testAccProviders,
    29  		CheckDestroy: testAccCheckServiceV1Destroy,
    30  		Steps: []resource.TestStep{
    31  			resource.TestStep{
    32  				Config: testAccServiceV1ConditionConfig(name, domainName1),
    33  				Check: resource.ComposeTestCheckFunc(
    34  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    35  					testAccCheckFastlyServiceV1ConditionalAttributes(&service, name, []*gofastly.Condition{&con1}),
    36  					resource.TestCheckResourceAttr(
    37  						"fastly_service_v1.foo", "name", name),
    38  					resource.TestCheckResourceAttr(
    39  						"fastly_service_v1.foo", "condition.#", "1"),
    40  				),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func testAccCheckFastlyServiceV1ConditionalAttributes(service *gofastly.ServiceDetail, name string, conditions []*gofastly.Condition) resource.TestCheckFunc {
    47  	return func(s *terraform.State) error {
    48  
    49  		if service.Name != name {
    50  			return fmt.Errorf("Bad name, expected (%s), got (%s)", name, service.Name)
    51  		}
    52  
    53  		conn := testAccProvider.Meta().(*FastlyClient).conn
    54  		conditionList, err := conn.ListConditions(&gofastly.ListConditionsInput{
    55  			Service: service.ID,
    56  			Version: service.ActiveVersion.Number,
    57  		})
    58  
    59  		if err != nil {
    60  			return fmt.Errorf("[ERR] Error looking up Conditions for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err)
    61  		}
    62  
    63  		if len(conditionList) != len(conditions) {
    64  			return fmt.Errorf("Error: mis match count of conditions, expected (%d), got (%d)", len(conditions), len(conditionList))
    65  		}
    66  
    67  		var found int
    68  		for _, c := range conditions {
    69  			for _, lc := range conditionList {
    70  				if c.Name == lc.Name {
    71  					// we don't know these things ahead of time, so populate them now
    72  					c.ServiceID = service.ID
    73  					c.Version = service.ActiveVersion.Number
    74  					if !reflect.DeepEqual(c, lc) {
    75  						return fmt.Errorf("Bad match Conditions match, expected (%#v), got (%#v)", c, lc)
    76  					}
    77  					found++
    78  				}
    79  			}
    80  		}
    81  
    82  		if found != len(conditions) {
    83  			return fmt.Errorf("Error matching Conditions rules")
    84  		}
    85  		return nil
    86  	}
    87  }
    88  
    89  func testAccServiceV1ConditionConfig(name, domain string) string {
    90  	return fmt.Sprintf(`
    91  resource "fastly_service_v1" "foo" {
    92    name = "%s"
    93  
    94    domain {
    95      name    = "%s"
    96      comment = "tf-testing-domain"
    97    }
    98  
    99    backend {
   100      address = "aws.amazon.com"
   101      name    = "amazon docs"
   102    }
   103  
   104    header {
   105      destination = "http.x-amz-request-id"
   106      type        = "cache"
   107      action      = "delete"
   108      name        = "remove x-amz-request-id"
   109    }
   110  
   111    condition {
   112      name = "some amz condition"
   113      type = "REQUEST"
   114  
   115      statement = "req.url ~ \"^/yolo/\""
   116  
   117      priority = 10
   118    }
   119  
   120    force_destroy = true
   121  }`, name, domain)
   122  }