From 6c194ca428a9d27865ef7549e0c383d943d83c9f Mon Sep 17 00:00:00 2001 From: Charles Treatman Date: Mon, 26 Feb 2018 12:50:34 -0500 Subject: [PATCH] Adds acceptance tests for EB environment ARN and tags --- ..._aws_elastic_beanstalk_environment_test.go | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/aws/resource_aws_elastic_beanstalk_environment_test.go b/aws/resource_aws_elastic_beanstalk_environment_test.go index 8b26da44463..6ef65629323 100644 --- a/aws/resource_aws_elastic_beanstalk_environment_test.go +++ b/aws/resource_aws_elastic_beanstalk_environment_test.go @@ -125,10 +125,14 @@ func TestAccAWSBeanstalkEnv_basic(t *testing.T) { Config: testAccBeanstalkEnvConfig(appName, envName), Check: resource.ComposeTestCheckFunc( testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app), + resource.TestMatchResourceAttr( + "aws_elastic_beanstalk_environment.tfenvtest", "arn", + regexp.MustCompile(fmt.Sprintf("^arn:[^:]+:elasticbeanstalk:[^:]+:[^:]+:environment/%s/%s$", appName, envName))), ), }, }, }) + } func TestAccAWSBeanstalkEnv_tier(t *testing.T) { @@ -283,6 +287,49 @@ func TestAccAWSBeanstalkEnv_resource(t *testing.T) { }) } +func TestAccAWSBeanstalkEnv_tags(t *testing.T) { + var app elasticbeanstalk.EnvironmentDescription + + rString := acctest.RandString(8) + appName := fmt.Sprintf("tf_acc_app_env_resource_%s", rString) + envName := fmt.Sprintf("tf-acc-env-resource-%s", rString) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBeanstalkEnvDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBeanstalkEnvConfig_empty_settings(appName, envName), + Check: resource.ComposeTestCheckFunc( + testAccCheckBeanstalkEnvTagsMatch(&app, map[string]string{}), + ), + }, + + { + Config: testAccBeanstalkTagsTemplate(appName, envName, "test1", "test2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckBeanstalkEnvTagsMatch(&app, map[string]string{"firstTag": "test1", "secondTag": "test2"}), + ), + }, + + { + Config: testAccBeanstalkTagsTemplate(appName, envName, "test2", "test1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckBeanstalkEnvTagsMatch(&app, map[string]string{"firstTag": "test2", "secondTag": "test1"}), + ), + }, + + { + Config: testAccBeanstalkEnvConfig_empty_settings(appName, envName), + Check: resource.ComposeTestCheckFunc( + testAccCheckBeanstalkEnvTagsMatch(&app, map[string]string{}), + ), + }, + }, + }) +} + func TestAccAWSBeanstalkEnv_vpc(t *testing.T) { var app elasticbeanstalk.EnvironmentDescription @@ -629,6 +676,32 @@ func testAccCheckBeanstalkEnvConfigValue(n string, expectedValue string) resourc } } +func testAccCheckBeanstalkEnvTagsMatch(env *elasticbeanstalk.EnvironmentDescription, expectedValue map[string]string) resource.TestCheckFunc { + return func(s *terraform.State) error { + if env == nil { + return fmt.Errorf("Nil environment in testAccCheckBeanstalkEnvTagsMatch") + } + + conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn + + tags, err := conn.ListTagsForResource(&elasticbeanstalk.ListTagsForResourceInput{ + ResourceArn: env.EnvironmentArn, + }) + + if err != nil { + return err + } + + foundTags := tagsToMapBeanstalk(tags.ResourceTags) + + if !reflect.DeepEqual(foundTags, expectedValue) { + return fmt.Errorf("Tag value: %s. Expected %s", foundTags, expectedValue) + } + + return nil + } +} + func testAccCheckBeanstalkApplicationVersionDeployed(n string, app *elasticbeanstalk.EnvironmentDescription) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -933,6 +1006,27 @@ resource "aws_elastic_beanstalk_environment" "tfenvtest" { }`, appName, envName) } +func testAccBeanstalkTagsTemplate(appName, envName, firstTag, secondTag string) string { + return fmt.Sprintf(` +resource "aws_elastic_beanstalk_application" "tftest" { + name = "%s" + description = "tf-test-desc" +} + +resource "aws_elastic_beanstalk_environment" "tfenvtest" { + name = "%s" + application = "${aws_elastic_beanstalk_application.tftest.name}" + solution_stack_name = "64bit Amazon Linux running Python" + + wait_for_ready_timeout = "15m" + + tags { + firstTag = "%s" + secondTag = "%s" + } +}`, appName, envName, firstTag, secondTag) +} + func testAccBeanstalkEnv_VPC(sgName, appName, envName string) string { return fmt.Sprintf(` resource "aws_vpc" "tf_b_test" {