2014-07-14 22:28:00 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2014-12-03 11:28:51 +01:00
|
|
|
"reflect"
|
2014-07-14 22:28:00 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-12-03 11:28:51 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-14 22:28:00 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
)
|
|
|
|
|
2014-07-15 06:56:37 +02:00
|
|
|
func TestAccAWSInstance_normal(t *testing.T) {
|
2014-07-14 22:28:00 +02:00
|
|
|
var v ec2.Instance
|
|
|
|
|
2014-07-15 06:56:37 +02:00
|
|
|
testCheck := func(*terraform.State) error {
|
2014-08-01 02:35:31 +02:00
|
|
|
if v.AvailZone != "us-west-2a" {
|
|
|
|
return fmt.Errorf("bad availability zone: %#v", v.AvailZone)
|
|
|
|
}
|
|
|
|
|
2014-07-15 06:56:37 +02:00
|
|
|
if len(v.SecurityGroups) == 0 {
|
|
|
|
return fmt.Errorf("no security groups: %#v", v.SecurityGroups)
|
|
|
|
}
|
|
|
|
if v.SecurityGroups[0].Name != "tf_test_foo" {
|
|
|
|
return fmt.Errorf("no security groups: %#v", v.SecurityGroups)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:28:00 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists(
|
|
|
|
"aws_instance.foo", &v),
|
2014-07-15 06:56:37 +02:00
|
|
|
testCheck,
|
2014-07-16 18:01:56 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo",
|
2014-07-17 01:41:01 +02:00
|
|
|
"user_data",
|
|
|
|
"0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
// We repeat the exact same test so that we can be sure
|
|
|
|
// that the user data hash stuff is working without generating
|
|
|
|
// an incorrect diff.
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists(
|
|
|
|
"aws_instance.foo", &v),
|
|
|
|
testCheck,
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo",
|
|
|
|
"user_data",
|
2014-07-16 18:01:56 +02:00
|
|
|
"0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"),
|
2014-07-14 22:28:00 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-10-31 21:25:16 +01:00
|
|
|
func TestAccAWSInstance_blockDevicesCheck(t *testing.T) {
|
|
|
|
var v ec2.Instance
|
|
|
|
|
|
|
|
testCheck := func() resource.TestCheckFunc {
|
|
|
|
return func(*terraform.State) error {
|
|
|
|
|
|
|
|
// Map out the block devices by name, which should be unique.
|
|
|
|
blockDevices := make(map[string]ec2.BlockDevice)
|
|
|
|
for _, blockDevice := range v.BlockDevices {
|
|
|
|
blockDevices[blockDevice.DeviceName] = blockDevice
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the secondary block device exists.
|
|
|
|
if _, ok := blockDevices["/dev/sdb"]; !ok {
|
|
|
|
fmt.Errorf("block device doesn't exist: /dev/sdb")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigBlockDevices,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists(
|
|
|
|
"aws_instance.foo", &v),
|
|
|
|
testCheck(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-14 23:16:59 +02:00
|
|
|
func TestAccAWSInstance_sourceDestCheck(t *testing.T) {
|
|
|
|
var v ec2.Instance
|
|
|
|
|
2014-07-15 02:38:39 +02:00
|
|
|
testCheck := func(enabled bool) resource.TestCheckFunc {
|
|
|
|
return func(*terraform.State) error {
|
|
|
|
if v.SourceDestCheck != enabled {
|
|
|
|
return fmt.Errorf("bad source_dest_check: %#v", v.SourceDestCheck)
|
|
|
|
}
|
2014-07-14 23:16:59 +02:00
|
|
|
|
2014-07-15 02:38:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-07-14 23:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigSourceDest,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists(
|
|
|
|
"aws_instance.foo", &v),
|
2014-07-15 02:38:39 +02:00
|
|
|
testCheck(true),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigSourceDestDisable,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists(
|
|
|
|
"aws_instance.foo", &v),
|
|
|
|
testCheck(false),
|
2014-07-14 23:16:59 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:46:32 +02:00
|
|
|
func TestAccAWSInstance_vpc(t *testing.T) {
|
|
|
|
var v ec2.Instance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigVPC,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists(
|
|
|
|
"aws_instance.foo", &v),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-10-13 22:55:59 +02:00
|
|
|
func TestAccInstance_tags(t *testing.T) {
|
|
|
|
var v ec2.Instance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckInstanceConfigTags,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
|
|
|
testAccCheckTags(&v.Tags, "foo", "bar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckInstanceConfigTagsUpdate,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
|
|
|
testAccCheckTags(&v.Tags, "foo", ""),
|
|
|
|
testAccCheckTags(&v.Tags, "bar", "baz"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:28:00 +02:00
|
|
|
func testAccCheckInstanceDestroy(s *terraform.State) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2014-07-14 22:28:00 +02:00
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-14 22:28:00 +02:00
|
|
|
if rs.Type != "aws_instance" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the resource
|
|
|
|
resp, err := conn.Instances(
|
2014-09-17 02:44:42 +02:00
|
|
|
[]string{rs.Primary.ID}, ec2.NewFilter())
|
2014-07-14 22:28:00 +02:00
|
|
|
if err == nil {
|
|
|
|
if len(resp.Reservations) > 0 {
|
|
|
|
return fmt.Errorf("still exist.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error is what we want
|
|
|
|
ec2err, ok := err.(*ec2.Error)
|
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ec2err.Code != "InvalidInstanceID.NotFound" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-14 22:28:00 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-14 22:28:00 +02:00
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2014-07-14 22:28:00 +02:00
|
|
|
resp, err := conn.Instances(
|
2014-09-17 02:44:42 +02:00
|
|
|
[]string{rs.Primary.ID}, ec2.NewFilter())
|
2014-07-14 22:28:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(resp.Reservations) == 0 {
|
|
|
|
return fmt.Errorf("Instance not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*i = resp.Reservations[0].Instances[0]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 11:28:51 +01:00
|
|
|
func TestInstanceTenancySchema(t *testing.T) {
|
|
|
|
actualSchema := resourceAwsInstance().Schema["tenancy"]
|
|
|
|
expectedSchema := &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actualSchema, expectedSchema ) {
|
|
|
|
t.Fatalf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
|
|
|
actualSchema,
|
|
|
|
expectedSchema)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:28:00 +02:00
|
|
|
const testAccInstanceConfig = `
|
2014-07-15 06:56:37 +02:00
|
|
|
resource "aws_security_group" "tf_test_foo" {
|
|
|
|
name = "tf_test_foo"
|
|
|
|
description = "foo"
|
2014-07-16 18:01:56 +02:00
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "icmp"
|
|
|
|
from_port = -1
|
|
|
|
to_port = -1
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
2014-07-15 06:56:37 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 22:28:00 +02:00
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-4fccb37f"
|
2014-08-01 02:35:31 +02:00
|
|
|
availability_zone = "us-west-2a"
|
|
|
|
|
2014-07-14 22:28:00 +02:00
|
|
|
instance_type = "m1.small"
|
2014-07-15 06:56:37 +02:00
|
|
|
security_groups = ["${aws_security_group.tf_test_foo.name}"]
|
2014-07-16 18:01:56 +02:00
|
|
|
user_data = "foo"
|
2014-07-14 22:28:00 +02:00
|
|
|
}
|
|
|
|
`
|
2014-07-14 22:46:32 +02:00
|
|
|
|
2014-10-31 21:25:16 +01:00
|
|
|
const testAccInstanceConfigBlockDevices = `
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-55a7ea65"
|
|
|
|
instance_type = "m1.small"
|
|
|
|
block_device {
|
|
|
|
device_name = "/dev/sdb"
|
|
|
|
volume_type = "gp2"
|
|
|
|
volume_size = 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-07-14 23:16:59 +02:00
|
|
|
const testAccInstanceConfigSourceDest = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
source_dest_check = true
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-07-15 02:38:39 +02:00
|
|
|
const testAccInstanceConfigSourceDestDisable = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
source_dest_check = false
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-07-14 22:46:32 +02:00
|
|
|
const testAccInstanceConfigVPC = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
2014-07-29 14:06:53 +02:00
|
|
|
associate_public_ip_address = true
|
2014-11-04 12:08:30 +01:00
|
|
|
tenancy = "dedicated"
|
2014-07-14 22:46:32 +02:00
|
|
|
}
|
|
|
|
`
|
2014-10-13 22:55:59 +02:00
|
|
|
|
|
|
|
const testAccCheckInstanceConfigTags = `
|
|
|
|
resource "aws_instance" "foo" {
|
2014-11-24 21:22:18 +01:00
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
2014-10-13 22:55:59 +02:00
|
|
|
tags {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccCheckInstanceConfigTagsUpdate = `
|
2014-10-14 21:15:46 +02:00
|
|
|
resource "aws_instance" "foo" {
|
2014-11-24 21:22:18 +01:00
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
2014-10-13 22:55:59 +02:00
|
|
|
tags {
|
|
|
|
bar = "baz"
|
|
|
|
}
|
|
|
|
}
|
2014-10-14 21:20:39 +02:00
|
|
|
`
|