github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_main_route_table_association_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccAWSMainRouteTableAssociation_basic(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck:     func() { testAccPreCheck(t) },
    14  		Providers:    testAccProviders,
    15  		CheckDestroy: testAccCheckMainRouteTableAssociationDestroy,
    16  		Steps: []resource.TestStep{
    17  			resource.TestStep{
    18  				Config: testAccMainRouteTableAssociationConfig,
    19  				Check: resource.ComposeTestCheckFunc(
    20  					testAccCheckMainRouteTableAssociation(
    21  						"aws_main_route_table_association.foo",
    22  						"aws_vpc.foo",
    23  						"aws_route_table.foo",
    24  					),
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccMainRouteTableAssociationConfigUpdate,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckMainRouteTableAssociation(
    31  						"aws_main_route_table_association.foo",
    32  						"aws_vpc.foo",
    33  						"aws_route_table.bar",
    34  					),
    35  				),
    36  			},
    37  		},
    38  	})
    39  }
    40  
    41  func testAccCheckMainRouteTableAssociationDestroy(s *terraform.State) error {
    42  	if len(s.RootModule().Resources) > 0 {
    43  		return fmt.Errorf("Expected all resources to be gone, but found: %#v", s.RootModule().Resources)
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func testAccCheckMainRouteTableAssociation(
    50  	mainRouteTableAssociationResource string,
    51  	vpcResource string,
    52  	routeTableResource string) resource.TestCheckFunc {
    53  	return func(s *terraform.State) error {
    54  		rs, ok := s.RootModule().Resources[mainRouteTableAssociationResource]
    55  		if !ok {
    56  			return fmt.Errorf("Not found: %s", mainRouteTableAssociationResource)
    57  		}
    58  
    59  		if rs.Primary.ID == "" {
    60  			return fmt.Errorf("No ID is set")
    61  		}
    62  
    63  		vpc, ok := s.RootModule().Resources[vpcResource]
    64  		if !ok {
    65  			return fmt.Errorf("Not found: %s", vpcResource)
    66  		}
    67  
    68  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    69  		mainAssociation, err := findMainRouteTableAssociation(conn, vpc.Primary.ID)
    70  		if err != nil {
    71  			return err
    72  		}
    73  
    74  		if *mainAssociation.RouteTableAssociationId != rs.Primary.ID {
    75  			return fmt.Errorf("Found wrong main association: %s",
    76  				*mainAssociation.RouteTableAssociationId)
    77  		}
    78  
    79  		return nil
    80  	}
    81  }
    82  
    83  const testAccMainRouteTableAssociationConfig = `
    84  resource "aws_vpc" "foo" {
    85  	cidr_block = "10.1.0.0/16"
    86  }
    87  
    88  resource "aws_subnet" "foo" {
    89  	vpc_id = "${aws_vpc.foo.id}"
    90  	cidr_block = "10.1.1.0/24"
    91  }
    92  
    93  resource "aws_internet_gateway" "foo" {
    94  	vpc_id = "${aws_vpc.foo.id}"
    95  }
    96  
    97  resource "aws_route_table" "foo" {
    98  	vpc_id = "${aws_vpc.foo.id}"
    99  	route {
   100  		cidr_block = "10.0.0.0/8"
   101  		gateway_id = "${aws_internet_gateway.foo.id}"
   102  	}
   103  }
   104  
   105  resource "aws_main_route_table_association" "foo" {
   106  	vpc_id = "${aws_vpc.foo.id}"
   107  	route_table_id = "${aws_route_table.foo.id}"
   108  }
   109  `
   110  
   111  const testAccMainRouteTableAssociationConfigUpdate = `
   112  resource "aws_vpc" "foo" {
   113  	cidr_block = "10.1.0.0/16"
   114  }
   115  
   116  resource "aws_subnet" "foo" {
   117  	vpc_id = "${aws_vpc.foo.id}"
   118  	cidr_block = "10.1.1.0/24"
   119  }
   120  
   121  resource "aws_internet_gateway" "foo" {
   122  	vpc_id = "${aws_vpc.foo.id}"
   123  }
   124  
   125  // Need to keep the old route table around when we update the
   126  // main_route_table_association, otherwise Terraform will try to destroy the
   127  // route table too early, and will fail because it's still the main one
   128  resource "aws_route_table" "foo" {
   129  	vpc_id = "${aws_vpc.foo.id}"
   130  	route {
   131  		cidr_block = "10.0.0.0/8"
   132  		gateway_id = "${aws_internet_gateway.foo.id}"
   133  	}
   134  }
   135  
   136  resource "aws_route_table" "bar" {
   137  	vpc_id = "${aws_vpc.foo.id}"
   138  	route {
   139  		cidr_block = "10.0.0.0/8"
   140  		gateway_id = "${aws_internet_gateway.foo.id}"
   141  	}
   142  }
   143  
   144  resource "aws_main_route_table_association" "foo" {
   145  	vpc_id = "${aws_vpc.foo.id}"
   146  	route_table_id = "${aws_route_table.bar.id}"
   147  }
   148  `