github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckAppCookieStickinessPolicyDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccAppCookieStickinessPolicyConfig(lbName),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckAppCookieStickinessPolicy(
    28  						"aws_elb.lb",
    29  						"aws_app_cookie_stickiness_policy.foo",
    30  					),
    31  				),
    32  			},
    33  			resource.TestStep{
    34  				Config: testAccAppCookieStickinessPolicyConfigUpdate(lbName),
    35  				Check: resource.ComposeTestCheckFunc(
    36  					testAccCheckAppCookieStickinessPolicy(
    37  						"aws_elb.lb",
    38  						"aws_app_cookie_stickiness_policy.foo",
    39  					),
    40  				),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func TestAccAWSAppCookieStickinessPolicy_missingLB(t *testing.T) {
    47  	lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5))
    48  
    49  	// check that we can destroy the policy if the LB is missing
    50  	removeLB := func() {
    51  		conn := testAccProvider.Meta().(*AWSClient).elbconn
    52  		deleteElbOpts := elb.DeleteLoadBalancerInput{
    53  			LoadBalancerName: aws.String(lbName),
    54  		}
    55  		if _, err := conn.DeleteLoadBalancer(&deleteElbOpts); err != nil {
    56  			t.Fatalf("Error deleting ELB: %s", err)
    57  		}
    58  	}
    59  
    60  	resource.Test(t, resource.TestCase{
    61  		PreCheck:     func() { testAccPreCheck(t) },
    62  		Providers:    testAccProviders,
    63  		CheckDestroy: testAccCheckAppCookieStickinessPolicyDestroy,
    64  		Steps: []resource.TestStep{
    65  			resource.TestStep{
    66  				Config: testAccAppCookieStickinessPolicyConfig(lbName),
    67  				Check: resource.ComposeTestCheckFunc(
    68  					testAccCheckAppCookieStickinessPolicy(
    69  						"aws_elb.lb",
    70  						"aws_app_cookie_stickiness_policy.foo",
    71  					),
    72  				),
    73  			},
    74  			resource.TestStep{
    75  				PreConfig: removeLB,
    76  				Config:    testAccAppCookieStickinessPolicyConfigDestroy(lbName),
    77  			},
    78  		},
    79  	})
    80  }
    81  
    82  func testAccCheckAppCookieStickinessPolicyDestroy(s *terraform.State) error {
    83  	conn := testAccProvider.Meta().(*AWSClient).elbconn
    84  
    85  	for _, rs := range s.RootModule().Resources {
    86  		if rs.Type != "aws_app_cookie_stickiness_policy" {
    87  			continue
    88  		}
    89  
    90  		lbName, _, policyName := resourceAwsAppCookieStickinessPolicyParseId(
    91  			rs.Primary.ID)
    92  		out, err := conn.DescribeLoadBalancerPolicies(
    93  			&elb.DescribeLoadBalancerPoliciesInput{
    94  				LoadBalancerName: aws.String(lbName),
    95  				PolicyNames:      []*string{aws.String(policyName)},
    96  			})
    97  		if err != nil {
    98  			if ec2err, ok := err.(awserr.Error); ok && (ec2err.Code() == "PolicyNotFound" || ec2err.Code() == "LoadBalancerNotFound") {
    99  				continue
   100  			}
   101  			return err
   102  		}
   103  
   104  		if len(out.PolicyDescriptions) > 0 {
   105  			return fmt.Errorf("Policy still exists")
   106  		}
   107  	}
   108  	return nil
   109  }
   110  
   111  func testAccCheckAppCookieStickinessPolicy(elbResource string, policyResource string) resource.TestCheckFunc {
   112  	return func(s *terraform.State) error {
   113  		rs, ok := s.RootModule().Resources[elbResource]
   114  		if !ok {
   115  			return fmt.Errorf("Not found: %s", elbResource)
   116  		}
   117  
   118  		if rs.Primary.ID == "" {
   119  			return fmt.Errorf("No ID is set")
   120  		}
   121  
   122  		policy, ok := s.RootModule().Resources[policyResource]
   123  		if !ok {
   124  			return fmt.Errorf("Not found: %s", policyResource)
   125  		}
   126  
   127  		elbconn := testAccProvider.Meta().(*AWSClient).elbconn
   128  		elbName, _, policyName := resourceAwsAppCookieStickinessPolicyParseId(policy.Primary.ID)
   129  		_, err := elbconn.DescribeLoadBalancerPolicies(&elb.DescribeLoadBalancerPoliciesInput{
   130  			LoadBalancerName: aws.String(elbName),
   131  			PolicyNames:      []*string{aws.String(policyName)},
   132  		})
   133  
   134  		if err != nil {
   135  			return err
   136  		}
   137  
   138  		return nil
   139  	}
   140  }
   141  
   142  // ensure the policy is re-added is it goes missing
   143  func TestAccAWSAppCookieStickinessPolicy_drift(t *testing.T) {
   144  	lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5))
   145  
   146  	// We only want to remove the reference to the policy from the listner,
   147  	// beacause that's all that can be done via the console.
   148  	removePolicy := func() {
   149  		conn := testAccProvider.Meta().(*AWSClient).elbconn
   150  
   151  		setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{
   152  			LoadBalancerName: aws.String(lbName),
   153  			LoadBalancerPort: aws.Int64(80),
   154  			PolicyNames:      []*string{},
   155  		}
   156  
   157  		if _, err := conn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil {
   158  			t.Fatalf("Error removing AppCookieStickinessPolicy: %s", err)
   159  		}
   160  	}
   161  
   162  	resource.Test(t, resource.TestCase{
   163  		PreCheck:     func() { testAccPreCheck(t) },
   164  		Providers:    testAccProviders,
   165  		CheckDestroy: testAccCheckAppCookieStickinessPolicyDestroy,
   166  		Steps: []resource.TestStep{
   167  			resource.TestStep{
   168  				Config: testAccAppCookieStickinessPolicyConfig(lbName),
   169  				Check: resource.ComposeTestCheckFunc(
   170  					testAccCheckAppCookieStickinessPolicy(
   171  						"aws_elb.lb",
   172  						"aws_app_cookie_stickiness_policy.foo",
   173  					),
   174  				),
   175  			},
   176  			resource.TestStep{
   177  				PreConfig: removePolicy,
   178  				Config:    testAccAppCookieStickinessPolicyConfig(lbName),
   179  				Check: resource.ComposeTestCheckFunc(
   180  					testAccCheckAppCookieStickinessPolicy(
   181  						"aws_elb.lb",
   182  						"aws_app_cookie_stickiness_policy.foo",
   183  					),
   184  				),
   185  			},
   186  		},
   187  	})
   188  }
   189  
   190  func testAccAppCookieStickinessPolicyConfig(rName string) string {
   191  	return fmt.Sprintf(`
   192  resource "aws_elb" "lb" {
   193  	name = "%s"
   194  	availability_zones = ["us-west-2a"]
   195  	listener {
   196  		instance_port = 8000
   197  		instance_protocol = "http"
   198  		lb_port = 80
   199  		lb_protocol = "http"
   200  	}
   201  }
   202  
   203  resource "aws_app_cookie_stickiness_policy" "foo" {
   204  	name = "foo-policy"
   205  	load_balancer = "${aws_elb.lb.id}"
   206  	lb_port = 80
   207  	cookie_name = "MyAppCookie"
   208  }`, rName)
   209  }
   210  
   211  // Change the cookie_name to "MyOtherAppCookie".
   212  func testAccAppCookieStickinessPolicyConfigUpdate(rName string) string {
   213  	return fmt.Sprintf(`
   214  resource "aws_elb" "lb" {
   215  	name = "%s"
   216  	availability_zones = ["us-west-2a"]
   217  	listener {
   218  		instance_port = 8000
   219  		instance_protocol = "http"
   220  		lb_port = 80
   221  		lb_protocol = "http"
   222  	}
   223  }
   224  
   225  resource "aws_app_cookie_stickiness_policy" "foo" {
   226  	name = "foo-policy"
   227  	load_balancer = "${aws_elb.lb.id}"
   228  	lb_port = 80
   229  	cookie_name = "MyOtherAppCookie"
   230  }`, rName)
   231  }
   232  
   233  // attempt to destroy the policy, but we'll delete the LB in the PreConfig
   234  func testAccAppCookieStickinessPolicyConfigDestroy(rName string) string {
   235  	return fmt.Sprintf(`
   236  resource "aws_elb" "lb" {
   237  	name = "%s"
   238  	availability_zones = ["us-west-2a"]
   239  	listener {
   240  		instance_port = 8000
   241  		instance_protocol = "http"
   242  		lb_port = 80
   243  		lb_protocol = "http"
   244  	}
   245  }`, rName)
   246  }