github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/builtin/providers/aws/resource_aws_app_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 TestAccAWSAppCookieStickinessPolicy_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: testAccCheckAppCookieStickinessPolicyDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccAppCookieStickinessPolicyConfig(lbName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckAppCookieStickinessPolicy( 27 "aws_elb.lb", 28 "aws_app_cookie_stickiness_policy.foo", 29 ), 30 ), 31 }, 32 resource.TestStep{ 33 Config: testAccAppCookieStickinessPolicyConfigUpdate(lbName), 34 Check: resource.ComposeTestCheckFunc( 35 testAccCheckAppCookieStickinessPolicy( 36 "aws_elb.lb", 37 "aws_app_cookie_stickiness_policy.foo", 38 ), 39 ), 40 }, 41 }, 42 }) 43 } 44 45 func testAccCheckAppCookieStickinessPolicyDestroy(s *terraform.State) error { 46 conn := testAccProvider.Meta().(*AWSClient).elbconn 47 48 for _, rs := range s.RootModule().Resources { 49 if rs.Type != "aws_app_cookie_stickiness_policy" { 50 continue 51 } 52 53 lbName, _, policyName := resourceAwsAppCookieStickinessPolicyParseId( 54 rs.Primary.ID) 55 out, err := conn.DescribeLoadBalancerPolicies( 56 &elb.DescribeLoadBalancerPoliciesInput{ 57 LoadBalancerName: aws.String(lbName), 58 PolicyNames: []*string{aws.String(policyName)}, 59 }) 60 if err != nil { 61 if ec2err, ok := err.(awserr.Error); ok && (ec2err.Code() == "PolicyNotFound" || ec2err.Code() == "LoadBalancerNotFound") { 62 continue 63 } 64 return err 65 } 66 67 if len(out.PolicyDescriptions) > 0 { 68 return fmt.Errorf("Policy still exists") 69 } 70 } 71 return nil 72 } 73 74 func testAccCheckAppCookieStickinessPolicy(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 := resourceAwsAppCookieStickinessPolicyParseId(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 testAccAppCookieStickinessPolicyConfig(rName string) string { 106 return fmt.Sprintf(` 107 resource "aws_elb" "lb" { 108 name = "%s" 109 availability_zones = ["us-west-2a"] 110 listener { 111 instance_port = 8000 112 instance_protocol = "http" 113 lb_port = 80 114 lb_protocol = "http" 115 } 116 } 117 118 resource "aws_app_cookie_stickiness_policy" "foo" { 119 name = "foo-policy" 120 load_balancer = "${aws_elb.lb.id}" 121 lb_port = 80 122 cookie_name = "MyAppCookie" 123 }`, rName) 124 } 125 126 // Change the cookie_name to "MyOtherAppCookie". 127 func testAccAppCookieStickinessPolicyConfigUpdate(rName string) string { 128 return fmt.Sprintf(` 129 resource "aws_elb" "lb" { 130 name = "%s" 131 availability_zones = ["us-west-2a"] 132 listener { 133 instance_port = 8000 134 instance_protocol = "http" 135 lb_port = 80 136 lb_protocol = "http" 137 } 138 } 139 140 resource "aws_app_cookie_stickiness_policy" "foo" { 141 name = "foo-policy" 142 load_balancer = "${aws_elb.lb.id}" 143 lb_port = 80 144 cookie_name = "MyOtherAppCookie" 145 }`, rName) 146 }