github.com/subuk/terraform@v0.6.14-0.20160317140351-de1567c2e732/builtin/providers/aws/resource_aws_elastic_beanstalk_environment_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSBeanstalkEnv_basic(t *testing.T) {
    16  	var app elasticbeanstalk.EnvironmentDescription
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccBeanstalkEnvConfig,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccAWSBeanstalkEnv_tier(t *testing.T) {
    34  	var app elasticbeanstalk.EnvironmentDescription
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
    40  		Steps: []resource.TestStep{
    41  			resource.TestStep{
    42  				Config: testAccBeanstalkWorkerEnvConfig,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckBeanstalkEnvTier("aws_elastic_beanstalk_environment.tfenvtest", &app),
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func testAccCheckBeanstalkEnvDestroy(s *terraform.State) error {
    52  	conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
    53  
    54  	for _, rs := range s.RootModule().Resources {
    55  		if rs.Type != "aws_elastic_beanstalk_environment" {
    56  			continue
    57  		}
    58  
    59  		// Try to find the environment
    60  		describeBeanstalkEnvOpts := &elasticbeanstalk.DescribeEnvironmentsInput{
    61  			EnvironmentIds: []*string{aws.String(rs.Primary.ID)},
    62  		}
    63  		resp, err := conn.DescribeEnvironments(describeBeanstalkEnvOpts)
    64  		if err == nil {
    65  			switch {
    66  			case len(resp.Environments) > 1:
    67  				return fmt.Errorf("Error %d environments match, expected 1", len(resp.Environments))
    68  			case len(resp.Environments) == 1:
    69  				if *resp.Environments[0].Status == "Terminated" {
    70  					return nil
    71  				}
    72  				return fmt.Errorf("Elastic Beanstalk ENV still exists.")
    73  			default:
    74  				return nil
    75  			}
    76  		}
    77  
    78  		// Verify the error is what we want
    79  		ec2err, ok := err.(awserr.Error)
    80  		if !ok {
    81  			return err
    82  		}
    83  		if ec2err.Code() != "InvalidBeanstalkEnvID.NotFound" {
    84  			return err
    85  		}
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  func testAccCheckBeanstalkEnvExists(n string, app *elasticbeanstalk.EnvironmentDescription) resource.TestCheckFunc {
    92  	return func(s *terraform.State) error {
    93  		rs, ok := s.RootModule().Resources[n]
    94  		if !ok {
    95  			return fmt.Errorf("Not found: %s", n)
    96  		}
    97  
    98  		if rs.Primary.ID == "" {
    99  			return fmt.Errorf("Elastic Beanstalk ENV is not set")
   100  		}
   101  
   102  		env, err := describeBeanstalkEnv(testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn, aws.String(rs.Primary.ID))
   103  		if err != nil {
   104  			return err
   105  		}
   106  
   107  		*app = *env
   108  
   109  		return nil
   110  	}
   111  }
   112  
   113  func testAccCheckBeanstalkEnvTier(n string, app *elasticbeanstalk.EnvironmentDescription) resource.TestCheckFunc {
   114  	return func(s *terraform.State) error {
   115  		rs, ok := s.RootModule().Resources[n]
   116  		if !ok {
   117  			return fmt.Errorf("Not found: %s", n)
   118  		}
   119  
   120  		if rs.Primary.ID == "" {
   121  			return fmt.Errorf("Elastic Beanstalk ENV is not set")
   122  		}
   123  
   124  		env, err := describeBeanstalkEnv(testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn, aws.String(rs.Primary.ID))
   125  		if err != nil {
   126  			return err
   127  		}
   128  		if *env.Tier.Name != "Worker" {
   129  			return fmt.Errorf("Beanstalk Environment tier is %s, expected Worker", *env.Tier.Name)
   130  		}
   131  
   132  		*app = *env
   133  
   134  		return nil
   135  	}
   136  }
   137  
   138  func describeBeanstalkEnv(conn *elasticbeanstalk.ElasticBeanstalk,
   139  	envID *string) (*elasticbeanstalk.EnvironmentDescription, error) {
   140  	describeBeanstalkEnvOpts := &elasticbeanstalk.DescribeEnvironmentsInput{
   141  		EnvironmentIds: []*string{envID},
   142  	}
   143  
   144  	log.Printf("[DEBUG] Elastic Beanstalk Environment TEST describe opts: %s", describeBeanstalkEnvOpts)
   145  
   146  	resp, err := conn.DescribeEnvironments(describeBeanstalkEnvOpts)
   147  	if err != nil {
   148  		return &elasticbeanstalk.EnvironmentDescription{}, err
   149  	}
   150  	if len(resp.Environments) == 0 {
   151  		return &elasticbeanstalk.EnvironmentDescription{}, fmt.Errorf("Elastic Beanstalk ENV not found.")
   152  	}
   153  	if len(resp.Environments) > 1 {
   154  		return &elasticbeanstalk.EnvironmentDescription{}, fmt.Errorf("Found %d environments, expected 1.", len(resp.Environments))
   155  	}
   156  	return resp.Environments[0], nil
   157  }
   158  
   159  const testAccBeanstalkEnvConfig = `
   160  resource "aws_elastic_beanstalk_application" "tftest" {
   161    name = "tf-test-name"
   162    description = "tf-test-desc"
   163  }
   164  
   165  resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   166    name = "tf-test-name"
   167    application = "${aws_elastic_beanstalk_application.tftest.name}"
   168    solution_stack_name = "64bit Amazon Linux 2015.09 v2.0.8 running Go 1.4"
   169    #solution_stack_name =
   170  }
   171  `
   172  
   173  const testAccBeanstalkWorkerEnvConfig = `
   174  resource "aws_elastic_beanstalk_application" "tftest" {
   175    name = "tf-test-name"
   176    description = "tf-test-desc"
   177  }
   178  
   179  resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   180    name = "tf-test-name"
   181    application = "${aws_elastic_beanstalk_application.tftest.name}"
   182    tier = "Worker"
   183    solution_stack_name = "64bit Amazon Linux 2015.09 v2.0.4 running Go 1.4"
   184  }
   185  `