github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_opsworks_instance_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/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/opsworks" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSOpsworksInstance_importBasic(t *testing.T) { 16 stackName := fmt.Sprintf("tf-%d", acctest.RandInt()) 17 resourceName := "aws_opsworks_instance.tf-acc" 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckAwsOpsworksInstanceDestroy, 23 Steps: []resource.TestStep{ 24 { 25 Config: testAccAwsOpsworksInstanceConfigCreate(stackName), 26 }, 27 28 { 29 ResourceName: resourceName, 30 ImportState: true, 31 ImportStateVerify: true, 32 ImportStateVerifyIgnore: []string{"state"}, //state is something we pass to the API and get back as status :( 33 }, 34 }, 35 }) 36 } 37 38 func TestAccAWSOpsworksInstance(t *testing.T) { 39 stackName := fmt.Sprintf("tf-%d", acctest.RandInt()) 40 var opsinst opsworks.Instance 41 resource.Test(t, resource.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Providers: testAccProviders, 44 CheckDestroy: testAccCheckAwsOpsworksInstanceDestroy, 45 Steps: []resource.TestStep{ 46 { 47 Config: testAccAwsOpsworksInstanceConfigCreate(stackName), 48 Check: resource.ComposeTestCheckFunc( 49 testAccCheckAWSOpsworksInstanceExists( 50 "aws_opsworks_instance.tf-acc", &opsinst), 51 testAccCheckAWSOpsworksInstanceAttributes(&opsinst), 52 resource.TestCheckResourceAttr( 53 "aws_opsworks_instance.tf-acc", "hostname", "tf-acc1", 54 ), 55 resource.TestCheckResourceAttr( 56 "aws_opsworks_instance.tf-acc", "instance_type", "t2.micro", 57 ), 58 resource.TestCheckResourceAttr( 59 "aws_opsworks_instance.tf-acc", "state", "stopped", 60 ), 61 resource.TestCheckResourceAttr( 62 "aws_opsworks_instance.tf-acc", "layer_ids.#", "1", 63 ), 64 resource.TestCheckResourceAttr( 65 "aws_opsworks_instance.tf-acc", "install_updates_on_boot", "true", 66 ), 67 resource.TestCheckResourceAttr( 68 "aws_opsworks_instance.tf-acc", "architecture", "x86_64", 69 ), 70 resource.TestCheckResourceAttr( 71 "aws_opsworks_instance.tf-acc", "tenancy", "default", 72 ), 73 resource.TestCheckResourceAttr( 74 "aws_opsworks_instance.tf-acc", "os", "Amazon Linux 2016.09", // inherited from opsworks_stack_test 75 ), 76 resource.TestCheckResourceAttr( 77 "aws_opsworks_instance.tf-acc", "root_device_type", "ebs", // inherited from opsworks_stack_test 78 ), 79 resource.TestCheckResourceAttr( 80 "aws_opsworks_instance.tf-acc", "availability_zone", "us-west-2a", // inherited from opsworks_stack_test 81 ), 82 ), 83 }, 84 { 85 Config: testAccAwsOpsworksInstanceConfigUpdate(stackName), 86 Check: resource.ComposeTestCheckFunc( 87 testAccCheckAWSOpsworksInstanceExists( 88 "aws_opsworks_instance.tf-acc", &opsinst), 89 testAccCheckAWSOpsworksInstanceAttributes(&opsinst), 90 resource.TestCheckResourceAttr( 91 "aws_opsworks_instance.tf-acc", "hostname", "tf-acc1", 92 ), 93 resource.TestCheckResourceAttr( 94 "aws_opsworks_instance.tf-acc", "instance_type", "t2.small", 95 ), 96 resource.TestCheckResourceAttr( 97 "aws_opsworks_instance.tf-acc", "layer_ids.#", "2", 98 ), 99 resource.TestCheckResourceAttr( 100 "aws_opsworks_instance.tf-acc", "os", "Amazon Linux 2015.09", 101 ), 102 resource.TestCheckResourceAttr( 103 "aws_opsworks_instance.tf-acc", "tenancy", "default", 104 ), 105 ), 106 }, 107 }, 108 }) 109 } 110 111 func TestAccAWSOpsworksInstance_UpdateHostNameForceNew(t *testing.T) { 112 stackName := fmt.Sprintf("tf-%d", acctest.RandInt()) 113 114 var before, after opsworks.Instance 115 resource.Test(t, resource.TestCase{ 116 PreCheck: func() { testAccPreCheck(t) }, 117 Providers: testAccProviders, 118 CheckDestroy: testAccCheckAwsOpsworksInstanceDestroy, 119 Steps: []resource.TestStep{ 120 { 121 Config: testAccAwsOpsworksInstanceConfigCreate(stackName), 122 Check: resource.ComposeTestCheckFunc( 123 testAccCheckAWSOpsworksInstanceExists("aws_opsworks_instance.tf-acc", &before), 124 resource.TestCheckResourceAttr("aws_opsworks_instance.tf-acc", "hostname", "tf-acc1"), 125 ), 126 }, 127 { 128 Config: testAccAwsOpsworksInstanceConfigUpdateHostName(stackName), 129 Check: resource.ComposeTestCheckFunc( 130 testAccCheckAWSOpsworksInstanceExists("aws_opsworks_instance.tf-acc", &after), 131 resource.TestCheckResourceAttr("aws_opsworks_instance.tf-acc", "hostname", "tf-acc2"), 132 testAccCheckAwsOpsworksInstanceRecreated(t, &before, &after), 133 ), 134 }, 135 }, 136 }) 137 } 138 139 func testAccCheckAwsOpsworksInstanceRecreated(t *testing.T, 140 before, after *opsworks.Instance) resource.TestCheckFunc { 141 return func(s *terraform.State) error { 142 if *before.InstanceId == *after.InstanceId { 143 t.Fatalf("Expected change of OpsWorks Instance IDs, but both were %s", *before.InstanceId) 144 } 145 return nil 146 } 147 } 148 149 func testAccCheckAWSOpsworksInstanceExists( 150 n string, opsinst *opsworks.Instance) resource.TestCheckFunc { 151 return func(s *terraform.State) error { 152 rs, ok := s.RootModule().Resources[n] 153 if !ok { 154 return fmt.Errorf("Not found: %s", n) 155 } 156 157 if rs.Primary.ID == "" { 158 return fmt.Errorf("No Opsworks Instance is set") 159 } 160 161 conn := testAccProvider.Meta().(*AWSClient).opsworksconn 162 163 params := &opsworks.DescribeInstancesInput{ 164 InstanceIds: []*string{&rs.Primary.ID}, 165 } 166 resp, err := conn.DescribeInstances(params) 167 168 if err != nil { 169 return err 170 } 171 172 if v := len(resp.Instances); v != 1 { 173 return fmt.Errorf("Expected 1 request returned, got %d", v) 174 } 175 176 *opsinst = *resp.Instances[0] 177 178 return nil 179 } 180 } 181 182 func testAccCheckAWSOpsworksInstanceAttributes( 183 opsinst *opsworks.Instance) resource.TestCheckFunc { 184 return func(s *terraform.State) error { 185 // Depending on the timing, the state could be requested or stopped 186 if *opsinst.Status != "stopped" && *opsinst.Status != "requested" { 187 return fmt.Errorf("Unexpected request status: %s", *opsinst.Status) 188 } 189 if *opsinst.AvailabilityZone != "us-west-2a" { 190 return fmt.Errorf("Unexpected availability zone: %s", *opsinst.AvailabilityZone) 191 } 192 if *opsinst.Architecture != "x86_64" { 193 return fmt.Errorf("Unexpected architecture: %s", *opsinst.Architecture) 194 } 195 if *opsinst.Tenancy != "default" { 196 return fmt.Errorf("Unexpected tenancy: %s", *opsinst.Tenancy) 197 } 198 if *opsinst.InfrastructureClass != "ec2" { 199 return fmt.Errorf("Unexpected infrastructure class: %s", *opsinst.InfrastructureClass) 200 } 201 if *opsinst.RootDeviceType != "ebs" { 202 return fmt.Errorf("Unexpected root device type: %s", *opsinst.RootDeviceType) 203 } 204 if *opsinst.VirtualizationType != "hvm" { 205 return fmt.Errorf("Unexpected virtualization type: %s", *opsinst.VirtualizationType) 206 } 207 return nil 208 } 209 } 210 211 func testAccCheckAwsOpsworksInstanceDestroy(s *terraform.State) error { 212 opsworksconn := testAccProvider.Meta().(*AWSClient).opsworksconn 213 for _, rs := range s.RootModule().Resources { 214 if rs.Type != "aws_opsworks_instance" { 215 continue 216 } 217 req := &opsworks.DescribeInstancesInput{ 218 InstanceIds: []*string{ 219 aws.String(rs.Primary.ID), 220 }, 221 } 222 223 _, err := opsworksconn.DescribeInstances(req) 224 if err != nil { 225 if awserr, ok := err.(awserr.Error); ok { 226 if awserr.Code() == "ResourceNotFoundException" { 227 // not found, good to go 228 return nil 229 } 230 } 231 return err 232 } 233 } 234 235 return fmt.Errorf("Fall through error on OpsWorks instance test") 236 } 237 238 func testAccAwsOpsworksInstanceConfigUpdateHostName(name string) string { 239 return fmt.Sprintf(` 240 resource "aws_security_group" "tf-ops-acc-web" { 241 name = "%s-web" 242 ingress { 243 from_port = 80 244 to_port = 80 245 protocol = "tcp" 246 cidr_blocks = ["0.0.0.0/0"] 247 } 248 } 249 250 resource "aws_security_group" "tf-ops-acc-php" { 251 name = "%s-php" 252 ingress { 253 from_port = 8080 254 to_port = 8080 255 protocol = "tcp" 256 cidr_blocks = ["0.0.0.0/0"] 257 } 258 } 259 260 resource "aws_opsworks_static_web_layer" "tf-acc" { 261 stack_id = "${aws_opsworks_stack.tf-acc.id}" 262 263 custom_security_group_ids = [ 264 "${aws_security_group.tf-ops-acc-web.id}", 265 ] 266 } 267 268 resource "aws_opsworks_php_app_layer" "tf-acc" { 269 stack_id = "${aws_opsworks_stack.tf-acc.id}" 270 271 custom_security_group_ids = [ 272 "${aws_security_group.tf-ops-acc-php.id}", 273 ] 274 } 275 276 resource "aws_opsworks_instance" "tf-acc" { 277 stack_id = "${aws_opsworks_stack.tf-acc.id}" 278 layer_ids = [ 279 "${aws_opsworks_static_web_layer.tf-acc.id}", 280 ] 281 instance_type = "t2.micro" 282 state = "stopped" 283 hostname = "tf-acc2" 284 } 285 286 %s 287 288 `, name, name, testAccAwsOpsworksStackConfigVpcCreate(name)) 289 } 290 291 func testAccAwsOpsworksInstanceConfigCreate(name string) string { 292 return fmt.Sprintf(` 293 resource "aws_security_group" "tf-ops-acc-web" { 294 name = "%s-web" 295 ingress { 296 from_port = 80 297 to_port = 80 298 protocol = "tcp" 299 cidr_blocks = ["0.0.0.0/0"] 300 } 301 } 302 303 resource "aws_security_group" "tf-ops-acc-php" { 304 name = "%s-php" 305 ingress { 306 from_port = 8080 307 to_port = 8080 308 protocol = "tcp" 309 cidr_blocks = ["0.0.0.0/0"] 310 } 311 } 312 313 resource "aws_opsworks_static_web_layer" "tf-acc" { 314 stack_id = "${aws_opsworks_stack.tf-acc.id}" 315 316 custom_security_group_ids = [ 317 "${aws_security_group.tf-ops-acc-web.id}", 318 ] 319 } 320 321 resource "aws_opsworks_php_app_layer" "tf-acc" { 322 stack_id = "${aws_opsworks_stack.tf-acc.id}" 323 324 custom_security_group_ids = [ 325 "${aws_security_group.tf-ops-acc-php.id}", 326 ] 327 } 328 329 resource "aws_opsworks_instance" "tf-acc" { 330 stack_id = "${aws_opsworks_stack.tf-acc.id}" 331 layer_ids = [ 332 "${aws_opsworks_static_web_layer.tf-acc.id}", 333 ] 334 instance_type = "t2.micro" 335 state = "stopped" 336 hostname = "tf-acc1" 337 } 338 339 %s 340 341 `, name, name, testAccAwsOpsworksStackConfigVpcCreate(name)) 342 } 343 344 func testAccAwsOpsworksInstanceConfigUpdate(name string) string { 345 return fmt.Sprintf(` 346 resource "aws_security_group" "tf-ops-acc-web" { 347 name = "%s-web" 348 ingress { 349 from_port = 80 350 to_port = 80 351 protocol = "tcp" 352 cidr_blocks = ["0.0.0.0/0"] 353 } 354 } 355 356 resource "aws_security_group" "tf-ops-acc-php" { 357 name = "%s-php" 358 ingress { 359 from_port = 8080 360 to_port = 8080 361 protocol = "tcp" 362 cidr_blocks = ["0.0.0.0/0"] 363 } 364 } 365 366 resource "aws_opsworks_static_web_layer" "tf-acc" { 367 stack_id = "${aws_opsworks_stack.tf-acc.id}" 368 369 custom_security_group_ids = [ 370 "${aws_security_group.tf-ops-acc-web.id}", 371 ] 372 } 373 374 resource "aws_opsworks_php_app_layer" "tf-acc" { 375 stack_id = "${aws_opsworks_stack.tf-acc.id}" 376 377 custom_security_group_ids = [ 378 "${aws_security_group.tf-ops-acc-php.id}", 379 ] 380 } 381 382 resource "aws_opsworks_instance" "tf-acc" { 383 stack_id = "${aws_opsworks_stack.tf-acc.id}" 384 layer_ids = [ 385 "${aws_opsworks_static_web_layer.tf-acc.id}", 386 "${aws_opsworks_php_app_layer.tf-acc.id}", 387 ] 388 instance_type = "t2.small" 389 state = "stopped" 390 hostname = "tf-acc1" 391 os = "Amazon Linux 2015.09" 392 } 393 394 %s 395 396 `, name, name, testAccAwsOpsworksStackConfigVpcCreate(name)) 397 }