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