github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 resource.TestStep{ 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 resource.TestStep{ 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 resource.TestStep{ 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 resource.TestStep{ 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 resource.TestStep{ 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 resource.TestStep{ 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 resource.TestStep{ 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 func TestAccAWSVpc_classiclinkOptionSet(t *testing.T) { 212 resource.Test(t, resource.TestCase{ 213 PreCheck: func() { testAccPreCheck(t) }, 214 Providers: testAccProviders, 215 CheckDestroy: testAccCheckVpcDestroy, 216 Steps: []resource.TestStep{ 217 resource.TestStep{ 218 Config: testAccVpcConfig_ClassiclinkOption, 219 Check: resource.ComposeTestCheckFunc( 220 resource.TestCheckResourceAttr( 221 "aws_vpc.bar", "enable_classiclink", "true"), 222 ), 223 }, 224 }, 225 }) 226 } 227 228 const testAccVpcConfig = ` 229 resource "aws_vpc" "foo" { 230 cidr_block = "10.1.0.0/16" 231 } 232 ` 233 234 const testAccVpcConfigUpdate = ` 235 resource "aws_vpc" "foo" { 236 cidr_block = "10.1.0.0/16" 237 enable_dns_hostnames = true 238 } 239 ` 240 241 const testAccVpcConfigTags = ` 242 resource "aws_vpc" "foo" { 243 cidr_block = "10.1.0.0/16" 244 245 tags { 246 foo = "bar" 247 } 248 } 249 ` 250 251 const testAccVpcConfigTagsUpdate = ` 252 resource "aws_vpc" "foo" { 253 cidr_block = "10.1.0.0/16" 254 255 tags { 256 bar = "baz" 257 } 258 } 259 ` 260 const testAccVpcDedicatedConfig = ` 261 resource "aws_vpc" "bar" { 262 instance_tenancy = "dedicated" 263 264 cidr_block = "10.2.0.0/16" 265 } 266 ` 267 268 const testAccVpcConfig_BothDnsOptions = ` 269 provider "aws" { 270 region = "eu-central-1" 271 } 272 273 resource "aws_vpc" "bar" { 274 cidr_block = "10.2.0.0/16" 275 276 enable_dns_hostnames = true 277 enable_dns_support = true 278 } 279 ` 280 281 const testAccVpcConfig_ClassiclinkOption = ` 282 resource "aws_vpc" "bar" { 283 cidr_block = "172.2.0.0/16" 284 285 enable_classiclink = true 286 } 287 `