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