github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/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/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSAppCookieStickinessPolicy_basic(t *testing.T) {
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAppCookieStickinessPolicyDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAppCookieStickinessPolicyConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAppCookieStickinessPolicy(
    25  						"aws_elb.lb",
    26  						"aws_app_cookie_stickiness_policy.foo",
    27  					),
    28  				),
    29  			},
    30  			resource.TestStep{
    31  				Config: testAccAppCookieStickinessPolicyConfigUpdate,
    32  				Check: resource.ComposeTestCheckFunc(
    33  					testAccCheckAppCookieStickinessPolicy(
    34  						"aws_elb.lb",
    35  						"aws_app_cookie_stickiness_policy.foo",
    36  					),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func testAccCheckAppCookieStickinessPolicyDestroy(s *terraform.State) error {
    44  	conn := testAccProvider.Meta().(*AWSClient).elbconn
    45  
    46  	for _, rs := range s.RootModule().Resources {
    47  		if rs.Type != "aws_app_cookie_stickiness_policy" {
    48  			continue
    49  		}
    50  
    51  		lbName, _, policyName := resourceAwsAppCookieStickinessPolicyParseId(
    52  			rs.Primary.ID)
    53  		out, err := conn.DescribeLoadBalancerPolicies(
    54  			&elb.DescribeLoadBalancerPoliciesInput{
    55  				LoadBalancerName: aws.String(lbName),
    56  				PolicyNames:      []*string{aws.String(policyName)},
    57  			})
    58  		if err != nil {
    59  			if ec2err, ok := err.(awserr.Error); ok && (ec2err.Code() == "PolicyNotFound" || ec2err.Code() == "LoadBalancerNotFound") {
    60  				continue
    61  			}
    62  			return err
    63  		}
    64  
    65  		if len(out.PolicyDescriptions) > 0 {
    66  			return fmt.Errorf("Policy still exists")
    67  		}
    68  	}
    69  	return nil
    70  }
    71  
    72  func testAccCheckAppCookieStickinessPolicy(elbResource string, policyResource string) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		rs, ok := s.RootModule().Resources[elbResource]
    75  		if !ok {
    76  			return fmt.Errorf("Not found: %s", elbResource)
    77  		}
    78  
    79  		if rs.Primary.ID == "" {
    80  			return fmt.Errorf("No ID is set")
    81  		}
    82  
    83  		policy, ok := s.RootModule().Resources[policyResource]
    84  		if !ok {
    85  			return fmt.Errorf("Not found: %s", policyResource)
    86  		}
    87  
    88  		elbconn := testAccProvider.Meta().(*AWSClient).elbconn
    89  		elbName, _, policyName := resourceAwsAppCookieStickinessPolicyParseId(policy.Primary.ID)
    90  		_, err := elbconn.DescribeLoadBalancerPolicies(&elb.DescribeLoadBalancerPoliciesInput{
    91  			LoadBalancerName: aws.String(elbName),
    92  			PolicyNames:      []*string{aws.String(policyName)},
    93  		})
    94  
    95  		if err != nil {
    96  			return err
    97  		}
    98  
    99  		return nil
   100  	}
   101  }
   102  
   103  const testAccAppCookieStickinessPolicyConfig = `
   104  resource "aws_elb" "lb" {
   105  	name = "test-lb"
   106  	availability_zones = ["us-west-2a"]
   107  	listener {
   108  		instance_port = 8000
   109  		instance_protocol = "http"
   110  		lb_port = 80
   111  		lb_protocol = "http"
   112  	}
   113  }
   114  
   115  resource "aws_app_cookie_stickiness_policy" "foo" {
   116  	name = "foo-policy"
   117  	load_balancer = "${aws_elb.lb.id}"
   118  	lb_port = 80
   119  	cookie_name = "MyAppCookie"
   120  }
   121  `
   122  
   123  // Change the cookie_name to "MyOtherAppCookie".
   124  const testAccAppCookieStickinessPolicyConfigUpdate = `
   125  resource "aws_elb" "lb" {
   126  	name = "test-lb"
   127  	availability_zones = ["us-west-2a"]
   128  	listener {
   129  		instance_port = 8000
   130  		instance_protocol = "http"
   131  		lb_port = 80
   132  		lb_protocol = "http"
   133  	}
   134  }
   135  
   136  resource "aws_app_cookie_stickiness_policy" "foo" {
   137  	name = "foo-policy"
   138  	load_balancer = "${aws_elb.lb.id}"
   139  	lb_port = 80
   140  	cookie_name = "MyOtherAppCookie"
   141  }
   142  `