github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/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_tags(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: testAccVpcConfigTags,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
    45  					testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
    46  					resource.TestCheckResourceAttr(
    47  						"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
    48  					testAccCheckTags(&vpc.Tags, "foo", "bar"),
    49  				),
    50  			},
    51  
    52  			resource.TestStep{
    53  				Config: testAccVpcConfigTagsUpdate,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
    56  					testAccCheckTags(&vpc.Tags, "foo", ""),
    57  					testAccCheckTags(&vpc.Tags, "bar", "baz"),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func TestAccVpcUpdate(t *testing.T) {
    65  	var vpc ec2.VPC
    66  
    67  	resource.Test(t, resource.TestCase{
    68  		PreCheck:     func() { testAccPreCheck(t) },
    69  		Providers:    testAccProviders,
    70  		CheckDestroy: testAccCheckVpcDestroy,
    71  		Steps: []resource.TestStep{
    72  			resource.TestStep{
    73  				Config: testAccVpcConfig,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
    76  					testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
    77  					resource.TestCheckResourceAttr(
    78  						"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
    79  				),
    80  			},
    81  			resource.TestStep{
    82  				Config: testAccVpcConfigUpdate,
    83  				Check: resource.ComposeTestCheckFunc(
    84  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
    85  					resource.TestCheckResourceAttr(
    86  						"aws_vpc.foo", "enable_dns_hostnames", "true"),
    87  				),
    88  			},
    89  		},
    90  	})
    91  }
    92  
    93  func testAccCheckVpcDestroy(s *terraform.State) error {
    94  	conn := testAccProvider.ec2conn
    95  
    96  	for _, rs := range s.RootModule().Resources {
    97  		if rs.Type != "aws_vpc" {
    98  			continue
    99  		}
   100  
   101  		// Try to find the VPC
   102  		resp, err := conn.DescribeVpcs([]string{rs.Primary.ID}, ec2.NewFilter())
   103  		if err == nil {
   104  			if len(resp.VPCs) > 0 {
   105  				return fmt.Errorf("VPCs still exist.")
   106  			}
   107  
   108  			return nil
   109  		}
   110  
   111  		// Verify the error is what we want
   112  		ec2err, ok := err.(*ec2.Error)
   113  		if !ok {
   114  			return err
   115  		}
   116  		if ec2err.Code != "InvalidVpcID.NotFound" {
   117  			return err
   118  		}
   119  	}
   120  
   121  	return nil
   122  }
   123  
   124  func testAccCheckVpcCidr(vpc *ec2.VPC, expected string) resource.TestCheckFunc {
   125  	return func(s *terraform.State) error {
   126  		if vpc.CidrBlock != expected {
   127  			return fmt.Errorf("Bad cidr: %s", vpc.CidrBlock)
   128  		}
   129  
   130  		return nil
   131  	}
   132  }
   133  
   134  func testAccCheckVpcExists(n string, vpc *ec2.VPC) resource.TestCheckFunc {
   135  	return func(s *terraform.State) error {
   136  		rs, ok := s.RootModule().Resources[n]
   137  		if !ok {
   138  			return fmt.Errorf("Not found: %s", n)
   139  		}
   140  
   141  		if rs.Primary.ID == "" {
   142  			return fmt.Errorf("No VPC ID is set")
   143  		}
   144  
   145  		conn := testAccProvider.ec2conn
   146  		resp, err := conn.DescribeVpcs([]string{rs.Primary.ID}, ec2.NewFilter())
   147  		if err != nil {
   148  			return err
   149  		}
   150  		if len(resp.VPCs) == 0 {
   151  			return fmt.Errorf("VPC not found")
   152  		}
   153  
   154  		*vpc = resp.VPCs[0]
   155  
   156  		return nil
   157  	}
   158  }
   159  
   160  const testAccVpcConfig = `
   161  resource "aws_vpc" "foo" {
   162  	cidr_block = "10.1.0.0/16"
   163  }
   164  `
   165  
   166  const testAccVpcConfigUpdate = `
   167  resource "aws_vpc" "foo" {
   168  	cidr_block = "10.1.0.0/16"
   169  	enable_dns_hostnames = true
   170  }
   171  `
   172  
   173  const testAccVpcConfigTags = `
   174  resource "aws_vpc" "foo" {
   175  	cidr_block = "10.1.0.0/16"
   176  
   177  	tags {
   178  		foo = "bar"
   179  	}
   180  }
   181  `
   182  
   183  const testAccVpcConfigTagsUpdate = `
   184  resource "aws_vpc" "foo" {
   185  	cidr_block = "10.1.0.0/16"
   186  
   187  	tags {
   188  		bar = "baz"
   189  	}
   190  }
   191  `