2014-07-11 00:52:56 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-04-11 00:09:36 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
2015-05-20 13:21:23 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws/awserr"
|
2015-04-11 00:09:36 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
2014-07-11 00:52:56 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2014-07-17 18:21:55 +02:00
|
|
|
func TestAccAWSRouteTable_normal(t *testing.T) {
|
2014-07-11 00:52:56 +02:00
|
|
|
var v ec2.RouteTable
|
|
|
|
|
|
|
|
testCheck := func(*terraform.State) error {
|
|
|
|
if len(v.Routes) != 2 {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
|
2015-04-11 00:09:36 +02:00
|
|
|
routes := make(map[string]*ec2.Route)
|
2014-07-11 00:52:56 +02:00
|
|
|
for _, r := range v.Routes {
|
2015-03-10 16:30:01 +01:00
|
|
|
routes[*r.DestinationCIDRBlock] = r
|
2014-07-11 00:52:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := routes["10.1.0.0/16"]; !ok {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
if _, ok := routes["10.2.0.0/16"]; !ok {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
testCheckChange := func(*terraform.State) error {
|
|
|
|
if len(v.Routes) != 3 {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
|
2015-04-11 00:09:36 +02:00
|
|
|
routes := make(map[string]*ec2.Route)
|
2014-07-11 00:52:56 +02:00
|
|
|
for _, r := range v.Routes {
|
2015-03-10 16:30:01 +01:00
|
|
|
routes[*r.DestinationCIDRBlock] = r
|
2014-07-11 00:52:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := routes["10.1.0.0/16"]; !ok {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
if _, ok := routes["10.3.0.0/16"]; !ok {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
if _, ok := routes["10.4.0.0/16"]; !ok {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckRouteTableDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRouteTableConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRouteTableExists(
|
|
|
|
"aws_route_table.foo", &v),
|
|
|
|
testCheck,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRouteTableConfigChange,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRouteTableExists(
|
|
|
|
"aws_route_table.foo", &v),
|
|
|
|
testCheckChange,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-17 18:21:55 +02:00
|
|
|
func TestAccAWSRouteTable_instance(t *testing.T) {
|
|
|
|
var v ec2.RouteTable
|
|
|
|
|
|
|
|
testCheck := func(*terraform.State) error {
|
|
|
|
if len(v.Routes) != 2 {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
|
2015-04-11 00:09:36 +02:00
|
|
|
routes := make(map[string]*ec2.Route)
|
2014-07-17 18:21:55 +02:00
|
|
|
for _, r := range v.Routes {
|
2015-03-10 16:30:01 +01:00
|
|
|
routes[*r.DestinationCIDRBlock] = r
|
2014-07-17 18:21:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := routes["10.1.0.0/16"]; !ok {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
if _, ok := routes["10.2.0.0/16"]; !ok {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckRouteTableDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRouteTableConfigInstance,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRouteTableExists(
|
|
|
|
"aws_route_table.foo", &v),
|
|
|
|
testCheck,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-10 19:33:13 +01:00
|
|
|
func TestAccAWSRouteTable_tags(t *testing.T) {
|
|
|
|
var route_table ec2.RouteTable
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckRouteTableDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2015-02-10 17:50:29 +01:00
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRouteTableConfigTags,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRouteTableExists("aws_route_table.foo", &route_table),
|
2015-05-12 21:58:10 +02:00
|
|
|
testAccCheckTags(&route_table.Tags, "foo", "bar"),
|
2015-02-10 17:50:29 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRouteTableConfigTagsUpdate,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRouteTableExists("aws_route_table.foo", &route_table),
|
2015-05-12 21:58:10 +02:00
|
|
|
testAccCheckTags(&route_table.Tags, "foo", ""),
|
|
|
|
testAccCheckTags(&route_table.Tags, "bar", "baz"),
|
2015-02-10 17:50:29 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
2014-12-10 19:33:13 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-11 00:52:56 +02:00
|
|
|
func testAccCheckRouteTableDestroy(s *terraform.State) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2014-07-11 00:52:56 +02:00
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-11 00:52:56 +02:00
|
|
|
if rs.Type != "aws_route_table" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the resource
|
2015-04-11 00:09:36 +02:00
|
|
|
resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
|
|
|
|
RouteTableIDs: []*string{aws.String(rs.Primary.ID)},
|
2015-03-10 16:30:01 +01:00
|
|
|
})
|
2014-07-11 00:52:56 +02:00
|
|
|
if err == nil {
|
|
|
|
if len(resp.RouteTables) > 0 {
|
|
|
|
return fmt.Errorf("still exist.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error is what we want
|
2015-05-20 13:21:23 +02:00
|
|
|
ec2err, ok := err.(awserr.Error)
|
2014-07-11 00:52:56 +02:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err.Code() != "InvalidRouteTableID.NotFound" {
|
2014-07-11 00:52:56 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckRouteTableExists(n string, v *ec2.RouteTable) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-11 00:52:56 +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-11 00:52:56 +02:00
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2015-04-11 00:09:36 +02:00
|
|
|
resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
|
|
|
|
RouteTableIDs: []*string{aws.String(rs.Primary.ID)},
|
2015-03-10 16:30:01 +01:00
|
|
|
})
|
2014-07-11 00:52:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(resp.RouteTables) == 0 {
|
|
|
|
return fmt.Errorf("RouteTable not found")
|
|
|
|
}
|
|
|
|
|
2015-04-11 00:09:36 +02:00
|
|
|
*v = *resp.RouteTables[0]
|
2014-07-11 00:52:56 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 16:23:14 +01:00
|
|
|
// TODO: re-enable this test.
|
|
|
|
// VPC Peering connections are prefixed with pcx
|
|
|
|
// Right now there is no VPC Peering resource
|
|
|
|
func _TestAccAWSRouteTable_vpcPeering(t *testing.T) {
|
2015-02-10 17:50:29 +01:00
|
|
|
var v ec2.RouteTable
|
|
|
|
|
|
|
|
testCheck := func(*terraform.State) error {
|
|
|
|
if len(v.Routes) != 2 {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
|
2015-04-11 00:09:36 +02:00
|
|
|
routes := make(map[string]*ec2.Route)
|
2015-02-10 17:50:29 +01:00
|
|
|
for _, r := range v.Routes {
|
2015-03-10 16:30:01 +01:00
|
|
|
routes[*r.DestinationCIDRBlock] = r
|
2015-02-10 17:50:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := routes["10.1.0.0/16"]; !ok {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
if _, ok := routes["10.2.0.0/16"]; !ok {
|
|
|
|
return fmt.Errorf("bad routes: %#v", v.Routes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckRouteTableDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRouteTableVpcPeeringConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRouteTableExists(
|
|
|
|
"aws_route_table.foo", &v),
|
|
|
|
testCheck,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-14 02:02:09 +02:00
|
|
|
func TestAccAWSRouteTable_vgwRoutePropagation(t *testing.T) {
|
|
|
|
var v ec2.RouteTable
|
|
|
|
var vgw ec2.VPNGateway
|
|
|
|
|
|
|
|
testCheck := func(*terraform.State) error {
|
|
|
|
if len(v.PropagatingVGWs) != 1 {
|
|
|
|
return fmt.Errorf("bad propagating vgws: %#v", v.PropagatingVGWs)
|
|
|
|
}
|
|
|
|
|
|
|
|
propagatingVGWs := make(map[string]*ec2.PropagatingVGW)
|
|
|
|
for _, gw := range v.PropagatingVGWs {
|
|
|
|
propagatingVGWs[*gw.GatewayID] = gw
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := propagatingVGWs[*vgw.VPNGatewayID]; !ok {
|
|
|
|
return fmt.Errorf("bad propagating vgws: %#v", v.PropagatingVGWs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpnGatewayDestroy,
|
|
|
|
testAccCheckRouteTableDestroy,
|
|
|
|
),
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRouteTableVgwRoutePropagationConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRouteTableExists(
|
|
|
|
"aws_route_table.foo", &v),
|
|
|
|
testAccCheckVpnGatewayExists(
|
|
|
|
"aws_vpn_gateway.foo", &vgw),
|
|
|
|
testCheck,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-11 00:52:56 +02:00
|
|
|
const testAccRouteTableConfig = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_internet_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
route {
|
|
|
|
cidr_block = "10.2.0.0/16"
|
|
|
|
gateway_id = "${aws_internet_gateway.foo.id}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccRouteTableConfigChange = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_internet_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
route {
|
|
|
|
cidr_block = "10.3.0.0/16"
|
|
|
|
gateway_id = "${aws_internet_gateway.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
route {
|
|
|
|
cidr_block = "10.4.0.0/16"
|
|
|
|
gateway_id = "${aws_internet_gateway.foo.id}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2014-07-17 18:21:55 +02:00
|
|
|
|
|
|
|
const testAccRouteTableConfigInstance = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
route {
|
|
|
|
cidr_block = "10.2.0.0/16"
|
|
|
|
instance_id = "${aws_instance.foo.id}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2014-12-10 19:33:13 +01:00
|
|
|
|
|
|
|
const testAccRouteTableConfigTags = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccRouteTableConfigTagsUpdate = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
bar = "baz"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2015-02-10 17:50:29 +01:00
|
|
|
|
2015-03-10 16:23:14 +01:00
|
|
|
// TODO: re-enable this test.
|
|
|
|
// VPC Peering connections are prefixed with pcx
|
|
|
|
// Right now there is no VPC Peering resource
|
2015-02-10 17:50:29 +01:00
|
|
|
const testAccRouteTableVpcPeeringConfig = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_internet_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
route {
|
|
|
|
cidr_block = "10.2.0.0/16"
|
2015-03-10 16:23:14 +01:00
|
|
|
vpc_peering_connection_id = "pcx-12345"
|
2015-02-10 17:50:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2015-04-14 02:02:09 +02:00
|
|
|
|
|
|
|
const testAccRouteTableVgwRoutePropagationConfig = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpn_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
propagating_vgws = ["${aws_vpn_gateway.foo.id}"]
|
|
|
|
}
|
|
|
|
`
|