github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/builtin/providers/aws/resource_aws_vpc_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/aws-sdk-go/aws" 8 "github.com/hashicorp/aws-sdk-go/gen/ec2" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccVpc_basic(t *testing.T) { 14 var vpc ec2.VPC 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckVpcDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccVpcConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckVpcExists("aws_vpc.foo", &vpc), 25 testAccCheckVpcCidr(&vpc, "10.1.0.0/16"), 26 resource.TestCheckResourceAttr( 27 "aws_vpc.foo", "cidr_block", "10.1.0.0/16"), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func TestAccVpc_dedicatedTenancy(t *testing.T) { 35 var vpc ec2.VPC 36 37 resource.Test(t, resource.TestCase{ 38 PreCheck: func() { testAccPreCheck(t) }, 39 Providers: testAccProviders, 40 CheckDestroy: testAccCheckVpcDestroy, 41 Steps: []resource.TestStep{ 42 resource.TestStep{ 43 Config: testAccVpcDedicatedConfig, 44 Check: resource.ComposeTestCheckFunc( 45 testAccCheckVpcExists("aws_vpc.bar", &vpc), 46 resource.TestCheckResourceAttr( 47 "aws_vpc.bar", "instance_tenancy", "dedicated"), 48 ), 49 }, 50 }, 51 }) 52 } 53 54 func TestAccVpc_tags(t *testing.T) { 55 var vpc ec2.VPC 56 57 resource.Test(t, resource.TestCase{ 58 PreCheck: func() { testAccPreCheck(t) }, 59 Providers: testAccProviders, 60 CheckDestroy: testAccCheckVpcDestroy, 61 Steps: []resource.TestStep{ 62 resource.TestStep{ 63 Config: testAccVpcConfigTags, 64 Check: resource.ComposeTestCheckFunc( 65 testAccCheckVpcExists("aws_vpc.foo", &vpc), 66 testAccCheckVpcCidr(&vpc, "10.1.0.0/16"), 67 resource.TestCheckResourceAttr( 68 "aws_vpc.foo", "cidr_block", "10.1.0.0/16"), 69 testAccCheckTags(&vpc.Tags, "foo", "bar"), 70 ), 71 }, 72 73 resource.TestStep{ 74 Config: testAccVpcConfigTagsUpdate, 75 Check: resource.ComposeTestCheckFunc( 76 testAccCheckVpcExists("aws_vpc.foo", &vpc), 77 testAccCheckTags(&vpc.Tags, "foo", ""), 78 testAccCheckTags(&vpc.Tags, "bar", "baz"), 79 ), 80 }, 81 }, 82 }) 83 } 84 85 func TestAccVpcUpdate(t *testing.T) { 86 var vpc ec2.VPC 87 88 resource.Test(t, resource.TestCase{ 89 PreCheck: func() { testAccPreCheck(t) }, 90 Providers: testAccProviders, 91 CheckDestroy: testAccCheckVpcDestroy, 92 Steps: []resource.TestStep{ 93 resource.TestStep{ 94 Config: testAccVpcConfig, 95 Check: resource.ComposeTestCheckFunc( 96 testAccCheckVpcExists("aws_vpc.foo", &vpc), 97 testAccCheckVpcCidr(&vpc, "10.1.0.0/16"), 98 resource.TestCheckResourceAttr( 99 "aws_vpc.foo", "cidr_block", "10.1.0.0/16"), 100 ), 101 }, 102 resource.TestStep{ 103 Config: testAccVpcConfigUpdate, 104 Check: resource.ComposeTestCheckFunc( 105 testAccCheckVpcExists("aws_vpc.foo", &vpc), 106 resource.TestCheckResourceAttr( 107 "aws_vpc.foo", "enable_dns_hostnames", "true"), 108 ), 109 }, 110 }, 111 }) 112 } 113 114 func testAccCheckVpcDestroy(s *terraform.State) error { 115 conn := testAccProvider.Meta().(*AWSClient).ec2conn 116 117 for _, rs := range s.RootModule().Resources { 118 if rs.Type != "aws_vpc" { 119 continue 120 } 121 122 // Try to find the VPC 123 DescribeVpcOpts := &ec2.DescribeVPCsRequest{ 124 VPCIDs: []string{rs.Primary.ID}, 125 } 126 resp, err := conn.DescribeVPCs(DescribeVpcOpts) 127 if err == nil { 128 if len(resp.VPCs) > 0 { 129 return fmt.Errorf("VPCs still exist.") 130 } 131 132 return nil 133 } 134 135 // Verify the error is what we want 136 ec2err, ok := err.(aws.APIError) 137 if !ok { 138 return err 139 } 140 if ec2err.Code != "InvalidVpcID.NotFound" { 141 return err 142 } 143 } 144 145 return nil 146 } 147 148 func testAccCheckVpcCidr(vpc *ec2.VPC, expected string) resource.TestCheckFunc { 149 return func(s *terraform.State) error { 150 CIDRBlock := vpc.CIDRBlock 151 if *CIDRBlock != expected { 152 return fmt.Errorf("Bad cidr: %s", *vpc.CIDRBlock) 153 } 154 155 return nil 156 } 157 } 158 159 func testAccCheckVpcExists(n string, vpc *ec2.VPC) resource.TestCheckFunc { 160 return func(s *terraform.State) error { 161 rs, ok := s.RootModule().Resources[n] 162 if !ok { 163 return fmt.Errorf("Not found: %s", n) 164 } 165 166 if rs.Primary.ID == "" { 167 return fmt.Errorf("No VPC ID is set") 168 } 169 170 conn := testAccProvider.Meta().(*AWSClient).ec2conn 171 DescribeVpcOpts := &ec2.DescribeVPCsRequest{ 172 VPCIDs: []string{rs.Primary.ID}, 173 } 174 resp, err := conn.DescribeVPCs(DescribeVpcOpts) 175 if err != nil { 176 return err 177 } 178 if len(resp.VPCs) == 0 { 179 return fmt.Errorf("VPC not found") 180 } 181 182 *vpc = resp.VPCs[0] 183 184 return nil 185 } 186 } 187 188 // https://github.com/hashicorp/terraform/issues/1301 189 func TestAccVpc_bothDnsOptionsSet(t *testing.T) { 190 resource.Test(t, resource.TestCase{ 191 PreCheck: func() { testAccPreCheck(t) }, 192 Providers: testAccProviders, 193 CheckDestroy: testAccCheckVpcDestroy, 194 Steps: []resource.TestStep{ 195 resource.TestStep{ 196 Config: testAccVpcConfig_BothDnsOptions, 197 Check: resource.ComposeTestCheckFunc( 198 resource.TestCheckResourceAttr( 199 "aws_vpc.bar", "enable_dns_hostnames", "true"), 200 resource.TestCheckResourceAttr( 201 "aws_vpc.bar", "enable_dns_support", "true"), 202 ), 203 }, 204 }, 205 }) 206 } 207 208 const testAccVpcConfig = ` 209 resource "aws_vpc" "foo" { 210 cidr_block = "10.1.0.0/16" 211 } 212 ` 213 214 const testAccVpcConfigUpdate = ` 215 resource "aws_vpc" "foo" { 216 cidr_block = "10.1.0.0/16" 217 enable_dns_hostnames = true 218 } 219 ` 220 221 const testAccVpcConfigTags = ` 222 resource "aws_vpc" "foo" { 223 cidr_block = "10.1.0.0/16" 224 225 tags { 226 foo = "bar" 227 } 228 } 229 ` 230 231 const testAccVpcConfigTagsUpdate = ` 232 resource "aws_vpc" "foo" { 233 cidr_block = "10.1.0.0/16" 234 235 tags { 236 bar = "baz" 237 } 238 } 239 ` 240 const testAccVpcDedicatedConfig = ` 241 resource "aws_vpc" "bar" { 242 instance_tenancy = "dedicated" 243 244 cidr_block = "10.2.0.0/16" 245 } 246 ` 247 248 const testAccVpcConfig_BothDnsOptions = ` 249 resource "aws_vpc" "bar" { 250 cidr_block = "10.2.0.0/16" 251 252 enable_dns_hostnames = true 253 enable_dns_support = true 254 } 255 `