|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/aws/aws-sdk-go/service/elb" |
| 9 | + "github.com/hashicorp/terraform/helper/resource" |
| 10 | + "github.com/hashicorp/terraform/terraform" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccAWSELBAttachment_basic(t *testing.T) { |
| 14 | + var conf elb.LoadBalancerDescription |
| 15 | + |
| 16 | + testCheckInstanceAttached := func(count int) resource.TestCheckFunc { |
| 17 | + return func(*terraform.State) error { |
| 18 | + if len(conf.Instances) != count { |
| 19 | + return fmt.Errorf("instance count does not match") |
| 20 | + } |
| 21 | + return nil |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + resource.Test(t, resource.TestCase{ |
| 26 | + PreCheck: func() { testAccPreCheck(t) }, |
| 27 | + IDRefreshName: "aws_elb.bar", |
| 28 | + Providers: testAccProviders, |
| 29 | + CheckDestroy: testAccCheckAWSELBDestroy, |
| 30 | + Steps: []resource.TestStep{ |
| 31 | + resource.TestStep{ |
| 32 | + Config: testAccAWSELBAttachmentConfig1, |
| 33 | + Check: resource.ComposeTestCheckFunc( |
| 34 | + testAccCheckAWSELBExists("aws_elb.bar", &conf), |
| 35 | + testCheckInstanceAttached(1), |
| 36 | + ), |
| 37 | + }, |
| 38 | + |
| 39 | + resource.TestStep{ |
| 40 | + Config: testAccAWSELBAttachmentConfig2, |
| 41 | + Check: resource.ComposeTestCheckFunc( |
| 42 | + testAccCheckAWSELBExists("aws_elb.bar", &conf), |
| 43 | + testCheckInstanceAttached(2), |
| 44 | + ), |
| 45 | + }, |
| 46 | + |
| 47 | + resource.TestStep{ |
| 48 | + Config: testAccAWSELBAttachmentConfig3, |
| 49 | + Check: resource.ComposeTestCheckFunc( |
| 50 | + testAccCheckAWSELBExists("aws_elb.bar", &conf), |
| 51 | + testCheckInstanceAttached(2), |
| 52 | + ), |
| 53 | + }, |
| 54 | + |
| 55 | + resource.TestStep{ |
| 56 | + Config: testAccAWSELBAttachmentConfig4, |
| 57 | + Check: resource.ComposeTestCheckFunc( |
| 58 | + testAccCheckAWSELBExists("aws_elb.bar", &conf), |
| 59 | + testCheckInstanceAttached(0), |
| 60 | + ), |
| 61 | + }, |
| 62 | + }, |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +// remove and instance and check that it's correctly re-attached. |
| 67 | +func TestAccAWSELBAttachment_drift(t *testing.T) { |
| 68 | + var conf elb.LoadBalancerDescription |
| 69 | + |
| 70 | + deregInstance := func() { |
| 71 | + conn := testAccProvider.Meta().(*AWSClient).elbconn |
| 72 | + |
| 73 | + deRegisterInstancesOpts := elb.DeregisterInstancesFromLoadBalancerInput{ |
| 74 | + LoadBalancerName: conf.LoadBalancerName, |
| 75 | + Instances: conf.Instances, |
| 76 | + } |
| 77 | + |
| 78 | + log.Printf("[DEBUG] deregistering instance %s from ELB", conf.Instances[0].InstanceId) |
| 79 | + |
| 80 | + _, err := conn.DeregisterInstancesFromLoadBalancer(&deRegisterInstancesOpts) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("Failure deregistering instances from ELB: %s", err) |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + testCheckInstanceAttached := func(count int) resource.TestCheckFunc { |
| 88 | + return func(*terraform.State) error { |
| 89 | + if len(conf.Instances) != count { |
| 90 | + return fmt.Errorf("instance count does not match") |
| 91 | + } |
| 92 | + return nil |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + resource.Test(t, resource.TestCase{ |
| 97 | + PreCheck: func() { testAccPreCheck(t) }, |
| 98 | + IDRefreshName: "aws_elb.bar", |
| 99 | + Providers: testAccProviders, |
| 100 | + CheckDestroy: testAccCheckAWSELBDestroy, |
| 101 | + Steps: []resource.TestStep{ |
| 102 | + resource.TestStep{ |
| 103 | + Config: testAccAWSELBAttachmentConfig1, |
| 104 | + Check: resource.ComposeTestCheckFunc( |
| 105 | + testAccCheckAWSELBExists("aws_elb.bar", &conf), |
| 106 | + testCheckInstanceAttached(1), |
| 107 | + ), |
| 108 | + }, |
| 109 | + |
| 110 | + // remove an instance from the ELB, and make sure it gets re-added |
| 111 | + resource.TestStep{ |
| 112 | + Config: testAccAWSELBAttachmentConfig1, |
| 113 | + PreConfig: deregInstance, |
| 114 | + Check: resource.ComposeTestCheckFunc( |
| 115 | + testAccCheckAWSELBExists("aws_elb.bar", &conf), |
| 116 | + testCheckInstanceAttached(1), |
| 117 | + ), |
| 118 | + }, |
| 119 | + }, |
| 120 | + }) |
| 121 | +} |
| 122 | + |
| 123 | +// add one attachment |
| 124 | +const testAccAWSELBAttachmentConfig1 = ` |
| 125 | +resource "aws_elb" "bar" { |
| 126 | + availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] |
| 127 | +
|
| 128 | + listener { |
| 129 | + instance_port = 8000 |
| 130 | + instance_protocol = "http" |
| 131 | + lb_port = 80 |
| 132 | + lb_protocol = "http" |
| 133 | + } |
| 134 | +} |
| 135 | +
|
| 136 | +resource "aws_instance" "foo1" { |
| 137 | + # us-west-2 |
| 138 | + ami = "ami-043a5034" |
| 139 | + instance_type = "t1.micro" |
| 140 | +} |
| 141 | +
|
| 142 | +resource "aws_elb_attachment" "foo1" { |
| 143 | + elb = "${aws_elb.bar.id}" |
| 144 | + instance = "${aws_instance.foo1.id}" |
| 145 | +} |
| 146 | +` |
| 147 | + |
| 148 | +// add a second attachment |
| 149 | +const testAccAWSELBAttachmentConfig2 = ` |
| 150 | +resource "aws_elb" "bar" { |
| 151 | + availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] |
| 152 | +
|
| 153 | + listener { |
| 154 | + instance_port = 8000 |
| 155 | + instance_protocol = "http" |
| 156 | + lb_port = 80 |
| 157 | + lb_protocol = "http" |
| 158 | + } |
| 159 | +} |
| 160 | +
|
| 161 | +resource "aws_instance" "foo1" { |
| 162 | + # us-west-2 |
| 163 | + ami = "ami-043a5034" |
| 164 | + instance_type = "t1.micro" |
| 165 | +} |
| 166 | +
|
| 167 | +resource "aws_instance" "foo2" { |
| 168 | + # us-west-2 |
| 169 | + ami = "ami-043a5034" |
| 170 | + instance_type = "t1.micro" |
| 171 | +} |
| 172 | +
|
| 173 | +resource "aws_elb_attachment" "foo1" { |
| 174 | + elb = "${aws_elb.bar.id}" |
| 175 | + instance = "${aws_instance.foo1.id}" |
| 176 | +} |
| 177 | +
|
| 178 | +resource "aws_elb_attachment" "foo2" { |
| 179 | + elb = "${aws_elb.bar.id}" |
| 180 | + instance = "${aws_instance.foo2.id}" |
| 181 | +} |
| 182 | +` |
| 183 | + |
| 184 | +// swap attachments between resources |
| 185 | +const testAccAWSELBAttachmentConfig3 = ` |
| 186 | +resource "aws_elb" "bar" { |
| 187 | + availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] |
| 188 | +
|
| 189 | + listener { |
| 190 | + instance_port = 8000 |
| 191 | + instance_protocol = "http" |
| 192 | + lb_port = 80 |
| 193 | + lb_protocol = "http" |
| 194 | + } |
| 195 | +} |
| 196 | +
|
| 197 | +resource "aws_instance" "foo1" { |
| 198 | + # us-west-2 |
| 199 | + ami = "ami-043a5034" |
| 200 | + instance_type = "t1.micro" |
| 201 | +} |
| 202 | +
|
| 203 | +resource "aws_instance" "foo2" { |
| 204 | + # us-west-2 |
| 205 | + ami = "ami-043a5034" |
| 206 | + instance_type = "t1.micro" |
| 207 | +} |
| 208 | +
|
| 209 | +resource "aws_elb_attachment" "foo1" { |
| 210 | + elb = "${aws_elb.bar.id}" |
| 211 | + instance = "${aws_instance.foo2.id}" |
| 212 | +} |
| 213 | +
|
| 214 | +resource "aws_elb_attachment" "foo2" { |
| 215 | + elb = "${aws_elb.bar.id}" |
| 216 | + instance = "${aws_instance.foo1.id}" |
| 217 | +} |
| 218 | +` |
| 219 | + |
| 220 | +// destroy attachments |
| 221 | +const testAccAWSELBAttachmentConfig4 = ` |
| 222 | +resource "aws_elb" "bar" { |
| 223 | + availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] |
| 224 | +
|
| 225 | + listener { |
| 226 | + instance_port = 8000 |
| 227 | + instance_protocol = "http" |
| 228 | + lb_port = 80 |
| 229 | + lb_protocol = "http" |
| 230 | + } |
| 231 | +} |
| 232 | +` |
0 commit comments