github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/resource_aws_launch_configuration_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/aws-sdk-go/aws" 8 "github.com/hashicorp/aws-sdk-go/gen/autoscaling" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAWSLaunchConfiguration(t *testing.T) { 14 var conf autoscaling.LaunchConfiguration 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccAWSLaunchConfigurationConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf), 25 testAccCheckAWSLaunchConfigurationAttributes(&conf), 26 resource.TestCheckResourceAttr( 27 "aws_launch_configuration.bar", "image_id", "ami-21f78e11"), 28 resource.TestCheckResourceAttr( 29 "aws_launch_configuration.bar", "name", "foobar-terraform-test"), 30 resource.TestCheckResourceAttr( 31 "aws_launch_configuration.bar", "instance_type", "t1.micro"), 32 resource.TestCheckResourceAttr( 33 "aws_launch_configuration.bar", "associate_public_ip_address", "true"), 34 resource.TestCheckResourceAttr( 35 "aws_launch_configuration.bar", "spot_price", ""), 36 ), 37 }, 38 39 resource.TestStep{ 40 Config: TestAccAWSLaunchConfigurationWithSpotPriceConfig, 41 Check: resource.ComposeTestCheckFunc( 42 testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf), 43 testAccCheckAWSLaunchConfigurationAttributes(&conf), 44 resource.TestCheckResourceAttr( 45 "aws_launch_configuration.bar", "spot_price", "0.01"), 46 ), 47 }, 48 }, 49 }) 50 } 51 52 func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error { 53 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 54 55 for _, rs := range s.RootModule().Resources { 56 if rs.Type != "aws_launch_configuration" { 57 continue 58 } 59 60 describe, err := conn.DescribeLaunchConfigurations( 61 &autoscaling.LaunchConfigurationNamesType{ 62 LaunchConfigurationNames: []string{rs.Primary.ID}, 63 }) 64 65 if err == nil { 66 if len(describe.LaunchConfigurations) != 0 && 67 *describe.LaunchConfigurations[0].LaunchConfigurationName == rs.Primary.ID { 68 return fmt.Errorf("Launch Configuration still exists") 69 } 70 } 71 72 // Verify the error 73 providerErr, ok := err.(aws.APIError) 74 if !ok { 75 return err 76 } 77 if providerErr.Code != "InvalidLaunchConfiguration.NotFound" { 78 return err 79 } 80 } 81 82 return nil 83 } 84 85 func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc { 86 return func(s *terraform.State) error { 87 if *conf.ImageID != "ami-21f78e11" { 88 return fmt.Errorf("Bad image_id: %s", *conf.ImageID) 89 } 90 91 if *conf.LaunchConfigurationName != "foobar-terraform-test" { 92 return fmt.Errorf("Bad name: %s", *conf.LaunchConfigurationName) 93 } 94 95 if *conf.InstanceType != "t1.micro" { 96 return fmt.Errorf("Bad instance_type: %s", *conf.InstanceType) 97 } 98 99 return nil 100 } 101 } 102 103 func testAccCheckAWSLaunchConfigurationExists(n string, res *autoscaling.LaunchConfiguration) resource.TestCheckFunc { 104 return func(s *terraform.State) error { 105 rs, ok := s.RootModule().Resources[n] 106 if !ok { 107 return fmt.Errorf("Not found: %s", n) 108 } 109 110 if rs.Primary.ID == "" { 111 return fmt.Errorf("No Launch Configuration ID is set") 112 } 113 114 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 115 116 describeOpts := autoscaling.LaunchConfigurationNamesType{ 117 LaunchConfigurationNames: []string{rs.Primary.ID}, 118 } 119 describe, err := conn.DescribeLaunchConfigurations(&describeOpts) 120 121 if err != nil { 122 return err 123 } 124 125 if len(describe.LaunchConfigurations) != 1 || 126 *describe.LaunchConfigurations[0].LaunchConfigurationName != rs.Primary.ID { 127 return fmt.Errorf("Launch Configuration Group not found") 128 } 129 130 *res = describe.LaunchConfigurations[0] 131 132 return nil 133 } 134 } 135 136 const testAccAWSLaunchConfigurationConfig = ` 137 resource "aws_launch_configuration" "bar" { 138 name = "foobar-terraform-test" 139 image_id = "ami-21f78e11" 140 instance_type = "t1.micro" 141 user_data = "foobar-user-data" 142 associate_public_ip_address = true 143 } 144 ` 145 146 const TestAccAWSLaunchConfigurationWithSpotPriceConfig = ` 147 resource "aws_launch_configuration" "bar" { 148 name = "foobar-terraform-test" 149 image_id = "ami-21f78e11" 150 instance_type = "t1.micro" 151 user_data = "foobar-user-data" 152 associate_public_ip_address = true 153 spot_price = "0.01" 154 } 155 `