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