github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_route_table_test.go (about)

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