github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_opsworks_custom_layer_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 // These tests assume the existence of predefined Opsworks IAM roles named `aws-opsworks-ec2-role` 16 // and `aws-opsworks-service-role`. 17 18 func TestAccAWSOpsworksCustomLayer(t *testing.T) { 19 stackName := fmt.Sprintf("tf-%d", acctest.RandInt()) 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckAwsOpsworksCustomLayerDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: testAccAwsOpsworksCustomLayerConfigCreate(stackName), 27 Check: resource.ComposeTestCheckFunc( 28 resource.TestCheckResourceAttr( 29 "aws_opsworks_custom_layer.tf-acc", "name", stackName, 30 ), 31 resource.TestCheckResourceAttr( 32 "aws_opsworks_custom_layer.tf-acc", "auto_assign_elastic_ips", "false", 33 ), 34 resource.TestCheckResourceAttr( 35 "aws_opsworks_custom_layer.tf-acc", "auto_healing", "true", 36 ), 37 resource.TestCheckResourceAttr( 38 "aws_opsworks_custom_layer.tf-acc", "drain_elb_on_shutdown", "true", 39 ), 40 resource.TestCheckResourceAttr( 41 "aws_opsworks_custom_layer.tf-acc", "instance_shutdown_timeout", "300", 42 ), 43 resource.TestCheckResourceAttr( 44 "aws_opsworks_custom_layer.tf-acc", "custom_security_group_ids.#", "2", 45 ), 46 resource.TestCheckResourceAttr( 47 "aws_opsworks_custom_layer.tf-acc", "system_packages.#", "2", 48 ), 49 resource.TestCheckResourceAttr( 50 "aws_opsworks_custom_layer.tf-acc", "system_packages.1368285564", "git", 51 ), 52 resource.TestCheckResourceAttr( 53 "aws_opsworks_custom_layer.tf-acc", "system_packages.2937857443", "golang", 54 ), 55 resource.TestCheckResourceAttr( 56 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.#", "1", 57 ), 58 resource.TestCheckResourceAttr( 59 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.type", "gp2", 60 ), 61 resource.TestCheckResourceAttr( 62 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.number_of_disks", "2", 63 ), 64 resource.TestCheckResourceAttr( 65 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.mount_point", "/home", 66 ), 67 resource.TestCheckResourceAttr( 68 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.size", "100", 69 ), 70 ), 71 }, 72 resource.TestStep{ 73 Config: testAccAwsOpsworksCustomLayerConfigUpdate(stackName), 74 Check: resource.ComposeTestCheckFunc( 75 resource.TestCheckResourceAttr( 76 "aws_opsworks_custom_layer.tf-acc", "name", stackName, 77 ), 78 resource.TestCheckResourceAttr( 79 "aws_opsworks_custom_layer.tf-acc", "drain_elb_on_shutdown", "false", 80 ), 81 resource.TestCheckResourceAttr( 82 "aws_opsworks_custom_layer.tf-acc", "instance_shutdown_timeout", "120", 83 ), 84 resource.TestCheckResourceAttr( 85 "aws_opsworks_custom_layer.tf-acc", "custom_security_group_ids.#", "3", 86 ), 87 resource.TestCheckResourceAttr( 88 "aws_opsworks_custom_layer.tf-acc", "system_packages.#", "3", 89 ), 90 resource.TestCheckResourceAttr( 91 "aws_opsworks_custom_layer.tf-acc", "system_packages.1368285564", "git", 92 ), 93 resource.TestCheckResourceAttr( 94 "aws_opsworks_custom_layer.tf-acc", "system_packages.2937857443", "golang", 95 ), 96 resource.TestCheckResourceAttr( 97 "aws_opsworks_custom_layer.tf-acc", "system_packages.4101929740", "subversion", 98 ), 99 resource.TestCheckResourceAttr( 100 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.#", "2", 101 ), 102 resource.TestCheckResourceAttr( 103 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.type", "gp2", 104 ), 105 resource.TestCheckResourceAttr( 106 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.number_of_disks", "2", 107 ), 108 resource.TestCheckResourceAttr( 109 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.mount_point", "/home", 110 ), 111 resource.TestCheckResourceAttr( 112 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.size", "100", 113 ), 114 resource.TestCheckResourceAttr( 115 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.type", "io1", 116 ), 117 resource.TestCheckResourceAttr( 118 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.number_of_disks", "4", 119 ), 120 resource.TestCheckResourceAttr( 121 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.mount_point", "/var", 122 ), 123 resource.TestCheckResourceAttr( 124 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.size", "100", 125 ), 126 resource.TestCheckResourceAttr( 127 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.raid_level", "1", 128 ), 129 resource.TestCheckResourceAttr( 130 "aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.iops", "3000", 131 ), 132 resource.TestCheckResourceAttr( 133 "aws_opsworks_custom_layer.tf-acc", "custom_json", `{"layer_key":"layer_value2"}`, 134 ), 135 ), 136 }, 137 }, 138 }) 139 } 140 141 func testAccCheckAwsOpsworksCustomLayerDestroy(s *terraform.State) error { 142 opsworksconn := testAccProvider.Meta().(*AWSClient).opsworksconn 143 for _, rs := range s.RootModule().Resources { 144 if rs.Type != "aws_opsworks_custom_layer" { 145 continue 146 } 147 req := &opsworks.DescribeLayersInput{ 148 LayerIds: []*string{ 149 aws.String(rs.Primary.ID), 150 }, 151 } 152 153 _, err := opsworksconn.DescribeLayers(req) 154 if err != nil { 155 if awserr, ok := err.(awserr.Error); ok { 156 if awserr.Code() == "ResourceNotFoundException" { 157 // not found, good to go 158 return nil 159 } 160 } 161 return err 162 } 163 } 164 165 return fmt.Errorf("Fall through error on OpsWorks custom layer test") 166 } 167 168 func testAccAwsOpsworksCustomLayerSecurityGroups(name string) string { 169 return fmt.Sprintf(` 170 resource "aws_security_group" "tf-ops-acc-layer1" { 171 name = "%s-layer1" 172 ingress { 173 from_port = 8 174 to_port = -1 175 protocol = "icmp" 176 cidr_blocks = ["0.0.0.0/0"] 177 } 178 } 179 resource "aws_security_group" "tf-ops-acc-layer2" { 180 name = "%s-layer2" 181 ingress { 182 from_port = 8 183 to_port = -1 184 protocol = "icmp" 185 cidr_blocks = ["0.0.0.0/0"] 186 } 187 }`, name, name) 188 } 189 190 func testAccAwsOpsworksCustomLayerConfigCreate(name string) string { 191 return fmt.Sprintf(` 192 provider "aws" { 193 region = "us-east-1" 194 } 195 196 resource "aws_opsworks_custom_layer" "tf-acc" { 197 stack_id = "${aws_opsworks_stack.tf-acc.id}" 198 name = "%s" 199 short_name = "tf-ops-acc-custom-layer" 200 auto_assign_public_ips = true 201 custom_security_group_ids = [ 202 "${aws_security_group.tf-ops-acc-layer1.id}", 203 "${aws_security_group.tf-ops-acc-layer2.id}", 204 ] 205 drain_elb_on_shutdown = true 206 instance_shutdown_timeout = 300 207 system_packages = [ 208 "git", 209 "golang", 210 ] 211 ebs_volume { 212 type = "gp2" 213 number_of_disks = 2 214 mount_point = "/home" 215 size = 100 216 raid_level = 0 217 } 218 } 219 220 %s 221 222 %s 223 224 `, name, testAccAwsOpsworksStackConfigNoVpcCreate(name), testAccAwsOpsworksCustomLayerSecurityGroups(name)) 225 } 226 227 func testAccAwsOpsworksCustomLayerConfigUpdate(name string) string { 228 return fmt.Sprintf(` 229 provider "aws" { 230 region = "us-east-1" 231 } 232 233 resource "aws_security_group" "tf-ops-acc-layer3" { 234 name = "tf-ops-acc-layer3" 235 ingress { 236 from_port = 8 237 to_port = -1 238 protocol = "icmp" 239 cidr_blocks = ["0.0.0.0/0"] 240 } 241 } 242 resource "aws_opsworks_custom_layer" "tf-acc" { 243 stack_id = "${aws_opsworks_stack.tf-acc.id}" 244 name = "%s" 245 short_name = "tf-ops-acc-custom-layer" 246 auto_assign_public_ips = true 247 custom_security_group_ids = [ 248 "${aws_security_group.tf-ops-acc-layer1.id}", 249 "${aws_security_group.tf-ops-acc-layer2.id}", 250 "${aws_security_group.tf-ops-acc-layer3.id}", 251 ] 252 drain_elb_on_shutdown = false 253 instance_shutdown_timeout = 120 254 system_packages = [ 255 "git", 256 "golang", 257 "subversion", 258 ] 259 ebs_volume { 260 type = "gp2" 261 number_of_disks = 2 262 mount_point = "/home" 263 size = 100 264 raid_level = 0 265 } 266 ebs_volume { 267 type = "io1" 268 number_of_disks = 4 269 mount_point = "/var" 270 size = 100 271 raid_level = 1 272 iops = 3000 273 } 274 custom_json = "{\"layer_key\": \"layer_value2\"}" 275 } 276 277 %s 278 279 %s 280 281 `, name, testAccAwsOpsworksStackConfigNoVpcCreate(name), testAccAwsOpsworksCustomLayerSecurityGroups(name)) 282 }