github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_load_balancer_policy_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/elb"
    12  
    13  	"github.com/hashicorp/terraform/helper/acctest"
    14  	"github.com/hashicorp/terraform/helper/resource"
    15  	"github.com/hashicorp/terraform/terraform"
    16  )
    17  
    18  func TestAccAWSLoadBalancerPolicy_basic(t *testing.T) {
    19  	rInt := acctest.RandInt()
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckAWSLoadBalancerPolicyDestroy,
    24  		Steps: []resource.TestStep{
    25  			{
    26  				Config: testAccAWSLoadBalancerPolicyConfig_basic(rInt),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckAWSLoadBalancerPolicyState("aws_elb.test-lb", "aws_load_balancer_policy.test-policy"),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccAWSLoadBalancerPolicy_updateWhileAssigned(t *testing.T) {
    36  	rInt := acctest.RandInt()
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckAWSLoadBalancerPolicyDestroy,
    41  		Steps: []resource.TestStep{
    42  			{
    43  				Config: testAccAWSLoadBalancerPolicyConfig_updateWhileAssigned0(rInt),
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckAWSLoadBalancerPolicyState("aws_elb.test-lb", "aws_load_balancer_policy.test-policy"),
    46  				),
    47  			},
    48  			{
    49  				Config: testAccAWSLoadBalancerPolicyConfig_updateWhileAssigned1(rInt),
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckAWSLoadBalancerPolicyState("aws_elb.test-lb", "aws_load_balancer_policy.test-policy"),
    52  				),
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func testAccCheckAWSLoadBalancerPolicyDestroy(s *terraform.State) error {
    59  	conn := testAccProvider.Meta().(*AWSClient).elbconn
    60  
    61  	for _, rs := range s.RootModule().Resources {
    62  		if rs.Type != "aws_load_balancer_policy" {
    63  			continue
    64  		}
    65  
    66  		loadBalancerName, policyName := resourceAwsLoadBalancerPolicyParseId(rs.Primary.ID)
    67  		out, err := conn.DescribeLoadBalancerPolicies(
    68  			&elb.DescribeLoadBalancerPoliciesInput{
    69  				LoadBalancerName: aws.String(loadBalancerName),
    70  				PolicyNames:      []*string{aws.String(policyName)},
    71  			})
    72  		if err != nil {
    73  			if ec2err, ok := err.(awserr.Error); ok && (ec2err.Code() == "PolicyNotFound" || ec2err.Code() == "LoadBalancerNotFound") {
    74  				continue
    75  			}
    76  			return err
    77  		}
    78  
    79  		if len(out.PolicyDescriptions) > 0 {
    80  			return fmt.Errorf("Policy still exists")
    81  		}
    82  	}
    83  	return nil
    84  }
    85  
    86  func testAccCheckAWSLoadBalancerPolicyState(elbResource string, policyResource string) resource.TestCheckFunc {
    87  	return func(s *terraform.State) error {
    88  		rs, ok := s.RootModule().Resources[elbResource]
    89  		if !ok {
    90  			return fmt.Errorf("Not found: %s", elbResource)
    91  		}
    92  
    93  		if rs.Primary.ID == "" {
    94  			return fmt.Errorf("No ID is set")
    95  		}
    96  
    97  		policy, ok := s.RootModule().Resources[policyResource]
    98  		if !ok {
    99  			return fmt.Errorf("Not found: %s", policyResource)
   100  		}
   101  
   102  		elbconn := testAccProvider.Meta().(*AWSClient).elbconn
   103  		loadBalancerName, policyName := resourceAwsLoadBalancerPolicyParseId(policy.Primary.ID)
   104  		loadBalancerPolicies, err := elbconn.DescribeLoadBalancerPolicies(&elb.DescribeLoadBalancerPoliciesInput{
   105  			LoadBalancerName: aws.String(loadBalancerName),
   106  			PolicyNames:      []*string{aws.String(policyName)},
   107  		})
   108  
   109  		if err != nil {
   110  			return err
   111  		}
   112  
   113  		for _, loadBalancerPolicy := range loadBalancerPolicies.PolicyDescriptions {
   114  			if *loadBalancerPolicy.PolicyName == policyName {
   115  				if *loadBalancerPolicy.PolicyTypeName != policy.Primary.Attributes["policy_type_name"] {
   116  					return fmt.Errorf("PolicyTypeName does not match")
   117  				}
   118  				policyAttributeCount, err := strconv.Atoi(policy.Primary.Attributes["policy_attribute.#"])
   119  				if err != nil {
   120  					return err
   121  				}
   122  				if len(loadBalancerPolicy.PolicyAttributeDescriptions) != policyAttributeCount {
   123  					return fmt.Errorf("PolicyAttributeDescriptions length mismatch")
   124  				}
   125  				policyAttributes := make(map[string]string)
   126  				for k, v := range policy.Primary.Attributes {
   127  					if strings.HasPrefix(k, "policy_attribute.") && strings.HasSuffix(k, ".name") {
   128  						key := v
   129  						value_key := fmt.Sprintf("%s.value", strings.TrimSuffix(k, ".name"))
   130  						policyAttributes[key] = policy.Primary.Attributes[value_key]
   131  					}
   132  				}
   133  				for _, policyAttribute := range loadBalancerPolicy.PolicyAttributeDescriptions {
   134  					if *policyAttribute.AttributeValue != policyAttributes[*policyAttribute.AttributeName] {
   135  						return fmt.Errorf("PollicyAttribute Value mismatch %s != %s: %s", *policyAttribute.AttributeValue, policyAttributes[*policyAttribute.AttributeName], policyAttributes)
   136  					}
   137  				}
   138  			}
   139  		}
   140  
   141  		return nil
   142  	}
   143  }
   144  
   145  func testAccAWSLoadBalancerPolicyConfig_basic(rInt int) string {
   146  	return fmt.Sprintf(`
   147  	resource "aws_elb" "test-lb" {
   148  		name = "test-lb-%d"
   149  		availability_zones = ["us-west-2a"]
   150  
   151  		listener {
   152  			instance_port = 80
   153  			instance_protocol = "http"
   154  			lb_port = 80
   155  			lb_protocol = "http"
   156  		}
   157  
   158  		tags {
   159  			Name = "tf-acc-test"
   160  		}
   161  	}
   162  
   163  	resource "aws_load_balancer_policy" "test-policy" {
   164  		load_balancer_name = "${aws_elb.test-lb.name}"
   165  		policy_name = "test-policy-%d"
   166  		policy_type_name = "AppCookieStickinessPolicyType"
   167  		policy_attribute = {
   168  			name = "CookieName"
   169  			value = "magic_cookie"
   170  		}
   171  	}`, rInt, rInt)
   172  }
   173  
   174  func testAccAWSLoadBalancerPolicyConfig_updateWhileAssigned0(rInt int) string {
   175  	return fmt.Sprintf(`
   176  	resource "aws_elb" "test-lb" {
   177  		name = "test-lb-%d"
   178  		availability_zones = ["us-west-2a"]
   179  
   180  		listener {
   181  			instance_port = 80
   182  			instance_protocol = "http"
   183  			lb_port = 80
   184  			lb_protocol = "http"
   185  		}
   186  
   187  		tags {
   188  			Name = "tf-acc-test"
   189  		}
   190  	}
   191  
   192  	resource "aws_load_balancer_policy" "test-policy" {
   193  		load_balancer_name = "${aws_elb.test-lb.name}"
   194  		policy_name = "test-policy-%d"
   195  		policy_type_name = "AppCookieStickinessPolicyType"
   196  		policy_attribute = {
   197  			name = "CookieName"
   198  			value = "magic_cookie"
   199  		}
   200  	}
   201  
   202  	resource "aws_load_balancer_listener_policy" "test-lb-test-policy-80" {
   203  		load_balancer_name = "${aws_elb.test-lb.name}"
   204  		load_balancer_port = 80
   205  		policy_names = [
   206  			"${aws_load_balancer_policy.test-policy.policy_name}"
   207  		]
   208  	}`, rInt, rInt)
   209  }
   210  
   211  func testAccAWSLoadBalancerPolicyConfig_updateWhileAssigned1(rInt int) string {
   212  	return fmt.Sprintf(`
   213  	resource "aws_elb" "test-lb" {
   214  		name = "test-lb-%d"
   215  		availability_zones = ["us-west-2a"]
   216  
   217  		listener {
   218  			instance_port = 80
   219  			instance_protocol = "http"
   220  			lb_port = 80
   221  			lb_protocol = "http"
   222  		}
   223  
   224  		tags {
   225  			Name = "tf-acc-test"
   226  		}
   227  	}
   228  
   229  	resource "aws_load_balancer_policy" "test-policy" {
   230  		load_balancer_name = "${aws_elb.test-lb.name}"
   231  		policy_name = "test-policy-%d"
   232  		policy_type_name = "AppCookieStickinessPolicyType"
   233  		policy_attribute = {
   234  			name = "CookieName"
   235  			value = "unicorn_cookie"
   236  		}
   237  	}
   238  
   239  	resource "aws_load_balancer_listener_policy" "test-lb-test-policy-80" {
   240  		load_balancer_name = "${aws_elb.test-lb.name}"
   241  		load_balancer_port = 80
   242  		policy_names = [
   243  			"${aws_load_balancer_policy.test-policy.policy_name}"
   244  		]
   245  	}`, rInt, rInt)
   246  }