github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_alb_target_group_attachment_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strconv"
     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 TestAccAWSALBTargetGroupAttachment_basic(t *testing.T) {
    18  	targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:      func() { testAccPreCheck(t) },
    22  		IDRefreshName: "aws_alb_target_group.test",
    23  		Providers:     testAccProviders,
    24  		CheckDestroy:  testAccCheckAWSALBTargetGroupAttachmentDestroy,
    25  		Steps: []resource.TestStep{
    26  			{
    27  				Config: testAccAWSALBTargetGroupAttachmentConfig_basic(targetGroupName),
    28  				Check: resource.ComposeAggregateTestCheckFunc(
    29  					testAccCheckAWSALBTargetGroupAttachmentExists("aws_alb_target_group_attachment.test"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestAccAWSALBTargetGroupAttachment_withoutPort(t *testing.T) {
    37  	targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:      func() { testAccPreCheck(t) },
    41  		IDRefreshName: "aws_alb_target_group.test",
    42  		Providers:     testAccProviders,
    43  		CheckDestroy:  testAccCheckAWSALBTargetGroupAttachmentDestroy,
    44  		Steps: []resource.TestStep{
    45  			{
    46  				Config: testAccAWSALBTargetGroupAttachmentConfigWithoutPort(targetGroupName),
    47  				Check: resource.ComposeAggregateTestCheckFunc(
    48  					testAccCheckAWSALBTargetGroupAttachmentExists("aws_alb_target_group_attachment.test"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testAccCheckAWSALBTargetGroupAttachmentExists(n string) resource.TestCheckFunc {
    56  	return func(s *terraform.State) error {
    57  		rs, ok := s.RootModule().Resources[n]
    58  		if !ok {
    59  			return fmt.Errorf("Not found: %s", n)
    60  		}
    61  
    62  		if rs.Primary.ID == "" {
    63  			return errors.New("No Target Group Attachment ID is set")
    64  		}
    65  
    66  		conn := testAccProvider.Meta().(*AWSClient).elbv2conn
    67  
    68  		_, hasPort := rs.Primary.Attributes["port"]
    69  		targetGroupArn, _ := rs.Primary.Attributes["target_group_arn"]
    70  
    71  		target := &elbv2.TargetDescription{
    72  			Id: aws.String(rs.Primary.Attributes["target_id"]),
    73  		}
    74  		if hasPort == true {
    75  			port, _ := strconv.Atoi(rs.Primary.Attributes["port"])
    76  			target.Port = aws.Int64(int64(port))
    77  		}
    78  
    79  		describe, err := conn.DescribeTargetHealth(&elbv2.DescribeTargetHealthInput{
    80  			TargetGroupArn: aws.String(targetGroupArn),
    81  			Targets:        []*elbv2.TargetDescription{target},
    82  		})
    83  
    84  		if err != nil {
    85  			return err
    86  		}
    87  
    88  		if len(describe.TargetHealthDescriptions) != 1 {
    89  			return errors.New("Target Group Attachment not found")
    90  		}
    91  
    92  		return nil
    93  	}
    94  }
    95  
    96  func testAccCheckAWSALBTargetGroupAttachmentDestroy(s *terraform.State) error {
    97  	conn := testAccProvider.Meta().(*AWSClient).elbv2conn
    98  
    99  	for _, rs := range s.RootModule().Resources {
   100  		if rs.Type != "aws_alb_target_group_attachment" {
   101  			continue
   102  		}
   103  
   104  		_, hasPort := rs.Primary.Attributes["port"]
   105  		targetGroupArn, _ := rs.Primary.Attributes["target_group_arn"]
   106  
   107  		target := &elbv2.TargetDescription{
   108  			Id: aws.String(rs.Primary.Attributes["target_id"]),
   109  		}
   110  		if hasPort == true {
   111  			port, _ := strconv.Atoi(rs.Primary.Attributes["port"])
   112  			target.Port = aws.Int64(int64(port))
   113  		}
   114  
   115  		describe, err := conn.DescribeTargetHealth(&elbv2.DescribeTargetHealthInput{
   116  			TargetGroupArn: aws.String(targetGroupArn),
   117  			Targets:        []*elbv2.TargetDescription{target},
   118  		})
   119  		if err == nil {
   120  			if len(describe.TargetHealthDescriptions) != 0 {
   121  				return fmt.Errorf("Target Group Attachment %q still exists", rs.Primary.ID)
   122  			}
   123  		}
   124  
   125  		// Verify the error
   126  		if isTargetGroupNotFound(err) || isInvalidTarget(err) {
   127  			return nil
   128  		} else {
   129  			return errwrap.Wrapf("Unexpected error checking ALB destroyed: {{err}}", err)
   130  		}
   131  	}
   132  
   133  	return nil
   134  }
   135  
   136  func testAccAWSALBTargetGroupAttachmentConfigWithoutPort(targetGroupName string) string {
   137  	return fmt.Sprintf(`
   138  resource "aws_alb_target_group_attachment" "test" {
   139    target_group_arn = "${aws_alb_target_group.test.arn}"
   140    target_id = "${aws_instance.test.id}"
   141  }
   142  
   143  resource "aws_instance" "test" {
   144    ami = "ami-f701cb97"
   145    instance_type = "t2.micro"
   146    subnet_id = "${aws_subnet.subnet.id}"
   147  }
   148  
   149  resource "aws_alb_target_group" "test" {
   150    name = "%s"
   151    port = 443
   152    protocol = "HTTPS"
   153    vpc_id = "${aws_vpc.test.id}"
   154  
   155    deregistration_delay = 200
   156  
   157    stickiness {
   158      type = "lb_cookie"
   159      cookie_duration = 10000
   160    }
   161  
   162    health_check {
   163      path = "/health"
   164      interval = 60
   165      port = 8081
   166      protocol = "HTTP"
   167      timeout = 3
   168      healthy_threshold = 3
   169      unhealthy_threshold = 3
   170      matcher = "200-299"
   171    }
   172  }
   173  
   174  resource "aws_subnet" "subnet" {
   175    cidr_block = "10.0.1.0/24"
   176    vpc_id = "${aws_vpc.test.id}"
   177  
   178  }
   179  
   180  resource "aws_vpc" "test" {
   181    cidr_block = "10.0.0.0/16"
   182  }`, targetGroupName)
   183  }
   184  
   185  func testAccAWSALBTargetGroupAttachmentConfig_basic(targetGroupName string) string {
   186  	return fmt.Sprintf(`
   187  resource "aws_alb_target_group_attachment" "test" {
   188    target_group_arn = "${aws_alb_target_group.test.arn}"
   189    target_id = "${aws_instance.test.id}"
   190    port = 80
   191  }
   192  
   193  resource "aws_instance" "test" {
   194    ami = "ami-f701cb97"
   195    instance_type = "t2.micro"
   196    subnet_id = "${aws_subnet.subnet.id}"
   197  }
   198  
   199  resource "aws_alb_target_group" "test" {
   200    name = "%s"
   201    port = 443
   202    protocol = "HTTPS"
   203    vpc_id = "${aws_vpc.test.id}"
   204  
   205    deregistration_delay = 200
   206  
   207    stickiness {
   208      type = "lb_cookie"
   209      cookie_duration = 10000
   210    }
   211  
   212    health_check {
   213      path = "/health"
   214      interval = 60
   215      port = 8081
   216      protocol = "HTTP"
   217      timeout = 3
   218      healthy_threshold = 3
   219      unhealthy_threshold = 3
   220      matcher = "200-299"
   221    }
   222  }
   223  
   224  resource "aws_subnet" "subnet" {
   225    cidr_block = "10.0.1.0/24"
   226    vpc_id = "${aws_vpc.test.id}"
   227  
   228  }
   229  
   230  resource "aws_vpc" "test" {
   231    cidr_block = "10.0.0.0/16"
   232  }`, targetGroupName)
   233  }