github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_alb_target_group_attachment_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/service/elbv2"
     8  	"github.com/hashicorp/errwrap"
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  	"strconv"
    13  	"testing"
    14  )
    15  
    16  func TestAccAWSALBTargetGroupAttachment_basic(t *testing.T) {
    17  	targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:      func() { testAccPreCheck(t) },
    21  		IDRefreshName: "aws_alb_target_group.test",
    22  		Providers:     testAccProviders,
    23  		CheckDestroy:  testAccCheckAWSALBTargetGroupAttachmentDestroy,
    24  		Steps: []resource.TestStep{
    25  			{
    26  				Config: testAccAWSALBTargetGroupAttachmentConfig_basic(targetGroupName),
    27  				Check: resource.ComposeAggregateTestCheckFunc(
    28  					testAccCheckAWSALBTargetGroupAttachmentExists("aws_alb_target_group_attachment.test"),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func testAccCheckAWSALBTargetGroupAttachmentExists(n string) resource.TestCheckFunc {
    36  	return func(s *terraform.State) error {
    37  		rs, ok := s.RootModule().Resources[n]
    38  		if !ok {
    39  			return fmt.Errorf("Not found: %s", n)
    40  		}
    41  
    42  		if rs.Primary.ID == "" {
    43  			return errors.New("No Target Group Attachment ID is set")
    44  		}
    45  
    46  		conn := testAccProvider.Meta().(*AWSClient).elbv2conn
    47  
    48  		port, _ := strconv.Atoi(rs.Primary.Attributes["port"])
    49  		describe, err := conn.DescribeTargetHealth(&elbv2.DescribeTargetHealthInput{
    50  			TargetGroupArn: aws.String(rs.Primary.Attributes["target_group_arn"]),
    51  			Targets: []*elbv2.TargetDescription{
    52  				{
    53  					Id:   aws.String(rs.Primary.Attributes["target_id"]),
    54  					Port: aws.Int64(int64(port)),
    55  				},
    56  			},
    57  		})
    58  
    59  		if err != nil {
    60  			return err
    61  		}
    62  
    63  		if len(describe.TargetHealthDescriptions) != 1 {
    64  			return errors.New("Target Group Attachment not found")
    65  		}
    66  
    67  		return nil
    68  	}
    69  }
    70  
    71  func testAccCheckAWSALBTargetGroupAttachmentDestroy(s *terraform.State) error {
    72  	conn := testAccProvider.Meta().(*AWSClient).elbv2conn
    73  
    74  	for _, rs := range s.RootModule().Resources {
    75  		if rs.Type != "aws_alb_target_group_attachment" {
    76  			continue
    77  		}
    78  
    79  		port, _ := strconv.Atoi(rs.Primary.Attributes["port"])
    80  		describe, err := conn.DescribeTargetHealth(&elbv2.DescribeTargetHealthInput{
    81  			TargetGroupArn: aws.String(rs.Primary.Attributes["target_group_arn"]),
    82  			Targets: []*elbv2.TargetDescription{
    83  				{
    84  					Id:   aws.String(rs.Primary.Attributes["target_id"]),
    85  					Port: aws.Int64(int64(port)),
    86  				},
    87  			},
    88  		})
    89  		if err == nil {
    90  			if len(describe.TargetHealthDescriptions) != 0 {
    91  				return fmt.Errorf("Target Group Attachment %q still exists", rs.Primary.ID)
    92  			}
    93  		}
    94  
    95  		// Verify the error
    96  		if isTargetGroupNotFound(err) || isInvalidTarget(err) {
    97  			return nil
    98  		} else {
    99  			return errwrap.Wrapf("Unexpected error checking ALB destroyed: {{err}}", err)
   100  		}
   101  	}
   102  
   103  	return nil
   104  }
   105  
   106  func testAccAWSALBTargetGroupAttachmentConfig_basic(targetGroupName string) string {
   107  	return fmt.Sprintf(`
   108  resource "aws_alb_target_group_attachment" "test" {
   109    target_group_arn = "${aws_alb_target_group.test.arn}"
   110    target_id = "${aws_instance.test.id}"
   111    port = 80
   112  }
   113  
   114  resource "aws_instance" "test" {
   115    ami = "ami-f701cb97"
   116    instance_type = "t2.micro"
   117    subnet_id = "${aws_subnet.subnet.id}"
   118  }
   119  
   120  resource "aws_alb_target_group" "test" {
   121    name = "%s"
   122    port = 443
   123    protocol = "HTTPS"
   124    vpc_id = "${aws_vpc.test.id}"
   125  
   126    deregistration_delay = 200
   127  
   128    stickiness {
   129      type = "lb_cookie"
   130      cookie_duration = 10000
   131    }
   132  
   133    health_check {
   134      path = "/health"
   135      interval = 60
   136      port = 8081
   137      protocol = "HTTP"
   138      timeout = 3
   139      healthy_threshold = 3
   140      unhealthy_threshold = 3
   141      matcher = "200-299"
   142    }
   143  }
   144  
   145  resource "aws_subnet" "subnet" {
   146    cidr_block = "10.0.1.0/24"
   147    vpc_id = "${aws_vpc.test.id}"
   148  
   149  }
   150  
   151  resource "aws_vpc" "test" {
   152    cidr_block = "10.0.0.0/16"
   153  }`, targetGroupName)
   154  }