github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/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_dedicatedTenancy(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: testAccVpcDedicatedConfig, 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckVpcExists("aws_vpc.bar", &vpc), 51 resource.TestCheckResourceAttr( 52 "aws_vpc.bar", "instance_tenancy", "dedicated"), 53 ), 54 }, 55 }, 56 }) 57 } 58 59 func TestAccAWSVpc_tags(t *testing.T) { 60 var vpc ec2.Vpc 61 62 resource.Test(t, resource.TestCase{ 63 PreCheck: func() { testAccPreCheck(t) }, 64 Providers: testAccProviders, 65 CheckDestroy: testAccCheckVpcDestroy, 66 Steps: []resource.TestStep{ 67 { 68 Config: testAccVpcConfigTags, 69 Check: resource.ComposeTestCheckFunc( 70 testAccCheckVpcExists("aws_vpc.foo", &vpc), 71 testAccCheckVpcCidr(&vpc, "10.1.0.0/16"), 72 resource.TestCheckResourceAttr( 73 "aws_vpc.foo", "cidr_block", "10.1.0.0/16"), 74 testAccCheckTags(&vpc.Tags, "foo", "bar"), 75 ), 76 }, 77 78 { 79 Config: testAccVpcConfigTagsUpdate, 80 Check: resource.ComposeTestCheckFunc( 81 testAccCheckVpcExists("aws_vpc.foo", &vpc), 82 testAccCheckTags(&vpc.Tags, "foo", ""), 83 testAccCheckTags(&vpc.Tags, "bar", "baz"), 84 ), 85 }, 86 }, 87 }) 88 } 89 90 func TestAccAWSVpc_update(t *testing.T) { 91 var vpc ec2.Vpc 92 93 resource.Test(t, resource.TestCase{ 94 PreCheck: func() { testAccPreCheck(t) }, 95 Providers: testAccProviders, 96 CheckDestroy: testAccCheckVpcDestroy, 97 Steps: []resource.TestStep{ 98 { 99 Config: testAccVpcConfig, 100 Check: resource.ComposeTestCheckFunc( 101 testAccCheckVpcExists("aws_vpc.foo", &vpc), 102 testAccCheckVpcCidr(&vpc, "10.1.0.0/16"), 103 resource.TestCheckResourceAttr( 104 "aws_vpc.foo", "cidr_block", "10.1.0.0/16"), 105 ), 106 }, 107 { 108 Config: testAccVpcConfigUpdate, 109 Check: resource.ComposeTestCheckFunc( 110 testAccCheckVpcExists("aws_vpc.foo", &vpc), 111 resource.TestCheckResourceAttr( 112 "aws_vpc.foo", "enable_dns_hostnames", "true"), 113 ), 114 }, 115 }, 116 }) 117 } 118 119 func testAccCheckVpcDestroy(s *terraform.State) error { 120 conn := testAccProvider.Meta().(*AWSClient).ec2conn 121 122 for _, rs := range s.RootModule().Resources { 123 if rs.Type != "aws_vpc" { 124 continue 125 } 126 127 // Try to find the VPC 128 DescribeVpcOpts := &ec2.DescribeVpcsInput{ 129 VpcIds: []*string{aws.String(rs.Primary.ID)}, 130 } 131 resp, err := conn.DescribeVpcs(DescribeVpcOpts) 132 if err == nil { 133 if len(resp.Vpcs) > 0 { 134 return fmt.Errorf("VPCs still exist.") 135 } 136 137 return nil 138 } 139 140 // Verify the error is what we want 141 ec2err, ok := err.(awserr.Error) 142 if !ok { 143 return err 144 } 145 if ec2err.Code() != "InvalidVpcID.NotFound" { 146 return err 147 } 148 } 149 150 return nil 151 } 152 153 func testAccCheckVpcCidr(vpc *ec2.Vpc, expected string) resource.TestCheckFunc { 154 return func(s *terraform.State) error { 155 CIDRBlock := vpc.CidrBlock 156 if *CIDRBlock != expected { 157 return fmt.Errorf("Bad cidr: %s", *vpc.CidrBlock) 158 } 159 160 return nil 161 } 162 } 163 164 func testAccCheckVpcExists(n string, vpc *ec2.Vpc) resource.TestCheckFunc { 165 return func(s *terraform.State) error { 166 rs, ok := s.RootModule().Resources[n] 167 if !ok { 168 return fmt.Errorf("Not found: %s", n) 169 } 170 171 if rs.Primary.ID == "" { 172 return fmt.Errorf("No VPC ID is set") 173 } 174 175 conn := testAccProvider.Meta().(*AWSClient).ec2conn 176 DescribeVpcOpts := &ec2.DescribeVpcsInput{ 177 VpcIds: []*string{aws.String(rs.Primary.ID)}, 178 } 179 resp, err := conn.DescribeVpcs(DescribeVpcOpts) 180 if err != nil { 181 return err 182 } 183 if len(resp.Vpcs) == 0 { 184 return fmt.Errorf("VPC not found") 185 } 186 187 *vpc = *resp.Vpcs[0] 188 189 return nil 190 } 191 } 192 193 // https://github.com/hashicorp/terraform/issues/1301 194 func TestAccAWSVpc_bothDnsOptionsSet(t *testing.T) { 195 resource.Test(t, resource.TestCase{ 196 PreCheck: func() { testAccPreCheck(t) }, 197 Providers: testAccProviders, 198 CheckDestroy: testAccCheckVpcDestroy, 199 Steps: []resource.TestStep{ 200 { 201 Config: testAccVpcConfig_BothDnsOptions, 202 Check: resource.ComposeTestCheckFunc( 203 resource.TestCheckResourceAttr( 204 "aws_vpc.bar", "enable_dns_hostnames", "true"), 205 resource.TestCheckResourceAttr( 206 "aws_vpc.bar", "enable_dns_support", "true"), 207 ), 208 }, 209 }, 210 }) 211 } 212 213 // https://github.com/hashicorp/terraform/issues/10168 214 func TestAccAWSVpc_DisabledDnsSupport(t *testing.T) { 215 resource.Test(t, resource.TestCase{ 216 PreCheck: func() { testAccPreCheck(t) }, 217 Providers: testAccProviders, 218 CheckDestroy: testAccCheckVpcDestroy, 219 Steps: []resource.TestStep{ 220 { 221 Config: testAccVpcConfig_DisabledDnsSupport, 222 Check: resource.ComposeTestCheckFunc( 223 resource.TestCheckResourceAttr( 224 "aws_vpc.bar", "enable_dns_support", "false"), 225 ), 226 }, 227 }, 228 }) 229 } 230 231 func TestAccAWSVpc_classiclinkOptionSet(t *testing.T) { 232 resource.Test(t, resource.TestCase{ 233 PreCheck: func() { testAccPreCheck(t) }, 234 Providers: testAccProviders, 235 CheckDestroy: testAccCheckVpcDestroy, 236 Steps: []resource.TestStep{ 237 { 238 Config: testAccVpcConfig_ClassiclinkOption, 239 Check: resource.ComposeTestCheckFunc( 240 resource.TestCheckResourceAttr( 241 "aws_vpc.bar", "enable_classiclink", "true"), 242 ), 243 }, 244 }, 245 }) 246 } 247 248 const testAccVpcConfig = ` 249 resource "aws_vpc" "foo" { 250 cidr_block = "10.1.0.0/16" 251 } 252 ` 253 254 const testAccVpcConfigUpdate = ` 255 resource "aws_vpc" "foo" { 256 cidr_block = "10.1.0.0/16" 257 enable_dns_hostnames = true 258 } 259 ` 260 261 const testAccVpcConfigTags = ` 262 resource "aws_vpc" "foo" { 263 cidr_block = "10.1.0.0/16" 264 265 tags { 266 foo = "bar" 267 } 268 } 269 ` 270 271 const testAccVpcConfigTagsUpdate = ` 272 resource "aws_vpc" "foo" { 273 cidr_block = "10.1.0.0/16" 274 275 tags { 276 bar = "baz" 277 } 278 } 279 ` 280 const testAccVpcDedicatedConfig = ` 281 resource "aws_vpc" "bar" { 282 instance_tenancy = "dedicated" 283 284 cidr_block = "10.2.0.0/16" 285 } 286 ` 287 288 const testAccVpcConfig_BothDnsOptions = ` 289 provider "aws" { 290 region = "eu-central-1" 291 } 292 293 resource "aws_vpc" "bar" { 294 cidr_block = "10.2.0.0/16" 295 296 enable_dns_hostnames = true 297 enable_dns_support = true 298 } 299 ` 300 301 const testAccVpcConfig_DisabledDnsSupport = ` 302 provider "aws" { 303 region = "us-west-2" 304 } 305 306 resource "aws_vpc" "bar" { 307 cidr_block = "10.2.0.0/16" 308 309 enable_dns_support = false 310 } 311 ` 312 313 const testAccVpcConfig_ClassiclinkOption = ` 314 resource "aws_vpc" "bar" { 315 cidr_block = "172.2.0.0/16" 316 317 enable_classiclink = true 318 } 319 `