github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/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 testAccLBCookieStickinessPolicyConfig(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_lb_cookie_stickiness_policy" "foo" {
   119  	name = "foo-policy"
   120  	load_balancer = "${aws_elb.lb.id}"
   121  	lb_port = 80
   122  }`, rName)
   123  }
   124  
   125  // Sets the cookie_expiration_period to 300s.
   126  func testAccLBCookieStickinessPolicyConfigUpdate(rName string) string {
   127  	return fmt.Sprintf(`
   128  resource "aws_elb" "lb" {
   129  	name = "%s"
   130  	availability_zones = ["us-west-2a"]
   131  	listener {
   132  		instance_port = 8000
   133  		instance_protocol = "http"
   134  		lb_port = 80
   135  		lb_protocol = "http"
   136  	}
   137  }
   138  
   139  resource "aws_lb_cookie_stickiness_policy" "foo" {
   140  	name = "foo-policy"
   141  	load_balancer = "${aws_elb.lb.id}"
   142  	lb_port = 80
   143  	cookie_expiration_period = 300
   144  }`, rName)
   145  }