github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/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 TestAccAWSBeanstalkEnv_resource(t *testing.T) {
   144  	var app elasticbeanstalk.EnvironmentDescription
   145  
   146  	resource.Test(t, resource.TestCase{
   147  		PreCheck:     func() { testAccPreCheck(t) },
   148  		Providers:    testAccProviders,
   149  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
   150  		Steps: []resource.TestStep{
   151  			resource.TestStep{
   152  				Config: testAccBeanstalkResourceOptionSetting,
   153  				Check: resource.ComposeTestCheckFunc(
   154  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
   155  				),
   156  			},
   157  		},
   158  	})
   159  }
   160  
   161  func testAccCheckBeanstalkEnvDestroy(s *terraform.State) error {
   162  	conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
   163  
   164  	for _, rs := range s.RootModule().Resources {
   165  		if rs.Type != "aws_elastic_beanstalk_environment" {
   166  			continue
   167  		}
   168  
   169  		// Try to find the environment
   170  		describeBeanstalkEnvOpts := &elasticbeanstalk.DescribeEnvironmentsInput{
   171  			EnvironmentIds: []*string{aws.String(rs.Primary.ID)},
   172  		}
   173  		resp, err := conn.DescribeEnvironments(describeBeanstalkEnvOpts)
   174  		if err == nil {
   175  			switch {
   176  			case len(resp.Environments) > 1:
   177  				return fmt.Errorf("Error %d environments match, expected 1", len(resp.Environments))
   178  			case len(resp.Environments) == 1:
   179  				if *resp.Environments[0].Status == "Terminated" {
   180  					return nil
   181  				}
   182  				return fmt.Errorf("Elastic Beanstalk ENV still exists.")
   183  			default:
   184  				return nil
   185  			}
   186  		}
   187  
   188  		// Verify the error is what we want
   189  		ec2err, ok := err.(awserr.Error)
   190  		if !ok {
   191  			return err
   192  		}
   193  		if ec2err.Code() != "InvalidBeanstalkEnvID.NotFound" {
   194  			return err
   195  		}
   196  	}
   197  
   198  	return nil
   199  }
   200  
   201  func testAccCheckBeanstalkEnvExists(n string, app *elasticbeanstalk.EnvironmentDescription) resource.TestCheckFunc {
   202  	return func(s *terraform.State) error {
   203  		rs, ok := s.RootModule().Resources[n]
   204  		if !ok {
   205  			return fmt.Errorf("Not found: %s", n)
   206  		}
   207  
   208  		if rs.Primary.ID == "" {
   209  			return fmt.Errorf("Elastic Beanstalk ENV is not set")
   210  		}
   211  
   212  		env, err := describeBeanstalkEnv(testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn, aws.String(rs.Primary.ID))
   213  		if err != nil {
   214  			return err
   215  		}
   216  
   217  		*app = *env
   218  
   219  		return nil
   220  	}
   221  }
   222  
   223  func testAccCheckBeanstalkEnvTier(n string, app *elasticbeanstalk.EnvironmentDescription) resource.TestCheckFunc {
   224  	return func(s *terraform.State) error {
   225  		rs, ok := s.RootModule().Resources[n]
   226  		if !ok {
   227  			return fmt.Errorf("Not found: %s", n)
   228  		}
   229  
   230  		if rs.Primary.ID == "" {
   231  			return fmt.Errorf("Elastic Beanstalk ENV is not set")
   232  		}
   233  
   234  		env, err := describeBeanstalkEnv(testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn, aws.String(rs.Primary.ID))
   235  		if err != nil {
   236  			return err
   237  		}
   238  		if *env.Tier.Name != "Worker" {
   239  			return fmt.Errorf("Beanstalk Environment tier is %s, expected Worker", *env.Tier.Name)
   240  		}
   241  
   242  		*app = *env
   243  
   244  		return nil
   245  	}
   246  }
   247  
   248  func testAccCheckBeanstalkEnvConfigValue(n string, expectedValue string) resource.TestCheckFunc {
   249  	return func(s *terraform.State) error {
   250  		conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
   251  
   252  		rs, ok := s.RootModule().Resources[n]
   253  		if !ok {
   254  			return fmt.Errorf("Not found: %s", n)
   255  		}
   256  
   257  		if rs.Primary.ID == "" {
   258  			return fmt.Errorf("Elastic Beanstalk ENV is not set")
   259  		}
   260  
   261  		resp, err := conn.DescribeConfigurationOptions(&elasticbeanstalk.DescribeConfigurationOptionsInput{
   262  			ApplicationName: aws.String(rs.Primary.Attributes["application"]),
   263  			EnvironmentName: aws.String(rs.Primary.Attributes["name"]),
   264  			Options: []*elasticbeanstalk.OptionSpecification{
   265  				{
   266  					Namespace:  aws.String("aws:elasticbeanstalk:application:environment"),
   267  					OptionName: aws.String("TEMPLATE"),
   268  				},
   269  			},
   270  		})
   271  		if err != nil {
   272  			return err
   273  		}
   274  
   275  		if len(resp.Options) != 1 {
   276  			return fmt.Errorf("Found %d options, expected 1.", len(resp.Options))
   277  		}
   278  
   279  		log.Printf("[DEBUG] %d Elastic Beanstalk Option values returned.", len(resp.Options[0].ValueOptions))
   280  
   281  		for _, value := range resp.Options[0].ValueOptions {
   282  			if *value != expectedValue {
   283  				return fmt.Errorf("Option setting value: %s. Expected %s", *value, expectedValue)
   284  			}
   285  		}
   286  
   287  		return nil
   288  	}
   289  }
   290  
   291  func describeBeanstalkEnv(conn *elasticbeanstalk.ElasticBeanstalk,
   292  	envID *string) (*elasticbeanstalk.EnvironmentDescription, error) {
   293  	describeBeanstalkEnvOpts := &elasticbeanstalk.DescribeEnvironmentsInput{
   294  		EnvironmentIds: []*string{envID},
   295  	}
   296  
   297  	log.Printf("[DEBUG] Elastic Beanstalk Environment TEST describe opts: %s", describeBeanstalkEnvOpts)
   298  
   299  	resp, err := conn.DescribeEnvironments(describeBeanstalkEnvOpts)
   300  	if err != nil {
   301  		return &elasticbeanstalk.EnvironmentDescription{}, err
   302  	}
   303  	if len(resp.Environments) == 0 {
   304  		return &elasticbeanstalk.EnvironmentDescription{}, fmt.Errorf("Elastic Beanstalk ENV not found.")
   305  	}
   306  	if len(resp.Environments) > 1 {
   307  		return &elasticbeanstalk.EnvironmentDescription{}, fmt.Errorf("Found %d environments, expected 1.", len(resp.Environments))
   308  	}
   309  	return resp.Environments[0], nil
   310  }
   311  
   312  const testAccBeanstalkEnvConfig = `
   313  resource "aws_elastic_beanstalk_application" "tftest" {
   314    name = "tf-test-name"
   315    description = "tf-test-desc"
   316  }
   317  
   318  resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   319    name = "tf-test-name"
   320    application = "${aws_elastic_beanstalk_application.tftest.name}"
   321    solution_stack_name = "64bit Amazon Linux running Python"
   322  }
   323  `
   324  
   325  const testAccBeanstalkWorkerEnvConfig = `
   326  resource "aws_elastic_beanstalk_application" "tftest" {
   327    name = "tf-test-name"
   328    description = "tf-test-desc"
   329  }
   330  
   331  resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   332    name = "tf-test-name"
   333    application = "${aws_elastic_beanstalk_application.tftest.name}"
   334    tier = "Worker"
   335    solution_stack_name = "64bit Amazon Linux running Python"
   336  }
   337  `
   338  
   339  func testAccBeanstalkEnvCnamePrefixConfig(randString string) string {
   340  	return fmt.Sprintf(`
   341  resource "aws_elastic_beanstalk_application" "tftest" {
   342  name = "tf-test-name"
   343  description = "tf-test-desc"
   344  }
   345  
   346  resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   347  name = "tf-test-name"
   348  application = "${aws_elastic_beanstalk_application.tftest.name}"
   349  cname_prefix = "%s"
   350  solution_stack_name = "64bit Amazon Linux running Python"
   351  }
   352  `, randString)
   353  }
   354  
   355  const testAccBeanstalkConfigTemplate = `
   356  resource "aws_elastic_beanstalk_application" "tftest" {
   357    name = "tf-test-name"
   358    description = "tf-test-desc"
   359  }
   360  
   361  resource "aws_elastic_beanstalk_environment" "tftest" {
   362    name = "tf-test-name"
   363    application = "${aws_elastic_beanstalk_application.tftest.name}"
   364    template_name = "${aws_elastic_beanstalk_configuration_template.tftest.name}"
   365  }
   366  
   367  resource "aws_elastic_beanstalk_configuration_template" "tftest" {
   368    name        = "tf-test-original"
   369    application = "${aws_elastic_beanstalk_application.tftest.name}"
   370    solution_stack_name = "64bit Amazon Linux running Python"
   371  
   372    setting {
   373      namespace = "aws:elasticbeanstalk:application:environment"
   374      name      = "TEMPLATE"
   375      value     = "1"
   376   }
   377  }
   378  `
   379  
   380  const testAccBeanstalkConfigTemplateUpdate = `
   381  resource "aws_elastic_beanstalk_application" "tftest" {
   382    name = "tf-test-name"
   383    description = "tf-test-desc"
   384  }
   385  
   386  resource "aws_elastic_beanstalk_environment" "tftest" {
   387    name = "tf-test-name"
   388    application = "${aws_elastic_beanstalk_application.tftest.name}"
   389    template_name = "${aws_elastic_beanstalk_configuration_template.tftest.name}"
   390  }
   391  
   392  resource "aws_elastic_beanstalk_configuration_template" "tftest" {
   393    name        = "tf-test-updated"
   394    application = "${aws_elastic_beanstalk_application.tftest.name}"
   395    solution_stack_name = "64bit Amazon Linux running Python"
   396  
   397    setting {
   398      namespace = "aws:elasticbeanstalk:application:environment"
   399      name      = "TEMPLATE"
   400      value     = "2"
   401    }
   402  }
   403  `
   404  
   405  const testAccBeanstalkConfigTemplateOverride = `
   406  resource "aws_elastic_beanstalk_application" "tftest" {
   407    name = "tf-test-name"
   408    description = "tf-test-desc"
   409  }
   410  
   411  resource "aws_elastic_beanstalk_environment" "tftest" {
   412    name = "tf-test-name"
   413    application = "${aws_elastic_beanstalk_application.tftest.name}"
   414    template_name = "${aws_elastic_beanstalk_configuration_template.tftest.name}"
   415  
   416    setting {
   417      namespace = "aws:elasticbeanstalk:application:environment"
   418      name      = "TEMPLATE"
   419      value     = "3"
   420    }
   421  }
   422  
   423  resource "aws_elastic_beanstalk_configuration_template" "tftest" {
   424    name        = "tf-test-updated"
   425    application = "${aws_elastic_beanstalk_application.tftest.name}"
   426    solution_stack_name = "64bit Amazon Linux running Python"
   427  
   428    setting {
   429      namespace = "aws:elasticbeanstalk:application:environment"
   430      name      = "TEMPLATE"
   431      value     = "2"
   432    }
   433  }
   434  `
   435  const testAccBeanstalkResourceOptionSetting = `
   436  resource "aws_elastic_beanstalk_application" "tftest" {
   437    name = "tf-test-name"
   438    description = "tf-test-desc"
   439  }
   440  
   441  resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   442    name = "tf-test-name"
   443    application = "${aws_elastic_beanstalk_application.tftest.name}"
   444    solution_stack_name = "64bit Amazon Linux running Python"
   445  
   446    setting {
   447      namespace = "aws:autoscaling:scheduledaction"
   448      resource = "ScheduledAction01"
   449      name = "MinSize"
   450      value = "2"
   451    }
   452  
   453    setting {
   454      namespace = "aws:autoscaling:scheduledaction"
   455      resource = "ScheduledAction01"
   456      name = "MaxSize"
   457      value = "6"
   458    }
   459  
   460    setting {
   461      namespace = "aws:autoscaling:scheduledaction"
   462      resource = "ScheduledAction01"
   463      name = "Recurrence"
   464      value = "0 8 * * *"
   465    }
   466  }
   467  `