github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/builtin/providers/aws/resource_aws_elastic_beanstalk_environment_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "regexp" 7 "testing" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/aws/awserr" 11 "github.com/aws/aws-sdk-go/service/elasticbeanstalk" 12 "github.com/hashicorp/terraform/helper/acctest" 13 "github.com/hashicorp/terraform/helper/resource" 14 "github.com/hashicorp/terraform/terraform" 15 ) 16 17 func TestAccAWSBeanstalkEnv_basic(t *testing.T) { 18 var app elasticbeanstalk.EnvironmentDescription 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckBeanstalkEnvDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: testAccBeanstalkEnvConfig, 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func TestAccAWSBeanstalkEnv_tier(t *testing.T) { 36 var app elasticbeanstalk.EnvironmentDescription 37 beanstalkQueuesNameRegexp := regexp.MustCompile("https://sqs.+?awseb[^,]+") 38 39 resource.Test(t, resource.TestCase{ 40 PreCheck: func() { testAccPreCheck(t) }, 41 Providers: testAccProviders, 42 CheckDestroy: testAccCheckBeanstalkEnvDestroy, 43 Steps: []resource.TestStep{ 44 resource.TestStep{ 45 Config: testAccBeanstalkWorkerEnvConfig, 46 Check: resource.ComposeTestCheckFunc( 47 testAccCheckBeanstalkEnvTier("aws_elastic_beanstalk_environment.tfenvtest", &app), 48 resource.TestMatchResourceAttr( 49 "aws_elastic_beanstalk_environment.tfenvtest", "queues.0", beanstalkQueuesNameRegexp), 50 ), 51 }, 52 }, 53 }) 54 } 55 56 func TestAccAWSBeanstalkEnv_outputs(t *testing.T) { 57 var app elasticbeanstalk.EnvironmentDescription 58 beanstalkAsgNameRegexp := regexp.MustCompile("awseb.+?AutoScalingGroup[^,]+") 59 beanstalkElbNameRegexp := regexp.MustCompile("awseb.+?EBLoa[^,]+") 60 beanstalkInstancesNameRegexp := regexp.MustCompile("i-([0-9a-fA-F]{8}|[0-9a-fA-F]{17})") 61 beanstalkLcNameRegexp := regexp.MustCompile("awseb.+?AutoScalingLaunch[^,]+") 62 63 resource.Test(t, resource.TestCase{ 64 PreCheck: func() { testAccPreCheck(t) }, 65 Providers: testAccProviders, 66 CheckDestroy: testAccCheckBeanstalkEnvDestroy, 67 Steps: []resource.TestStep{ 68 resource.TestStep{ 69 Config: testAccBeanstalkEnvConfig, 70 Check: resource.ComposeTestCheckFunc( 71 testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app), 72 resource.TestMatchResourceAttr( 73 "aws_elastic_beanstalk_environment.tfenvtest", "autoscaling_groups.0", beanstalkAsgNameRegexp), 74 resource.TestMatchResourceAttr( 75 "aws_elastic_beanstalk_environment.tfenvtest", "load_balancers.0", beanstalkElbNameRegexp), 76 resource.TestMatchResourceAttr( 77 "aws_elastic_beanstalk_environment.tfenvtest", "instances.0", beanstalkInstancesNameRegexp), 78 resource.TestMatchResourceAttr( 79 "aws_elastic_beanstalk_environment.tfenvtest", "launch_configurations.0", beanstalkLcNameRegexp), 80 ), 81 }, 82 }, 83 }) 84 } 85 86 func TestAccAWSBeanstalkEnv_cname_prefix(t *testing.T) { 87 var app elasticbeanstalk.EnvironmentDescription 88 cnamePrefix := acctest.RandString(8) 89 beanstalkCnameRegexp := regexp.MustCompile("^" + cnamePrefix + ".+?elasticbeanstalk.com$") 90 91 resource.Test(t, resource.TestCase{ 92 PreCheck: func() { testAccPreCheck(t) }, 93 Providers: testAccProviders, 94 CheckDestroy: testAccCheckBeanstalkEnvDestroy, 95 Steps: []resource.TestStep{ 96 resource.TestStep{ 97 Config: testAccBeanstalkEnvCnamePrefixConfig(cnamePrefix), 98 Check: resource.ComposeTestCheckFunc( 99 testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app), 100 resource.TestMatchResourceAttr( 101 "aws_elastic_beanstalk_environment.tfenvtest", "cname", beanstalkCnameRegexp), 102 ), 103 }, 104 }, 105 }) 106 } 107 108 func TestAccAWSBeanstalkEnv_config(t *testing.T) { 109 var app elasticbeanstalk.EnvironmentDescription 110 111 resource.Test(t, resource.TestCase{ 112 PreCheck: func() { testAccPreCheck(t) }, 113 Providers: testAccProviders, 114 CheckDestroy: testAccCheckBeanstalkEnvDestroy, 115 Steps: []resource.TestStep{ 116 resource.TestStep{ 117 Config: testAccBeanstalkConfigTemplate, 118 Check: resource.ComposeTestCheckFunc( 119 testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tftest", &app), 120 testAccCheckBeanstalkEnvConfigValue("aws_elastic_beanstalk_environment.tftest", "1"), 121 ), 122 }, 123 124 resource.TestStep{ 125 Config: testAccBeanstalkConfigTemplateUpdate, 126 Check: resource.ComposeTestCheckFunc( 127 testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tftest", &app), 128 testAccCheckBeanstalkEnvConfigValue("aws_elastic_beanstalk_environment.tftest", "2"), 129 ), 130 }, 131 132 resource.TestStep{ 133 Config: testAccBeanstalkConfigTemplateUpdate, 134 Check: resource.ComposeTestCheckFunc( 135 testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tftest", &app), 136 testAccCheckBeanstalkEnvConfigValue("aws_elastic_beanstalk_environment.tftest", "3"), 137 ), 138 }, 139 }, 140 }) 141 } 142 143 func testAccCheckBeanstalkEnvDestroy(s *terraform.State) error { 144 conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn 145 146 for _, rs := range s.RootModule().Resources { 147 if rs.Type != "aws_elastic_beanstalk_environment" { 148 continue 149 } 150 151 // Try to find the environment 152 describeBeanstalkEnvOpts := &elasticbeanstalk.DescribeEnvironmentsInput{ 153 EnvironmentIds: []*string{aws.String(rs.Primary.ID)}, 154 } 155 resp, err := conn.DescribeEnvironments(describeBeanstalkEnvOpts) 156 if err == nil { 157 switch { 158 case len(resp.Environments) > 1: 159 return fmt.Errorf("Error %d environments match, expected 1", len(resp.Environments)) 160 case len(resp.Environments) == 1: 161 if *resp.Environments[0].Status == "Terminated" { 162 return nil 163 } 164 return fmt.Errorf("Elastic Beanstalk ENV still exists.") 165 default: 166 return nil 167 } 168 } 169 170 // Verify the error is what we want 171 ec2err, ok := err.(awserr.Error) 172 if !ok { 173 return err 174 } 175 if ec2err.Code() != "InvalidBeanstalkEnvID.NotFound" { 176 return err 177 } 178 } 179 180 return nil 181 } 182 183 func testAccCheckBeanstalkEnvExists(n string, app *elasticbeanstalk.EnvironmentDescription) resource.TestCheckFunc { 184 return func(s *terraform.State) error { 185 rs, ok := s.RootModule().Resources[n] 186 if !ok { 187 return fmt.Errorf("Not found: %s", n) 188 } 189 190 if rs.Primary.ID == "" { 191 return fmt.Errorf("Elastic Beanstalk ENV is not set") 192 } 193 194 env, err := describeBeanstalkEnv(testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn, aws.String(rs.Primary.ID)) 195 if err != nil { 196 return err 197 } 198 199 *app = *env 200 201 return nil 202 } 203 } 204 205 func testAccCheckBeanstalkEnvTier(n string, app *elasticbeanstalk.EnvironmentDescription) resource.TestCheckFunc { 206 return func(s *terraform.State) error { 207 rs, ok := s.RootModule().Resources[n] 208 if !ok { 209 return fmt.Errorf("Not found: %s", n) 210 } 211 212 if rs.Primary.ID == "" { 213 return fmt.Errorf("Elastic Beanstalk ENV is not set") 214 } 215 216 env, err := describeBeanstalkEnv(testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn, aws.String(rs.Primary.ID)) 217 if err != nil { 218 return err 219 } 220 if *env.Tier.Name != "Worker" { 221 return fmt.Errorf("Beanstalk Environment tier is %s, expected Worker", *env.Tier.Name) 222 } 223 224 *app = *env 225 226 return nil 227 } 228 } 229 230 func testAccCheckBeanstalkEnvConfigValue(n string, expectedValue string) resource.TestCheckFunc { 231 return func(s *terraform.State) error { 232 conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn 233 234 rs, ok := s.RootModule().Resources[n] 235 if !ok { 236 return fmt.Errorf("Not found: %s", n) 237 } 238 239 if rs.Primary.ID == "" { 240 return fmt.Errorf("Elastic Beanstalk ENV is not set") 241 } 242 243 resp, err := conn.DescribeConfigurationOptions(&elasticbeanstalk.DescribeConfigurationOptionsInput{ 244 ApplicationName: aws.String(rs.Primary.Attributes["application"]), 245 EnvironmentName: aws.String(rs.Primary.Attributes["name"]), 246 Options: []*elasticbeanstalk.OptionSpecification{ 247 { 248 Namespace: aws.String("aws:elasticbeanstalk:application:environment"), 249 OptionName: aws.String("TEMPLATE"), 250 }, 251 }, 252 }) 253 if err != nil { 254 return err 255 } 256 257 if len(resp.Options) != 1 { 258 return fmt.Errorf("Found %d options, expected 1.", len(resp.Options)) 259 } 260 261 log.Printf("[DEBUG] %d Elastic Beanstalk Option values returned.", len(resp.Options[0].ValueOptions)) 262 263 for _, value := range resp.Options[0].ValueOptions { 264 if *value != expectedValue { 265 return fmt.Errorf("Option setting value: %s. Expected %s", *value, expectedValue) 266 } 267 } 268 269 return nil 270 } 271 } 272 273 func describeBeanstalkEnv(conn *elasticbeanstalk.ElasticBeanstalk, 274 envID *string) (*elasticbeanstalk.EnvironmentDescription, error) { 275 describeBeanstalkEnvOpts := &elasticbeanstalk.DescribeEnvironmentsInput{ 276 EnvironmentIds: []*string{envID}, 277 } 278 279 log.Printf("[DEBUG] Elastic Beanstalk Environment TEST describe opts: %s", describeBeanstalkEnvOpts) 280 281 resp, err := conn.DescribeEnvironments(describeBeanstalkEnvOpts) 282 if err != nil { 283 return &elasticbeanstalk.EnvironmentDescription{}, err 284 } 285 if len(resp.Environments) == 0 { 286 return &elasticbeanstalk.EnvironmentDescription{}, fmt.Errorf("Elastic Beanstalk ENV not found.") 287 } 288 if len(resp.Environments) > 1 { 289 return &elasticbeanstalk.EnvironmentDescription{}, fmt.Errorf("Found %d environments, expected 1.", len(resp.Environments)) 290 } 291 return resp.Environments[0], nil 292 } 293 294 const testAccBeanstalkEnvConfig = ` 295 resource "aws_elastic_beanstalk_application" "tftest" { 296 name = "tf-test-name" 297 description = "tf-test-desc" 298 } 299 300 resource "aws_elastic_beanstalk_environment" "tfenvtest" { 301 name = "tf-test-name" 302 application = "${aws_elastic_beanstalk_application.tftest.name}" 303 solution_stack_name = "64bit Amazon Linux running Python" 304 } 305 ` 306 307 const testAccBeanstalkWorkerEnvConfig = ` 308 resource "aws_elastic_beanstalk_application" "tftest" { 309 name = "tf-test-name" 310 description = "tf-test-desc" 311 } 312 313 resource "aws_elastic_beanstalk_environment" "tfenvtest" { 314 name = "tf-test-name" 315 application = "${aws_elastic_beanstalk_application.tftest.name}" 316 tier = "Worker" 317 solution_stack_name = "64bit Amazon Linux running Python" 318 } 319 ` 320 321 func testAccBeanstalkEnvCnamePrefixConfig(randString string) string { 322 return fmt.Sprintf(` 323 resource "aws_elastic_beanstalk_application" "tftest" { 324 name = "tf-test-name" 325 description = "tf-test-desc" 326 } 327 328 resource "aws_elastic_beanstalk_environment" "tfenvtest" { 329 name = "tf-test-name" 330 application = "${aws_elastic_beanstalk_application.tftest.name}" 331 cname_prefix = "%s" 332 solution_stack_name = "64bit Amazon Linux running Python" 333 } 334 `, randString) 335 } 336 337 const testAccBeanstalkConfigTemplate = ` 338 resource "aws_elastic_beanstalk_application" "tftest" { 339 name = "tf-test-name" 340 description = "tf-test-desc" 341 } 342 343 resource "aws_elastic_beanstalk_environment" "tftest" { 344 name = "tf-test-name" 345 application = "${aws_elastic_beanstalk_application.tftest.name}" 346 template_name = "${aws_elastic_beanstalk_configuration_template.tftest.name}" 347 } 348 349 resource "aws_elastic_beanstalk_configuration_template" "tftest" { 350 name = "tf-test-original" 351 application = "${aws_elastic_beanstalk_application.tftest.name}" 352 solution_stack_name = "64bit Amazon Linux running Python" 353 354 setting { 355 namespace = "aws:elasticbeanstalk:application:environment" 356 name = "TEMPLATE" 357 value = "1" 358 } 359 } 360 ` 361 362 const testAccBeanstalkConfigTemplateUpdate = ` 363 resource "aws_elastic_beanstalk_application" "tftest" { 364 name = "tf-test-name" 365 description = "tf-test-desc" 366 } 367 368 resource "aws_elastic_beanstalk_environment" "tftest" { 369 name = "tf-test-name" 370 application = "${aws_elastic_beanstalk_application.tftest.name}" 371 template_name = "${aws_elastic_beanstalk_configuration_template.tftest.name}" 372 } 373 374 resource "aws_elastic_beanstalk_configuration_template" "tftest" { 375 name = "tf-test-updated" 376 application = "${aws_elastic_beanstalk_application.tftest.name}" 377 solution_stack_name = "64bit Amazon Linux running Python" 378 379 setting { 380 namespace = "aws:elasticbeanstalk:application:environment" 381 name = "TEMPLATE" 382 value = "2" 383 } 384 } 385 ` 386 387 const testAccBeanstalkConfigTemplateOverride = ` 388 resource "aws_elastic_beanstalk_application" "tftest" { 389 name = "tf-test-name" 390 description = "tf-test-desc" 391 } 392 393 resource "aws_elastic_beanstalk_environment" "tftest" { 394 name = "tf-test-name" 395 application = "${aws_elastic_beanstalk_application.tftest.name}" 396 template_name = "${aws_elastic_beanstalk_configuration_template.tftest.name}" 397 398 setting { 399 namespace = "aws:elasticbeanstalk:application:environment" 400 name = "TEMPLATE" 401 value = "3" 402 } 403 } 404 405 resource "aws_elastic_beanstalk_configuration_template" "tftest" { 406 name = "tf-test-updated" 407 application = "${aws_elastic_beanstalk_application.tftest.name}" 408 solution_stack_name = "64bit Amazon Linux running Python" 409 410 setting { 411 namespace = "aws:elasticbeanstalk:application:environment" 412 name = "TEMPLATE" 413 value = "2" 414 } 415 } 416 `