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