github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_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 TestAccAWSRouteTable_basic(t *testing.T) {
    15  	var v ec2.RouteTable
    16  
    17  	testCheck := func(*terraform.State) error {
    18  		if len(v.Routes) != 2 {
    19  			return fmt.Errorf("bad routes: %#v", v.Routes)
    20  		}
    21  
    22  		routes := make(map[string]*ec2.Route)
    23  		for _, r := range v.Routes {
    24  			routes[*r.DestinationCidrBlock] = r
    25  		}
    26  
    27  		if _, ok := routes["10.1.0.0/16"]; !ok {
    28  			return fmt.Errorf("bad routes: %#v", v.Routes)
    29  		}
    30  		if _, ok := routes["10.2.0.0/16"]; !ok {
    31  			return fmt.Errorf("bad routes: %#v", v.Routes)
    32  		}
    33  
    34  		return nil
    35  	}
    36  
    37  	testCheckChange := func(*terraform.State) error {
    38  		if len(v.Routes) != 3 {
    39  			return fmt.Errorf("bad routes: %#v", v.Routes)
    40  		}
    41  
    42  		routes := make(map[string]*ec2.Route)
    43  		for _, r := range v.Routes {
    44  			routes[*r.DestinationCidrBlock] = r
    45  		}
    46  
    47  		if _, ok := routes["10.1.0.0/16"]; !ok {
    48  			return fmt.Errorf("bad routes: %#v", v.Routes)
    49  		}
    50  		if _, ok := routes["10.3.0.0/16"]; !ok {
    51  			return fmt.Errorf("bad routes: %#v", v.Routes)
    52  		}
    53  		if _, ok := routes["10.4.0.0/16"]; !ok {
    54  			return fmt.Errorf("bad routes: %#v", v.Routes)
    55  		}
    56  
    57  		return nil
    58  	}
    59  
    60  	resource.Test(t, resource.TestCase{
    61  		PreCheck:      func() { testAccPreCheck(t) },
    62  		IDRefreshName: "aws_route_table.foo",
    63  		Providers:     testAccProviders,
    64  		CheckDestroy:  testAccCheckRouteTableDestroy,
    65  		Steps: []resource.TestStep{
    66  			resource.TestStep{
    67  				Config: testAccRouteTableConfig,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckRouteTableExists(
    70  						"aws_route_table.foo", &v),
    71  					testCheck,
    72  				),
    73  			},
    74  
    75  			resource.TestStep{
    76  				Config: testAccRouteTableConfigChange,
    77  				Check: resource.ComposeTestCheckFunc(
    78  					testAccCheckRouteTableExists(
    79  						"aws_route_table.foo", &v),
    80  					testCheckChange,
    81  				),
    82  			},
    83  		},
    84  	})
    85  }
    86  
    87  func TestAccAWSRouteTable_instance(t *testing.T) {
    88  	var v ec2.RouteTable
    89  
    90  	testCheck := func(*terraform.State) error {
    91  		if len(v.Routes) != 2 {
    92  			return fmt.Errorf("bad routes: %#v", v.Routes)
    93  		}
    94  
    95  		routes := make(map[string]*ec2.Route)
    96  		for _, r := range v.Routes {
    97  			routes[*r.DestinationCidrBlock] = r
    98  		}
    99  
   100  		if _, ok := routes["10.1.0.0/16"]; !ok {
   101  			return fmt.Errorf("bad routes: %#v", v.Routes)
   102  		}
   103  		if _, ok := routes["10.2.0.0/16"]; !ok {
   104  			return fmt.Errorf("bad routes: %#v", v.Routes)
   105  		}
   106  
   107  		return nil
   108  	}
   109  
   110  	resource.Test(t, resource.TestCase{
   111  		PreCheck:      func() { testAccPreCheck(t) },
   112  		IDRefreshName: "aws_route_table.foo",
   113  		Providers:     testAccProviders,
   114  		CheckDestroy:  testAccCheckRouteTableDestroy,
   115  		Steps: []resource.TestStep{
   116  			resource.TestStep{
   117  				Config: testAccRouteTableConfigInstance,
   118  				Check: resource.ComposeTestCheckFunc(
   119  					testAccCheckRouteTableExists(
   120  						"aws_route_table.foo", &v),
   121  					testCheck,
   122  				),
   123  			},
   124  		},
   125  	})
   126  }
   127  
   128  func TestAccAWSRouteTable_tags(t *testing.T) {
   129  	var route_table ec2.RouteTable
   130  
   131  	resource.Test(t, resource.TestCase{
   132  		PreCheck:      func() { testAccPreCheck(t) },
   133  		IDRefreshName: "aws_route_table.foo",
   134  		Providers:     testAccProviders,
   135  		CheckDestroy:  testAccCheckRouteTableDestroy,
   136  		Steps: []resource.TestStep{
   137  			resource.TestStep{
   138  				Config: testAccRouteTableConfigTags,
   139  				Check: resource.ComposeTestCheckFunc(
   140  					testAccCheckRouteTableExists("aws_route_table.foo", &route_table),
   141  					testAccCheckTags(&route_table.Tags, "foo", "bar"),
   142  				),
   143  			},
   144  
   145  			resource.TestStep{
   146  				Config: testAccRouteTableConfigTagsUpdate,
   147  				Check: resource.ComposeTestCheckFunc(
   148  					testAccCheckRouteTableExists("aws_route_table.foo", &route_table),
   149  					testAccCheckTags(&route_table.Tags, "foo", ""),
   150  					testAccCheckTags(&route_table.Tags, "bar", "baz"),
   151  				),
   152  			},
   153  		},
   154  	})
   155  }
   156  
   157  func testAccCheckRouteTableDestroy(s *terraform.State) error {
   158  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
   159  
   160  	for _, rs := range s.RootModule().Resources {
   161  		if rs.Type != "aws_route_table" {
   162  			continue
   163  		}
   164  
   165  		// Try to find the resource
   166  		resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
   167  			RouteTableIds: []*string{aws.String(rs.Primary.ID)},
   168  		})
   169  		if err == nil {
   170  			if len(resp.RouteTables) > 0 {
   171  				return fmt.Errorf("still exist.")
   172  			}
   173  
   174  			return nil
   175  		}
   176  
   177  		// Verify the error is what we want
   178  		ec2err, ok := err.(awserr.Error)
   179  		if !ok {
   180  			return err
   181  		}
   182  		if ec2err.Code() != "InvalidRouteTableID.NotFound" {
   183  			return err
   184  		}
   185  	}
   186  
   187  	return nil
   188  }
   189  
   190  func testAccCheckRouteTableExists(n string, v *ec2.RouteTable) resource.TestCheckFunc {
   191  	return func(s *terraform.State) error {
   192  		rs, ok := s.RootModule().Resources[n]
   193  		if !ok {
   194  			return fmt.Errorf("Not found: %s", n)
   195  		}
   196  
   197  		if rs.Primary.ID == "" {
   198  			return fmt.Errorf("No ID is set")
   199  		}
   200  
   201  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   202  		resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
   203  			RouteTableIds: []*string{aws.String(rs.Primary.ID)},
   204  		})
   205  		if err != nil {
   206  			return err
   207  		}
   208  		if len(resp.RouteTables) == 0 {
   209  			return fmt.Errorf("RouteTable not found")
   210  		}
   211  
   212  		*v = *resp.RouteTables[0]
   213  
   214  		return nil
   215  	}
   216  }
   217  
   218  // VPC Peering connections are prefixed with pcx
   219  // Right now there is no VPC Peering resource
   220  func TestAccAWSRouteTable_vpcPeering(t *testing.T) {
   221  	var v ec2.RouteTable
   222  
   223  	testCheck := func(*terraform.State) error {
   224  		if len(v.Routes) != 2 {
   225  			return fmt.Errorf("bad routes: %#v", v.Routes)
   226  		}
   227  
   228  		routes := make(map[string]*ec2.Route)
   229  		for _, r := range v.Routes {
   230  			routes[*r.DestinationCidrBlock] = r
   231  		}
   232  
   233  		if _, ok := routes["10.1.0.0/16"]; !ok {
   234  			return fmt.Errorf("bad routes: %#v", v.Routes)
   235  		}
   236  		if _, ok := routes["10.2.0.0/16"]; !ok {
   237  			return fmt.Errorf("bad routes: %#v", v.Routes)
   238  		}
   239  
   240  		return nil
   241  	}
   242  	resource.Test(t, resource.TestCase{
   243  		PreCheck:     func() { testAccPreCheck(t) },
   244  		Providers:    testAccProviders,
   245  		CheckDestroy: testAccCheckRouteTableDestroy,
   246  		Steps: []resource.TestStep{
   247  			resource.TestStep{
   248  				Config: testAccRouteTableVpcPeeringConfig,
   249  				Check: resource.ComposeTestCheckFunc(
   250  					testAccCheckRouteTableExists(
   251  						"aws_route_table.foo", &v),
   252  					testCheck,
   253  				),
   254  			},
   255  		},
   256  	})
   257  }
   258  
   259  func TestAccAWSRouteTable_vgwRoutePropagation(t *testing.T) {
   260  	var v ec2.RouteTable
   261  	var vgw ec2.VpnGateway
   262  
   263  	testCheck := func(*terraform.State) error {
   264  		if len(v.PropagatingVgws) != 1 {
   265  			return fmt.Errorf("bad propagating vgws: %#v", v.PropagatingVgws)
   266  		}
   267  
   268  		propagatingVGWs := make(map[string]*ec2.PropagatingVgw)
   269  		for _, gw := range v.PropagatingVgws {
   270  			propagatingVGWs[*gw.GatewayId] = gw
   271  		}
   272  
   273  		if _, ok := propagatingVGWs[*vgw.VpnGatewayId]; !ok {
   274  			return fmt.Errorf("bad propagating vgws: %#v", v.PropagatingVgws)
   275  		}
   276  
   277  		return nil
   278  
   279  	}
   280  	resource.Test(t, resource.TestCase{
   281  		PreCheck:  func() { testAccPreCheck(t) },
   282  		Providers: testAccProviders,
   283  		CheckDestroy: resource.ComposeTestCheckFunc(
   284  			testAccCheckVpnGatewayDestroy,
   285  			testAccCheckRouteTableDestroy,
   286  		),
   287  		Steps: []resource.TestStep{
   288  			resource.TestStep{
   289  				Config: testAccRouteTableVgwRoutePropagationConfig,
   290  				Check: resource.ComposeTestCheckFunc(
   291  					testAccCheckRouteTableExists(
   292  						"aws_route_table.foo", &v),
   293  					testAccCheckVpnGatewayExists(
   294  						"aws_vpn_gateway.foo", &vgw),
   295  					testCheck,
   296  				),
   297  			},
   298  		},
   299  	})
   300  }
   301  
   302  const testAccRouteTableConfig = `
   303  resource "aws_vpc" "foo" {
   304  	cidr_block = "10.1.0.0/16"
   305  }
   306  
   307  resource "aws_internet_gateway" "foo" {
   308  	vpc_id = "${aws_vpc.foo.id}"
   309  }
   310  
   311  resource "aws_route_table" "foo" {
   312  	vpc_id = "${aws_vpc.foo.id}"
   313  
   314  	route {
   315  		cidr_block = "10.2.0.0/16"
   316  		gateway_id = "${aws_internet_gateway.foo.id}"
   317  	}
   318  }
   319  `
   320  
   321  const testAccRouteTableConfigChange = `
   322  resource "aws_vpc" "foo" {
   323  	cidr_block = "10.1.0.0/16"
   324  }
   325  
   326  resource "aws_internet_gateway" "foo" {
   327  	vpc_id = "${aws_vpc.foo.id}"
   328  }
   329  
   330  resource "aws_route_table" "foo" {
   331  	vpc_id = "${aws_vpc.foo.id}"
   332  
   333  	route {
   334  		cidr_block = "10.3.0.0/16"
   335  		gateway_id = "${aws_internet_gateway.foo.id}"
   336  	}
   337  
   338  	route {
   339  		cidr_block = "10.4.0.0/16"
   340  		gateway_id = "${aws_internet_gateway.foo.id}"
   341  	}
   342  }
   343  `
   344  
   345  const testAccRouteTableConfigInstance = `
   346  resource "aws_vpc" "foo" {
   347  	cidr_block = "10.1.0.0/16"
   348  }
   349  
   350  resource "aws_subnet" "foo" {
   351  	cidr_block = "10.1.1.0/24"
   352  	vpc_id = "${aws_vpc.foo.id}"
   353  }
   354  
   355  resource "aws_instance" "foo" {
   356  	# us-west-2
   357  	ami = "ami-4fccb37f"
   358  	instance_type = "m1.small"
   359  	subnet_id = "${aws_subnet.foo.id}"
   360  }
   361  
   362  resource "aws_route_table" "foo" {
   363  	vpc_id = "${aws_vpc.foo.id}"
   364  
   365  	route {
   366  		cidr_block = "10.2.0.0/16"
   367  		instance_id = "${aws_instance.foo.id}"
   368  	}
   369  }
   370  `
   371  
   372  const testAccRouteTableConfigTags = `
   373  resource "aws_vpc" "foo" {
   374  	cidr_block = "10.1.0.0/16"
   375  }
   376  
   377  resource "aws_route_table" "foo" {
   378  	vpc_id = "${aws_vpc.foo.id}"
   379  
   380  	tags {
   381  		foo = "bar"
   382  	}
   383  }
   384  `
   385  
   386  const testAccRouteTableConfigTagsUpdate = `
   387  resource "aws_vpc" "foo" {
   388  	cidr_block = "10.1.0.0/16"
   389  }
   390  
   391  resource "aws_route_table" "foo" {
   392  	vpc_id = "${aws_vpc.foo.id}"
   393  
   394  	tags {
   395  		bar = "baz"
   396  	}
   397  }
   398  `
   399  
   400  // VPC Peering connections are prefixed with pcx
   401  const testAccRouteTableVpcPeeringConfig = `
   402  resource "aws_vpc" "foo" {
   403  	cidr_block = "10.1.0.0/16"
   404  }
   405  
   406  resource "aws_internet_gateway" "foo" {
   407  	vpc_id = "${aws_vpc.foo.id}"
   408  }
   409  
   410  resource "aws_vpc" "bar" {
   411  	cidr_block = "10.3.0.0/16"
   412  }
   413  
   414  resource "aws_internet_gateway" "bar" {
   415  	vpc_id = "${aws_vpc.bar.id}"
   416  }
   417  
   418  resource "aws_vpc_peering_connection" "foo" {
   419  		vpc_id = "${aws_vpc.foo.id}"
   420  		peer_vpc_id = "${aws_vpc.bar.id}"
   421  		tags {
   422  			foo = "bar"
   423  		}
   424  }
   425  
   426  resource "aws_route_table" "foo" {
   427  	vpc_id = "${aws_vpc.foo.id}"
   428  
   429  	route {
   430  		cidr_block = "10.2.0.0/16"
   431  		vpc_peering_connection_id = "${aws_vpc_peering_connection.foo.id}"
   432  	}
   433  }
   434  `
   435  
   436  const testAccRouteTableVgwRoutePropagationConfig = `
   437  resource "aws_vpc" "foo" {
   438  	cidr_block = "10.1.0.0/16"
   439  }
   440  
   441  resource "aws_vpn_gateway" "foo" {
   442  	vpc_id = "${aws_vpc.foo.id}"
   443  }
   444  
   445  resource "aws_route_table" "foo" {
   446  	vpc_id = "${aws_vpc.foo.id}"
   447  
   448  	propagating_vgws = ["${aws_vpn_gateway.foo.id}"]
   449  }
   450  `