providers/aws: route table tests
This commit is contained in:
parent
265cc4fffa
commit
9afebeb07e
|
@ -288,7 +288,7 @@ func IGAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resourc
|
|||
|
||||
ig := &resp.InternetGateways[0]
|
||||
|
||||
if time.Now().Sub(start) > 10 * time.Second {
|
||||
if time.Now().Sub(start) > 10*time.Second {
|
||||
return ig, expected, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -4,9 +4,11 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform/flatmap"
|
||||
"github.com/hashicorp/terraform/helper/diff"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/mitchellh/goamz/ec2"
|
||||
)
|
||||
|
@ -33,6 +35,22 @@ func resource_aws_route_table_create(
|
|||
s.ID = rt.RouteTableId
|
||||
log.Printf("[INFO] Route Table ID: %s", s.ID)
|
||||
|
||||
// Wait for the route table to become available
|
||||
log.Printf(
|
||||
"[DEBUG] Waiting for route table (%s) to become available",
|
||||
s.ID)
|
||||
stateConf := &resource.StateChangeConf{
|
||||
Pending: []string{"pending"},
|
||||
Target: "ready",
|
||||
Refresh: RouteTableStateRefreshFunc(ec2conn, s.ID),
|
||||
Timeout: 1 * time.Minute,
|
||||
}
|
||||
if _, err := stateConf.WaitForState(); err != nil {
|
||||
return s, fmt.Errorf(
|
||||
"Error waiting for route table (%s) to become available: %s",
|
||||
s.ID, err)
|
||||
}
|
||||
|
||||
// Update our routes
|
||||
return resource_aws_route_table_update(s, d, meta)
|
||||
}
|
||||
|
@ -124,6 +142,22 @@ func resource_aws_route_table_destroy(
|
|||
return fmt.Errorf("Error deleting route table: %s", err)
|
||||
}
|
||||
|
||||
// Wait for the route table to really destroy
|
||||
log.Printf(
|
||||
"[DEBUG] Waiting for route table (%s) to become destroyed",
|
||||
s.ID)
|
||||
stateConf := &resource.StateChangeConf{
|
||||
Pending: []string{"ready"},
|
||||
Target: "",
|
||||
Refresh: RouteTableStateRefreshFunc(ec2conn, s.ID),
|
||||
Timeout: 1 * time.Minute,
|
||||
}
|
||||
if _, err := stateConf.WaitForState(); err != nil {
|
||||
return fmt.Errorf(
|
||||
"Error waiting for route table (%s) to become destroyed: %s",
|
||||
s.ID, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -133,21 +167,15 @@ func resource_aws_route_table_refresh(
|
|||
p := meta.(*ResourceProvider)
|
||||
ec2conn := p.ec2conn
|
||||
|
||||
resp, err := ec2conn.DescribeRouteTables([]string{s.ID}, ec2.NewFilter())
|
||||
rtRaw, _, err := RouteTableStateRefreshFunc(ec2conn, s.ID)()
|
||||
if err != nil {
|
||||
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidRouteTableID.NotFound" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
log.Printf("[ERROR] Error searching for route table: %s", err)
|
||||
return s, err
|
||||
}
|
||||
|
||||
if len(resp.RouteTables) == 0 {
|
||||
if rtRaw == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
rt := &resp.RouteTables[0]
|
||||
rt := rtRaw.(*ec2.RouteTable)
|
||||
return resource_aws_route_table_update_state(s, rt)
|
||||
}
|
||||
|
||||
|
@ -259,3 +287,28 @@ func routeTableOps(a interface{}, b interface{}) []routeTableOp {
|
|||
|
||||
return ops
|
||||
}
|
||||
|
||||
// RouteTableStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
||||
// a RouteTable.
|
||||
func RouteTableStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
||||
return func() (interface{}, string, error) {
|
||||
resp, err := conn.DescribeRouteTables([]string{id}, ec2.NewFilter())
|
||||
if err != nil {
|
||||
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidRouteTableID.NotFound" {
|
||||
resp = nil
|
||||
} else {
|
||||
log.Printf("Error on RouteTableStateRefresh: %s", err)
|
||||
return nil, "", err
|
||||
}
|
||||
}
|
||||
|
||||
if resp == nil {
|
||||
// Sometimes AWS just has consistency issues and doesn't see
|
||||
// our instance yet. Return an empty state.
|
||||
return nil, "", nil
|
||||
}
|
||||
|
||||
rt := &resp.RouteTables[0]
|
||||
return rt, "ready", nil
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,184 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/mitchellh/goamz/ec2"
|
||||
)
|
||||
|
||||
func TestAccAWSRouteTable(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)
|
||||
}
|
||||
|
||||
routes := make(map[string]ec2.Route)
|
||||
for _, r := range v.Routes {
|
||||
routes[r.DestinationCidrBlock] = r
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
routes := make(map[string]ec2.Route)
|
||||
for _, r := range v.Routes {
|
||||
routes[r.DestinationCidrBlock] = r
|
||||
}
|
||||
|
||||
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,
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckRouteTableDestroy(s *terraform.State) error {
|
||||
conn := testAccProvider.ec2conn
|
||||
|
||||
for _, rs := range s.Resources {
|
||||
if rs.Type != "aws_route_table" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Try to find the resource
|
||||
resp, err := conn.DescribeRouteTables(
|
||||
[]string{rs.ID}, ec2.NewFilter())
|
||||
if err == nil {
|
||||
if len(resp.RouteTables) > 0 {
|
||||
return fmt.Errorf("still exist.")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Verify the error is what we want
|
||||
ec2err, ok := err.(*ec2.Error)
|
||||
if !ok {
|
||||
return err
|
||||
}
|
||||
if ec2err.Code != "InvalidRouteTableID.NotFound" {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func testAccCheckRouteTableExists(n string, v *ec2.RouteTable) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
rs, ok := s.Resources[n]
|
||||
if !ok {
|
||||
return fmt.Errorf("Not found: %s", n)
|
||||
}
|
||||
|
||||
if rs.ID == "" {
|
||||
return fmt.Errorf("No ID is set")
|
||||
}
|
||||
|
||||
conn := testAccProvider.ec2conn
|
||||
resp, err := conn.DescribeRouteTables(
|
||||
[]string{rs.ID}, ec2.NewFilter())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(resp.RouteTables) == 0 {
|
||||
return fmt.Errorf("RouteTable not found")
|
||||
}
|
||||
|
||||
*v = resp.RouteTables[0]
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
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}"
|
||||
}
|
||||
}
|
||||
`
|
|
@ -70,6 +70,7 @@ func init() {
|
|||
Destroy: resource_aws_route_table_destroy,
|
||||
Diff: resource_aws_route_table_diff,
|
||||
Refresh: resource_aws_route_table_refresh,
|
||||
Update: resource_aws_route_table_update,
|
||||
},
|
||||
|
||||
"aws_security_group": resource.Resource{
|
||||
|
|
Loading…
Reference in New Issue