github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 { 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 { 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 { 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 resource "aws_opsworks_custom_layer" "tf-acc" { 291 stack_id = "${aws_opsworks_stack.tf-acc.id}" 292 name = "%s" 293 short_name = "tf-ops-acc-custom-layer" 294 auto_assign_public_ips = true 295 custom_security_group_ids = [ 296 "${aws_security_group.tf-ops-acc-layer1.id}", 297 "${aws_security_group.tf-ops-acc-layer2.id}", 298 ] 299 drain_elb_on_shutdown = true 300 instance_shutdown_timeout = 300 301 system_packages = [ 302 "git", 303 "golang", 304 ] 305 ebs_volume { 306 type = "gp2" 307 number_of_disks = 2 308 mount_point = "/home" 309 size = 100 310 raid_level = 0 311 } 312 } 313 314 %s 315 316 %s 317 318 `, name, testAccAwsOpsworksStackConfigNoVpcCreate(name), testAccAwsOpsworksCustomLayerSecurityGroups(name)) 319 } 320 321 func testAccAwsOpsworksCustomLayerConfigVpcCreate(name string) string { 322 return fmt.Sprintf(` 323 provider "aws" { 324 region = "us-west-2" 325 } 326 327 resource "aws_opsworks_custom_layer" "tf-acc" { 328 stack_id = "${aws_opsworks_stack.tf-acc.id}" 329 name = "%s" 330 short_name = "tf-ops-acc-custom-layer" 331 auto_assign_public_ips = false 332 custom_security_group_ids = [ 333 "${aws_security_group.tf-ops-acc-layer1.id}", 334 "${aws_security_group.tf-ops-acc-layer2.id}", 335 ] 336 drain_elb_on_shutdown = true 337 instance_shutdown_timeout = 300 338 system_packages = [ 339 "git", 340 "golang", 341 ] 342 ebs_volume { 343 type = "gp2" 344 number_of_disks = 2 345 mount_point = "/home" 346 size = 100 347 raid_level = 0 348 } 349 } 350 351 %s 352 353 %s 354 355 `, name, testAccAwsOpsworksStackConfigVpcCreate(name), testAccAwsOpsworksCustomLayerSecurityGroups(name)) 356 } 357 358 func testAccAwsOpsworksCustomLayerConfigUpdate(name string) string { 359 return fmt.Sprintf(` 360 resource "aws_security_group" "tf-ops-acc-layer3" { 361 name = "tf-ops-acc-layer3" 362 ingress { 363 from_port = 8 364 to_port = -1 365 protocol = "icmp" 366 cidr_blocks = ["0.0.0.0/0"] 367 } 368 } 369 resource "aws_opsworks_custom_layer" "tf-acc" { 370 stack_id = "${aws_opsworks_stack.tf-acc.id}" 371 name = "%s" 372 short_name = "tf-ops-acc-custom-layer" 373 auto_assign_public_ips = true 374 custom_security_group_ids = [ 375 "${aws_security_group.tf-ops-acc-layer1.id}", 376 "${aws_security_group.tf-ops-acc-layer2.id}", 377 "${aws_security_group.tf-ops-acc-layer3.id}", 378 ] 379 drain_elb_on_shutdown = false 380 instance_shutdown_timeout = 120 381 system_packages = [ 382 "git", 383 "golang", 384 "subversion", 385 ] 386 ebs_volume { 387 type = "gp2" 388 number_of_disks = 2 389 mount_point = "/home" 390 size = 100 391 raid_level = 0 392 } 393 ebs_volume { 394 type = "io1" 395 number_of_disks = 4 396 mount_point = "/var" 397 size = 100 398 raid_level = 1 399 iops = 3000 400 } 401 custom_json = "{\"layer_key\": \"layer_value2\"}" 402 } 403 404 %s 405 406 %s 407 408 `, name, testAccAwsOpsworksStackConfigNoVpcCreate(name), testAccAwsOpsworksCustomLayerSecurityGroups(name)) 409 }