github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 testAccLBCookieStickinessPolicyConfig(rName string) string { 153 return fmt.Sprintf(` 154 resource "aws_elb" "lb" { 155 name = "%s" 156 availability_zones = ["us-west-2a"] 157 listener { 158 instance_port = 8000 159 instance_protocol = "http" 160 lb_port = 80 161 lb_protocol = "http" 162 } 163 } 164 165 resource "aws_lb_cookie_stickiness_policy" "foo" { 166 name = "foo-policy" 167 load_balancer = "${aws_elb.lb.id}" 168 lb_port = 80 169 }`, rName) 170 } 171 172 // Sets the cookie_expiration_period to 300s. 173 func testAccLBCookieStickinessPolicyConfigUpdate(rName string) string { 174 return fmt.Sprintf(` 175 resource "aws_elb" "lb" { 176 name = "%s" 177 availability_zones = ["us-west-2a"] 178 listener { 179 instance_port = 8000 180 instance_protocol = "http" 181 lb_port = 80 182 lb_protocol = "http" 183 } 184 } 185 186 resource "aws_lb_cookie_stickiness_policy" "foo" { 187 name = "foo-policy" 188 load_balancer = "${aws_elb.lb.id}" 189 lb_port = 80 190 cookie_expiration_period = 300 191 }`, rName) 192 }