github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/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/aws/aws-sdk-go/service/ec2"
    14  	"github.com/hashicorp/terraform/helper/acctest"
    15  	"github.com/hashicorp/terraform/helper/resource"
    16  	"github.com/hashicorp/terraform/terraform"
    17  )
    18  
    19  func TestAccAWSLaunchConfiguration_basic(t *testing.T) {
    20  	var conf autoscaling.LaunchConfiguration
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
    26  		Steps: []resource.TestStep{
    27  			{
    28  				Config: testAccAWSLaunchConfigurationNoNameConfig,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
    31  					testAccCheckAWSLaunchConfigurationGeneratedNamePrefix(
    32  						"aws_launch_configuration.bar", "terraform-"),
    33  				),
    34  			},
    35  			{
    36  				Config: testAccAWSLaunchConfigurationPrefixNameConfig,
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.baz", &conf),
    39  					testAccCheckAWSLaunchConfigurationGeneratedNamePrefix(
    40  						"aws_launch_configuration.baz", "baz-"),
    41  				),
    42  			},
    43  		},
    44  	})
    45  }
    46  
    47  func TestAccAWSLaunchConfiguration_withBlockDevices(t *testing.T) {
    48  	var conf autoscaling.LaunchConfiguration
    49  
    50  	resource.Test(t, resource.TestCase{
    51  		PreCheck:     func() { testAccPreCheck(t) },
    52  		Providers:    testAccProviders,
    53  		CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
    54  		Steps: []resource.TestStep{
    55  			{
    56  				Config: testAccAWSLaunchConfigurationConfig,
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
    59  					testAccCheckAWSLaunchConfigurationAttributes(&conf),
    60  					resource.TestCheckResourceAttr(
    61  						"aws_launch_configuration.bar", "image_id", "ami-21f78e11"),
    62  					resource.TestCheckResourceAttr(
    63  						"aws_launch_configuration.bar", "instance_type", "m1.small"),
    64  					resource.TestCheckResourceAttr(
    65  						"aws_launch_configuration.bar", "associate_public_ip_address", "true"),
    66  					resource.TestCheckResourceAttr(
    67  						"aws_launch_configuration.bar", "spot_price", ""),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func TestAccAWSLaunchConfiguration_updateRootBlockDevice(t *testing.T) {
    75  	var conf autoscaling.LaunchConfiguration
    76  	rInt := acctest.RandInt()
    77  
    78  	resource.Test(t, resource.TestCase{
    79  		PreCheck:     func() { testAccPreCheck(t) },
    80  		Providers:    testAccProviders,
    81  		CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
    82  		Steps: []resource.TestStep{
    83  			{
    84  				Config: testAccAWSLaunchConfigurationConfigWithRootBlockDevice(rInt),
    85  				Check: resource.ComposeTestCheckFunc(
    86  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
    87  					resource.TestCheckResourceAttr(
    88  						"aws_launch_configuration.bar", "root_block_device.0.volume_size", "11"),
    89  				),
    90  			},
    91  			{
    92  				Config: testAccAWSLaunchConfigurationConfigWithRootBlockDeviceUpdated(rInt),
    93  				Check: resource.ComposeTestCheckFunc(
    94  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
    95  					resource.TestCheckResourceAttr(
    96  						"aws_launch_configuration.bar", "root_block_device.0.volume_size", "20"),
    97  				),
    98  			},
    99  		},
   100  	})
   101  }
   102  
   103  func TestAccAWSLaunchConfiguration_withSpotPrice(t *testing.T) {
   104  	var conf autoscaling.LaunchConfiguration
   105  
   106  	resource.Test(t, resource.TestCase{
   107  		PreCheck:     func() { testAccPreCheck(t) },
   108  		Providers:    testAccProviders,
   109  		CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
   110  		Steps: []resource.TestStep{
   111  			{
   112  				Config: testAccAWSLaunchConfigurationWithSpotPriceConfig,
   113  				Check: resource.ComposeTestCheckFunc(
   114  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
   115  					resource.TestCheckResourceAttr(
   116  						"aws_launch_configuration.bar", "spot_price", "0.01"),
   117  				),
   118  			},
   119  		},
   120  	})
   121  }
   122  
   123  func TestAccAWSLaunchConfiguration_withVpcClassicLink(t *testing.T) {
   124  	var vpc ec2.Vpc
   125  	var group ec2.SecurityGroup
   126  	var conf autoscaling.LaunchConfiguration
   127  
   128  	resource.Test(t, resource.TestCase{
   129  		PreCheck:     func() { testAccPreCheck(t) },
   130  		Providers:    testAccProviders,
   131  		CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
   132  		Steps: []resource.TestStep{
   133  			{
   134  				Config: testAccAWSLaunchConfigurationConfig_withVpcClassicLink,
   135  				Check: resource.ComposeTestCheckFunc(
   136  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.foo", &conf),
   137  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
   138  					testAccCheckAWSSecurityGroupExists("aws_security_group.foo", &group),
   139  				),
   140  			},
   141  		},
   142  	})
   143  }
   144  
   145  func TestAccAWSLaunchConfiguration_withIAMProfile(t *testing.T) {
   146  	var conf autoscaling.LaunchConfiguration
   147  
   148  	resource.Test(t, resource.TestCase{
   149  		PreCheck:     func() { testAccPreCheck(t) },
   150  		Providers:    testAccProviders,
   151  		CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
   152  		Steps: []resource.TestStep{
   153  			{
   154  				Config: testAccAWSLaunchConfigurationConfig_withIAMProfile,
   155  				Check: resource.ComposeTestCheckFunc(
   156  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
   157  				),
   158  			},
   159  		},
   160  	})
   161  }
   162  
   163  func testAccCheckAWSLaunchConfigurationWithEncryption(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
   164  	return func(s *terraform.State) error {
   165  		// Map out the block devices by name, which should be unique.
   166  		blockDevices := make(map[string]*autoscaling.BlockDeviceMapping)
   167  		for _, blockDevice := range conf.BlockDeviceMappings {
   168  			blockDevices[*blockDevice.DeviceName] = blockDevice
   169  		}
   170  
   171  		// Check if the root block device exists.
   172  		if _, ok := blockDevices["/dev/sda1"]; !ok {
   173  			return fmt.Errorf("block device doesn't exist: /dev/sda1")
   174  		} else if blockDevices["/dev/sda1"].Ebs.Encrypted != nil {
   175  			return fmt.Errorf("root device should not include value for Encrypted")
   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  		} else if !*blockDevices["/dev/sdb"].Ebs.Encrypted {
   182  			return fmt.Errorf("block device isn't encrypted as expected: /dev/sdb")
   183  		}
   184  
   185  		return nil
   186  	}
   187  }
   188  
   189  func TestAccAWSLaunchConfiguration_withEncryption(t *testing.T) {
   190  	var conf autoscaling.LaunchConfiguration
   191  
   192  	resource.Test(t, resource.TestCase{
   193  		PreCheck:     func() { testAccPreCheck(t) },
   194  		Providers:    testAccProviders,
   195  		CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
   196  		Steps: []resource.TestStep{
   197  			{
   198  				Config: testAccAWSLaunchConfigurationWithEncryption,
   199  				Check: resource.ComposeTestCheckFunc(
   200  					testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.baz", &conf),
   201  
   202  					testAccCheckAWSLaunchConfigurationWithEncryption(&conf),
   203  				),
   204  			},
   205  		},
   206  	})
   207  }
   208  
   209  func testAccCheckAWSLaunchConfigurationGeneratedNamePrefix(
   210  	resource, prefix string) resource.TestCheckFunc {
   211  	return func(s *terraform.State) error {
   212  		r, ok := s.RootModule().Resources[resource]
   213  		if !ok {
   214  			return fmt.Errorf("Resource not found")
   215  		}
   216  		name, ok := r.Primary.Attributes["name"]
   217  		if !ok {
   218  			return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes)
   219  		}
   220  		if !strings.HasPrefix(name, prefix) {
   221  			return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix)
   222  		}
   223  		return nil
   224  	}
   225  }
   226  
   227  func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error {
   228  	conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
   229  
   230  	for _, rs := range s.RootModule().Resources {
   231  		if rs.Type != "aws_launch_configuration" {
   232  			continue
   233  		}
   234  
   235  		describe, err := conn.DescribeLaunchConfigurations(
   236  			&autoscaling.DescribeLaunchConfigurationsInput{
   237  				LaunchConfigurationNames: []*string{aws.String(rs.Primary.ID)},
   238  			})
   239  
   240  		if err == nil {
   241  			if len(describe.LaunchConfigurations) != 0 &&
   242  				*describe.LaunchConfigurations[0].LaunchConfigurationName == rs.Primary.ID {
   243  				return fmt.Errorf("Launch Configuration still exists")
   244  			}
   245  		}
   246  
   247  		// Verify the error
   248  		providerErr, ok := err.(awserr.Error)
   249  		if !ok {
   250  			return err
   251  		}
   252  		if providerErr.Code() != "InvalidLaunchConfiguration.NotFound" {
   253  			return err
   254  		}
   255  	}
   256  
   257  	return nil
   258  }
   259  
   260  func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
   261  	return func(s *terraform.State) error {
   262  		if *conf.ImageId != "ami-21f78e11" {
   263  			return fmt.Errorf("Bad image_id: %s", *conf.ImageId)
   264  		}
   265  
   266  		if !strings.HasPrefix(*conf.LaunchConfigurationName, "terraform-") {
   267  			return fmt.Errorf("Bad name: %s", *conf.LaunchConfigurationName)
   268  		}
   269  
   270  		if *conf.InstanceType != "m1.small" {
   271  			return fmt.Errorf("Bad instance_type: %s", *conf.InstanceType)
   272  		}
   273  
   274  		// Map out the block devices by name, which should be unique.
   275  		blockDevices := make(map[string]*autoscaling.BlockDeviceMapping)
   276  		for _, blockDevice := range conf.BlockDeviceMappings {
   277  			blockDevices[*blockDevice.DeviceName] = blockDevice
   278  		}
   279  
   280  		// Check if the root block device exists.
   281  		if _, ok := blockDevices["/dev/sda1"]; !ok {
   282  			return fmt.Errorf("block device doesn't exist: /dev/sda1")
   283  		}
   284  
   285  		// Check if the secondary block device exists.
   286  		if _, ok := blockDevices["/dev/sdb"]; !ok {
   287  			return fmt.Errorf("block device doesn't exist: /dev/sdb")
   288  		}
   289  
   290  		// Check if the third block device exists.
   291  		if _, ok := blockDevices["/dev/sdc"]; !ok {
   292  			return fmt.Errorf("block device doesn't exist: /dev/sdc")
   293  		}
   294  
   295  		// Check if the secondary block device exists.
   296  		if _, ok := blockDevices["/dev/sdb"]; !ok {
   297  			return fmt.Errorf("block device doesn't exist: /dev/sdb")
   298  		}
   299  
   300  		return nil
   301  	}
   302  }
   303  
   304  func testAccCheckAWSLaunchConfigurationExists(n string, res *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
   305  	return func(s *terraform.State) error {
   306  		rs, ok := s.RootModule().Resources[n]
   307  		if !ok {
   308  			return fmt.Errorf("Not found: %s", n)
   309  		}
   310  
   311  		if rs.Primary.ID == "" {
   312  			return fmt.Errorf("No Launch Configuration ID is set")
   313  		}
   314  
   315  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
   316  
   317  		describeOpts := autoscaling.DescribeLaunchConfigurationsInput{
   318  			LaunchConfigurationNames: []*string{aws.String(rs.Primary.ID)},
   319  		}
   320  		describe, err := conn.DescribeLaunchConfigurations(&describeOpts)
   321  
   322  		if err != nil {
   323  			return err
   324  		}
   325  
   326  		if len(describe.LaunchConfigurations) != 1 ||
   327  			*describe.LaunchConfigurations[0].LaunchConfigurationName != rs.Primary.ID {
   328  			return fmt.Errorf("Launch Configuration Group not found")
   329  		}
   330  
   331  		*res = *describe.LaunchConfigurations[0]
   332  
   333  		return nil
   334  	}
   335  }
   336  
   337  func testAccAWSLaunchConfigurationConfigWithRootBlockDevice(rInt int) string {
   338  	return fmt.Sprintf(`
   339  resource "aws_launch_configuration" "bar" {
   340    name_prefix = "tf-acc-test-%d"
   341    image_id = "ami-21f78e11"
   342    instance_type = "m1.small"
   343    user_data = "foobar-user-data"
   344    associate_public_ip_address = true
   345  
   346  	root_block_device {
   347  		volume_type = "gp2"
   348  		volume_size = 11
   349  	}
   350  
   351  }
   352  `, rInt)
   353  }
   354  
   355  func testAccAWSLaunchConfigurationConfigWithRootBlockDeviceUpdated(rInt int) string {
   356  	return fmt.Sprintf(`
   357  resource "aws_launch_configuration" "bar" {
   358    name_prefix = "tf-acc-test-%d"
   359    image_id = "ami-21f78e11"
   360    instance_type = "m1.small"
   361    user_data = "foobar-user-data"
   362    associate_public_ip_address = true
   363  
   364  	root_block_device {
   365  		volume_type = "gp2"
   366  		volume_size = 20
   367  	}
   368  
   369  }
   370  `, rInt)
   371  }
   372  
   373  var testAccAWSLaunchConfigurationConfig = fmt.Sprintf(`
   374  resource "aws_launch_configuration" "bar" {
   375    name = "terraform-test-%d"
   376    image_id = "ami-21f78e11"
   377    instance_type = "m1.small"
   378    user_data = "foobar-user-data"
   379    associate_public_ip_address = true
   380  
   381  	root_block_device {
   382  		volume_type = "gp2"
   383  		volume_size = 11
   384  	}
   385  	ebs_block_device {
   386  		device_name = "/dev/sdb"
   387  		volume_size = 9
   388  	}
   389  	ebs_block_device {
   390  		device_name = "/dev/sdc"
   391  		volume_size = 10
   392  		volume_type = "io1"
   393  		iops = 100
   394  	}
   395  	ephemeral_block_device {
   396  		device_name = "/dev/sde"
   397  		virtual_name = "ephemeral0"
   398  	}
   399  }
   400  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
   401  
   402  var testAccAWSLaunchConfigurationWithSpotPriceConfig = fmt.Sprintf(`
   403  resource "aws_launch_configuration" "bar" {
   404    name = "terraform-test-%d"
   405    image_id = "ami-21f78e11"
   406    instance_type = "t1.micro"
   407    spot_price = "0.01"
   408  }
   409  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
   410  
   411  const testAccAWSLaunchConfigurationNoNameConfig = `
   412  resource "aws_launch_configuration" "bar" {
   413     image_id = "ami-21f78e11"
   414     instance_type = "t1.micro"
   415     user_data = "foobar-user-data-change"
   416     associate_public_ip_address = false
   417  }
   418  `
   419  
   420  const testAccAWSLaunchConfigurationPrefixNameConfig = `
   421  resource "aws_launch_configuration" "baz" {
   422     name_prefix = "baz-"
   423     image_id = "ami-21f78e11"
   424     instance_type = "t1.micro"
   425     user_data = "foobar-user-data-change"
   426     associate_public_ip_address = false
   427  }
   428  `
   429  
   430  const testAccAWSLaunchConfigurationWithEncryption = `
   431  resource "aws_launch_configuration" "baz" {
   432     image_id = "ami-5189a661"
   433     instance_type = "t2.micro"
   434     associate_public_ip_address = false
   435  
   436     	root_block_device {
   437     		volume_type = "gp2"
   438  		volume_size = 11
   439  	}
   440  	ebs_block_device {
   441  		device_name = "/dev/sdb"
   442  		volume_size = 9
   443  		encrypted = true
   444  	}
   445  }
   446  `
   447  const testAccAWSLaunchConfigurationConfig_withVpcClassicLink = `
   448  resource "aws_vpc" "foo" {
   449     cidr_block = "10.0.0.0/16"
   450     enable_classiclink = true
   451  	tags {
   452  		Name = "testAccAWSLaunchConfigurationConfig_withVpcClassicLink"
   453  	}
   454  }
   455  
   456  resource "aws_security_group" "foo" {
   457    name = "foo"
   458    vpc_id = "${aws_vpc.foo.id}"
   459  }
   460  
   461  resource "aws_launch_configuration" "foo" {
   462     name = "TestAccAWSLaunchConfiguration_withVpcClassicLink"
   463     image_id = "ami-21f78e11"
   464     instance_type = "t1.micro"
   465  
   466     vpc_classic_link_id = "${aws_vpc.foo.id}"
   467     vpc_classic_link_security_groups = ["${aws_security_group.foo.id}"]
   468  }
   469  `
   470  
   471  const testAccAWSLaunchConfigurationConfig_withIAMProfile = `
   472  resource "aws_iam_role" "role" {
   473  	name  = "TestAccAWSLaunchConfiguration-withIAMProfile"
   474    assume_role_policy = <<EOF
   475  {
   476    "Version": "2012-10-17",
   477    "Statement": [
   478      {
   479        "Action": "sts:AssumeRole",
   480        "Principal": {
   481          "Service": "ec2.amazonaws.com"
   482        },
   483        "Effect": "Allow",
   484        "Sid": ""
   485      }
   486    ]
   487  }
   488  EOF
   489  }
   490  
   491  resource "aws_iam_instance_profile" "profile" {
   492  	name  = "TestAccAWSLaunchConfiguration-withIAMProfile"
   493  	roles = ["${aws_iam_role.role.name}"]
   494  }
   495  
   496  resource "aws_launch_configuration" "bar" {
   497  	image_id             = "ami-5189a661"
   498  	instance_type        = "t2.nano"
   499  	iam_instance_profile = "${aws_iam_instance_profile.profile.name}"
   500  }
   501  `