github.com/lamielle/terraform@v0.3.2-0.20141121070651-81f008ba53d5/builtin/providers/aws/resource_aws_instance_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/goamz/ec2"
    10  )
    11  
    12  func TestAccAWSInstance_normal(t *testing.T) {
    13  	var v ec2.Instance
    14  
    15  	testCheck := func(*terraform.State) error {
    16  		if v.AvailZone != "us-west-2a" {
    17  			return fmt.Errorf("bad availability zone: %#v", v.AvailZone)
    18  		}
    19  
    20  		if len(v.SecurityGroups) == 0 {
    21  			return fmt.Errorf("no security groups: %#v", v.SecurityGroups)
    22  		}
    23  		if v.SecurityGroups[0].Name != "tf_test_foo" {
    24  			return fmt.Errorf("no security groups: %#v", v.SecurityGroups)
    25  		}
    26  
    27  		return nil
    28  	}
    29  
    30  	resource.Test(t, resource.TestCase{
    31  		PreCheck:     func() { testAccPreCheck(t) },
    32  		Providers:    testAccProviders,
    33  		CheckDestroy: testAccCheckInstanceDestroy,
    34  		Steps: []resource.TestStep{
    35  			resource.TestStep{
    36  				Config: testAccInstanceConfig,
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckInstanceExists(
    39  						"aws_instance.foo", &v),
    40  					testCheck,
    41  					resource.TestCheckResourceAttr(
    42  						"aws_instance.foo",
    43  						"user_data",
    44  						"0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"),
    45  				),
    46  			},
    47  
    48  			// We repeat the exact same test so that we can be sure
    49  			// that the user data hash stuff is working without generating
    50  			// an incorrect diff.
    51  			resource.TestStep{
    52  				Config: testAccInstanceConfig,
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccCheckInstanceExists(
    55  						"aws_instance.foo", &v),
    56  					testCheck,
    57  					resource.TestCheckResourceAttr(
    58  						"aws_instance.foo",
    59  						"user_data",
    60  						"0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"),
    61  				),
    62  			},
    63  		},
    64  	})
    65  }
    66  
    67  func TestAccAWSInstance_blockDevicesCheck(t *testing.T) {
    68  	var v ec2.Instance
    69  
    70  	testCheck := func() resource.TestCheckFunc {
    71  		return func(*terraform.State) error {
    72  
    73  			// Map out the block devices by name, which should be unique.
    74  			blockDevices := make(map[string]ec2.BlockDevice)
    75  			for _, blockDevice := range v.BlockDevices {
    76  				blockDevices[blockDevice.DeviceName] = blockDevice
    77  			}
    78  
    79  			// Check if the secondary block device exists.
    80  			if _, ok := blockDevices["/dev/sdb"]; !ok {
    81  				fmt.Errorf("block device doesn't exist: /dev/sdb")
    82  			}
    83  
    84  			return nil
    85  		}
    86  	}
    87  
    88  	resource.Test(t, resource.TestCase{
    89  		PreCheck:     func() { testAccPreCheck(t) },
    90  		Providers:    testAccProviders,
    91  		CheckDestroy: testAccCheckInstanceDestroy,
    92  		Steps: []resource.TestStep{
    93  			resource.TestStep{
    94  				Config: testAccInstanceConfigBlockDevices,
    95  				Check: resource.ComposeTestCheckFunc(
    96  					testAccCheckInstanceExists(
    97  						"aws_instance.foo", &v),
    98  					testCheck(),
    99  				),
   100  			},
   101  		},
   102  	})
   103  }
   104  
   105  func TestAccAWSInstance_sourceDestCheck(t *testing.T) {
   106  	var v ec2.Instance
   107  
   108  	testCheck := func(enabled bool) resource.TestCheckFunc {
   109  		return func(*terraform.State) error {
   110  			if v.SourceDestCheck != enabled {
   111  				return fmt.Errorf("bad source_dest_check: %#v", v.SourceDestCheck)
   112  			}
   113  
   114  			return nil
   115  		}
   116  	}
   117  
   118  	resource.Test(t, resource.TestCase{
   119  		PreCheck:     func() { testAccPreCheck(t) },
   120  		Providers:    testAccProviders,
   121  		CheckDestroy: testAccCheckInstanceDestroy,
   122  		Steps: []resource.TestStep{
   123  			resource.TestStep{
   124  				Config: testAccInstanceConfigSourceDest,
   125  				Check: resource.ComposeTestCheckFunc(
   126  					testAccCheckInstanceExists(
   127  						"aws_instance.foo", &v),
   128  					testCheck(true),
   129  				),
   130  			},
   131  
   132  			resource.TestStep{
   133  				Config: testAccInstanceConfigSourceDestDisable,
   134  				Check: resource.ComposeTestCheckFunc(
   135  					testAccCheckInstanceExists(
   136  						"aws_instance.foo", &v),
   137  					testCheck(false),
   138  				),
   139  			},
   140  		},
   141  	})
   142  }
   143  
   144  func TestAccAWSInstance_vpc(t *testing.T) {
   145  	var v ec2.Instance
   146  
   147  	resource.Test(t, resource.TestCase{
   148  		PreCheck:     func() { testAccPreCheck(t) },
   149  		Providers:    testAccProviders,
   150  		CheckDestroy: testAccCheckInstanceDestroy,
   151  		Steps: []resource.TestStep{
   152  			resource.TestStep{
   153  				Config: testAccInstanceConfigVPC,
   154  				Check: resource.ComposeTestCheckFunc(
   155  					testAccCheckInstanceExists(
   156  						"aws_instance.foo", &v),
   157  				),
   158  			},
   159  		},
   160  	})
   161  }
   162  
   163  func TestAccInstance_tags(t *testing.T) {
   164  	var v ec2.Instance
   165  
   166  	resource.Test(t, resource.TestCase{
   167  		PreCheck:     func() { testAccPreCheck(t) },
   168  		Providers:    testAccProviders,
   169  		CheckDestroy: testAccCheckInstanceDestroy,
   170  		Steps: []resource.TestStep{
   171  			resource.TestStep{
   172  				Config: testAccCheckInstanceConfigTags,
   173  				Check: resource.ComposeTestCheckFunc(
   174  					testAccCheckInstanceExists("aws_instance.foo", &v),
   175  					testAccCheckTags(&v.Tags, "foo", "bar"),
   176  				),
   177  			},
   178  
   179  			resource.TestStep{
   180  				Config: testAccCheckInstanceConfigTagsUpdate,
   181  				Check: resource.ComposeTestCheckFunc(
   182  					testAccCheckInstanceExists("aws_instance.foo", &v),
   183  					testAccCheckTags(&v.Tags, "foo", ""),
   184  					testAccCheckTags(&v.Tags, "bar", "baz"),
   185  				),
   186  			},
   187  		},
   188  	})
   189  }
   190  
   191  func testAccCheckInstanceDestroy(s *terraform.State) error {
   192  	conn := testAccProvider.ec2conn
   193  
   194  	for _, rs := range s.RootModule().Resources {
   195  		if rs.Type != "aws_instance" {
   196  			continue
   197  		}
   198  
   199  		// Try to find the resource
   200  		resp, err := conn.Instances(
   201  			[]string{rs.Primary.ID}, ec2.NewFilter())
   202  		if err == nil {
   203  			if len(resp.Reservations) > 0 {
   204  				return fmt.Errorf("still exist.")
   205  			}
   206  
   207  			return nil
   208  		}
   209  
   210  		// Verify the error is what we want
   211  		ec2err, ok := err.(*ec2.Error)
   212  		if !ok {
   213  			return err
   214  		}
   215  		if ec2err.Code != "InvalidInstanceID.NotFound" {
   216  			return err
   217  		}
   218  	}
   219  
   220  	return nil
   221  }
   222  
   223  func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc {
   224  	return func(s *terraform.State) error {
   225  		rs, ok := s.RootModule().Resources[n]
   226  		if !ok {
   227  			return fmt.Errorf("Not found: %s", n)
   228  		}
   229  
   230  		if rs.Primary.ID == "" {
   231  			return fmt.Errorf("No ID is set")
   232  		}
   233  
   234  		conn := testAccProvider.ec2conn
   235  		resp, err := conn.Instances(
   236  			[]string{rs.Primary.ID}, ec2.NewFilter())
   237  		if err != nil {
   238  			return err
   239  		}
   240  		if len(resp.Reservations) == 0 {
   241  			return fmt.Errorf("Instance not found")
   242  		}
   243  
   244  		*i = resp.Reservations[0].Instances[0]
   245  
   246  		return nil
   247  	}
   248  }
   249  
   250  const testAccInstanceConfig = `
   251  resource "aws_security_group" "tf_test_foo" {
   252  	name = "tf_test_foo"
   253  	description = "foo"
   254  
   255  	ingress {
   256  		protocol = "icmp"
   257  		from_port = -1
   258  		to_port = -1
   259  		cidr_blocks = ["0.0.0.0/0"]
   260  	}
   261  }
   262  
   263  resource "aws_instance" "foo" {
   264  	# us-west-2
   265  	ami = "ami-4fccb37f"
   266  	availability_zone = "us-west-2a"
   267  
   268  	instance_type = "m1.small"
   269  	security_groups = ["${aws_security_group.tf_test_foo.name}"]
   270  	user_data = "foo"
   271  }
   272  `
   273  
   274  const testAccInstanceConfigBlockDevices = `
   275  resource "aws_instance" "foo" {
   276  	# us-west-2
   277  	ami = "ami-55a7ea65"
   278  	instance_type = "m1.small"
   279  	block_device {
   280  	  device_name = "/dev/sdb"
   281  	  volume_type = "gp2"
   282  	  volume_size = 10
   283  	}
   284  }
   285  `
   286  
   287  const testAccInstanceConfigSourceDest = `
   288  resource "aws_vpc" "foo" {
   289  	cidr_block = "10.1.0.0/16"
   290  }
   291  
   292  resource "aws_subnet" "foo" {
   293  	cidr_block = "10.1.1.0/24"
   294  	vpc_id = "${aws_vpc.foo.id}"
   295  }
   296  
   297  resource "aws_instance" "foo" {
   298  	# us-west-2
   299  	ami = "ami-4fccb37f"
   300  	instance_type = "m1.small"
   301  	subnet_id = "${aws_subnet.foo.id}"
   302  	source_dest_check = true
   303  }
   304  `
   305  
   306  const testAccInstanceConfigSourceDestDisable = `
   307  resource "aws_vpc" "foo" {
   308  	cidr_block = "10.1.0.0/16"
   309  }
   310  
   311  resource "aws_subnet" "foo" {
   312  	cidr_block = "10.1.1.0/24"
   313  	vpc_id = "${aws_vpc.foo.id}"
   314  }
   315  
   316  resource "aws_instance" "foo" {
   317  	# us-west-2
   318  	ami = "ami-4fccb37f"
   319  	instance_type = "m1.small"
   320  	subnet_id = "${aws_subnet.foo.id}"
   321  	source_dest_check = false
   322  }
   323  `
   324  
   325  const testAccInstanceConfigVPC = `
   326  resource "aws_vpc" "foo" {
   327  	cidr_block = "10.1.0.0/16"
   328  }
   329  
   330  resource "aws_subnet" "foo" {
   331  	cidr_block = "10.1.1.0/24"
   332  	vpc_id = "${aws_vpc.foo.id}"
   333  }
   334  
   335  resource "aws_instance" "foo" {
   336  	# us-west-2
   337  	ami = "ami-4fccb37f"
   338  	instance_type = "m1.small"
   339  	subnet_id = "${aws_subnet.foo.id}"
   340  	associate_public_ip_address = true
   341  }
   342  `
   343  
   344  const testAccCheckInstanceConfigTags = `
   345  resource "aws_instance" "foo" {
   346  	tags {
   347  		foo = "bar"
   348  	}
   349  }
   350  `
   351  
   352  const testAccCheckInstanceConfigTagsUpdate = `
   353  resource "aws_instance" "foo" {
   354  	tags {
   355  		bar = "baz"
   356  	}
   357  }
   358  `