github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/service/elb" 9 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAwsLBCookieStickinessPolicy_basic(t *testing.T) { 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckLBCookieStickinessPolicyDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccLBCookieStickinessPolicyConfig, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckLBCookieStickinessPolicy( 24 "aws_elb.lb", 25 "aws_lb_cookie_stickiness_policy.foo", 26 ), 27 ), 28 }, 29 resource.TestStep{ 30 Config: testAccLBCookieStickinessPolicyConfigUpdate, 31 Check: resource.ComposeTestCheckFunc( 32 testAccCheckLBCookieStickinessPolicy( 33 "aws_elb.lb", 34 "aws_lb_cookie_stickiness_policy.foo", 35 ), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func testAccCheckLBCookieStickinessPolicyDestroy(s *terraform.State) error { 43 if len(s.RootModule().Resources) > 0 { 44 return fmt.Errorf("Expected all resources to be gone, but found: %#v", s.RootModule().Resources) 45 } 46 47 return nil 48 } 49 50 func testAccCheckLBCookieStickinessPolicy(elbResource string, policyResource string) resource.TestCheckFunc { 51 return func(s *terraform.State) error { 52 rs, ok := s.RootModule().Resources[elbResource] 53 if !ok { 54 return fmt.Errorf("Not found: %s", elbResource) 55 } 56 57 if rs.Primary.ID == "" { 58 return fmt.Errorf("No ID is set") 59 } 60 61 policy, ok := s.RootModule().Resources[policyResource] 62 if !ok { 63 return fmt.Errorf("Not found: %s", policyResource) 64 } 65 66 elbconn := testAccProvider.Meta().(*AWSClient).elbconn 67 elbName, _, policyName := resourceAwsLBCookieStickinessPolicyParseId(policy.Primary.ID) 68 _, err := elbconn.DescribeLoadBalancerPolicies(&elb.DescribeLoadBalancerPoliciesInput{ 69 LoadBalancerName: aws.String(elbName), 70 PolicyNames: []*string{aws.String(policyName)}, 71 }) 72 73 if err != nil { 74 return err 75 } 76 77 return nil 78 } 79 } 80 81 const testAccLBCookieStickinessPolicyConfig = ` 82 resource "aws_elb" "lb" { 83 name = "test-lb" 84 availability_zones = ["us-west-2a"] 85 listener { 86 instance_port = 8000 87 instance_protocol = "http" 88 lb_port = 80 89 lb_protocol = "http" 90 } 91 } 92 93 resource "aws_lb_cookie_stickiness_policy" "foo" { 94 name = "foo-policy" 95 load_balancer = "${aws_elb.lb.id}" 96 lb_port = 80 97 } 98 ` 99 100 // Sets the cookie_expiration_period to 300s. 101 const testAccLBCookieStickinessPolicyConfigUpdate = ` 102 resource "aws_elb" "lb" { 103 name = "test-lb" 104 availability_zones = ["us-west-2a"] 105 listener { 106 instance_port = 8000 107 instance_protocol = "http" 108 lb_port = 80 109 lb_protocol = "http" 110 } 111 } 112 113 resource "aws_lb_cookie_stickiness_policy" "foo" { 114 name = "foo-policy" 115 load_balancer = "${aws_elb.lb.id}" 116 lb_port = 80 117 cookie_expiration_period = 300 118 } 119 `