github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/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 testAccCheckAWSLaunchConfigurationWithEncryption(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
    93  	return func(s *terraform.State) error {
    94  		// Map out the block devices by name, which should be unique.
    95  		blockDevices := make(map[string]*autoscaling.BlockDeviceMapping)
    96  		for _, blockDevice := range conf.BlockDeviceMappings {
    97  			blockDevices[*blockDevice.DeviceName] = blockDevice
    98  		}
    99  
   100  		// Check if the root block device exists.
   101  		if _, ok := blockDevices["/dev/sda1"]; !ok {
   102  			return fmt.Errorf("block device doesn't exist: /dev/sda1")
   103  		} else if blockDevices["/dev/sda1"].Ebs.Encrypted != nil {
   104  			return fmt.Errorf("root device should not include value for Encrypted")
   105  		}
   106  
   107  		// Check if the secondary block device exists.
   108  		if _, ok := blockDevices["/dev/sdb"]; !ok {
   109  			return fmt.Errorf("block device doesn't exist: /dev/sdb")
   110  		} else if !*blockDevices["/dev/sdb"].Ebs.Encrypted {
   111  			return fmt.Errorf("block device isn't encrypted as expected: /dev/sdb")
   112  		}
   113  
   114  		return nil
   115  	}
   116  }
   117  
   118  func TestAccAWSLaunchConfiguration_withEncryption(t *testing.T) {
   119  	var conf autoscaling.LaunchConfiguration
   120  
   121  	resource.Test(t, resource.TestCase{
   122  		PreCheck:     func() { testAccPreCheck(t) },
   123  		Providers:    testAccProviders,
   124  		CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
   125  		Steps: []resource.TestStep{
   126  			resource.TestStep{
   127  				Config: testAccAWSLaunchConfigurationWithEncryption,
   128  				Check: resource.ComposeTestCheckFunc(
   129  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.baz", &conf),
   130  
   131  					testAccCheckAWSLaunchConfigurationWithEncryption(&conf),
   132  				),
   133  			},
   134  		},
   135  	})
   136  }
   137  
   138  func testAccCheckAWSLaunchConfigurationGeneratedNamePrefix(
   139  	resource, prefix string) resource.TestCheckFunc {
   140  	return func(s *terraform.State) error {
   141  		r, ok := s.RootModule().Resources[resource]
   142  		if !ok {
   143  			return fmt.Errorf("Resource not found")
   144  		}
   145  		name, ok := r.Primary.Attributes["name"]
   146  		if !ok {
   147  			return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes)
   148  		}
   149  		if !strings.HasPrefix(name, prefix) {
   150  			return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix)
   151  		}
   152  		return nil
   153  	}
   154  }
   155  
   156  func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error {
   157  	conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
   158  
   159  	for _, rs := range s.RootModule().Resources {
   160  		if rs.Type != "aws_launch_configuration" {
   161  			continue
   162  		}
   163  
   164  		describe, err := conn.DescribeLaunchConfigurations(
   165  			&autoscaling.DescribeLaunchConfigurationsInput{
   166  				LaunchConfigurationNames: []*string{aws.String(rs.Primary.ID)},
   167  			})
   168  
   169  		if err == nil {
   170  			if len(describe.LaunchConfigurations) != 0 &&
   171  				*describe.LaunchConfigurations[0].LaunchConfigurationName == rs.Primary.ID {
   172  				return fmt.Errorf("Launch Configuration still exists")
   173  			}
   174  		}
   175  
   176  		// Verify the error
   177  		providerErr, ok := err.(awserr.Error)
   178  		if !ok {
   179  			return err
   180  		}
   181  		if providerErr.Code() != "InvalidLaunchConfiguration.NotFound" {
   182  			return err
   183  		}
   184  	}
   185  
   186  	return nil
   187  }
   188  
   189  func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
   190  	return func(s *terraform.State) error {
   191  		if *conf.ImageId != "ami-21f78e11" {
   192  			return fmt.Errorf("Bad image_id: %s", *conf.ImageId)
   193  		}
   194  
   195  		if !strings.HasPrefix(*conf.LaunchConfigurationName, "terraform-") {
   196  			return fmt.Errorf("Bad name: %s", *conf.LaunchConfigurationName)
   197  		}
   198  
   199  		if *conf.InstanceType != "m1.small" {
   200  			return fmt.Errorf("Bad instance_type: %s", *conf.InstanceType)
   201  		}
   202  
   203  		// Map out the block devices by name, which should be unique.
   204  		blockDevices := make(map[string]*autoscaling.BlockDeviceMapping)
   205  		for _, blockDevice := range conf.BlockDeviceMappings {
   206  			blockDevices[*blockDevice.DeviceName] = blockDevice
   207  		}
   208  
   209  		// Check if the root block device exists.
   210  		if _, ok := blockDevices["/dev/sda1"]; !ok {
   211  			return fmt.Errorf("block device doesn't exist: /dev/sda1")
   212  		}
   213  
   214  		// Check if the secondary block device exists.
   215  		if _, ok := blockDevices["/dev/sdb"]; !ok {
   216  			return fmt.Errorf("block device doesn't exist: /dev/sdb")
   217  		}
   218  
   219  		// Check if the third block device exists.
   220  		if _, ok := blockDevices["/dev/sdc"]; !ok {
   221  			return fmt.Errorf("block device doesn't exist: /dev/sdc")
   222  		}
   223  
   224  		// Check if the secondary block device exists.
   225  		if _, ok := blockDevices["/dev/sdb"]; !ok {
   226  			return fmt.Errorf("block device doesn't exist: /dev/sdb")
   227  		}
   228  
   229  		return nil
   230  	}
   231  }
   232  
   233  func testAccCheckAWSLaunchConfigurationExists(n string, res *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
   234  	return func(s *terraform.State) error {
   235  		rs, ok := s.RootModule().Resources[n]
   236  		if !ok {
   237  			return fmt.Errorf("Not found: %s", n)
   238  		}
   239  
   240  		if rs.Primary.ID == "" {
   241  			return fmt.Errorf("No Launch Configuration ID is set")
   242  		}
   243  
   244  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
   245  
   246  		describeOpts := autoscaling.DescribeLaunchConfigurationsInput{
   247  			LaunchConfigurationNames: []*string{aws.String(rs.Primary.ID)},
   248  		}
   249  		describe, err := conn.DescribeLaunchConfigurations(&describeOpts)
   250  
   251  		if err != nil {
   252  			return err
   253  		}
   254  
   255  		if len(describe.LaunchConfigurations) != 1 ||
   256  			*describe.LaunchConfigurations[0].LaunchConfigurationName != rs.Primary.ID {
   257  			return fmt.Errorf("Launch Configuration Group not found")
   258  		}
   259  
   260  		*res = *describe.LaunchConfigurations[0]
   261  
   262  		return nil
   263  	}
   264  }
   265  
   266  var testAccAWSLaunchConfigurationConfig = fmt.Sprintf(`
   267  resource "aws_launch_configuration" "bar" {
   268    name = "terraform-test-%d"
   269    image_id = "ami-21f78e11"
   270    instance_type = "m1.small"
   271    user_data = "foobar-user-data"
   272    associate_public_ip_address = true
   273  
   274  	root_block_device {
   275  		volume_type = "gp2"
   276  		volume_size = 11
   277  	}
   278  	ebs_block_device {
   279  		device_name = "/dev/sdb"
   280  		volume_size = 9
   281  	}
   282  	ebs_block_device {
   283  		device_name = "/dev/sdc"
   284  		volume_size = 10
   285  		volume_type = "io1"
   286  		iops = 100
   287  	}
   288  	ephemeral_block_device {
   289  		device_name = "/dev/sde"
   290  		virtual_name = "ephemeral0"
   291  	}
   292  }
   293  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
   294  
   295  var testAccAWSLaunchConfigurationWithSpotPriceConfig = fmt.Sprintf(`
   296  resource "aws_launch_configuration" "bar" {
   297    name = "terraform-test-%d"
   298    image_id = "ami-21f78e11"
   299    instance_type = "t1.micro"
   300    spot_price = "0.01"
   301  }
   302  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
   303  
   304  const testAccAWSLaunchConfigurationNoNameConfig = `
   305  resource "aws_launch_configuration" "bar" {
   306     image_id = "ami-21f78e11"
   307     instance_type = "t1.micro"
   308     user_data = "foobar-user-data-change"
   309     associate_public_ip_address = false
   310  }
   311  `
   312  
   313  const testAccAWSLaunchConfigurationPrefixNameConfig = `
   314  resource "aws_launch_configuration" "baz" {
   315     name_prefix = "baz-"
   316     image_id = "ami-21f78e11"
   317     instance_type = "t1.micro"
   318     user_data = "foobar-user-data-change"
   319     associate_public_ip_address = false
   320  }
   321  `
   322  
   323  const testAccAWSLaunchConfigurationWithEncryption = `
   324  resource "aws_launch_configuration" "baz" {
   325     image_id = "ami-5189a661"
   326     instance_type = "t2.micro"
   327     associate_public_ip_address = false
   328  
   329     	root_block_device {
   330     		volume_type = "gp2"
   331  		volume_size = 11
   332  	}
   333  	ebs_block_device {
   334  		device_name = "/dev/sdb"
   335  		volume_size = 9
   336  		encrypted = true
   337  	}
   338  }
   339  `