github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_vpc_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/ec2" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSVpc_basic(t *testing.T) { 15 var vpc ec2.Vpc 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckVpcDestroy, 21 Steps: []resource.TestStep{ 22 { 23 Config: testAccVpcConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckVpcExists("aws_vpc.foo", &vpc), 26 testAccCheckVpcCidr(&vpc, "10.1.0.0/16"), 27 resource.TestCheckResourceAttr( 28 "aws_vpc.foo", "cidr_block", "10.1.0.0/16"), 29 resource.TestCheckResourceAttrSet( 30 "aws_vpc.foo", "default_route_table_id"), 31 resource.TestCheckResourceAttr( 32 "aws_vpc.foo", "enable_dns_support", "true"), 33 ), 34 }, 35 }, 36 }) 37 } 38 39 func TestAccAWSVpc_enableIpv6(t *testing.T) { 40 var vpc ec2.Vpc 41 42 resource.Test(t, resource.TestCase{ 43 PreCheck: func() { testAccPreCheck(t) }, 44 Providers: testAccProviders, 45 CheckDestroy: testAccCheckVpcDestroy, 46 Steps: []resource.TestStep{ 47 { 48 Config: testAccVpcConfigIpv6Enabled, 49 Check: resource.ComposeAggregateTestCheckFunc( 50 testAccCheckVpcExists("aws_vpc.foo", &vpc), 51 testAccCheckVpcCidr(&vpc, "10.1.0.0/16"), 52 resource.TestCheckResourceAttr( 53 "aws_vpc.foo", "cidr_block", "10.1.0.0/16"), 54 resource.TestCheckResourceAttrSet( 55 "aws_vpc.foo", "ipv6_association_id"), 56 resource.TestCheckResourceAttrSet( 57 "aws_vpc.foo", "ipv6_cidr_block"), 58 resource.TestCheckResourceAttr( 59 "aws_vpc.foo", "assign_generated_ipv6_cidr_block", "true"), 60 ), 61 }, 62 { 63 Config: testAccVpcConfigIpv6Disabled, 64 Check: resource.ComposeAggregateTestCheckFunc( 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 resource.TestCheckResourceAttr( 70 "aws_vpc.foo", "assign_generated_ipv6_cidr_block", "false"), 71 ), 72 }, 73 { 74 Config: testAccVpcConfigIpv6Enabled, 75 Check: resource.ComposeAggregateTestCheckFunc( 76 testAccCheckVpcExists("aws_vpc.foo", &vpc), 77 testAccCheckVpcCidr(&vpc, "10.1.0.0/16"), 78 resource.TestCheckResourceAttr( 79 "aws_vpc.foo", "cidr_block", "10.1.0.0/16"), 80 resource.TestCheckResourceAttrSet( 81 "aws_vpc.foo", "ipv6_association_id"), 82 resource.TestCheckResourceAttrSet( 83 "aws_vpc.foo", "ipv6_cidr_block"), 84 resource.TestCheckResourceAttr( 85 "aws_vpc.foo", "assign_generated_ipv6_cidr_block", "true"), 86 ), 87 }, 88 }, 89 }) 90 } 91 92 func TestAccAWSVpc_dedicatedTenancy(t *testing.T) { 93 var vpc ec2.Vpc 94 95 resource.Test(t, resource.TestCase{ 96 PreCheck: func() { testAccPreCheck(t) }, 97 Providers: testAccProviders, 98 CheckDestroy: testAccCheckVpcDestroy, 99 Steps: []resource.TestStep{ 100 { 101 Config: testAccVpcDedicatedConfig, 102 Check: resource.ComposeTestCheckFunc( 103 testAccCheckVpcExists("aws_vpc.bar", &vpc), 104 resource.TestCheckResourceAttr( 105 "aws_vpc.bar", "instance_tenancy", "dedicated"), 106 ), 107 }, 108 }, 109 }) 110 } 111 112 func TestAccAWSVpc_tags(t *testing.T) { 113 var vpc ec2.Vpc 114 115 resource.Test(t, resource.TestCase{ 116 PreCheck: func() { testAccPreCheck(t) }, 117 Providers: testAccProviders, 118 CheckDestroy: testAccCheckVpcDestroy, 119 Steps: []resource.TestStep{ 120 { 121 Config: testAccVpcConfigTags, 122 Check: resource.ComposeTestCheckFunc( 123 testAccCheckVpcExists("aws_vpc.foo", &vpc), 124 testAccCheckVpcCidr(&vpc, "10.1.0.0/16"), 125 resource.TestCheckResourceAttr( 126 "aws_vpc.foo", "cidr_block", "10.1.0.0/16"), 127 testAccCheckTags(&vpc.Tags, "foo", "bar"), 128 ), 129 }, 130 131 { 132 Config: testAccVpcConfigTagsUpdate, 133 Check: resource.ComposeTestCheckFunc( 134 testAccCheckVpcExists("aws_vpc.foo", &vpc), 135 testAccCheckTags(&vpc.Tags, "foo", ""), 136 testAccCheckTags(&vpc.Tags, "bar", "baz"), 137 ), 138 }, 139 }, 140 }) 141 } 142 143 func TestAccAWSVpc_update(t *testing.T) { 144 var vpc ec2.Vpc 145 146 resource.Test(t, resource.TestCase{ 147 PreCheck: func() { testAccPreCheck(t) }, 148 Providers: testAccProviders, 149 CheckDestroy: testAccCheckVpcDestroy, 150 Steps: []resource.TestStep{ 151 { 152 Config: testAccVpcConfig, 153 Check: resource.ComposeTestCheckFunc( 154 testAccCheckVpcExists("aws_vpc.foo", &vpc), 155 testAccCheckVpcCidr(&vpc, "10.1.0.0/16"), 156 resource.TestCheckResourceAttr( 157 "aws_vpc.foo", "cidr_block", "10.1.0.0/16"), 158 ), 159 }, 160 { 161 Config: testAccVpcConfigUpdate, 162 Check: resource.ComposeTestCheckFunc( 163 testAccCheckVpcExists("aws_vpc.foo", &vpc), 164 resource.TestCheckResourceAttr( 165 "aws_vpc.foo", "enable_dns_hostnames", "true"), 166 ), 167 }, 168 }, 169 }) 170 } 171 172 func testAccCheckVpcDestroy(s *terraform.State) error { 173 conn := testAccProvider.Meta().(*AWSClient).ec2conn 174 175 for _, rs := range s.RootModule().Resources { 176 if rs.Type != "aws_vpc" { 177 continue 178 } 179 180 // Try to find the VPC 181 DescribeVpcOpts := &ec2.DescribeVpcsInput{ 182 VpcIds: []*string{aws.String(rs.Primary.ID)}, 183 } 184 resp, err := conn.DescribeVpcs(DescribeVpcOpts) 185 if err == nil { 186 if len(resp.Vpcs) > 0 { 187 return fmt.Errorf("VPCs still exist.") 188 } 189 190 return nil 191 } 192 193 // Verify the error is what we want 194 ec2err, ok := err.(awserr.Error) 195 if !ok { 196 return err 197 } 198 if ec2err.Code() != "InvalidVpcID.NotFound" { 199 return err 200 } 201 } 202 203 return nil 204 } 205 206 func testAccCheckVpcCidr(vpc *ec2.Vpc, expected string) resource.TestCheckFunc { 207 return func(s *terraform.State) error { 208 CIDRBlock := vpc.CidrBlock 209 if *CIDRBlock != expected { 210 return fmt.Errorf("Bad cidr: %s", *vpc.CidrBlock) 211 } 212 213 return nil 214 } 215 } 216 217 func testAccCheckVpcExists(n string, vpc *ec2.Vpc) resource.TestCheckFunc { 218 return func(s *terraform.State) error { 219 rs, ok := s.RootModule().Resources[n] 220 if !ok { 221 return fmt.Errorf("Not found: %s", n) 222 } 223 224 if rs.Primary.ID == "" { 225 return fmt.Errorf("No VPC ID is set") 226 } 227 228 conn := testAccProvider.Meta().(*AWSClient).ec2conn 229 DescribeVpcOpts := &ec2.DescribeVpcsInput{ 230 VpcIds: []*string{aws.String(rs.Primary.ID)}, 231 } 232 resp, err := conn.DescribeVpcs(DescribeVpcOpts) 233 if err != nil { 234 return err 235 } 236 if len(resp.Vpcs) == 0 { 237 return fmt.Errorf("VPC not found") 238 } 239 240 *vpc = *resp.Vpcs[0] 241 242 return nil 243 } 244 } 245 246 // https://github.com/hashicorp/terraform/issues/1301 247 func TestAccAWSVpc_bothDnsOptionsSet(t *testing.T) { 248 resource.Test(t, resource.TestCase{ 249 PreCheck: func() { testAccPreCheck(t) }, 250 Providers: testAccProviders, 251 CheckDestroy: testAccCheckVpcDestroy, 252 Steps: []resource.TestStep{ 253 { 254 Config: testAccVpcConfig_BothDnsOptions, 255 Check: resource.ComposeTestCheckFunc( 256 resource.TestCheckResourceAttr( 257 "aws_vpc.bar", "enable_dns_hostnames", "true"), 258 resource.TestCheckResourceAttr( 259 "aws_vpc.bar", "enable_dns_support", "true"), 260 ), 261 }, 262 }, 263 }) 264 } 265 266 // https://github.com/hashicorp/terraform/issues/10168 267 func TestAccAWSVpc_DisabledDnsSupport(t *testing.T) { 268 resource.Test(t, resource.TestCase{ 269 PreCheck: func() { testAccPreCheck(t) }, 270 Providers: testAccProviders, 271 CheckDestroy: testAccCheckVpcDestroy, 272 Steps: []resource.TestStep{ 273 { 274 Config: testAccVpcConfig_DisabledDnsSupport, 275 Check: resource.ComposeTestCheckFunc( 276 resource.TestCheckResourceAttr( 277 "aws_vpc.bar", "enable_dns_support", "false"), 278 ), 279 }, 280 }, 281 }) 282 } 283 284 func TestAccAWSVpc_classiclinkOptionSet(t *testing.T) { 285 resource.Test(t, resource.TestCase{ 286 PreCheck: func() { testAccPreCheck(t) }, 287 Providers: testAccProviders, 288 CheckDestroy: testAccCheckVpcDestroy, 289 Steps: []resource.TestStep{ 290 { 291 Config: testAccVpcConfig_ClassiclinkOption, 292 Check: resource.ComposeTestCheckFunc( 293 resource.TestCheckResourceAttr( 294 "aws_vpc.bar", "enable_classiclink", "true"), 295 ), 296 }, 297 }, 298 }) 299 } 300 301 const testAccVpcConfig = ` 302 resource "aws_vpc" "foo" { 303 cidr_block = "10.1.0.0/16" 304 } 305 ` 306 307 const testAccVpcConfigIpv6Enabled = ` 308 resource "aws_vpc" "foo" { 309 cidr_block = "10.1.0.0/16" 310 assign_generated_ipv6_cidr_block = true 311 } 312 ` 313 314 const testAccVpcConfigIpv6Disabled = ` 315 resource "aws_vpc" "foo" { 316 cidr_block = "10.1.0.0/16" 317 } 318 ` 319 320 const testAccVpcConfigUpdate = ` 321 resource "aws_vpc" "foo" { 322 cidr_block = "10.1.0.0/16" 323 enable_dns_hostnames = true 324 } 325 ` 326 327 const testAccVpcConfigTags = ` 328 resource "aws_vpc" "foo" { 329 cidr_block = "10.1.0.0/16" 330 331 tags { 332 foo = "bar" 333 } 334 } 335 ` 336 337 const testAccVpcConfigTagsUpdate = ` 338 resource "aws_vpc" "foo" { 339 cidr_block = "10.1.0.0/16" 340 341 tags { 342 bar = "baz" 343 } 344 } 345 ` 346 const testAccVpcDedicatedConfig = ` 347 resource "aws_vpc" "bar" { 348 instance_tenancy = "dedicated" 349 350 cidr_block = "10.2.0.0/16" 351 } 352 ` 353 354 const testAccVpcConfig_BothDnsOptions = ` 355 provider "aws" { 356 region = "eu-central-1" 357 } 358 359 resource "aws_vpc" "bar" { 360 cidr_block = "10.2.0.0/16" 361 362 enable_dns_hostnames = true 363 enable_dns_support = true 364 } 365 ` 366 367 const testAccVpcConfig_DisabledDnsSupport = ` 368 provider "aws" { 369 region = "us-west-2" 370 } 371 372 resource "aws_vpc" "bar" { 373 cidr_block = "10.2.0.0/16" 374 375 enable_dns_support = false 376 } 377 ` 378 379 const testAccVpcConfig_ClassiclinkOption = ` 380 resource "aws_vpc" "bar" { 381 cidr_block = "172.2.0.0/16" 382 383 enable_classiclink = true 384 } 385 `