github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/builtin/providers/aws/resource_aws_route_table_test.go (about)

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