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