github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 TestAccAWSDefaultRouteTable_vpc_endpoint(t *testing.T) {
    69  	var v ec2.RouteTable
    70  
    71  	resource.Test(t, resource.TestCase{
    72  		PreCheck:      func() { testAccPreCheck(t) },
    73  		IDRefreshName: "aws_default_route_table.foo",
    74  		Providers:     testAccProviders,
    75  		CheckDestroy:  testAccCheckDefaultRouteTableDestroy,
    76  		Steps: []resource.TestStep{
    77  			resource.TestStep{
    78  				Config: testAccDefaultRouteTable_vpc_endpoint,
    79  				Check: resource.ComposeTestCheckFunc(
    80  					testAccCheckRouteTableExists(
    81  						"aws_default_route_table.foo", &v),
    82  				),
    83  			},
    84  		},
    85  	})
    86  }
    87  
    88  func testAccCheckDefaultRouteTableDestroy(s *terraform.State) error {
    89  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    90  
    91  	for _, rs := range s.RootModule().Resources {
    92  		if rs.Type != "aws_default_route_table" {
    93  			continue
    94  		}
    95  
    96  		// Try to find the resource
    97  		resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
    98  			RouteTableIds: []*string{aws.String(rs.Primary.ID)},
    99  		})
   100  		if err == nil {
   101  			if len(resp.RouteTables) > 0 {
   102  				return fmt.Errorf("still exist.")
   103  			}
   104  
   105  			return nil
   106  		}
   107  
   108  		// Verify the error is what we want
   109  		ec2err, ok := err.(awserr.Error)
   110  		if !ok {
   111  			return err
   112  		}
   113  		if ec2err.Code() != "InvalidRouteTableID.NotFound" {
   114  			return err
   115  		}
   116  	}
   117  
   118  	return nil
   119  }
   120  
   121  func testAccCheckDefaultRouteTableExists(s *terraform.State) error {
   122  	// We can't destroy this resource; it comes and goes with the VPC itself.
   123  	return nil
   124  }
   125  
   126  const testAccDefaultRouteTableConfig = `
   127  resource "aws_vpc" "foo" {
   128    cidr_block           = "10.1.0.0/16"
   129    enable_dns_hostnames = true
   130  
   131    tags {
   132      Name = "tf-default-route-table-test"
   133    }
   134  }
   135  
   136  resource "aws_default_route_table" "foo" {
   137    default_route_table_id = "${aws_vpc.foo.default_route_table_id}"
   138  
   139    route {
   140      cidr_block = "10.0.1.0/32"
   141      gateway_id = "${aws_internet_gateway.gw.id}"
   142    }
   143  
   144    tags {
   145      Name = "tf-default-route-table-test"
   146    }
   147  }
   148  
   149  resource "aws_internet_gateway" "gw" {
   150    vpc_id = "${aws_vpc.foo.id}"
   151  
   152    tags {
   153      Name = "tf-default-route-table-test"
   154    }
   155  }`
   156  
   157  const testAccDefaultRouteTable_change = `
   158  provider "aws" {
   159    region = "us-west-2"
   160  }
   161  
   162  resource "aws_vpc" "foo" {
   163    cidr_block           = "10.1.0.0/16"
   164    enable_dns_hostnames = true
   165  
   166    tags {
   167      Name = "tf-default-route-table"
   168    }
   169  }
   170  
   171  resource "aws_default_route_table" "foo" {
   172    default_route_table_id = "${aws_vpc.foo.default_route_table_id}"
   173  
   174    route {
   175      cidr_block = "10.0.1.0/32"
   176      gateway_id = "${aws_internet_gateway.gw.id}"
   177    }
   178  
   179    tags {
   180      Name = "this was the first main"
   181    }
   182  }
   183  
   184  resource "aws_internet_gateway" "gw" {
   185    vpc_id = "${aws_vpc.foo.id}"
   186  
   187    tags {
   188      Name = "main-igw"
   189    }
   190  }
   191  
   192  # Thing to help testing changes
   193  resource "aws_route_table" "r" {
   194    vpc_id = "${aws_vpc.foo.id}"
   195  
   196    route {
   197      cidr_block = "10.0.1.0/24"
   198      gateway_id = "${aws_internet_gateway.gw.id}"
   199    }
   200  
   201    tags {
   202      Name = "other"
   203    }
   204  }
   205  `
   206  
   207  const testAccDefaultRouteTable_change_mod = `
   208  provider "aws" {
   209    region = "us-west-2"
   210  }
   211  
   212  resource "aws_vpc" "foo" {
   213    cidr_block           = "10.1.0.0/16"
   214    enable_dns_hostnames = true
   215  
   216    tags {
   217      Name = "tf-default-route-table"
   218    }
   219  }
   220  
   221  resource "aws_default_route_table" "foo" {
   222    default_route_table_id = "${aws_vpc.foo.default_route_table_id}"
   223  
   224    route {
   225      cidr_block = "10.0.1.0/32"
   226      gateway_id = "${aws_internet_gateway.gw.id}"
   227    }
   228  
   229    tags {
   230      Name = "this was the first main"
   231    }
   232  }
   233  
   234  resource "aws_internet_gateway" "gw" {
   235    vpc_id = "${aws_vpc.foo.id}"
   236  
   237    tags {
   238      Name = "main-igw"
   239    }
   240  }
   241  
   242  # Thing to help testing changes
   243  resource "aws_route_table" "r" {
   244    vpc_id = "${aws_vpc.foo.id}"
   245  
   246    route {
   247      cidr_block = "10.0.1.0/24"
   248      gateway_id = "${aws_internet_gateway.gw.id}"
   249    }
   250  
   251    tags {
   252      Name = "other"
   253    }
   254  }
   255  
   256  resource "aws_main_route_table_association" "a" {
   257    vpc_id         = "${aws_vpc.foo.id}"
   258    route_table_id = "${aws_route_table.r.id}"
   259  }
   260  `
   261  
   262  const testAccDefaultRouteTable_vpc_endpoint = `
   263  provider "aws" {
   264      region = "us-west-2"
   265  }
   266  
   267  resource "aws_vpc" "test" {
   268      cidr_block = "10.0.0.0/16"
   269  
   270      tags {
   271          Name = "test"
   272      }
   273  }
   274  
   275  resource "aws_internet_gateway" "igw" {
   276      vpc_id = "${aws_vpc.test.id}"
   277  
   278      tags {
   279          Name = "test"
   280      }
   281  }
   282  
   283  resource "aws_vpc_endpoint" "s3" {
   284      vpc_id = "${aws_vpc.test.id}"
   285      service_name = "com.amazonaws.us-west-2.s3"
   286      route_table_ids = [
   287          "${aws_vpc.test.default_route_table_id}"
   288      ]
   289  }
   290  
   291  resource "aws_default_route_table" "foo" {
   292      default_route_table_id = "${aws_vpc.test.default_route_table_id}"
   293  
   294      tags {
   295          Name = "test"
   296      }
   297  
   298      route {
   299          cidr_block = "0.0.0.0/0"
   300          gateway_id = "${aws_internet_gateway.igw.id}"
   301      }
   302  }
   303  `