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