github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_default_route_table_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSDefaultRouteTable_basic(t *testing.T) {
    15  	var v ec2.RouteTable
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:      func() { testAccPreCheck(t) },
    19  		IDRefreshName: "aws_default_route_table.foo",
    20  		Providers:     testAccProviders,
    21  		CheckDestroy:  testAccCheckDefaultRouteTableDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccDefaultRouteTableConfig,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckRouteTableExists(
    27  						"aws_default_route_table.foo", &v),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAWSDefaultRouteTable_swap(t *testing.T) {
    35  	var v ec2.RouteTable
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:      func() { testAccPreCheck(t) },
    39  		IDRefreshName: "aws_default_route_table.foo",
    40  		Providers:     testAccProviders,
    41  		CheckDestroy:  testAccCheckDefaultRouteTableDestroy,
    42  		Steps: []resource.TestStep{
    43  			resource.TestStep{
    44  				Config: testAccDefaultRouteTable_change,
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testAccCheckRouteTableExists(
    47  						"aws_default_route_table.foo", &v),
    48  				),
    49  			},
    50  
    51  			// This config will swap out the original Default Route Table and replace
    52  			// it with the custom route table. While this is not advised, it's a
    53  			// behavior that may happen, in which case a follow up plan will show (in
    54  			// this case) a diff as the table now needs to be updated to match the
    55  			// config
    56  			resource.TestStep{
    57  				Config: testAccDefaultRouteTable_change_mod,
    58  				Check: resource.ComposeTestCheckFunc(
    59  					testAccCheckRouteTableExists(
    60  						"aws_default_route_table.foo", &v),
    61  				),
    62  				ExpectNonEmptyPlan: true,
    63  			},
    64  		},
    65  	})
    66  }
    67  
    68  func testAccCheckDefaultRouteTableDestroy(s *terraform.State) error {
    69  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    70  
    71  	for _, rs := range s.RootModule().Resources {
    72  		if rs.Type != "aws_default_route_table" {
    73  			continue
    74  		}
    75  
    76  		// Try to find the resource
    77  		resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
    78  			RouteTableIds: []*string{aws.String(rs.Primary.ID)},
    79  		})
    80  		if err == nil {
    81  			if len(resp.RouteTables) > 0 {
    82  				return fmt.Errorf("still exist.")
    83  			}
    84  
    85  			return nil
    86  		}
    87  
    88  		// Verify the error is what we want
    89  		ec2err, ok := err.(awserr.Error)
    90  		if !ok {
    91  			return err
    92  		}
    93  		if ec2err.Code() != "InvalidRouteTableID.NotFound" {
    94  			return err
    95  		}
    96  	}
    97  
    98  	return nil
    99  }
   100  
   101  func testAccCheckDefaultRouteTableExists(s *terraform.State) error {
   102  	// We can't destroy this resource; it comes and goes with the VPC itself.
   103  	return nil
   104  }
   105  
   106  const testAccDefaultRouteTableConfig = `
   107  resource "aws_vpc" "foo" {
   108    cidr_block           = "10.1.0.0/16"
   109    enable_dns_hostnames = true
   110  
   111    tags {
   112      Name = "tf-default-route-table-test"
   113    }
   114  }
   115  
   116  resource "aws_default_route_table" "foo" {
   117    default_route_table_id = "${aws_vpc.foo.default_route_table_id}"
   118  
   119    route {
   120      cidr_block = "10.0.1.0/32"
   121      gateway_id = "${aws_internet_gateway.gw.id}"
   122    }
   123  
   124    tags {
   125      Name = "tf-default-route-table-test"
   126    }
   127  }
   128  
   129  resource "aws_internet_gateway" "gw" {
   130    vpc_id = "${aws_vpc.foo.id}"
   131  
   132    tags {
   133      Name = "tf-default-route-table-test"
   134    }
   135  }`
   136  
   137  const testAccDefaultRouteTable_change = `
   138  provider "aws" {
   139    region = "us-west-2"
   140  }
   141  
   142  resource "aws_vpc" "foo" {
   143    cidr_block           = "10.1.0.0/16"
   144    enable_dns_hostnames = true
   145  
   146    tags {
   147      Name = "tf-default-route-table"
   148    }
   149  }
   150  
   151  resource "aws_default_route_table" "foo" {
   152    default_route_table_id = "${aws_vpc.foo.default_route_table_id}"
   153  
   154    route {
   155      cidr_block = "10.0.1.0/32"
   156      gateway_id = "${aws_internet_gateway.gw.id}"
   157    }
   158  
   159    tags {
   160      Name = "this was the first main"
   161    }
   162  }
   163  
   164  resource "aws_internet_gateway" "gw" {
   165    vpc_id = "${aws_vpc.foo.id}"
   166  
   167    tags {
   168      Name = "main-igw"
   169    }
   170  }
   171  
   172  # Thing to help testing changes
   173  resource "aws_route_table" "r" {
   174    vpc_id = "${aws_vpc.foo.id}"
   175  
   176    route {
   177      cidr_block = "10.0.1.0/24"
   178      gateway_id = "${aws_internet_gateway.gw.id}"
   179    }
   180  
   181    tags {
   182      Name = "other"
   183    }
   184  }
   185  `
   186  
   187  const testAccDefaultRouteTable_change_mod = `
   188  provider "aws" {
   189    region = "us-west-2"
   190  }
   191  
   192  resource "aws_vpc" "foo" {
   193    cidr_block           = "10.1.0.0/16"
   194    enable_dns_hostnames = true
   195  
   196    tags {
   197      Name = "tf-default-route-table"
   198    }
   199  }
   200  
   201  resource "aws_default_route_table" "foo" {
   202    default_route_table_id = "${aws_vpc.foo.default_route_table_id}"
   203  
   204    route {
   205      cidr_block = "10.0.1.0/32"
   206      gateway_id = "${aws_internet_gateway.gw.id}"
   207    }
   208  
   209    tags {
   210      Name = "this was the first main"
   211    }
   212  }
   213  
   214  resource "aws_internet_gateway" "gw" {
   215    vpc_id = "${aws_vpc.foo.id}"
   216  
   217    tags {
   218      Name = "main-igw"
   219    }
   220  }
   221  
   222  # Thing to help testing changes
   223  resource "aws_route_table" "r" {
   224    vpc_id = "${aws_vpc.foo.id}"
   225  
   226    route {
   227      cidr_block = "10.0.1.0/24"
   228      gateway_id = "${aws_internet_gateway.gw.id}"
   229    }
   230  
   231    tags {
   232      Name = "other"
   233    }
   234  }
   235  
   236  resource "aws_main_route_table_association" "a" {
   237    vpc_id         = "${aws_vpc.foo.id}"
   238    route_table_id = "${aws_route_table.r.id}"
   239  }
   240  `