github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_elastic_beanstalk_application_version_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/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSBeanstalkAppVersion_basic(t *testing.T) { 17 18 var appVersion elasticbeanstalk.ApplicationVersionDescription 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckApplicationVersionDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: testAccBeanstalkApplicationVersionConfig(acctest.RandInt()), 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckApplicationVersionExists("aws_elastic_beanstalk_application_version.default", &appVersion), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccCheckApplicationVersionDestroy(s *terraform.State) error { 36 conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn 37 38 for _, rs := range s.RootModule().Resources { 39 if rs.Type != "aws_elastic_beanstalk_application_version" { 40 continue 41 } 42 43 describeApplicationVersionOpts := &elasticbeanstalk.DescribeApplicationVersionsInput{ 44 VersionLabels: []*string{aws.String(rs.Primary.ID)}, 45 } 46 resp, err := conn.DescribeApplicationVersions(describeApplicationVersionOpts) 47 if err == nil { 48 if len(resp.ApplicationVersions) > 0 { 49 return fmt.Errorf("Elastic Beanstalk Application Verson still exists.") 50 } 51 52 return nil 53 } 54 ec2err, ok := err.(awserr.Error) 55 if !ok { 56 return err 57 } 58 if ec2err.Code() != "InvalidParameterValue" { 59 return err 60 } 61 } 62 63 return nil 64 } 65 66 func testAccCheckApplicationVersionExists(n string, app *elasticbeanstalk.ApplicationVersionDescription) resource.TestCheckFunc { 67 return func(s *terraform.State) error { 68 rs, ok := s.RootModule().Resources[n] 69 if !ok { 70 return fmt.Errorf("Not found: %s", n) 71 } 72 73 if rs.Primary.ID == "" { 74 return fmt.Errorf("Elastic Beanstalk Application Version is not set") 75 } 76 77 conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn 78 describeApplicationVersionOpts := &elasticbeanstalk.DescribeApplicationVersionsInput{ 79 VersionLabels: []*string{aws.String(rs.Primary.ID)}, 80 } 81 82 log.Printf("[DEBUG] Elastic Beanstalk Application Version TEST describe opts: %s", describeApplicationVersionOpts) 83 84 resp, err := conn.DescribeApplicationVersions(describeApplicationVersionOpts) 85 if err != nil { 86 return err 87 } 88 if len(resp.ApplicationVersions) == 0 { 89 return fmt.Errorf("Elastic Beanstalk Application Version not found.") 90 } 91 92 *app = *resp.ApplicationVersions[0] 93 94 return nil 95 } 96 } 97 98 func testAccBeanstalkApplicationVersionConfig(randInt int) string { 99 return fmt.Sprintf(` 100 resource "aws_s3_bucket" "default" { 101 bucket = "tftest.applicationversion.bucket-%d" 102 } 103 104 resource "aws_s3_bucket_object" "default" { 105 bucket = "${aws_s3_bucket.default.id}" 106 key = "beanstalk/python-v1.zip" 107 source = "test-fixtures/python-v1.zip" 108 } 109 110 resource "aws_elastic_beanstalk_application" "default" { 111 name = "tf-test-name-%d" 112 description = "tf-test-desc" 113 } 114 115 resource "aws_elastic_beanstalk_application_version" "default" { 116 application = "tf-test-name-%d" 117 name = "tf-test-version-label" 118 bucket = "${aws_s3_bucket.default.id}" 119 key = "${aws_s3_bucket_object.default.id}" 120 } 121 `, randInt, randInt, randInt) 122 }