github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_alb_target_group_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"regexp"
     7  	"testing"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/service/elbv2"
    11  	"github.com/hashicorp/errwrap"
    12  	"github.com/hashicorp/terraform/helper/acctest"
    13  	"github.com/hashicorp/terraform/helper/resource"
    14  	"github.com/hashicorp/terraform/terraform"
    15  )
    16  
    17  func TestALBTargetGroupCloudwatchSuffixFromARN(t *testing.T) {
    18  	cases := []struct {
    19  		name   string
    20  		arn    *string
    21  		suffix string
    22  	}{
    23  		{
    24  			name:   "valid suffix",
    25  			arn:    aws.String(`arn:aws:elasticloadbalancing:us-east-1:123456:targetgroup/my-targets/73e2d6bc24d8a067`),
    26  			suffix: `targetgroup/my-targets/73e2d6bc24d8a067`,
    27  		},
    28  		{
    29  			name:   "no suffix",
    30  			arn:    aws.String(`arn:aws:elasticloadbalancing:us-east-1:123456:targetgroup`),
    31  			suffix: ``,
    32  		},
    33  		{
    34  			name:   "nil ARN",
    35  			arn:    nil,
    36  			suffix: ``,
    37  		},
    38  	}
    39  
    40  	for _, tc := range cases {
    41  		actual := albTargetGroupSuffixFromARN(tc.arn)
    42  		if actual != tc.suffix {
    43  			t.Fatalf("bad suffix: %q\nExpected: %s\n     Got: %s", tc.name, tc.suffix, actual)
    44  		}
    45  	}
    46  }
    47  
    48  func TestAccAWSALBTargetGroup_basic(t *testing.T) {
    49  	var conf elbv2.TargetGroup
    50  	targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
    51  
    52  	resource.Test(t, resource.TestCase{
    53  		PreCheck:      func() { testAccPreCheck(t) },
    54  		IDRefreshName: "aws_alb_target_group.test",
    55  		Providers:     testAccProviders,
    56  		CheckDestroy:  testAccCheckAWSALBTargetGroupDestroy,
    57  		Steps: []resource.TestStep{
    58  			{
    59  				Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName),
    60  				Check: resource.ComposeAggregateTestCheckFunc(
    61  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
    62  					resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"),
    63  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName),
    64  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"),
    65  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"),
    66  					resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"),
    67  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"),
    68  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"),
    69  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.enabled", "true"),
    70  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"),
    71  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"),
    72  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"),
    73  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health"),
    74  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "60"),
    75  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8081"),
    76  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTP"),
    77  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "3"),
    78  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "3"),
    79  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "3"),
    80  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200-299"),
    81  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "1"),
    82  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.TestName", "TestAccAWSALBTargetGroup_basic"),
    83  				),
    84  			},
    85  		},
    86  	})
    87  }
    88  
    89  func TestAccAWSALBTargetGroup_namePrefix(t *testing.T) {
    90  	var conf elbv2.TargetGroup
    91  
    92  	resource.Test(t, resource.TestCase{
    93  		PreCheck:      func() { testAccPreCheck(t) },
    94  		IDRefreshName: "aws_alb_target_group.test",
    95  		Providers:     testAccProviders,
    96  		CheckDestroy:  testAccCheckAWSALBTargetGroupDestroy,
    97  		Steps: []resource.TestStep{
    98  			{
    99  				Config: testAccAWSALBTargetGroupConfig_namePrefix,
   100  				Check: resource.ComposeTestCheckFunc(
   101  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
   102  					resource.TestMatchResourceAttr("aws_alb_target_group.test", "name", regexp.MustCompile("^tf-")),
   103  				),
   104  			},
   105  		},
   106  	})
   107  }
   108  
   109  func TestAccAWSALBTargetGroup_generatedName(t *testing.T) {
   110  	var conf elbv2.TargetGroup
   111  
   112  	resource.Test(t, resource.TestCase{
   113  		PreCheck:      func() { testAccPreCheck(t) },
   114  		IDRefreshName: "aws_alb_target_group.test",
   115  		Providers:     testAccProviders,
   116  		CheckDestroy:  testAccCheckAWSALBTargetGroupDestroy,
   117  		Steps: []resource.TestStep{
   118  			{
   119  				Config: testAccAWSALBTargetGroupConfig_generatedName,
   120  				Check: resource.ComposeTestCheckFunc(
   121  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
   122  				),
   123  			},
   124  		},
   125  	})
   126  }
   127  
   128  func TestAccAWSALBTargetGroup_changeNameForceNew(t *testing.T) {
   129  	var before, after elbv2.TargetGroup
   130  	targetGroupNameBefore := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
   131  	targetGroupNameAfter := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(4, acctest.CharSetAlphaNum))
   132  
   133  	resource.Test(t, resource.TestCase{
   134  		PreCheck:      func() { testAccPreCheck(t) },
   135  		IDRefreshName: "aws_alb_target_group.test",
   136  		Providers:     testAccProviders,
   137  		CheckDestroy:  testAccCheckAWSALBTargetGroupDestroy,
   138  		Steps: []resource.TestStep{
   139  			{
   140  				Config: testAccAWSALBTargetGroupConfig_basic(targetGroupNameBefore),
   141  				Check: resource.ComposeAggregateTestCheckFunc(
   142  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before),
   143  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupNameBefore),
   144  				),
   145  			},
   146  			{
   147  				Config: testAccAWSALBTargetGroupConfig_basic(targetGroupNameAfter),
   148  				Check: resource.ComposeAggregateTestCheckFunc(
   149  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after),
   150  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupNameAfter),
   151  				),
   152  			},
   153  		},
   154  	})
   155  }
   156  
   157  func TestAccAWSALBTargetGroup_changeProtocolForceNew(t *testing.T) {
   158  	var before, after elbv2.TargetGroup
   159  	targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
   160  
   161  	resource.Test(t, resource.TestCase{
   162  		PreCheck:      func() { testAccPreCheck(t) },
   163  		IDRefreshName: "aws_alb_target_group.test",
   164  		Providers:     testAccProviders,
   165  		CheckDestroy:  testAccCheckAWSALBTargetGroupDestroy,
   166  		Steps: []resource.TestStep{
   167  			{
   168  				Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName),
   169  				Check: resource.ComposeAggregateTestCheckFunc(
   170  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before),
   171  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"),
   172  				),
   173  			},
   174  			{
   175  				Config: testAccAWSALBTargetGroupConfig_updatedProtocol(targetGroupName),
   176  				Check: resource.ComposeAggregateTestCheckFunc(
   177  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after),
   178  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTP"),
   179  				),
   180  			},
   181  		},
   182  	})
   183  }
   184  
   185  func TestAccAWSALBTargetGroup_changePortForceNew(t *testing.T) {
   186  	var before, after elbv2.TargetGroup
   187  	targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
   188  
   189  	resource.Test(t, resource.TestCase{
   190  		PreCheck:      func() { testAccPreCheck(t) },
   191  		IDRefreshName: "aws_alb_target_group.test",
   192  		Providers:     testAccProviders,
   193  		CheckDestroy:  testAccCheckAWSALBTargetGroupDestroy,
   194  		Steps: []resource.TestStep{
   195  			{
   196  				Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName),
   197  				Check: resource.ComposeAggregateTestCheckFunc(
   198  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before),
   199  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"),
   200  				),
   201  			},
   202  			{
   203  				Config: testAccAWSALBTargetGroupConfig_updatedPort(targetGroupName),
   204  				Check: resource.ComposeAggregateTestCheckFunc(
   205  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after),
   206  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "442"),
   207  				),
   208  			},
   209  		},
   210  	})
   211  }
   212  
   213  func TestAccAWSALBTargetGroup_changeVpcForceNew(t *testing.T) {
   214  	var before, after elbv2.TargetGroup
   215  	targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
   216  
   217  	resource.Test(t, resource.TestCase{
   218  		PreCheck:      func() { testAccPreCheck(t) },
   219  		IDRefreshName: "aws_alb_target_group.test",
   220  		Providers:     testAccProviders,
   221  		CheckDestroy:  testAccCheckAWSALBTargetGroupDestroy,
   222  		Steps: []resource.TestStep{
   223  			{
   224  				Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName),
   225  				Check: resource.ComposeAggregateTestCheckFunc(
   226  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before),
   227  				),
   228  			},
   229  			{
   230  				Config: testAccAWSALBTargetGroupConfig_updatedVpc(targetGroupName),
   231  				Check: resource.ComposeAggregateTestCheckFunc(
   232  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after),
   233  				),
   234  			},
   235  		},
   236  	})
   237  }
   238  
   239  func TestAccAWSALBTargetGroup_tags(t *testing.T) {
   240  	var conf elbv2.TargetGroup
   241  	targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
   242  
   243  	resource.Test(t, resource.TestCase{
   244  		PreCheck:      func() { testAccPreCheck(t) },
   245  		IDRefreshName: "aws_alb_target_group.test",
   246  		Providers:     testAccProviders,
   247  		CheckDestroy:  testAccCheckAWSALBTargetGroupDestroy,
   248  		Steps: []resource.TestStep{
   249  			{
   250  				Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName),
   251  				Check: resource.ComposeAggregateTestCheckFunc(
   252  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
   253  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "1"),
   254  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.TestName", "TestAccAWSALBTargetGroup_basic"),
   255  				),
   256  			},
   257  			{
   258  				Config: testAccAWSALBTargetGroupConfig_updateTags(targetGroupName),
   259  				Check: resource.ComposeAggregateTestCheckFunc(
   260  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
   261  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "2"),
   262  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.Environment", "Production"),
   263  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.Type", "ALB Target Group"),
   264  				),
   265  			},
   266  		},
   267  	})
   268  }
   269  
   270  func TestAccAWSALBTargetGroup_updateHealthCheck(t *testing.T) {
   271  	var conf elbv2.TargetGroup
   272  	targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
   273  
   274  	resource.Test(t, resource.TestCase{
   275  		PreCheck:      func() { testAccPreCheck(t) },
   276  		IDRefreshName: "aws_alb_target_group.test",
   277  		Providers:     testAccProviders,
   278  		CheckDestroy:  testAccCheckAWSALBTargetGroupDestroy,
   279  		Steps: []resource.TestStep{
   280  			{
   281  				Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName),
   282  				Check: resource.ComposeAggregateTestCheckFunc(
   283  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
   284  					resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"),
   285  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName),
   286  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"),
   287  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"),
   288  					resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"),
   289  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"),
   290  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"),
   291  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"),
   292  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"),
   293  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"),
   294  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health"),
   295  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "60"),
   296  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8081"),
   297  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTP"),
   298  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "3"),
   299  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "3"),
   300  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "3"),
   301  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200-299"),
   302  				),
   303  			},
   304  			{
   305  				Config: testAccAWSALBTargetGroupConfig_updateHealthCheck(targetGroupName),
   306  				Check: resource.ComposeAggregateTestCheckFunc(
   307  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
   308  					resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"),
   309  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName),
   310  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"),
   311  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"),
   312  					resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"),
   313  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"),
   314  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"),
   315  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"),
   316  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"),
   317  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"),
   318  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"),
   319  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"),
   320  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"),
   321  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"),
   322  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"),
   323  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"),
   324  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"),
   325  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"),
   326  				),
   327  			},
   328  		},
   329  	})
   330  }
   331  
   332  func TestAccAWSALBTargetGroup_updateSticknessEnabled(t *testing.T) {
   333  	var conf elbv2.TargetGroup
   334  	targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
   335  
   336  	resource.Test(t, resource.TestCase{
   337  		PreCheck:      func() { testAccPreCheck(t) },
   338  		IDRefreshName: "aws_alb_target_group.test",
   339  		Providers:     testAccProviders,
   340  		CheckDestroy:  testAccCheckAWSALBTargetGroupDestroy,
   341  		Steps: []resource.TestStep{
   342  			{
   343  				Config: testAccAWSALBTargetGroupConfig_stickiness(targetGroupName, false, false),
   344  				Check: resource.ComposeAggregateTestCheckFunc(
   345  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
   346  					resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"),
   347  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName),
   348  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"),
   349  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"),
   350  					resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"),
   351  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"),
   352  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"),
   353  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"),
   354  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"),
   355  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"),
   356  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"),
   357  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"),
   358  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"),
   359  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"),
   360  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"),
   361  				),
   362  			},
   363  			{
   364  				Config: testAccAWSALBTargetGroupConfig_stickiness(targetGroupName, true, true),
   365  				Check: resource.ComposeAggregateTestCheckFunc(
   366  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
   367  					resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"),
   368  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName),
   369  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"),
   370  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"),
   371  					resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"),
   372  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"),
   373  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"),
   374  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.enabled", "true"),
   375  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"),
   376  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"),
   377  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"),
   378  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"),
   379  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"),
   380  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"),
   381  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"),
   382  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"),
   383  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"),
   384  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"),
   385  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"),
   386  				),
   387  			},
   388  			{
   389  				Config: testAccAWSALBTargetGroupConfig_stickiness(targetGroupName, true, false),
   390  				Check: resource.ComposeAggregateTestCheckFunc(
   391  					testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
   392  					resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"),
   393  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName),
   394  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"),
   395  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"),
   396  					resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"),
   397  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"),
   398  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"),
   399  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.enabled", "false"),
   400  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"),
   401  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"),
   402  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"),
   403  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"),
   404  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"),
   405  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"),
   406  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"),
   407  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"),
   408  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"),
   409  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"),
   410  					resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"),
   411  				),
   412  			},
   413  		},
   414  	})
   415  }
   416  
   417  func testAccCheckAWSALBTargetGroupExists(n string, res *elbv2.TargetGroup) resource.TestCheckFunc {
   418  	return func(s *terraform.State) error {
   419  		rs, ok := s.RootModule().Resources[n]
   420  		if !ok {
   421  			return fmt.Errorf("Not found: %s", n)
   422  		}
   423  
   424  		if rs.Primary.ID == "" {
   425  			return errors.New("No Target Group ID is set")
   426  		}
   427  
   428  		conn := testAccProvider.Meta().(*AWSClient).elbv2conn
   429  
   430  		describe, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{
   431  			TargetGroupArns: []*string{aws.String(rs.Primary.ID)},
   432  		})
   433  
   434  		if err != nil {
   435  			return err
   436  		}
   437  
   438  		if len(describe.TargetGroups) != 1 ||
   439  			*describe.TargetGroups[0].TargetGroupArn != rs.Primary.ID {
   440  			return errors.New("Target Group not found")
   441  		}
   442  
   443  		*res = *describe.TargetGroups[0]
   444  		return nil
   445  	}
   446  }
   447  
   448  func testAccCheckAWSALBTargetGroupDestroy(s *terraform.State) error {
   449  	conn := testAccProvider.Meta().(*AWSClient).elbv2conn
   450  
   451  	for _, rs := range s.RootModule().Resources {
   452  		if rs.Type != "aws_alb_target_group" {
   453  			continue
   454  		}
   455  
   456  		describe, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{
   457  			TargetGroupArns: []*string{aws.String(rs.Primary.ID)},
   458  		})
   459  
   460  		if err == nil {
   461  			if len(describe.TargetGroups) != 0 &&
   462  				*describe.TargetGroups[0].TargetGroupArn == rs.Primary.ID {
   463  				return fmt.Errorf("Target Group %q still exists", rs.Primary.ID)
   464  			}
   465  		}
   466  
   467  		// Verify the error
   468  		if isTargetGroupNotFound(err) {
   469  			return nil
   470  		} else {
   471  			return errwrap.Wrapf("Unexpected error checking ALB destroyed: {{err}}", err)
   472  		}
   473  	}
   474  
   475  	return nil
   476  }
   477  
   478  func testAccAWSALBTargetGroupConfig_basic(targetGroupName string) string {
   479  	return fmt.Sprintf(`resource "aws_alb_target_group" "test" {
   480    name = "%s"
   481    port = 443
   482    protocol = "HTTPS"
   483    vpc_id = "${aws_vpc.test.id}"
   484  
   485    deregistration_delay = 200
   486  
   487    stickiness {
   488      type = "lb_cookie"
   489      cookie_duration = 10000
   490    }
   491  
   492    health_check {
   493      path = "/health"
   494      interval = 60
   495      port = 8081
   496      protocol = "HTTP"
   497      timeout = 3
   498      healthy_threshold = 3
   499      unhealthy_threshold = 3
   500      matcher = "200-299"
   501    }
   502  
   503    tags {
   504      TestName = "TestAccAWSALBTargetGroup_basic"
   505    }
   506  }
   507  
   508  resource "aws_vpc" "test" {
   509    cidr_block = "10.0.0.0/16"
   510  
   511    tags {
   512      TestName = "TestAccAWSALBTargetGroup_basic"
   513    }
   514  }`, targetGroupName)
   515  }
   516  
   517  func testAccAWSALBTargetGroupConfig_updatedPort(targetGroupName string) string {
   518  	return fmt.Sprintf(`resource "aws_alb_target_group" "test" {
   519    name = "%s"
   520    port = 442
   521    protocol = "HTTPS"
   522    vpc_id = "${aws_vpc.test.id}"
   523  
   524    deregistration_delay = 200
   525  
   526    stickiness {
   527      type = "lb_cookie"
   528      cookie_duration = 10000
   529    }
   530  
   531    health_check {
   532      path = "/health"
   533      interval = 60
   534      port = 8081
   535      protocol = "HTTP"
   536      timeout = 3
   537      healthy_threshold = 3
   538      unhealthy_threshold = 3
   539      matcher = "200-299"
   540    }
   541  
   542    tags {
   543      TestName = "TestAccAWSALBTargetGroup_basic"
   544    }
   545  }
   546  
   547  resource "aws_vpc" "test" {
   548    cidr_block = "10.0.0.0/16"
   549  
   550    tags {
   551      TestName = "TestAccAWSALBTargetGroup_basic"
   552    }
   553  }`, targetGroupName)
   554  }
   555  
   556  func testAccAWSALBTargetGroupConfig_updatedProtocol(targetGroupName string) string {
   557  	return fmt.Sprintf(`resource "aws_alb_target_group" "test" {
   558    name = "%s"
   559    port = 443
   560    protocol = "HTTP"
   561    vpc_id = "${aws_vpc.test2.id}"
   562  
   563    deregistration_delay = 200
   564  
   565    stickiness {
   566      type = "lb_cookie"
   567      cookie_duration = 10000
   568    }
   569  
   570    health_check {
   571      path = "/health"
   572      interval = 60
   573      port = 8081
   574      protocol = "HTTP"
   575      timeout = 3
   576      healthy_threshold = 3
   577      unhealthy_threshold = 3
   578      matcher = "200-299"
   579    }
   580  
   581    tags {
   582      TestName = "TestAccAWSALBTargetGroup_basic"
   583    }
   584  }
   585  
   586  resource "aws_vpc" "test2" {
   587    cidr_block = "10.10.0.0/16"
   588  
   589    tags {
   590      TestName = "TestAccAWSALBTargetGroup_basic"
   591    }
   592  }
   593  
   594  resource "aws_vpc" "test" {
   595    cidr_block = "10.0.0.0/16"
   596  
   597    tags {
   598      TestName = "TestAccAWSALBTargetGroup_basic"
   599    }
   600  }`, targetGroupName)
   601  }
   602  
   603  func testAccAWSALBTargetGroupConfig_updatedVpc(targetGroupName string) string {
   604  	return fmt.Sprintf(`resource "aws_alb_target_group" "test" {
   605    name = "%s"
   606    port = 443
   607    protocol = "HTTPS"
   608    vpc_id = "${aws_vpc.test.id}"
   609  
   610    deregistration_delay = 200
   611  
   612    stickiness {
   613      type = "lb_cookie"
   614      cookie_duration = 10000
   615    }
   616  
   617    health_check {
   618      path = "/health"
   619      interval = 60
   620      port = 8081
   621      protocol = "HTTP"
   622      timeout = 3
   623      healthy_threshold = 3
   624      unhealthy_threshold = 3
   625      matcher = "200-299"
   626    }
   627  
   628    tags {
   629      TestName = "TestAccAWSALBTargetGroup_basic"
   630    }
   631  }
   632  
   633  resource "aws_vpc" "test" {
   634    cidr_block = "10.0.0.0/16"
   635  
   636    tags {
   637      TestName = "TestAccAWSALBTargetGroup_basic"
   638    }
   639  }`, targetGroupName)
   640  }
   641  
   642  func testAccAWSALBTargetGroupConfig_updateTags(targetGroupName string) string {
   643  	return fmt.Sprintf(`resource "aws_alb_target_group" "test" {
   644    name = "%s"
   645    port = 443
   646    protocol = "HTTPS"
   647    vpc_id = "${aws_vpc.test.id}"
   648  
   649    deregistration_delay = 200
   650  
   651    stickiness {
   652      type = "lb_cookie"
   653      cookie_duration = 10000
   654    }
   655  
   656    health_check {
   657      path = "/health"
   658      interval = 60
   659      port = 8081
   660      protocol = "HTTP"
   661      timeout = 3
   662      healthy_threshold = 3
   663      unhealthy_threshold = 3
   664      matcher = "200-299"
   665    }
   666  
   667    tags {
   668      Environment = "Production"
   669      Type = "ALB Target Group"
   670    }
   671  }
   672  
   673  resource "aws_vpc" "test" {
   674    cidr_block = "10.0.0.0/16"
   675  
   676    tags {
   677      TestName = "TestAccAWSALBTargetGroup_basic"
   678    }
   679  }`, targetGroupName)
   680  }
   681  
   682  func testAccAWSALBTargetGroupConfig_updateHealthCheck(targetGroupName string) string {
   683  	return fmt.Sprintf(`resource "aws_alb_target_group" "test" {
   684    name = "%s"
   685    port = 443
   686    protocol = "HTTPS"
   687    vpc_id = "${aws_vpc.test.id}"
   688  
   689    deregistration_delay = 200
   690  
   691    stickiness {
   692      type = "lb_cookie"
   693      cookie_duration = 10000
   694    }
   695  
   696    health_check {
   697      path = "/health2"
   698      interval = 30
   699      port = 8082
   700      protocol = "HTTPS"
   701      timeout = 4
   702      healthy_threshold = 4
   703      unhealthy_threshold = 4
   704      matcher = "200"
   705    }
   706  }
   707  
   708  resource "aws_vpc" "test" {
   709    cidr_block = "10.0.0.0/16"
   710  
   711    tags {
   712      TestName = "TestAccAWSALBTargetGroup_basic"
   713    }
   714  }`, targetGroupName)
   715  }
   716  
   717  func testAccAWSALBTargetGroupConfig_stickiness(targetGroupName string, addStickinessBlock bool, enabled bool) string {
   718  	var stickinessBlock string
   719  
   720  	if addStickinessBlock {
   721  		stickinessBlock = fmt.Sprintf(`stickiness {
   722  	    enabled = "%t"
   723  	    type = "lb_cookie"
   724  	    cookie_duration = 10000
   725  	  }`, enabled)
   726  	}
   727  
   728  	return fmt.Sprintf(`resource "aws_alb_target_group" "test" {
   729    name = "%s"
   730    port = 443
   731    protocol = "HTTPS"
   732    vpc_id = "${aws_vpc.test.id}"
   733  
   734    deregistration_delay = 200
   735  
   736    %s
   737  
   738    health_check {
   739      path = "/health2"
   740      interval = 30
   741      port = 8082
   742      protocol = "HTTPS"
   743      timeout = 4
   744      healthy_threshold = 4
   745      unhealthy_threshold = 4
   746      matcher = "200"
   747    }
   748  }
   749  
   750  resource "aws_vpc" "test" {
   751    cidr_block = "10.0.0.0/16"
   752  
   753    tags {
   754      TestName = "TestAccAWSALBTargetGroup_stickiness"
   755    }
   756  }`, targetGroupName, stickinessBlock)
   757  }
   758  
   759  const testAccAWSALBTargetGroupConfig_namePrefix = `
   760  resource "aws_alb_target_group" "test" {
   761    name_prefix = "tf-"
   762    port = 80
   763    protocol = "HTTP"
   764    vpc_id = "${aws_vpc.test.id}"
   765  }
   766  
   767  resource "aws_vpc" "test" {
   768    cidr_block = "10.0.0.0/16"
   769  }
   770  `
   771  
   772  const testAccAWSALBTargetGroupConfig_generatedName = `
   773  resource "aws_alb_target_group" "test" {
   774    port = 80
   775    protocol = "HTTP"
   776    vpc_id = "${aws_vpc.test.id}"
   777  }
   778  
   779  resource "aws_vpc" "test" {
   780    cidr_block = "10.0.0.0/16"
   781  }
   782  `