github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/resource_aws_vpc_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "github.com/hashicorp/aws-sdk-go/aws" 6 "github.com/hashicorp/aws-sdk-go/gen/ec2" 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "testing" 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 DescribeVpcOpts := &ec2.DescribeVPCsRequest{ 123 VPCIDs: []string{rs.Primary.ID}, 124 } 125 resp, err := conn.DescribeVPCs(DescribeVpcOpts) 126 if err == nil { 127 if len(resp.VPCs) > 0 { 128 return fmt.Errorf("VPCs still exist.") 129 } 130 131 return nil 132 } 133 134 // Verify the error is what we want 135 ec2err, ok := err.(*aws.APIError) 136 if !ok { 137 return err 138 } 139 if ec2err.Code != "InvalidVpcID.NotFound" { 140 return err 141 } 142 } 143 144 return nil 145 } 146 147 func testAccCheckVpcCidr(vpc *ec2.VPC, expected string) resource.TestCheckFunc { 148 return func(s *terraform.State) error { 149 CIDRBlock := vpc.CIDRBlock 150 if *CIDRBlock != expected { 151 return fmt.Errorf("Bad cidr: %s", *vpc.CIDRBlock) 152 } 153 154 return nil 155 } 156 } 157 158 func testAccCheckVpcExists(n string, vpc *ec2.VPC) resource.TestCheckFunc { 159 return func(s *terraform.State) error { 160 rs, ok := s.RootModule().Resources[n] 161 if !ok { 162 return fmt.Errorf("Not found: %s", n) 163 } 164 165 if rs.Primary.ID == "" { 166 return fmt.Errorf("No VPC ID is set") 167 } 168 169 conn := testAccProvider.Meta().(*AWSClient).ec2conn 170 DescribeVpcOpts := &ec2.DescribeVPCsRequest{ 171 VPCIDs: []string{rs.Primary.ID}, 172 } 173 resp, err := conn.DescribeVPCs(DescribeVpcOpts) 174 if err != nil { 175 return err 176 } 177 if len(resp.VPCs) == 0 { 178 return fmt.Errorf("VPC not found") 179 } 180 181 *vpc = resp.VPCs[0] 182 183 return nil 184 } 185 } 186 187 const testAccVpcConfig = ` 188 resource "aws_vpc" "foo" { 189 cidr_block = "10.1.0.0/16" 190 } 191 ` 192 193 const testAccVpcConfigUpdate = ` 194 resource "aws_vpc" "foo" { 195 cidr_block = "10.1.0.0/16" 196 enable_dns_hostnames = true 197 } 198 ` 199 200 const testAccVpcConfigTags = ` 201 resource "aws_vpc" "foo" { 202 cidr_block = "10.1.0.0/16" 203 204 tags { 205 foo = "bar" 206 } 207 } 208 ` 209 210 const testAccVpcConfigTagsUpdate = ` 211 resource "aws_vpc" "foo" { 212 cidr_block = "10.1.0.0/16" 213 214 tags { 215 bar = "baz" 216 } 217 } 218 ` 219 const testAccVpcDedicatedConfig = ` 220 resource "aws_vpc" "bar" { 221 instance_tenancy = "dedicated" 222 223 cidr_block = "10.2.0.0/16" 224 } 225 `