github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/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/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/goamz/ec2"
    10  )
    11  
    12  func TestAccAWSRouteTable_normal(t *testing.T) {
    13  	var v ec2.RouteTable
    14  
    15  	testCheck := func(*terraform.State) error {
    16  		if len(v.Routes) != 2 {
    17  			return fmt.Errorf("bad routes: %#v", v.Routes)
    18  		}
    19  
    20  		routes := make(map[string]ec2.Route)
    21  		for _, r := range v.Routes {
    22  			routes[r.DestinationCidrBlock] = r
    23  		}
    24  
    25  		if _, ok := routes["10.1.0.0/16"]; !ok {
    26  			return fmt.Errorf("bad routes: %#v", v.Routes)
    27  		}
    28  		if _, ok := routes["10.2.0.0/16"]; !ok {
    29  			return fmt.Errorf("bad routes: %#v", v.Routes)
    30  		}
    31  
    32  		return nil
    33  	}
    34  
    35  	testCheckChange := func(*terraform.State) error {
    36  		if len(v.Routes) != 3 {
    37  			return fmt.Errorf("bad routes: %#v", v.Routes)
    38  		}
    39  
    40  		routes := make(map[string]ec2.Route)
    41  		for _, r := range v.Routes {
    42  			routes[r.DestinationCidrBlock] = r
    43  		}
    44  
    45  		if _, ok := routes["10.1.0.0/16"]; !ok {
    46  			return fmt.Errorf("bad routes: %#v", v.Routes)
    47  		}
    48  		if _, ok := routes["10.3.0.0/16"]; !ok {
    49  			return fmt.Errorf("bad routes: %#v", v.Routes)
    50  		}
    51  		if _, ok := routes["10.4.0.0/16"]; !ok {
    52  			return fmt.Errorf("bad routes: %#v", v.Routes)
    53  		}
    54  
    55  		return nil
    56  	}
    57  
    58  	resource.Test(t, resource.TestCase{
    59  		PreCheck:     func() { testAccPreCheck(t) },
    60  		Providers:    testAccProviders,
    61  		CheckDestroy: testAccCheckRouteTableDestroy,
    62  		Steps: []resource.TestStep{
    63  			resource.TestStep{
    64  				Config: testAccRouteTableConfig,
    65  				Check: resource.ComposeTestCheckFunc(
    66  					testAccCheckRouteTableExists(
    67  						"aws_route_table.foo", &v),
    68  					testCheck,
    69  				),
    70  			},
    71  
    72  			resource.TestStep{
    73  				Config: testAccRouteTableConfigChange,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckRouteTableExists(
    76  						"aws_route_table.foo", &v),
    77  					testCheckChange,
    78  				),
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func TestAccAWSRouteTable_instance(t *testing.T) {
    85  	var v ec2.RouteTable
    86  
    87  	testCheck := func(*terraform.State) error {
    88  		if len(v.Routes) != 2 {
    89  			return fmt.Errorf("bad routes: %#v", v.Routes)
    90  		}
    91  
    92  		routes := make(map[string]ec2.Route)
    93  		for _, r := range v.Routes {
    94  			routes[r.DestinationCidrBlock] = r
    95  		}
    96  
    97  		if _, ok := routes["10.1.0.0/16"]; !ok {
    98  			return fmt.Errorf("bad routes: %#v", v.Routes)
    99  		}
   100  		if _, ok := routes["10.2.0.0/16"]; !ok {
   101  			return fmt.Errorf("bad routes: %#v", v.Routes)
   102  		}
   103  
   104  		return nil
   105  	}
   106  
   107  	resource.Test(t, resource.TestCase{
   108  		PreCheck:     func() { testAccPreCheck(t) },
   109  		Providers:    testAccProviders,
   110  		CheckDestroy: testAccCheckRouteTableDestroy,
   111  		Steps: []resource.TestStep{
   112  			resource.TestStep{
   113  				Config: testAccRouteTableConfigInstance,
   114  				Check: resource.ComposeTestCheckFunc(
   115  					testAccCheckRouteTableExists(
   116  						"aws_route_table.foo", &v),
   117  					testCheck,
   118  				),
   119  			},
   120  		},
   121  	})
   122  }
   123  
   124  func testAccCheckRouteTableDestroy(s *terraform.State) error {
   125  	conn := testAccProvider.ec2conn
   126  
   127  	for _, rs := range s.RootModule().Resources {
   128  		if rs.Type != "aws_route_table" {
   129  			continue
   130  		}
   131  
   132  		// Try to find the resource
   133  		resp, err := conn.DescribeRouteTables(
   134  			[]string{rs.Primary.ID}, ec2.NewFilter())
   135  		if err == nil {
   136  			if len(resp.RouteTables) > 0 {
   137  				return fmt.Errorf("still exist.")
   138  			}
   139  
   140  			return nil
   141  		}
   142  
   143  		// Verify the error is what we want
   144  		ec2err, ok := err.(*ec2.Error)
   145  		if !ok {
   146  			return err
   147  		}
   148  		if ec2err.Code != "InvalidRouteTableID.NotFound" {
   149  			return err
   150  		}
   151  	}
   152  
   153  	return nil
   154  }
   155  
   156  func testAccCheckRouteTableExists(n string, v *ec2.RouteTable) resource.TestCheckFunc {
   157  	return func(s *terraform.State) error {
   158  		rs, ok := s.RootModule().Resources[n]
   159  		if !ok {
   160  			return fmt.Errorf("Not found: %s", n)
   161  		}
   162  
   163  		if rs.Primary.ID == "" {
   164  			return fmt.Errorf("No ID is set")
   165  		}
   166  
   167  		conn := testAccProvider.ec2conn
   168  		resp, err := conn.DescribeRouteTables(
   169  			[]string{rs.Primary.ID}, ec2.NewFilter())
   170  		if err != nil {
   171  			return err
   172  		}
   173  		if len(resp.RouteTables) == 0 {
   174  			return fmt.Errorf("RouteTable not found")
   175  		}
   176  
   177  		*v = resp.RouteTables[0]
   178  
   179  		return nil
   180  	}
   181  }
   182  
   183  const testAccRouteTableConfig = `
   184  resource "aws_vpc" "foo" {
   185  	cidr_block = "10.1.0.0/16"
   186  }
   187  
   188  resource "aws_internet_gateway" "foo" {
   189  	vpc_id = "${aws_vpc.foo.id}"
   190  }
   191  
   192  resource "aws_route_table" "foo" {
   193  	vpc_id = "${aws_vpc.foo.id}"
   194  
   195  	route {
   196  		cidr_block = "10.2.0.0/16"
   197  		gateway_id = "${aws_internet_gateway.foo.id}"
   198  	}
   199  }
   200  `
   201  
   202  const testAccRouteTableConfigChange = `
   203  resource "aws_vpc" "foo" {
   204  	cidr_block = "10.1.0.0/16"
   205  }
   206  
   207  resource "aws_internet_gateway" "foo" {
   208  	vpc_id = "${aws_vpc.foo.id}"
   209  }
   210  
   211  resource "aws_route_table" "foo" {
   212  	vpc_id = "${aws_vpc.foo.id}"
   213  
   214  	route {
   215  		cidr_block = "10.3.0.0/16"
   216  		gateway_id = "${aws_internet_gateway.foo.id}"
   217  	}
   218  
   219  	route {
   220  		cidr_block = "10.4.0.0/16"
   221  		gateway_id = "${aws_internet_gateway.foo.id}"
   222  	}
   223  }
   224  `
   225  
   226  const testAccRouteTableConfigInstance = `
   227  resource "aws_vpc" "foo" {
   228  	cidr_block = "10.1.0.0/16"
   229  }
   230  
   231  resource "aws_subnet" "foo" {
   232  	cidr_block = "10.1.1.0/24"
   233  	vpc_id = "${aws_vpc.foo.id}"
   234  }
   235  
   236  resource "aws_instance" "foo" {
   237  	# us-west-2
   238  	ami = "ami-4fccb37f"
   239  	instance_type = "m1.small"
   240  	subnet_id = "${aws_subnet.foo.id}"
   241  }
   242  
   243  resource "aws_route_table" "foo" {
   244  	vpc_id = "${aws_vpc.foo.id}"
   245  
   246  	route {
   247  		cidr_block = "10.2.0.0/16"
   248  		instance_id = "${aws_instance.foo.id}"
   249  	}
   250  }
   251  `