terraform/builtin/providers/aws/resource_aws_vpc_test.go

352 lines
7.9 KiB
Go
Raw Normal View History

2014-07-10 20:31:16 +02:00
package aws
import (
"fmt"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
2014-07-10 20:31:16 +02:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSVpc_basic(t *testing.T) {
var vpc ec2.Vpc
2014-07-10 20:31:16 +02:00
resource.Test(t, resource.TestCase{
2014-07-10 22:12:47 +02:00
PreCheck: func() { testAccPreCheck(t) },
2014-07-10 20:31:16 +02:00
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{
{
2014-07-10 20:31:16 +02:00
Config: testAccVpcConfig,
2014-07-10 22:49:09 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcExists("aws_vpc.foo", &vpc),
testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
resource.TestCheckResourceAttr(
"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
resource.TestCheckResourceAttrSet(
"aws_vpc.foo", "default_route_table_id"),
resource.TestCheckResourceAttr(
"aws_vpc.foo", "enable_dns_support", "true"),
2014-07-10 22:49:09 +02:00
),
2014-07-10 20:31:16 +02:00
},
},
})
}
provider/aws: Implement IPV6 Support for ec2 / VPC (#10538) * provider/aws: Add support for IPV6 enabled VPC ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSVpc' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/12/09 14:07:31 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSVpc -timeout 120m === RUN TestAccAWSVpc_importBasic --- PASS: TestAccAWSVpc_importBasic (43.03s) === RUN TestAccAWSVpc_basic --- PASS: TestAccAWSVpc_basic (36.32s) === RUN TestAccAWSVpc_enableIpv6 --- PASS: TestAccAWSVpc_enableIpv6 (29.37s) === RUN TestAccAWSVpc_dedicatedTenancy --- PASS: TestAccAWSVpc_dedicatedTenancy (36.63s) === RUN TestAccAWSVpc_tags --- PASS: TestAccAWSVpc_tags (67.54s) === RUN TestAccAWSVpc_update --- PASS: TestAccAWSVpc_update (66.16s) === RUN TestAccAWSVpc_bothDnsOptionsSet --- PASS: TestAccAWSVpc_bothDnsOptionsSet (16.82s) === RUN TestAccAWSVpc_DisabledDnsSupport --- PASS: TestAccAWSVpc_DisabledDnsSupport (36.52s) === RUN TestAccAWSVpc_classiclinkOptionSet --- PASS: TestAccAWSVpc_classiclinkOptionSet (38.13s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 739.543s ``` * provider/aws: New Resource: aws_egress_only_internet_gateway ``` make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSEgressOnlyInternetGateway_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/12/09 14:22:16 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSEgressOnlyInternetGateway_ -timeout 120m === RUN TestAccAWSEgressOnlyInternetGateway_basic --- PASS: TestAccAWSEgressOnlyInternetGateway_basic (32.67s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 32.692s ``` * provider/aws: Add IPV6 support to aws_subnet ``` % make testacc TEST=./builtin/providers/aws % TESTARGS='-run=TestAccAWSSubnet_' % 1 ↵ ✹ ✭ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/02/27 19:08:34 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSubnet_ -timeout 120m === RUN TestAccAWSSubnet_importBasic --- PASS: TestAccAWSSubnet_importBasic (69.88s) === RUN TestAccAWSSubnet_basic --- PASS: TestAccAWSSubnet_basic (51.28s) === RUN TestAccAWSSubnet_ipv6 --- PASS: TestAccAWSSubnet_ipv6 (90.39s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws211.574s ``` * provider/aws: Add support for running aws_instances with ipv6 addresses
2017-03-01 17:16:59 +01:00
func TestAccAWSVpc_enableIpv6(t *testing.T) {
var vpc ec2.Vpc
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{
{
Config: testAccVpcConfigIpv6Enabled,
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcExists("aws_vpc.foo", &vpc),
testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
resource.TestCheckResourceAttr(
"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
resource.TestCheckResourceAttrSet(
"aws_vpc.foo", "ipv6_association_id"),
resource.TestCheckResourceAttrSet(
"aws_vpc.foo", "ipv6_cidr_block"),
),
},
},
})
}
func TestAccAWSVpc_dedicatedTenancy(t *testing.T) {
var vpc ec2.Vpc
2014-12-10 11:39:14 +01:00
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{
{
2014-12-10 11:39:14 +01:00
Config: testAccVpcDedicatedConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcExists("aws_vpc.bar", &vpc),
resource.TestCheckResourceAttr(
"aws_vpc.bar", "instance_tenancy", "dedicated"),
),
},
},
})
}
func TestAccAWSVpc_tags(t *testing.T) {
var vpc ec2.Vpc
2015-03-04 14:07:30 +01:00
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{
{
2015-03-04 14:07:30 +01:00
Config: testAccVpcConfigTags,
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcExists("aws_vpc.foo", &vpc),
testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
resource.TestCheckResourceAttr(
"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
testAccCheckTags(&vpc.Tags, "foo", "bar"),
2015-03-04 14:07:30 +01:00
),
},
{
2015-03-04 14:07:30 +01:00
Config: testAccVpcConfigTagsUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcExists("aws_vpc.foo", &vpc),
testAccCheckTags(&vpc.Tags, "foo", ""),
testAccCheckTags(&vpc.Tags, "bar", "baz"),
2015-03-04 14:07:30 +01:00
),
},
},
})
}
2014-10-09 02:54:00 +02:00
func TestAccAWSVpc_update(t *testing.T) {
var vpc ec2.Vpc
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{
{
Config: testAccVpcConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcExists("aws_vpc.foo", &vpc),
testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
resource.TestCheckResourceAttr(
"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
),
},
{
Config: testAccVpcConfigUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcExists("aws_vpc.foo", &vpc),
resource.TestCheckResourceAttr(
"aws_vpc.foo", "enable_dns_hostnames", "true"),
),
},
},
})
}
2014-07-10 20:31:16 +02:00
func testAccCheckVpcDestroy(s *terraform.State) error {
2015-04-16 22:05:55 +02:00
conn := testAccProvider.Meta().(*AWSClient).ec2conn
2014-07-10 20:31:16 +02:00
2014-09-17 02:44:42 +02:00
for _, rs := range s.RootModule().Resources {
2014-07-10 20:31:16 +02:00
if rs.Type != "aws_vpc" {
continue
}
// Try to find the VPC
DescribeVpcOpts := &ec2.DescribeVpcsInput{
VpcIds: []*string{aws.String(rs.Primary.ID)},
2015-02-25 10:34:17 +01:00
}
resp, err := conn.DescribeVpcs(DescribeVpcOpts)
2014-07-10 20:31:16 +02:00
if err == nil {
if len(resp.Vpcs) > 0 {
2014-07-10 20:31:16 +02:00
return fmt.Errorf("VPCs still exist.")
}
return nil
}
// Verify the error is what we want
ec2err, ok := err.(awserr.Error)
2014-07-10 20:31:16 +02:00
if !ok {
return err
}
if ec2err.Code() != "InvalidVpcID.NotFound" {
2014-07-10 20:31:16 +02:00
return err
}
}
return nil
}
func testAccCheckVpcCidr(vpc *ec2.Vpc, expected string) resource.TestCheckFunc {
2014-07-10 22:49:09 +02:00
return func(s *terraform.State) error {
CIDRBlock := vpc.CidrBlock
2015-02-25 10:34:17 +01:00
if *CIDRBlock != expected {
return fmt.Errorf("Bad cidr: %s", *vpc.CidrBlock)
2014-07-10 22:49:09 +02:00
}
return nil
}
}
func testAccCheckVpcExists(n string, vpc *ec2.Vpc) resource.TestCheckFunc {
2014-07-10 20:31:16 +02:00
return func(s *terraform.State) error {
2014-09-17 02:44:42 +02:00
rs, ok := s.RootModule().Resources[n]
2014-07-10 20:31:16 +02:00
if !ok {
return fmt.Errorf("Not found: %s", n)
}
2014-09-17 02:44:42 +02:00
if rs.Primary.ID == "" {
2014-07-10 20:31:16 +02:00
return fmt.Errorf("No VPC ID is set")
}
2015-04-16 22:05:55 +02:00
conn := testAccProvider.Meta().(*AWSClient).ec2conn
DescribeVpcOpts := &ec2.DescribeVpcsInput{
VpcIds: []*string{aws.String(rs.Primary.ID)},
2015-02-25 10:34:17 +01:00
}
resp, err := conn.DescribeVpcs(DescribeVpcOpts)
2014-07-10 20:31:16 +02:00
if err != nil {
return err
}
if len(resp.Vpcs) == 0 {
2014-07-10 20:31:16 +02:00
return fmt.Errorf("VPC not found")
}
*vpc = *resp.Vpcs[0]
2014-07-10 20:31:16 +02:00
return nil
}
}
// https://github.com/hashicorp/terraform/issues/1301
func TestAccAWSVpc_bothDnsOptionsSet(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{
{
Config: testAccVpcConfig_BothDnsOptions,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"aws_vpc.bar", "enable_dns_hostnames", "true"),
resource.TestCheckResourceAttr(
"aws_vpc.bar", "enable_dns_support", "true"),
),
},
},
})
}
// https://github.com/hashicorp/terraform/issues/10168
func TestAccAWSVpc_DisabledDnsSupport(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{
{
Config: testAccVpcConfig_DisabledDnsSupport,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"aws_vpc.bar", "enable_dns_support", "false"),
),
},
},
})
}
func TestAccAWSVpc_classiclinkOptionSet(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{
{
Config: testAccVpcConfig_ClassiclinkOption,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"aws_vpc.bar", "enable_classiclink", "true"),
),
},
},
})
}
2014-07-10 20:31:16 +02:00
const testAccVpcConfig = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
}
`
provider/aws: Implement IPV6 Support for ec2 / VPC (#10538) * provider/aws: Add support for IPV6 enabled VPC ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSVpc' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/12/09 14:07:31 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSVpc -timeout 120m === RUN TestAccAWSVpc_importBasic --- PASS: TestAccAWSVpc_importBasic (43.03s) === RUN TestAccAWSVpc_basic --- PASS: TestAccAWSVpc_basic (36.32s) === RUN TestAccAWSVpc_enableIpv6 --- PASS: TestAccAWSVpc_enableIpv6 (29.37s) === RUN TestAccAWSVpc_dedicatedTenancy --- PASS: TestAccAWSVpc_dedicatedTenancy (36.63s) === RUN TestAccAWSVpc_tags --- PASS: TestAccAWSVpc_tags (67.54s) === RUN TestAccAWSVpc_update --- PASS: TestAccAWSVpc_update (66.16s) === RUN TestAccAWSVpc_bothDnsOptionsSet --- PASS: TestAccAWSVpc_bothDnsOptionsSet (16.82s) === RUN TestAccAWSVpc_DisabledDnsSupport --- PASS: TestAccAWSVpc_DisabledDnsSupport (36.52s) === RUN TestAccAWSVpc_classiclinkOptionSet --- PASS: TestAccAWSVpc_classiclinkOptionSet (38.13s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 739.543s ``` * provider/aws: New Resource: aws_egress_only_internet_gateway ``` make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSEgressOnlyInternetGateway_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/12/09 14:22:16 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSEgressOnlyInternetGateway_ -timeout 120m === RUN TestAccAWSEgressOnlyInternetGateway_basic --- PASS: TestAccAWSEgressOnlyInternetGateway_basic (32.67s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 32.692s ``` * provider/aws: Add IPV6 support to aws_subnet ``` % make testacc TEST=./builtin/providers/aws % TESTARGS='-run=TestAccAWSSubnet_' % 1 ↵ ✹ ✭ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/02/27 19:08:34 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSubnet_ -timeout 120m === RUN TestAccAWSSubnet_importBasic --- PASS: TestAccAWSSubnet_importBasic (69.88s) === RUN TestAccAWSSubnet_basic --- PASS: TestAccAWSSubnet_basic (51.28s) === RUN TestAccAWSSubnet_ipv6 --- PASS: TestAccAWSSubnet_ipv6 (90.39s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws211.574s ``` * provider/aws: Add support for running aws_instances with ipv6 addresses
2017-03-01 17:16:59 +01:00
const testAccVpcConfigIpv6Enabled = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
assign_generated_ipv6_cidr_block = true
}
`
const testAccVpcConfigUpdate = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
enable_dns_hostnames = true
}
`
2014-10-09 02:54:00 +02:00
const testAccVpcConfigTags = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
tags {
foo = "bar"
}
}
`
2014-10-09 03:21:21 +02:00
const testAccVpcConfigTagsUpdate = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
tags {
bar = "baz"
}
}
`
2014-12-10 11:39:14 +01:00
const testAccVpcDedicatedConfig = `
resource "aws_vpc" "bar" {
instance_tenancy = "dedicated"
cidr_block = "10.2.0.0/16"
}
`
const testAccVpcConfig_BothDnsOptions = `
provider "aws" {
region = "eu-central-1"
}
resource "aws_vpc" "bar" {
cidr_block = "10.2.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
}
`
const testAccVpcConfig_DisabledDnsSupport = `
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "bar" {
cidr_block = "10.2.0.0/16"
enable_dns_support = false
}
`
const testAccVpcConfig_ClassiclinkOption = `
resource "aws_vpc" "bar" {
cidr_block = "172.2.0.0/16"
enable_classiclink = true
}
`