Skip to content

Commit

Permalink
Improve acceptance tests for route table import
Browse files Browse the repository at this point in the history
Use checks that align with inline routes in the state. Improve
tests to avoid network values that will conflict with fewer AWS
accounts.

Previously, route table import acceptance tests only checked the
length of the state (1 for route table, and 1 for each separate
route resource). This simple test did not check much, especially
when checking route tables with inline routes.

Now, the state is checked to make sure it has routes and a route-
count attribute that matches to expected value.
  • Loading branch information
YakDriver committed Dec 26, 2018
1 parent 3f472eb commit 68f28fa
Showing 1 changed file with 57 additions and 20 deletions.
77 changes: 57 additions & 20 deletions aws/import_aws_route_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (

func TestAccAWSRouteTable_importBasic(t *testing.T) {
checkFn := func(s []*terraform.InstanceState) error {
// Expect 2: group, 1 rules
if len(s) != 2 {
return fmt.Errorf("bad states: %#v", s)
// Expect, 1 resource in state, and route count to be 1
v, ok := s[0].Attributes["route.#"]
if len(s) != 1 || !ok || v != "1" {
return fmt.Errorf("bad state: %s", s)
}

return nil
Expand All @@ -36,11 +37,12 @@ func TestAccAWSRouteTable_importBasic(t *testing.T) {
})
}

func TestAccAWSRouteTable_complex(t *testing.T) {
func TestAccAWSRouteTable_importWithCreate(t *testing.T) {
checkFn := func(s []*terraform.InstanceState) error {
// Expect 3: group, 2 rules
if len(s) != 3 {
return fmt.Errorf("bad states: %#v", s)
// Expect, 1 resource in state, and route count to be 1
v, ok := s[0].Attributes["route.#"]
if len(s) != 1 || !ok || v != "1" {
return fmt.Errorf("bad state: %s", s)
}

return nil
Expand All @@ -52,7 +54,42 @@ func TestAccAWSRouteTable_complex(t *testing.T) {
CheckDestroy: testAccCheckRouteTableDestroy,
Steps: []resource.TestStep{
{
Config: testAccRouteTableConfig_complexImport,
Config: testAccRouteTableConfig,
ImportStateCheck: checkFn,
},

{
ResourceName: "aws_route_table.foo",
ImportState: true,
ImportStateVerify: true,
},

{
Config: testAccRouteTableConfig,
ImportStateCheck: checkFn,
},
},
})
}

func TestAccAWSRouteTable_importComplex(t *testing.T) {
checkFn := func(s []*terraform.InstanceState) error {
// Expect, 1 resource in state, and route count to be 2
v, ok := s[0].Attributes["route.#"]
if len(s) != 1 || !ok || v != "2" {
return fmt.Errorf("bad state: %s", s)
}

return nil
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRouteTableDestroy,
Steps: []resource.TestStep{
{
Config: testAccRouteTableConfigImportComplex,
},

{
Expand All @@ -64,9 +101,9 @@ func TestAccAWSRouteTable_complex(t *testing.T) {
})
}

const testAccRouteTableConfig_complexImport = `
resource "aws_vpc" "default" {
cidr_block = "10.0.0.0/16"
const testAccRouteTableConfigImportComplex = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
enable_dns_hostnames = true
tags = {
Expand All @@ -75,8 +112,8 @@ resource "aws_vpc" "default" {
}
resource "aws_subnet" "tf_test_subnet" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "10.0.0.0/24"
vpc_id = "${aws_vpc.foo.id}"
cidr_block = "10.1.0.0/24"
map_public_ip_on_launch = true
tags = {
Expand All @@ -86,19 +123,19 @@ resource "aws_subnet" "tf_test_subnet" {
resource "aws_eip" "nat" {
vpc = true
associate_with_private_ip = "10.0.0.10"
associate_with_private_ip = "10.1.0.10"
}
resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.default.id}"
vpc_id = "${aws_vpc.foo.id}"
tags = {
Name = "terraform-testacc-route-table-import-complex-default"
}
}
variable "private_subnet_cidrs" {
default = "10.0.0.0/24"
default = "10.1.0.0/24"
}
resource "aws_nat_gateway" "nat" {
Expand All @@ -113,7 +150,7 @@ resource "aws_nat_gateway" "nat" {
resource "aws_route_table" "mod" {
count = "${length(split(",", var.private_subnet_cidrs))}"
vpc_id = "${aws_vpc.default.id}"
vpc_id = "${aws_vpc.foo.id}"
tags = {
Name = "tf-rt-import-test"
Expand All @@ -135,15 +172,15 @@ resource "aws_route" "mod" {
}
resource "aws_vpc_endpoint" "s3" {
vpc_id = "${aws_vpc.default.id}"
vpc_id = "${aws_vpc.foo.id}"
service_name = "com.amazonaws.us-west-2.s3"
route_table_ids = ["${aws_route_table.mod.*.id}"]
}
### vpc bar
resource "aws_vpc" "bar" {
cidr_block = "10.1.0.0/16"
cidr_block = "10.2.0.0/16"
tags = {
Name = "terraform-testacc-route-table-import-complex-bar"
Expand All @@ -161,7 +198,7 @@ resource "aws_internet_gateway" "ogw" {
### vpc peer connection
resource "aws_vpc_peering_connection" "foo" {
vpc_id = "${aws_vpc.default.id}"
vpc_id = "${aws_vpc.foo.id}"
peer_vpc_id = "${aws_vpc.bar.id}"
peer_owner_id = "187416307283"
Expand Down

0 comments on commit 68f28fa

Please sign in to comment.