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