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