github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_vpc_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 TestAccVpc_basic(t *testing.T) {
    13  	var vpc ec2.VPC
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckVpcDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccVpcConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
    24  					testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
    25  					resource.TestCheckResourceAttr(
    26  						"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccVpc_dedicatedTenancy(t *testing.T) {
    34  	var vpc ec2.VPC
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckVpcDestroy,
    40  		Steps: []resource.TestStep{
    41  			resource.TestStep{
    42  				Config: testAccVpcDedicatedConfig,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckVpcExists("aws_vpc.bar", &vpc),
    45  					resource.TestCheckResourceAttr(
    46  						"aws_vpc.bar", "instance_tenancy", "dedicated"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func TestAccVpc_tags(t *testing.T) {
    54  	var vpc ec2.VPC
    55  
    56  	resource.Test(t, resource.TestCase{
    57  		PreCheck:     func() { testAccPreCheck(t) },
    58  		Providers:    testAccProviders,
    59  		CheckDestroy: testAccCheckVpcDestroy,
    60  		Steps: []resource.TestStep{
    61  			resource.TestStep{
    62  				Config: testAccVpcConfigTags,
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
    65  					testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
    66  					resource.TestCheckResourceAttr(
    67  						"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
    68  					testAccCheckTags(&vpc.Tags, "foo", "bar"),
    69  				),
    70  			},
    71  
    72  			resource.TestStep{
    73  				Config: testAccVpcConfigTagsUpdate,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
    76  					testAccCheckTags(&vpc.Tags, "foo", ""),
    77  					testAccCheckTags(&vpc.Tags, "bar", "baz"),
    78  				),
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func TestAccVpcUpdate(t *testing.T) {
    85  	var vpc ec2.VPC
    86  
    87  	resource.Test(t, resource.TestCase{
    88  		PreCheck:     func() { testAccPreCheck(t) },
    89  		Providers:    testAccProviders,
    90  		CheckDestroy: testAccCheckVpcDestroy,
    91  		Steps: []resource.TestStep{
    92  			resource.TestStep{
    93  				Config: testAccVpcConfig,
    94  				Check: resource.ComposeTestCheckFunc(
    95  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
    96  					testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
    97  					resource.TestCheckResourceAttr(
    98  						"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
    99  				),
   100  			},
   101  			resource.TestStep{
   102  				Config: testAccVpcConfigUpdate,
   103  				Check: resource.ComposeTestCheckFunc(
   104  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
   105  					resource.TestCheckResourceAttr(
   106  						"aws_vpc.foo", "enable_dns_hostnames", "true"),
   107  				),
   108  			},
   109  		},
   110  	})
   111  }
   112  
   113  func testAccCheckVpcDestroy(s *terraform.State) error {
   114  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
   115  
   116  	for _, rs := range s.RootModule().Resources {
   117  		if rs.Type != "aws_vpc" {
   118  			continue
   119  		}
   120  
   121  		// Try to find the VPC
   122  		resp, err := conn.DescribeVpcs([]string{rs.Primary.ID}, ec2.NewFilter())
   123  		if err == nil {
   124  			if len(resp.VPCs) > 0 {
   125  				return fmt.Errorf("VPCs still exist.")
   126  			}
   127  
   128  			return nil
   129  		}
   130  
   131  		// Verify the error is what we want
   132  		ec2err, ok := err.(*ec2.Error)
   133  		if !ok {
   134  			return err
   135  		}
   136  		if ec2err.Code != "InvalidVpcID.NotFound" {
   137  			return err
   138  		}
   139  	}
   140  
   141  	return nil
   142  }
   143  
   144  func testAccCheckVpcCidr(vpc *ec2.VPC, expected string) resource.TestCheckFunc {
   145  	return func(s *terraform.State) error {
   146  		if vpc.CidrBlock != expected {
   147  			return fmt.Errorf("Bad cidr: %s", vpc.CidrBlock)
   148  		}
   149  
   150  		return nil
   151  	}
   152  }
   153  
   154  func testAccCheckVpcExists(n string, vpc *ec2.VPC) resource.TestCheckFunc {
   155  	return func(s *terraform.State) error {
   156  		rs, ok := s.RootModule().Resources[n]
   157  		if !ok {
   158  			return fmt.Errorf("Not found: %s", n)
   159  		}
   160  
   161  		if rs.Primary.ID == "" {
   162  			return fmt.Errorf("No VPC ID is set")
   163  		}
   164  
   165  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   166  		resp, err := conn.DescribeVpcs([]string{rs.Primary.ID}, ec2.NewFilter())
   167  		if err != nil {
   168  			return err
   169  		}
   170  		if len(resp.VPCs) == 0 {
   171  			return fmt.Errorf("VPC not found")
   172  		}
   173  
   174  		*vpc = resp.VPCs[0]
   175  
   176  		return nil
   177  	}
   178  }
   179  
   180  const testAccVpcConfig = `
   181  resource "aws_vpc" "foo" {
   182  	cidr_block = "10.1.0.0/16"
   183  }
   184  `
   185  
   186  const testAccVpcConfigUpdate = `
   187  resource "aws_vpc" "foo" {
   188  	cidr_block = "10.1.0.0/16"
   189  	enable_dns_hostnames = true
   190  }
   191  `
   192  
   193  const testAccVpcConfigTags = `
   194  resource "aws_vpc" "foo" {
   195  	cidr_block = "10.1.0.0/16"
   196  
   197  	tags {
   198  		foo = "bar"
   199  	}
   200  }
   201  `
   202  
   203  const testAccVpcConfigTagsUpdate = `
   204  resource "aws_vpc" "foo" {
   205  	cidr_block = "10.1.0.0/16"
   206  
   207  	tags {
   208  		bar = "baz"
   209  	}
   210  }
   211  `
   212  const testAccVpcDedicatedConfig = `
   213  resource "aws_vpc" "bar" {
   214  	instance_tenancy = "dedicated"
   215  
   216  	cidr_block = "10.2.0.0/16"
   217  }
   218  `
   219