github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/service/elb"
     9  
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSAppCookieStickinessPolicy_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAppCookieStickinessPolicyDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAppCookieStickinessPolicyConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAppCookieStickinessPolicy(
    24  						"aws_elb.lb",
    25  						"aws_app_cookie_stickiness_policy.foo",
    26  					),
    27  				),
    28  			},
    29  			resource.TestStep{
    30  				Config: testAccAppCookieStickinessPolicyConfigUpdate,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckAppCookieStickinessPolicy(
    33  						"aws_elb.lb",
    34  						"aws_app_cookie_stickiness_policy.foo",
    35  					),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func testAccCheckAppCookieStickinessPolicyDestroy(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 testAccCheckAppCookieStickinessPolicy(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 := resourceAwsAppCookieStickinessPolicyParseId(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 testAccAppCookieStickinessPolicyConfig = `
    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_app_cookie_stickiness_policy" "foo" {
    94  	name = "foo-policy"
    95  	load_balancer = "${aws_elb.lb.id}"
    96  	lb_port = 80
    97  	cookie_name = "MyAppCookie"
    98  }
    99  `
   100  
   101  // Change the cookie_name to "MyOtherAppCookie".
   102  const testAccAppCookieStickinessPolicyConfigUpdate = `
   103  resource "aws_elb" "lb" {
   104  	name = "test-lb"
   105  	availability_zones = ["us-west-2a"]
   106  	listener {
   107  		instance_port = 8000
   108  		instance_protocol = "http"
   109  		lb_port = 80
   110  		lb_protocol = "http"
   111  	}
   112  }
   113  
   114  resource "aws_app_cookie_stickiness_policy" "foo" {
   115  	name = "foo-policy"
   116  	load_balancer = "${aws_elb.lb.id}"
   117  	lb_port = 80
   118  	cookie_name = "MyOtherAppCookie"
   119  }
   120  `