github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/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  					resource.TestCheckResourceAttr(
    32  						"aws_launch_configuration.bar", "associate_public_ip_address", "true"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error {
    40  	conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    41  
    42  	for _, rs := range s.RootModule().Resources {
    43  		if rs.Type != "aws_launch_configuration" {
    44  			continue
    45  		}
    46  
    47  		describe, err := conn.DescribeLaunchConfigurations(
    48  			&autoscaling.DescribeLaunchConfigurations{
    49  				Names: []string{rs.Primary.ID},
    50  			})
    51  
    52  		if err == nil {
    53  			if len(describe.LaunchConfigurations) != 0 &&
    54  				describe.LaunchConfigurations[0].Name == rs.Primary.ID {
    55  				return fmt.Errorf("Launch Configuration still exists")
    56  			}
    57  		}
    58  
    59  		// Verify the error
    60  		providerErr, ok := err.(*autoscaling.Error)
    61  		if !ok {
    62  			return err
    63  		}
    64  		if providerErr.Code != "InvalidLaunchConfiguration.NotFound" {
    65  			return err
    66  		}
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		if conf.ImageId != "ami-21f78e11" {
    75  			return fmt.Errorf("Bad image_id: %s", conf.ImageId)
    76  		}
    77  
    78  		if conf.Name != "foobar-terraform-test" {
    79  			return fmt.Errorf("Bad name: %s", conf.Name)
    80  		}
    81  
    82  		if conf.InstanceType != "t1.micro" {
    83  			return fmt.Errorf("Bad instance_type: %s", conf.InstanceType)
    84  		}
    85  
    86  		return nil
    87  	}
    88  }
    89  
    90  func testAccCheckAWSLaunchConfigurationExists(n string, res *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
    91  	return func(s *terraform.State) error {
    92  		rs, ok := s.RootModule().Resources[n]
    93  		if !ok {
    94  			return fmt.Errorf("Not found: %s", n)
    95  		}
    96  
    97  		if rs.Primary.ID == "" {
    98  			return fmt.Errorf("No Launch Configuration ID is set")
    99  		}
   100  
   101  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
   102  
   103  		describeOpts := autoscaling.DescribeLaunchConfigurations{
   104  			Names: []string{rs.Primary.ID},
   105  		}
   106  		describe, err := conn.DescribeLaunchConfigurations(&describeOpts)
   107  
   108  		if err != nil {
   109  			return err
   110  		}
   111  
   112  		if len(describe.LaunchConfigurations) != 1 ||
   113  			describe.LaunchConfigurations[0].Name != rs.Primary.ID {
   114  			return fmt.Errorf("Launch Configuration Group not found")
   115  		}
   116  
   117  		*res = describe.LaunchConfigurations[0]
   118  
   119  		return nil
   120  	}
   121  }
   122  
   123  const testAccAWSLaunchConfigurationConfig = `
   124  resource "aws_launch_configuration" "bar" {
   125    name = "foobar-terraform-test"
   126    image_id = "ami-21f78e11"
   127    instance_type = "t1.micro"
   128    user_data = "foobar-user-data"
   129    associate_public_ip_address = true
   130  }
   131  `