github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_autoscaling_attachment_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/autoscaling" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAwsAutoscalingAttachment_basic(t *testing.T) { 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 Steps: []resource.TestStep{ 18 resource.TestStep{ 19 Config: testAccAWSAutoscalingAttachment_basic, 20 Check: resource.ComposeTestCheckFunc( 21 testAccCheckAWSAutocalingAttachmentExists("aws_autoscaling_group.asg", 0), 22 ), 23 }, 24 // Add in one association 25 resource.TestStep{ 26 Config: testAccAWSAutoscalingAttachment_associated, 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckAWSAutocalingAttachmentExists("aws_autoscaling_group.asg", 1), 29 ), 30 }, 31 // Test adding a 2nd 32 resource.TestStep{ 33 Config: testAccAWSAutoscalingAttachment_double_associated, 34 Check: resource.ComposeTestCheckFunc( 35 testAccCheckAWSAutocalingAttachmentExists("aws_autoscaling_group.asg", 2), 36 ), 37 }, 38 // Now remove that newest one 39 resource.TestStep{ 40 Config: testAccAWSAutoscalingAttachment_associated, 41 Check: resource.ComposeTestCheckFunc( 42 testAccCheckAWSAutocalingAttachmentExists("aws_autoscaling_group.asg", 1), 43 ), 44 }, 45 // Now remove them both 46 resource.TestStep{ 47 Config: testAccAWSAutoscalingAttachment_basic, 48 Check: resource.ComposeTestCheckFunc( 49 testAccCheckAWSAutocalingAttachmentExists("aws_autoscaling_group.asg", 0), 50 ), 51 }, 52 }, 53 }) 54 } 55 56 func testAccCheckAWSAutocalingAttachmentExists(asgname string, loadBalancerCount int) resource.TestCheckFunc { 57 return func(s *terraform.State) error { 58 rs, ok := s.RootModule().Resources[asgname] 59 if !ok { 60 return fmt.Errorf("Not found: %s", asgname) 61 } 62 63 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 64 asg := rs.Primary.ID 65 66 actual, err := conn.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{ 67 AutoScalingGroupNames: []*string{aws.String(asg)}, 68 }) 69 70 if err != nil { 71 return fmt.Errorf("Recieved an error when attempting to load %s: %s", asg, err) 72 } 73 74 if loadBalancerCount != len(actual.AutoScalingGroups[0].LoadBalancerNames) { 75 return fmt.Errorf("Error: ASG has the wrong number of load balacners associated. Expected [%d] but got [%d]", loadBalancerCount, len(actual.AutoScalingGroups[0].LoadBalancerNames)) 76 } 77 78 return nil 79 } 80 } 81 82 const testAccAWSAutoscalingAttachment_basic = ` 83 resource "aws_elb" "foo" { 84 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 85 86 listener { 87 instance_port = 8000 88 instance_protocol = "http" 89 lb_port = 80 90 lb_protocol = "http" 91 } 92 } 93 94 resource "aws_elb" "bar" { 95 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 96 97 listener { 98 instance_port = 8000 99 instance_protocol = "http" 100 lb_port = 80 101 lb_protocol = "http" 102 } 103 } 104 105 resource "aws_launch_configuration" "as_conf" { 106 name = "test_config" 107 image_id = "ami-f34032c3" 108 instance_type = "t1.micro" 109 } 110 111 resource "aws_autoscaling_group" "asg" { 112 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 113 name = "asg-lb-assoc-terraform-test" 114 max_size = 1 115 min_size = 0 116 desired_capacity = 0 117 health_check_grace_period = 300 118 force_delete = true 119 launch_configuration = "${aws_launch_configuration.as_conf.name}" 120 121 tag { 122 key = "Name" 123 value = "terraform-asg-lg-assoc-test" 124 propagate_at_launch = true 125 } 126 } 127 128 ` 129 130 const testAccAWSAutoscalingAttachment_associated = testAccAWSAutoscalingAttachment_basic + ` 131 resource "aws_autoscaling_attachment" "asg_attachment_foo" { 132 autoscaling_group_name = "${aws_autoscaling_group.asg.id}" 133 elb = "${aws_elb.foo.id}" 134 } 135 136 ` 137 138 const testAccAWSAutoscalingAttachment_double_associated = testAccAWSAutoscalingAttachment_associated + ` 139 resource "aws_autoscaling_attachment" "asg_attachment_bar" { 140 autoscaling_group_name = "${aws_autoscaling_group.asg.id}" 141 elb = "${aws_elb.bar.id}" 142 } 143 144 `