github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/elb" 10 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSLBCookieStickinessPolicy_basic(t *testing.T) { 17 lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5)) 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckLBCookieStickinessPolicyDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccLBCookieStickinessPolicyConfig(lbName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckLBCookieStickinessPolicy( 27 "aws_elb.lb", 28 "aws_lb_cookie_stickiness_policy.foo", 29 ), 30 ), 31 }, 32 resource.TestStep{ 33 Config: testAccLBCookieStickinessPolicyConfigUpdate(lbName), 34 Check: resource.ComposeTestCheckFunc( 35 testAccCheckLBCookieStickinessPolicy( 36 "aws_elb.lb", 37 "aws_lb_cookie_stickiness_policy.foo", 38 ), 39 ), 40 }, 41 }, 42 }) 43 } 44 45 func testAccCheckLBCookieStickinessPolicyDestroy(s *terraform.State) error { 46 conn := testAccProvider.Meta().(*AWSClient).elbconn 47 48 for _, rs := range s.RootModule().Resources { 49 if rs.Type != "aws_lb_cookie_stickiness_policy" { 50 continue 51 } 52 53 lbName, _, policyName := resourceAwsLBCookieStickinessPolicyParseId(rs.Primary.ID) 54 out, err := conn.DescribeLoadBalancerPolicies( 55 &elb.DescribeLoadBalancerPoliciesInput{ 56 LoadBalancerName: aws.String(lbName), 57 PolicyNames: []*string{aws.String(policyName)}, 58 }) 59 if err != nil { 60 if ec2err, ok := err.(awserr.Error); ok && (ec2err.Code() == "PolicyNotFound" || ec2err.Code() == "LoadBalancerNotFound") { 61 continue 62 } 63 return err 64 } 65 66 if len(out.PolicyDescriptions) > 0 { 67 return fmt.Errorf("Policy still exists") 68 } 69 } 70 71 return nil 72 } 73 74 func testAccCheckLBCookieStickinessPolicy(elbResource string, policyResource string) resource.TestCheckFunc { 75 return func(s *terraform.State) error { 76 rs, ok := s.RootModule().Resources[elbResource] 77 if !ok { 78 return fmt.Errorf("Not found: %s", elbResource) 79 } 80 81 if rs.Primary.ID == "" { 82 return fmt.Errorf("No ID is set") 83 } 84 85 policy, ok := s.RootModule().Resources[policyResource] 86 if !ok { 87 return fmt.Errorf("Not found: %s", policyResource) 88 } 89 90 elbconn := testAccProvider.Meta().(*AWSClient).elbconn 91 elbName, _, policyName := resourceAwsLBCookieStickinessPolicyParseId(policy.Primary.ID) 92 _, err := elbconn.DescribeLoadBalancerPolicies(&elb.DescribeLoadBalancerPoliciesInput{ 93 LoadBalancerName: aws.String(elbName), 94 PolicyNames: []*string{aws.String(policyName)}, 95 }) 96 97 if err != nil { 98 return err 99 } 100 101 return nil 102 } 103 } 104 105 func TestAccCheckLBCookieStickinessPolicy_drift(t *testing.T) { 106 lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5)) 107 108 // We only want to remove the reference to the policy from the listner, 109 // beacause that's all that can be done via the console. 110 removePolicy := func() { 111 conn := testAccProvider.Meta().(*AWSClient).elbconn 112 113 setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{ 114 LoadBalancerName: aws.String(lbName), 115 LoadBalancerPort: aws.Int64(80), 116 PolicyNames: []*string{}, 117 } 118 119 if _, err := conn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil { 120 t.Fatalf("Error removing LBCookieStickinessPolicy: %s", err) 121 } 122 } 123 124 resource.Test(t, resource.TestCase{ 125 PreCheck: func() { testAccPreCheck(t) }, 126 Providers: testAccProviders, 127 CheckDestroy: testAccCheckLBCookieStickinessPolicyDestroy, 128 Steps: []resource.TestStep{ 129 resource.TestStep{ 130 Config: testAccLBCookieStickinessPolicyConfig(lbName), 131 Check: resource.ComposeTestCheckFunc( 132 testAccCheckLBCookieStickinessPolicy( 133 "aws_elb.lb", 134 "aws_lb_cookie_stickiness_policy.foo", 135 ), 136 ), 137 }, 138 resource.TestStep{ 139 PreConfig: removePolicy, 140 Config: testAccLBCookieStickinessPolicyConfig(lbName), 141 Check: resource.ComposeTestCheckFunc( 142 testAccCheckLBCookieStickinessPolicy( 143 "aws_elb.lb", 144 "aws_lb_cookie_stickiness_policy.foo", 145 ), 146 ), 147 }, 148 }, 149 }) 150 } 151 152 func TestAccAWSLBCookieStickinessPolicy_missingLB(t *testing.T) { 153 lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5)) 154 155 // check that we can destroy the policy if the LB is missing 156 removeLB := func() { 157 conn := testAccProvider.Meta().(*AWSClient).elbconn 158 deleteElbOpts := elb.DeleteLoadBalancerInput{ 159 LoadBalancerName: aws.String(lbName), 160 } 161 if _, err := conn.DeleteLoadBalancer(&deleteElbOpts); err != nil { 162 t.Fatalf("Error deleting ELB: %s", err) 163 } 164 } 165 166 resource.Test(t, resource.TestCase{ 167 PreCheck: func() { testAccPreCheck(t) }, 168 Providers: testAccProviders, 169 CheckDestroy: testAccCheckLBCookieStickinessPolicyDestroy, 170 Steps: []resource.TestStep{ 171 resource.TestStep{ 172 Config: testAccLBCookieStickinessPolicyConfig(lbName), 173 Check: resource.ComposeTestCheckFunc( 174 testAccCheckLBCookieStickinessPolicy( 175 "aws_elb.lb", 176 "aws_lb_cookie_stickiness_policy.foo", 177 ), 178 ), 179 }, 180 resource.TestStep{ 181 PreConfig: removeLB, 182 Config: testAccLBCookieStickinessPolicyConfigDestroy(lbName), 183 }, 184 }, 185 }) 186 } 187 188 func testAccLBCookieStickinessPolicyConfig(rName string) string { 189 return fmt.Sprintf(` 190 resource "aws_elb" "lb" { 191 name = "%s" 192 availability_zones = ["us-west-2a"] 193 listener { 194 instance_port = 8000 195 instance_protocol = "http" 196 lb_port = 80 197 lb_protocol = "http" 198 } 199 } 200 201 resource "aws_lb_cookie_stickiness_policy" "foo" { 202 name = "foo-policy" 203 load_balancer = "${aws_elb.lb.id}" 204 lb_port = 80 205 }`, rName) 206 } 207 208 // Sets the cookie_expiration_period to 300s. 209 func testAccLBCookieStickinessPolicyConfigUpdate(rName string) string { 210 return fmt.Sprintf(` 211 resource "aws_elb" "lb" { 212 name = "%s" 213 availability_zones = ["us-west-2a"] 214 listener { 215 instance_port = 8000 216 instance_protocol = "http" 217 lb_port = 80 218 lb_protocol = "http" 219 } 220 } 221 222 resource "aws_lb_cookie_stickiness_policy" "foo" { 223 name = "foo-policy" 224 load_balancer = "${aws_elb.lb.id}" 225 lb_port = 80 226 cookie_expiration_period = 300 227 }`, rName) 228 } 229 230 // attempt to destroy the policy, but we'll delete the LB in the PreConfig 231 func testAccLBCookieStickinessPolicyConfigDestroy(rName string) string { 232 return fmt.Sprintf(` 233 resource "aws_elb" "lb" { 234 name = "%s" 235 availability_zones = ["us-west-2a"] 236 listener { 237 instance_port = 8000 238 instance_protocol = "http" 239 lb_port = 80 240 lb_protocol = "http" 241 } 242 }`, rName) 243 }