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