github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/resource_aws_route_table_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/aws-sdk-go/aws"
     8  	"github.com/hashicorp/aws-sdk-go/gen/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  					testAccCheckTags(&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  					testAccCheckTags(&route_table.Tags, "foo", ""),
   146  					testAccCheckTags(&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.DescribeRouteTablesRequest{
   163  			RouteTableIDs: []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.DescribeRouteTablesRequest{
   199  			RouteTableIDs: []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  const testAccRouteTableConfig = `
   257  resource "aws_vpc" "foo" {
   258  	cidr_block = "10.1.0.0/16"
   259  }
   260  
   261  resource "aws_internet_gateway" "foo" {
   262  	vpc_id = "${aws_vpc.foo.id}"
   263  }
   264  
   265  resource "aws_route_table" "foo" {
   266  	vpc_id = "${aws_vpc.foo.id}"
   267  
   268  	route {
   269  		cidr_block = "10.2.0.0/16"
   270  		gateway_id = "${aws_internet_gateway.foo.id}"
   271  	}
   272  }
   273  `
   274  
   275  const testAccRouteTableConfigChange = `
   276  resource "aws_vpc" "foo" {
   277  	cidr_block = "10.1.0.0/16"
   278  }
   279  
   280  resource "aws_internet_gateway" "foo" {
   281  	vpc_id = "${aws_vpc.foo.id}"
   282  }
   283  
   284  resource "aws_route_table" "foo" {
   285  	vpc_id = "${aws_vpc.foo.id}"
   286  
   287  	route {
   288  		cidr_block = "10.3.0.0/16"
   289  		gateway_id = "${aws_internet_gateway.foo.id}"
   290  	}
   291  
   292  	route {
   293  		cidr_block = "10.4.0.0/16"
   294  		gateway_id = "${aws_internet_gateway.foo.id}"
   295  	}
   296  }
   297  `
   298  
   299  const testAccRouteTableConfigInstance = `
   300  resource "aws_vpc" "foo" {
   301  	cidr_block = "10.1.0.0/16"
   302  }
   303  
   304  resource "aws_subnet" "foo" {
   305  	cidr_block = "10.1.1.0/24"
   306  	vpc_id = "${aws_vpc.foo.id}"
   307  }
   308  
   309  resource "aws_instance" "foo" {
   310  	# us-west-2
   311  	ami = "ami-4fccb37f"
   312  	instance_type = "m1.small"
   313  	subnet_id = "${aws_subnet.foo.id}"
   314  }
   315  
   316  resource "aws_route_table" "foo" {
   317  	vpc_id = "${aws_vpc.foo.id}"
   318  
   319  	route {
   320  		cidr_block = "10.2.0.0/16"
   321  		instance_id = "${aws_instance.foo.id}"
   322  	}
   323  }
   324  `
   325  
   326  const testAccRouteTableConfigTags = `
   327  resource "aws_vpc" "foo" {
   328  	cidr_block = "10.1.0.0/16"
   329  }
   330  
   331  resource "aws_route_table" "foo" {
   332  	vpc_id = "${aws_vpc.foo.id}"
   333  
   334  	tags {
   335  		foo = "bar"
   336  	}
   337  }
   338  `
   339  
   340  const testAccRouteTableConfigTagsUpdate = `
   341  resource "aws_vpc" "foo" {
   342  	cidr_block = "10.1.0.0/16"
   343  }
   344  
   345  resource "aws_route_table" "foo" {
   346  	vpc_id = "${aws_vpc.foo.id}"
   347  
   348  	tags {
   349  		bar = "baz"
   350  	}
   351  }
   352  `
   353  
   354  // TODO: re-enable this test.
   355  // VPC Peering connections are prefixed with pcx
   356  // Right now there is no VPC Peering resource
   357  const testAccRouteTableVpcPeeringConfig = `
   358  resource "aws_vpc" "foo" {
   359  	cidr_block = "10.1.0.0/16"
   360  }
   361  
   362  resource "aws_internet_gateway" "foo" {
   363  	vpc_id = "${aws_vpc.foo.id}"
   364  }
   365  
   366  resource "aws_route_table" "foo" {
   367  	vpc_id = "${aws_vpc.foo.id}"
   368  
   369  	route {
   370  		cidr_block = "10.2.0.0/16"
   371          vpc_peering_connection_id = "pcx-12345"
   372  	}
   373  }
   374  `