github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/builtin/providers/aws/resource_aws_launch_configuration_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/aws/awserr"
    12  	"github.com/aws/aws-sdk-go/service/autoscaling"
    13  	"github.com/hashicorp/terraform/helper/resource"
    14  	"github.com/hashicorp/terraform/terraform"
    15  )
    16  
    17  func TestAccAWSLaunchConfiguration_basic(t *testing.T) {
    18  	var conf autoscaling.LaunchConfiguration
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccAWSLaunchConfigurationNoNameConfig,
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
    29  					testAccCheckAWSLaunchConfigurationGeneratedNamePrefix(
    30  						"aws_launch_configuration.bar", "terraform-"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccAWSLaunchConfiguration_withBlockDevices(t *testing.T) {
    38  	var conf autoscaling.LaunchConfiguration
    39  
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
    44  		Steps: []resource.TestStep{
    45  			resource.TestStep{
    46  				Config: testAccAWSLaunchConfigurationConfig,
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
    49  					testAccCheckAWSLaunchConfigurationAttributes(&conf),
    50  					resource.TestCheckResourceAttr(
    51  						"aws_launch_configuration.bar", "image_id", "ami-21f78e11"),
    52  					resource.TestCheckResourceAttr(
    53  						"aws_launch_configuration.bar", "instance_type", "m1.small"),
    54  					resource.TestCheckResourceAttr(
    55  						"aws_launch_configuration.bar", "associate_public_ip_address", "true"),
    56  					resource.TestCheckResourceAttr(
    57  						"aws_launch_configuration.bar", "spot_price", ""),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func TestAccAWSLaunchConfiguration_withSpotPrice(t *testing.T) {
    65  	var conf autoscaling.LaunchConfiguration
    66  
    67  	resource.Test(t, resource.TestCase{
    68  		PreCheck:     func() { testAccPreCheck(t) },
    69  		Providers:    testAccProviders,
    70  		CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
    71  		Steps: []resource.TestStep{
    72  			resource.TestStep{
    73  				Config: testAccAWSLaunchConfigurationWithSpotPriceConfig,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
    76  					resource.TestCheckResourceAttr(
    77  						"aws_launch_configuration.bar", "spot_price", "0.01"),
    78  				),
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func testAccCheckAWSLaunchConfigurationGeneratedNamePrefix(
    85  	resource, prefix string) resource.TestCheckFunc {
    86  	return func(s *terraform.State) error {
    87  		r, ok := s.RootModule().Resources[resource]
    88  		if !ok {
    89  			return fmt.Errorf("Resource not found")
    90  		}
    91  		name, ok := r.Primary.Attributes["name"]
    92  		if !ok {
    93  			return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes)
    94  		}
    95  		if !strings.HasPrefix(name, prefix) {
    96  			return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix)
    97  		}
    98  		return nil
    99  	}
   100  }
   101  
   102  func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error {
   103  	conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
   104  
   105  	for _, rs := range s.RootModule().Resources {
   106  		if rs.Type != "aws_launch_configuration" {
   107  			continue
   108  		}
   109  
   110  		describe, err := conn.DescribeLaunchConfigurations(
   111  			&autoscaling.DescribeLaunchConfigurationsInput{
   112  				LaunchConfigurationNames: []*string{aws.String(rs.Primary.ID)},
   113  			})
   114  
   115  		if err == nil {
   116  			if len(describe.LaunchConfigurations) != 0 &&
   117  				*describe.LaunchConfigurations[0].LaunchConfigurationName == rs.Primary.ID {
   118  				return fmt.Errorf("Launch Configuration still exists")
   119  			}
   120  		}
   121  
   122  		// Verify the error
   123  		providerErr, ok := err.(awserr.Error)
   124  		if !ok {
   125  			return err
   126  		}
   127  		if providerErr.Code() != "InvalidLaunchConfiguration.NotFound" {
   128  			return err
   129  		}
   130  	}
   131  
   132  	return nil
   133  }
   134  
   135  func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
   136  	return func(s *terraform.State) error {
   137  		if *conf.ImageId != "ami-21f78e11" {
   138  			return fmt.Errorf("Bad image_id: %s", *conf.ImageId)
   139  		}
   140  
   141  		if !strings.HasPrefix(*conf.LaunchConfigurationName, "terraform-") {
   142  			return fmt.Errorf("Bad name: %s", *conf.LaunchConfigurationName)
   143  		}
   144  
   145  		if *conf.InstanceType != "m1.small" {
   146  			return fmt.Errorf("Bad instance_type: %s", *conf.InstanceType)
   147  		}
   148  
   149  		// Map out the block devices by name, which should be unique.
   150  		blockDevices := make(map[string]*autoscaling.BlockDeviceMapping)
   151  		for _, blockDevice := range conf.BlockDeviceMappings {
   152  			blockDevices[*blockDevice.DeviceName] = blockDevice
   153  		}
   154  
   155  		// Check if the root block device exists.
   156  		if _, ok := blockDevices["/dev/sda1"]; !ok {
   157  			fmt.Errorf("block device doesn't exist: /dev/sda1")
   158  		}
   159  
   160  		// Check if the secondary block device exists.
   161  		if _, ok := blockDevices["/dev/sdb"]; !ok {
   162  			fmt.Errorf("block device doesn't exist: /dev/sdb")
   163  		}
   164  
   165  		// Check if the third block device exists.
   166  		if _, ok := blockDevices["/dev/sdc"]; !ok {
   167  			fmt.Errorf("block device doesn't exist: /dev/sdc")
   168  		}
   169  
   170  		// Check if the secondary block device exists.
   171  		if _, ok := blockDevices["/dev/sdb"]; !ok {
   172  			return fmt.Errorf("block device doesn't exist: /dev/sdb")
   173  		}
   174  
   175  		return nil
   176  	}
   177  }
   178  
   179  func testAccCheckAWSLaunchConfigurationExists(n string, res *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
   180  	return func(s *terraform.State) error {
   181  		rs, ok := s.RootModule().Resources[n]
   182  		if !ok {
   183  			return fmt.Errorf("Not found: %s", n)
   184  		}
   185  
   186  		if rs.Primary.ID == "" {
   187  			return fmt.Errorf("No Launch Configuration ID is set")
   188  		}
   189  
   190  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
   191  
   192  		describeOpts := autoscaling.DescribeLaunchConfigurationsInput{
   193  			LaunchConfigurationNames: []*string{aws.String(rs.Primary.ID)},
   194  		}
   195  		describe, err := conn.DescribeLaunchConfigurations(&describeOpts)
   196  
   197  		if err != nil {
   198  			return err
   199  		}
   200  
   201  		if len(describe.LaunchConfigurations) != 1 ||
   202  			*describe.LaunchConfigurations[0].LaunchConfigurationName != rs.Primary.ID {
   203  			return fmt.Errorf("Launch Configuration Group not found")
   204  		}
   205  
   206  		*res = *describe.LaunchConfigurations[0]
   207  
   208  		return nil
   209  	}
   210  }
   211  
   212  var testAccAWSLaunchConfigurationConfig = fmt.Sprintf(`
   213  resource "aws_launch_configuration" "bar" {
   214    name = "terraform-test-%d"
   215    image_id = "ami-21f78e11"
   216    instance_type = "m1.small"
   217    user_data = "foobar-user-data"
   218    associate_public_ip_address = true
   219  
   220  	root_block_device {
   221  		volume_type = "gp2"
   222  		volume_size = 11
   223  	}
   224  	ebs_block_device {
   225  		device_name = "/dev/sdb"
   226  		volume_size = 9
   227  	}
   228  	ebs_block_device {
   229  		device_name = "/dev/sdc"
   230  		volume_size = 10
   231  		volume_type = "io1"
   232  		iops = 100
   233  	}
   234  	ephemeral_block_device {
   235  		device_name = "/dev/sde"
   236  		virtual_name = "ephemeral0"
   237  	}
   238  }
   239  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
   240  
   241  var testAccAWSLaunchConfigurationWithSpotPriceConfig = fmt.Sprintf(`
   242  resource "aws_launch_configuration" "bar" {
   243    name = "terraform-test-%d"
   244    image_id = "ami-21f78e11"
   245    instance_type = "t1.micro"
   246    spot_price = "0.01"
   247  }
   248  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
   249  
   250  const testAccAWSLaunchConfigurationNoNameConfig = `
   251  resource "aws_launch_configuration" "bar" {
   252     image_id = "ami-21f78e11"
   253     instance_type = "t1.micro"
   254     user_data = "foobar-user-data-change"
   255     associate_public_ip_address = false
   256  }
   257  `