github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_opsworks_custom_layer_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/aws/awserr" 10 "github.com/aws/aws-sdk-go/service/opsworks" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 // These tests assume the existence of predefined Opsworks IAM roles named `aws-opsworks-ec2-role` 17 // and `aws-opsworks-service-role`. 18 19 func TestAccAWSOpsworksCustomLayer(t *testing.T) { 20 stackName := fmt.Sprintf("tf-%d", acctest.RandInt()) 21 var opslayer opsworks.Layer 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckAwsOpsworksCustomLayerDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccAwsOpsworksCustomLayerConfigNoVpcCreate(stackName), 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckAWSOpsworksCustomLayerExists( 31 "aws_opsworks_custom_layer.tf-acc", &opslayer), 32 testAccCheckAWSOpsworksCreateLayerAttributes(&opslayer, stackName), 33 resource.TestCheckResourceAttr( 34 "aws_opsworks_custom_layer.tf-acc", "name", stackName, 35 ), 36 resource.TestCheckResourceAttr( 37 "aws_opsworks_custom_layer.tf-acc", "auto_assign_elastic_ips", "false", 38 ), 39 resource.TestCheckResourceAttr( 40 "aws_opsworks_custom_layer.tf-acc", "auto_healing", "true", 41 ), 42 resource.TestCheckResourceAttr( 43 "aws_opsworks_custom_layer.tf-acc", "drain_elb_on_shutdown", "true", 44 ), 45 resource.TestCheckResourceAttr( 46 "aws_opsworks_custom_layer.tf-acc", "instance_shutdown_timeout", "300", 47 ), 48 resource.TestCheckResourceAttr( 49 "aws_opsworks_custom_layer.tf-acc", "custom_security_group_ids.#", "2", 50 ), 51 resource.TestCheckResourceAttr( 52 "aws_opsworks_custom_layer.tf-acc", "system_packages.#", "2", 53 ), 54 resource.TestCheckResourceAttr( 55 "aws_opsworks_custom_layer.tf-acc", "system_packages.1368285564", "git", 56 ), 57 resource.TestCheckResourceAttr( 58 "aws_opsworks_custom_layer.tf-acc", "system_packages.2937857443", "golang", 59 ), 60 resource.TestCheckResourceAttr( 61 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.#", "1", 62 ), 63 resource.TestCheckResourceAttr( 64 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.type", "gp2", 65 ), 66 resource.TestCheckResourceAttr( 67 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.number_of_disks", "2", 68 ), 69 resource.TestCheckResourceAttr( 70 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.mount_point", "/home", 71 ), 72 resource.TestCheckResourceAttr( 73 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.size", "100", 74 ), 75 ), 76 }, 77 resource.TestStep{ 78 Config: testAccAwsOpsworksCustomLayerConfigUpdate(stackName), 79 Check: resource.ComposeTestCheckFunc( 80 resource.TestCheckResourceAttr( 81 "aws_opsworks_custom_layer.tf-acc", "name", stackName, 82 ), 83 resource.TestCheckResourceAttr( 84 "aws_opsworks_custom_layer.tf-acc", "drain_elb_on_shutdown", "false", 85 ), 86 resource.TestCheckResourceAttr( 87 "aws_opsworks_custom_layer.tf-acc", "instance_shutdown_timeout", "120", 88 ), 89 resource.TestCheckResourceAttr( 90 "aws_opsworks_custom_layer.tf-acc", "custom_security_group_ids.#", "3", 91 ), 92 resource.TestCheckResourceAttr( 93 "aws_opsworks_custom_layer.tf-acc", "system_packages.#", "3", 94 ), 95 resource.TestCheckResourceAttr( 96 "aws_opsworks_custom_layer.tf-acc", "system_packages.1368285564", "git", 97 ), 98 resource.TestCheckResourceAttr( 99 "aws_opsworks_custom_layer.tf-acc", "system_packages.2937857443", "golang", 100 ), 101 resource.TestCheckResourceAttr( 102 "aws_opsworks_custom_layer.tf-acc", "system_packages.4101929740", "subversion", 103 ), 104 resource.TestCheckResourceAttr( 105 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.#", "2", 106 ), 107 resource.TestCheckResourceAttr( 108 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.type", "gp2", 109 ), 110 resource.TestCheckResourceAttr( 111 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.number_of_disks", "2", 112 ), 113 resource.TestCheckResourceAttr( 114 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.mount_point", "/home", 115 ), 116 resource.TestCheckResourceAttr( 117 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.size", "100", 118 ), 119 resource.TestCheckResourceAttr( 120 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.type", "io1", 121 ), 122 resource.TestCheckResourceAttr( 123 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.number_of_disks", "4", 124 ), 125 resource.TestCheckResourceAttr( 126 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.mount_point", "/var", 127 ), 128 resource.TestCheckResourceAttr( 129 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.size", "100", 130 ), 131 resource.TestCheckResourceAttr( 132 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.raid_level", "1", 133 ), 134 resource.TestCheckResourceAttr( 135 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.iops", "3000", 136 ), 137 resource.TestCheckResourceAttr( 138 "aws_opsworks_custom_layer.tf-acc", "custom_json", `{"layer_key":"layer_value2"}`, 139 ), 140 ), 141 }, 142 }, 143 }) 144 } 145 146 func testAccCheckAWSOpsworksCustomLayerExists( 147 n string, opslayer *opsworks.Layer) resource.TestCheckFunc { 148 return func(s *terraform.State) error { 149 rs, ok := s.RootModule().Resources[n] 150 if !ok { 151 return fmt.Errorf("Not found: %s", n) 152 } 153 154 if rs.Primary.ID == "" { 155 return fmt.Errorf("No ID is set") 156 } 157 158 conn := testAccProvider.Meta().(*AWSClient).opsworksconn 159 160 params := &opsworks.DescribeLayersInput{ 161 LayerIds: []*string{aws.String(rs.Primary.ID)}, 162 } 163 resp, err := conn.DescribeLayers(params) 164 165 if err != nil { 166 return err 167 } 168 169 if v := len(resp.Layers); v != 1 { 170 return fmt.Errorf("Expected 1 response returned, got %d", v) 171 } 172 173 *opslayer = *resp.Layers[0] 174 175 return nil 176 } 177 } 178 179 func testAccCheckAWSOpsworksCreateLayerAttributes( 180 opslayer *opsworks.Layer, stackName string) resource.TestCheckFunc { 181 return func(s *terraform.State) error { 182 if *opslayer.Name != stackName { 183 return fmt.Errorf("Unexpected name: %s", *opslayer.Name) 184 } 185 186 if *opslayer.AutoAssignElasticIps { 187 return fmt.Errorf( 188 "Unexpected AutoAssignElasticIps: %t", *opslayer.AutoAssignElasticIps) 189 } 190 191 if !*opslayer.EnableAutoHealing { 192 return fmt.Errorf( 193 "Unexpected EnableAutoHealing: %t", *opslayer.EnableAutoHealing) 194 } 195 196 if !*opslayer.LifecycleEventConfiguration.Shutdown.DelayUntilElbConnectionsDrained { 197 return fmt.Errorf( 198 "Unexpected DelayUntilElbConnectionsDrained: %t", 199 *opslayer.LifecycleEventConfiguration.Shutdown.DelayUntilElbConnectionsDrained) 200 } 201 202 if *opslayer.LifecycleEventConfiguration.Shutdown.ExecutionTimeout != 300 { 203 return fmt.Errorf( 204 "Unexpected ExecutionTimeout: %d", 205 *opslayer.LifecycleEventConfiguration.Shutdown.ExecutionTimeout) 206 } 207 208 if v := len(opslayer.CustomSecurityGroupIds); v != 2 { 209 return fmt.Errorf("Expected 2 customSecurityGroupIds, got %d", v) 210 } 211 212 expectedPackages := []*string{ 213 aws.String("git"), 214 aws.String("golang"), 215 } 216 217 if !reflect.DeepEqual(expectedPackages, opslayer.Packages) { 218 return fmt.Errorf("Unexpected Packages: %v", aws.StringValueSlice(opslayer.Packages)) 219 } 220 221 expectedEbsVolumes := []*opsworks.VolumeConfiguration{ 222 &opsworks.VolumeConfiguration{ 223 VolumeType: aws.String("gp2"), 224 NumberOfDisks: aws.Int64(2), 225 MountPoint: aws.String("/home"), 226 Size: aws.Int64(100), 227 RaidLevel: aws.Int64(0), 228 }, 229 } 230 231 if !reflect.DeepEqual(expectedEbsVolumes, opslayer.VolumeConfigurations) { 232 return fmt.Errorf("Unnexpected VolumeConfiguration: %s", opslayer.VolumeConfigurations) 233 } 234 235 return nil 236 } 237 } 238 239 func testAccCheckAwsOpsworksCustomLayerDestroy(s *terraform.State) error { 240 opsworksconn := testAccProvider.Meta().(*AWSClient).opsworksconn 241 for _, rs := range s.RootModule().Resources { 242 if rs.Type != "aws_opsworks_custom_layer" { 243 continue 244 } 245 req := &opsworks.DescribeLayersInput{ 246 LayerIds: []*string{ 247 aws.String(rs.Primary.ID), 248 }, 249 } 250 251 _, err := opsworksconn.DescribeLayers(req) 252 if err != nil { 253 if awserr, ok := err.(awserr.Error); ok { 254 if awserr.Code() == "ResourceNotFoundException" { 255 // not found, good to go 256 return nil 257 } 258 } 259 return err 260 } 261 } 262 263 return fmt.Errorf("Fall through error on OpsWorks custom layer test") 264 } 265 266 func testAccAwsOpsworksCustomLayerSecurityGroups(name string) string { 267 return fmt.Sprintf(` 268 resource "aws_security_group" "tf-ops-acc-layer1" { 269 name = "%s-layer1" 270 ingress { 271 from_port = 8 272 to_port = -1 273 protocol = "icmp" 274 cidr_blocks = ["0.0.0.0/0"] 275 } 276 } 277 resource "aws_security_group" "tf-ops-acc-layer2" { 278 name = "%s-layer2" 279 ingress { 280 from_port = 8 281 to_port = -1 282 protocol = "icmp" 283 cidr_blocks = ["0.0.0.0/0"] 284 } 285 }`, name, name) 286 } 287 288 func testAccAwsOpsworksCustomLayerConfigNoVpcCreate(name string) string { 289 return fmt.Sprintf(` 290 provider "aws" { 291 region = "us-east-1" 292 } 293 294 resource "aws_opsworks_custom_layer" "tf-acc" { 295 stack_id = "${aws_opsworks_stack.tf-acc.id}" 296 name = "%s" 297 short_name = "tf-ops-acc-custom-layer" 298 auto_assign_public_ips = true 299 custom_security_group_ids = [ 300 "${aws_security_group.tf-ops-acc-layer1.id}", 301 "${aws_security_group.tf-ops-acc-layer2.id}", 302 ] 303 drain_elb_on_shutdown = true 304 instance_shutdown_timeout = 300 305 system_packages = [ 306 "git", 307 "golang", 308 ] 309 ebs_volume { 310 type = "gp2" 311 number_of_disks = 2 312 mount_point = "/home" 313 size = 100 314 raid_level = 0 315 } 316 } 317 318 %s 319 320 %s 321 322 `, name, testAccAwsOpsworksStackConfigNoVpcCreate(name), testAccAwsOpsworksCustomLayerSecurityGroups(name)) 323 } 324 325 func testAccAwsOpsworksCustomLayerConfigVpcCreate(name string) string { 326 return fmt.Sprintf(` 327 provider "aws" { 328 region = "us-west-2" 329 } 330 331 resource "aws_opsworks_custom_layer" "tf-acc" { 332 stack_id = "${aws_opsworks_stack.tf-acc.id}" 333 name = "%s" 334 short_name = "tf-ops-acc-custom-layer" 335 auto_assign_public_ips = false 336 custom_security_group_ids = [ 337 "${aws_security_group.tf-ops-acc-layer1.id}", 338 "${aws_security_group.tf-ops-acc-layer2.id}", 339 ] 340 drain_elb_on_shutdown = true 341 instance_shutdown_timeout = 300 342 system_packages = [ 343 "git", 344 "golang", 345 ] 346 ebs_volume { 347 type = "gp2" 348 number_of_disks = 2 349 mount_point = "/home" 350 size = 100 351 raid_level = 0 352 } 353 } 354 355 %s 356 357 %s 358 359 `, name, testAccAwsOpsworksStackConfigVpcCreate(name), testAccAwsOpsworksCustomLayerSecurityGroups(name)) 360 } 361 362 func testAccAwsOpsworksCustomLayerConfigUpdate(name string) string { 363 return fmt.Sprintf(` 364 provider "aws" { 365 region = "us-east-1" 366 } 367 368 resource "aws_security_group" "tf-ops-acc-layer3" { 369 name = "tf-ops-acc-layer3" 370 ingress { 371 from_port = 8 372 to_port = -1 373 protocol = "icmp" 374 cidr_blocks = ["0.0.0.0/0"] 375 } 376 } 377 resource "aws_opsworks_custom_layer" "tf-acc" { 378 stack_id = "${aws_opsworks_stack.tf-acc.id}" 379 name = "%s" 380 short_name = "tf-ops-acc-custom-layer" 381 auto_assign_public_ips = true 382 custom_security_group_ids = [ 383 "${aws_security_group.tf-ops-acc-layer1.id}", 384 "${aws_security_group.tf-ops-acc-layer2.id}", 385 "${aws_security_group.tf-ops-acc-layer3.id}", 386 ] 387 drain_elb_on_shutdown = false 388 instance_shutdown_timeout = 120 389 system_packages = [ 390 "git", 391 "golang", 392 "subversion", 393 ] 394 ebs_volume { 395 type = "gp2" 396 number_of_disks = 2 397 mount_point = "/home" 398 size = 100 399 raid_level = 0 400 } 401 ebs_volume { 402 type = "io1" 403 number_of_disks = 4 404 mount_point = "/var" 405 size = 100 406 raid_level = 1 407 iops = 3000 408 } 409 custom_json = "{\"layer_key\": \"layer_value2\"}" 410 } 411 412 %s 413 414 %s 415 416 `, name, testAccAwsOpsworksStackConfigNoVpcCreate(name), testAccAwsOpsworksCustomLayerSecurityGroups(name)) 417 }