github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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(t *testing.T) { 16 stackName := fmt.Sprintf("tf-%d", acctest.RandInt()) 17 var opsinst opsworks.Instance 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckAwsOpsworksInstanceDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccAwsOpsworksInstanceConfigCreate(stackName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckAWSOpsworksInstanceExists( 27 "aws_opsworks_instance.tf-acc", &opsinst), 28 testAccCheckAWSOpsworksInstanceAttributes(&opsinst), 29 resource.TestCheckResourceAttr( 30 "aws_opsworks_instance.tf-acc", "hostname", "tf-acc1", 31 ), 32 resource.TestCheckResourceAttr( 33 "aws_opsworks_instance.tf-acc", "instance_type", "t2.micro", 34 ), 35 resource.TestCheckResourceAttr( 36 "aws_opsworks_instance.tf-acc", "state", "stopped", 37 ), 38 resource.TestCheckResourceAttr( 39 "aws_opsworks_instance.tf-acc", "layer_ids.#", "1", 40 ), 41 resource.TestCheckResourceAttr( 42 "aws_opsworks_instance.tf-acc", "install_updates_on_boot", "true", 43 ), 44 resource.TestCheckResourceAttr( 45 "aws_opsworks_instance.tf-acc", "architecture", "x86_64", 46 ), 47 resource.TestCheckResourceAttr( 48 "aws_opsworks_instance.tf-acc", "tenancy", "default", 49 ), 50 resource.TestCheckResourceAttr( 51 "aws_opsworks_instance.tf-acc", "os", "Amazon Linux 2014.09", // inherited from opsworks_stack_test 52 ), 53 resource.TestCheckResourceAttr( 54 "aws_opsworks_instance.tf-acc", "root_device_type", "ebs", // inherited from opsworks_stack_test 55 ), 56 resource.TestCheckResourceAttr( 57 "aws_opsworks_instance.tf-acc", "availability_zone", "us-west-2a", // inherited from opsworks_stack_test 58 ), 59 ), 60 }, 61 resource.TestStep{ 62 Config: testAccAwsOpsworksInstanceConfigUpdate(stackName), 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckAWSOpsworksInstanceExists( 65 "aws_opsworks_instance.tf-acc", &opsinst), 66 testAccCheckAWSOpsworksInstanceAttributes(&opsinst), 67 resource.TestCheckResourceAttr( 68 "aws_opsworks_instance.tf-acc", "hostname", "tf-acc1", 69 ), 70 resource.TestCheckResourceAttr( 71 "aws_opsworks_instance.tf-acc", "instance_type", "t2.small", 72 ), 73 resource.TestCheckResourceAttr( 74 "aws_opsworks_instance.tf-acc", "layer_ids.#", "2", 75 ), 76 resource.TestCheckResourceAttr( 77 "aws_opsworks_instance.tf-acc", "os", "Amazon Linux 2015.09", 78 ), 79 resource.TestCheckResourceAttr( 80 "aws_opsworks_instance.tf-acc", "tenancy", "default", 81 ), 82 ), 83 }, 84 }, 85 }) 86 } 87 88 func testAccCheckAWSOpsworksInstanceExists( 89 n string, opsinst *opsworks.Instance) resource.TestCheckFunc { 90 return func(s *terraform.State) error { 91 rs, ok := s.RootModule().Resources[n] 92 if !ok { 93 return fmt.Errorf("Not found: %s", n) 94 } 95 96 if rs.Primary.ID == "" { 97 return fmt.Errorf("No Opsworks Instance is set") 98 } 99 100 conn := testAccProvider.Meta().(*AWSClient).opsworksconn 101 102 params := &opsworks.DescribeInstancesInput{ 103 InstanceIds: []*string{&rs.Primary.ID}, 104 } 105 resp, err := conn.DescribeInstances(params) 106 107 if err != nil { 108 return err 109 } 110 111 if v := len(resp.Instances); v != 1 { 112 return fmt.Errorf("Expected 1 request returned, got %d", v) 113 } 114 115 *opsinst = *resp.Instances[0] 116 117 return nil 118 } 119 } 120 121 func testAccCheckAWSOpsworksInstanceAttributes( 122 opsinst *opsworks.Instance) resource.TestCheckFunc { 123 return func(s *terraform.State) error { 124 // Depending on the timing, the state could be requested or stopped 125 if *opsinst.Status != "stopped" && *opsinst.Status != "requested" { 126 return fmt.Errorf("Unexpected request status: %s", *opsinst.Status) 127 } 128 if *opsinst.AvailabilityZone != "us-west-2a" { 129 return fmt.Errorf("Unexpected availability zone: %s", *opsinst.AvailabilityZone) 130 } 131 if *opsinst.Architecture != "x86_64" { 132 return fmt.Errorf("Unexpected architecture: %s", *opsinst.Architecture) 133 } 134 if *opsinst.Tenancy != "default" { 135 return fmt.Errorf("Unexpected tenancy: %s", *opsinst.Tenancy) 136 } 137 if *opsinst.InfrastructureClass != "ec2" { 138 return fmt.Errorf("Unexpected infrastructure class: %s", *opsinst.InfrastructureClass) 139 } 140 if *opsinst.RootDeviceType != "ebs" { 141 return fmt.Errorf("Unexpected root device type: %s", *opsinst.RootDeviceType) 142 } 143 if *opsinst.VirtualizationType != "hvm" { 144 return fmt.Errorf("Unexpected virtualization type: %s", *opsinst.VirtualizationType) 145 } 146 return nil 147 } 148 } 149 150 func testAccCheckAwsOpsworksInstanceDestroy(s *terraform.State) error { 151 opsworksconn := testAccProvider.Meta().(*AWSClient).opsworksconn 152 for _, rs := range s.RootModule().Resources { 153 if rs.Type != "aws_opsworks_instance" { 154 continue 155 } 156 req := &opsworks.DescribeInstancesInput{ 157 InstanceIds: []*string{ 158 aws.String(rs.Primary.ID), 159 }, 160 } 161 162 _, err := opsworksconn.DescribeInstances(req) 163 if err != nil { 164 if awserr, ok := err.(awserr.Error); ok { 165 if awserr.Code() == "ResourceNotFoundException" { 166 // not found, good to go 167 return nil 168 } 169 } 170 return err 171 } 172 } 173 174 return fmt.Errorf("Fall through error on OpsWorks instance test") 175 } 176 177 func testAccAwsOpsworksInstanceConfigCreate(name string) string { 178 return fmt.Sprintf(` 179 resource "aws_security_group" "tf-ops-acc-web" { 180 name = "%s-web" 181 ingress { 182 from_port = 80 183 to_port = 80 184 protocol = "tcp" 185 cidr_blocks = ["0.0.0.0/0"] 186 } 187 } 188 189 resource "aws_security_group" "tf-ops-acc-php" { 190 name = "%s-php" 191 ingress { 192 from_port = 8080 193 to_port = 8080 194 protocol = "tcp" 195 cidr_blocks = ["0.0.0.0/0"] 196 } 197 } 198 199 resource "aws_opsworks_static_web_layer" "tf-acc" { 200 stack_id = "${aws_opsworks_stack.tf-acc.id}" 201 202 custom_security_group_ids = [ 203 "${aws_security_group.tf-ops-acc-web.id}", 204 ] 205 } 206 207 resource "aws_opsworks_php_app_layer" "tf-acc" { 208 stack_id = "${aws_opsworks_stack.tf-acc.id}" 209 210 custom_security_group_ids = [ 211 "${aws_security_group.tf-ops-acc-php.id}", 212 ] 213 } 214 215 resource "aws_opsworks_instance" "tf-acc" { 216 stack_id = "${aws_opsworks_stack.tf-acc.id}" 217 layer_ids = [ 218 "${aws_opsworks_static_web_layer.tf-acc.id}", 219 ] 220 instance_type = "t2.micro" 221 state = "stopped" 222 hostname = "tf-acc1" 223 } 224 225 %s 226 227 `, name, name, testAccAwsOpsworksStackConfigVpcCreate(name)) 228 } 229 230 func testAccAwsOpsworksInstanceConfigUpdate(name string) string { 231 return fmt.Sprintf(` 232 resource "aws_security_group" "tf-ops-acc-web" { 233 name = "%s-web" 234 ingress { 235 from_port = 80 236 to_port = 80 237 protocol = "tcp" 238 cidr_blocks = ["0.0.0.0/0"] 239 } 240 } 241 242 resource "aws_security_group" "tf-ops-acc-php" { 243 name = "%s-php" 244 ingress { 245 from_port = 8080 246 to_port = 8080 247 protocol = "tcp" 248 cidr_blocks = ["0.0.0.0/0"] 249 } 250 } 251 252 resource "aws_opsworks_static_web_layer" "tf-acc" { 253 stack_id = "${aws_opsworks_stack.tf-acc.id}" 254 255 custom_security_group_ids = [ 256 "${aws_security_group.tf-ops-acc-web.id}", 257 ] 258 } 259 260 resource "aws_opsworks_php_app_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-php.id}", 265 ] 266 } 267 268 resource "aws_opsworks_instance" "tf-acc" { 269 stack_id = "${aws_opsworks_stack.tf-acc.id}" 270 layer_ids = [ 271 "${aws_opsworks_static_web_layer.tf-acc.id}", 272 "${aws_opsworks_php_app_layer.tf-acc.id}", 273 ] 274 instance_type = "t2.small" 275 state = "stopped" 276 hostname = "tf-acc1" 277 os = "Amazon Linux 2015.09" 278 } 279 280 %s 281 282 `, name, name, testAccAwsOpsworksStackConfigVpcCreate(name)) 283 }