github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/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 len(v.SecurityGroups) == 0 {
    17  			return fmt.Errorf("no security groups: %#v", v.SecurityGroups)
    18  		}
    19  		if v.SecurityGroups[0].Name != "tf_test_foo" {
    20  			return fmt.Errorf("no security groups: %#v", v.SecurityGroups)
    21  		}
    22  
    23  		return nil
    24  	}
    25  
    26  	resource.Test(t, resource.TestCase{
    27  		PreCheck:     func() { testAccPreCheck(t) },
    28  		Providers:    testAccProviders,
    29  		CheckDestroy: testAccCheckInstanceDestroy,
    30  		Steps: []resource.TestStep{
    31  			resource.TestStep{
    32  				Config: testAccInstanceConfig,
    33  				Check: resource.ComposeTestCheckFunc(
    34  					testAccCheckInstanceExists(
    35  						"aws_instance.foo", &v),
    36  					testCheck,
    37  					resource.TestCheckResourceAttr(
    38  						"aws_instance.foo",
    39  						"user_data",
    40  						"0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"),
    41  				),
    42  			},
    43  
    44  			// We repeat the exact same test so that we can be sure
    45  			// that the user data hash stuff is working without generating
    46  			// an incorrect diff.
    47  			resource.TestStep{
    48  				Config: testAccInstanceConfig,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckInstanceExists(
    51  						"aws_instance.foo", &v),
    52  					testCheck,
    53  					resource.TestCheckResourceAttr(
    54  						"aws_instance.foo",
    55  						"user_data",
    56  						"0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func TestAccAWSInstance_sourceDestCheck(t *testing.T) {
    64  	var v ec2.Instance
    65  
    66  	testCheck := func(enabled bool) resource.TestCheckFunc {
    67  		return func(*terraform.State) error {
    68  			if v.SourceDestCheck != enabled {
    69  				return fmt.Errorf("bad source_dest_check: %#v", v.SourceDestCheck)
    70  			}
    71  
    72  			return nil
    73  		}
    74  	}
    75  
    76  	resource.Test(t, resource.TestCase{
    77  		PreCheck:     func() { testAccPreCheck(t) },
    78  		Providers:    testAccProviders,
    79  		CheckDestroy: testAccCheckInstanceDestroy,
    80  		Steps: []resource.TestStep{
    81  			resource.TestStep{
    82  				Config: testAccInstanceConfigSourceDest,
    83  				Check: resource.ComposeTestCheckFunc(
    84  					testAccCheckInstanceExists(
    85  						"aws_instance.foo", &v),
    86  					testCheck(true),
    87  				),
    88  			},
    89  
    90  			resource.TestStep{
    91  				Config: testAccInstanceConfigSourceDestDisable,
    92  				Check: resource.ComposeTestCheckFunc(
    93  					testAccCheckInstanceExists(
    94  						"aws_instance.foo", &v),
    95  					testCheck(false),
    96  				),
    97  			},
    98  		},
    99  	})
   100  }
   101  
   102  func TestAccAWSInstance_vpc(t *testing.T) {
   103  	var v ec2.Instance
   104  
   105  	resource.Test(t, resource.TestCase{
   106  		PreCheck:     func() { testAccPreCheck(t) },
   107  		Providers:    testAccProviders,
   108  		CheckDestroy: testAccCheckInstanceDestroy,
   109  		Steps: []resource.TestStep{
   110  			resource.TestStep{
   111  				Config: testAccInstanceConfigVPC,
   112  				Check: resource.ComposeTestCheckFunc(
   113  					testAccCheckInstanceExists(
   114  						"aws_instance.foo", &v),
   115  				),
   116  			},
   117  		},
   118  	})
   119  }
   120  
   121  func testAccCheckInstanceDestroy(s *terraform.State) error {
   122  	conn := testAccProvider.ec2conn
   123  
   124  	for _, rs := range s.Resources {
   125  		if rs.Type != "aws_instance" {
   126  			continue
   127  		}
   128  
   129  		// Try to find the resource
   130  		resp, err := conn.Instances(
   131  			[]string{rs.ID}, ec2.NewFilter())
   132  		if err == nil {
   133  			if len(resp.Reservations) > 0 {
   134  				return fmt.Errorf("still exist.")
   135  			}
   136  
   137  			return nil
   138  		}
   139  
   140  		// Verify the error is what we want
   141  		ec2err, ok := err.(*ec2.Error)
   142  		if !ok {
   143  			return err
   144  		}
   145  		if ec2err.Code != "InvalidInstanceID.NotFound" {
   146  			return err
   147  		}
   148  	}
   149  
   150  	return nil
   151  }
   152  
   153  func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc {
   154  	return func(s *terraform.State) error {
   155  		rs, ok := s.Resources[n]
   156  		if !ok {
   157  			return fmt.Errorf("Not found: %s", n)
   158  		}
   159  
   160  		if rs.ID == "" {
   161  			return fmt.Errorf("No ID is set")
   162  		}
   163  
   164  		conn := testAccProvider.ec2conn
   165  		resp, err := conn.Instances(
   166  			[]string{rs.ID}, ec2.NewFilter())
   167  		if err != nil {
   168  			return err
   169  		}
   170  		if len(resp.Reservations) == 0 {
   171  			return fmt.Errorf("Instance not found")
   172  		}
   173  
   174  		*i = resp.Reservations[0].Instances[0]
   175  
   176  		return nil
   177  	}
   178  }
   179  
   180  const testAccInstanceConfig = `
   181  resource "aws_security_group" "tf_test_foo" {
   182  	name = "tf_test_foo"
   183  	description = "foo"
   184  
   185  	ingress {
   186  		protocol = "icmp"
   187  		from_port = -1
   188  		to_port = -1
   189  		cidr_blocks = ["0.0.0.0/0"]
   190  	}
   191  }
   192  
   193  resource "aws_instance" "foo" {
   194  	# us-west-2
   195  	ami = "ami-4fccb37f"
   196  	instance_type = "m1.small"
   197  	security_groups = ["${aws_security_group.tf_test_foo.name}"]
   198  	user_data = "foo"
   199  }
   200  `
   201  
   202  const testAccInstanceConfigSourceDest = `
   203  resource "aws_vpc" "foo" {
   204  	cidr_block = "10.1.0.0/16"
   205  }
   206  
   207  resource "aws_subnet" "foo" {
   208  	cidr_block = "10.1.1.0/24"
   209  	vpc_id = "${aws_vpc.foo.id}"
   210  }
   211  
   212  resource "aws_instance" "foo" {
   213  	# us-west-2
   214  	ami = "ami-4fccb37f"
   215  	instance_type = "m1.small"
   216  	subnet_id = "${aws_subnet.foo.id}"
   217  	source_dest_check = true
   218  }
   219  `
   220  
   221  const testAccInstanceConfigSourceDestDisable = `
   222  resource "aws_vpc" "foo" {
   223  	cidr_block = "10.1.0.0/16"
   224  }
   225  
   226  resource "aws_subnet" "foo" {
   227  	cidr_block = "10.1.1.0/24"
   228  	vpc_id = "${aws_vpc.foo.id}"
   229  }
   230  
   231  resource "aws_instance" "foo" {
   232  	# us-west-2
   233  	ami = "ami-4fccb37f"
   234  	instance_type = "m1.small"
   235  	subnet_id = "${aws_subnet.foo.id}"
   236  	source_dest_check = false
   237  }
   238  `
   239  
   240  const testAccInstanceConfigVPC = `
   241  resource "aws_vpc" "foo" {
   242  	cidr_block = "10.1.0.0/16"
   243  }
   244  
   245  resource "aws_subnet" "foo" {
   246  	cidr_block = "10.1.1.0/24"
   247  	vpc_id = "${aws_vpc.foo.id}"
   248  }
   249  
   250  resource "aws_instance" "foo" {
   251  	# us-west-2
   252  	ami = "ami-4fccb37f"
   253  	instance_type = "m1.small"
   254  	subnet_id = "${aws_subnet.foo.id}"
   255  }
   256  `