github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/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/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/goamz/autoscaling"
    10  )
    11  
    12  func TestAccAWSLaunchConfiguration(t *testing.T) {
    13  	var conf autoscaling.LaunchConfiguration
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAWSLaunchConfigurationConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
    24  					testAccCheckAWSLaunchConfigurationAttributes(&conf),
    25  					resource.TestCheckResourceAttr(
    26  						"aws_launch_configuration.bar", "image_id", "ami-21f78e11"),
    27  					resource.TestCheckResourceAttr(
    28  						"aws_launch_configuration.bar", "name", "foobar-terraform-test"),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_launch_configuration.bar", "instance_type", "t1.micro"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error {
    38  	conn := testAccProvider.autoscalingconn
    39  
    40  	for _, rs := range s.RootModule().Resources {
    41  		if rs.Type != "aws_launch_configuration" {
    42  			continue
    43  		}
    44  
    45  		describe, err := conn.DescribeLaunchConfigurations(
    46  			&autoscaling.DescribeLaunchConfigurations{
    47  				Names: []string{rs.Primary.ID},
    48  			})
    49  
    50  		if err == nil {
    51  			if len(describe.LaunchConfigurations) != 0 &&
    52  				describe.LaunchConfigurations[0].Name == rs.Primary.ID {
    53  				return fmt.Errorf("Launch Configuration still exists")
    54  			}
    55  		}
    56  
    57  		// Verify the error
    58  		providerErr, ok := err.(*autoscaling.Error)
    59  		if !ok {
    60  			return err
    61  		}
    62  		if providerErr.Code != "InvalidLaunchConfiguration.NotFound" {
    63  			return err
    64  		}
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
    71  	return func(s *terraform.State) error {
    72  		if conf.ImageId != "ami-21f78e11" {
    73  			return fmt.Errorf("Bad image_id: %s", conf.ImageId)
    74  		}
    75  
    76  		if conf.Name != "foobar-terraform-test" {
    77  			return fmt.Errorf("Bad name: %s", conf.Name)
    78  		}
    79  
    80  		if conf.InstanceType != "t1.micro" {
    81  			return fmt.Errorf("Bad instance_type: %s", conf.InstanceType)
    82  		}
    83  
    84  		return nil
    85  	}
    86  }
    87  
    88  func testAccCheckAWSLaunchConfigurationExists(n string, res *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
    89  	return func(s *terraform.State) error {
    90  		rs, ok := s.RootModule().Resources[n]
    91  		if !ok {
    92  			return fmt.Errorf("Not found: %s", n)
    93  		}
    94  
    95  		if rs.Primary.ID == "" {
    96  			return fmt.Errorf("No Launch Configuration ID is set")
    97  		}
    98  
    99  		conn := testAccProvider.autoscalingconn
   100  
   101  		describeOpts := autoscaling.DescribeLaunchConfigurations{
   102  			Names: []string{rs.Primary.ID},
   103  		}
   104  		describe, err := conn.DescribeLaunchConfigurations(&describeOpts)
   105  
   106  		if err != nil {
   107  			return err
   108  		}
   109  
   110  		if len(describe.LaunchConfigurations) != 1 ||
   111  			describe.LaunchConfigurations[0].Name != rs.Primary.ID {
   112  			return fmt.Errorf("Launch Configuration Group not found")
   113  		}
   114  
   115  		*res = describe.LaunchConfigurations[0]
   116  
   117  		return nil
   118  	}
   119  }
   120  
   121  const testAccAWSLaunchConfigurationConfig = `
   122  resource "aws_launch_configuration" "bar" {
   123    name = "foobar-terraform-test"
   124    image_id = "ami-21f78e11"
   125    instance_type = "t1.micro"
   126    user_data = "foobar-user-data"
   127  }
   128  `