github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/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 TestAccAWSRouteTable_tags(t *testing.T) {
   125  	var route_table ec2.RouteTable
   126  
   127  	resource.Test(t, resource.TestCase{
   128  		PreCheck:     func() { testAccPreCheck(t) },
   129  		Providers:    testAccProviders,
   130  		CheckDestroy: testAccCheckRouteTableDestroy,
   131  		Steps: []resource.TestStep{
   132  		resource.TestStep{
   133  		Config: testAccRouteTableConfigTags,
   134  		Check: resource.ComposeTestCheckFunc(
   135  			testAccCheckRouteTableExists("aws_route_table.foo", &route_table),
   136  			testAccCheckTags(&route_table.Tags, "foo", "bar"),
   137  		),
   138  	},
   139  
   140  		resource.TestStep{
   141  		Config: testAccRouteTableConfigTagsUpdate,
   142  		Check: resource.ComposeTestCheckFunc(
   143  			testAccCheckRouteTableExists("aws_route_table.foo", &route_table),
   144  			testAccCheckTags(&route_table.Tags, "foo", ""),
   145  			testAccCheckTags(&route_table.Tags, "bar", "baz"),
   146  		),
   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(
   163  			[]string{rs.Primary.ID}, ec2.NewFilter())
   164  		if err == nil {
   165  			if len(resp.RouteTables) > 0 {
   166  				return fmt.Errorf("still exist.")
   167  			}
   168  
   169  			return nil
   170  		}
   171  
   172  		// Verify the error is what we want
   173  		ec2err, ok := err.(*ec2.Error)
   174  		if !ok {
   175  			return err
   176  		}
   177  		if ec2err.Code != "InvalidRouteTableID.NotFound" {
   178  			return err
   179  		}
   180  	}
   181  
   182  	return nil
   183  }
   184  
   185  func testAccCheckRouteTableExists(n string, v *ec2.RouteTable) resource.TestCheckFunc {
   186  	return func(s *terraform.State) error {
   187  		rs, ok := s.RootModule().Resources[n]
   188  		if !ok {
   189  			return fmt.Errorf("Not found: %s", n)
   190  		}
   191  
   192  		if rs.Primary.ID == "" {
   193  			return fmt.Errorf("No ID is set")
   194  		}
   195  
   196  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   197  		resp, err := conn.DescribeRouteTables(
   198  			[]string{rs.Primary.ID}, ec2.NewFilter())
   199  		if err != nil {
   200  			return err
   201  		}
   202  		if len(resp.RouteTables) == 0 {
   203  			return fmt.Errorf("RouteTable not found")
   204  		}
   205  
   206  		*v = resp.RouteTables[0]
   207  
   208  		return nil
   209  	}
   210  }
   211  
   212  const testAccRouteTableConfig = `
   213  resource "aws_vpc" "foo" {
   214  	cidr_block = "10.1.0.0/16"
   215  }
   216  
   217  resource "aws_internet_gateway" "foo" {
   218  	vpc_id = "${aws_vpc.foo.id}"
   219  }
   220  
   221  resource "aws_route_table" "foo" {
   222  	vpc_id = "${aws_vpc.foo.id}"
   223  
   224  	route {
   225  		cidr_block = "10.2.0.0/16"
   226  		gateway_id = "${aws_internet_gateway.foo.id}"
   227  	}
   228  }
   229  `
   230  
   231  const testAccRouteTableConfigChange = `
   232  resource "aws_vpc" "foo" {
   233  	cidr_block = "10.1.0.0/16"
   234  }
   235  
   236  resource "aws_internet_gateway" "foo" {
   237  	vpc_id = "${aws_vpc.foo.id}"
   238  }
   239  
   240  resource "aws_route_table" "foo" {
   241  	vpc_id = "${aws_vpc.foo.id}"
   242  
   243  	route {
   244  		cidr_block = "10.3.0.0/16"
   245  		gateway_id = "${aws_internet_gateway.foo.id}"
   246  	}
   247  
   248  	route {
   249  		cidr_block = "10.4.0.0/16"
   250  		gateway_id = "${aws_internet_gateway.foo.id}"
   251  	}
   252  }
   253  `
   254  
   255  const testAccRouteTableConfigInstance = `
   256  resource "aws_vpc" "foo" {
   257  	cidr_block = "10.1.0.0/16"
   258  }
   259  
   260  resource "aws_subnet" "foo" {
   261  	cidr_block = "10.1.1.0/24"
   262  	vpc_id = "${aws_vpc.foo.id}"
   263  }
   264  
   265  resource "aws_instance" "foo" {
   266  	# us-west-2
   267  	ami = "ami-4fccb37f"
   268  	instance_type = "m1.small"
   269  	subnet_id = "${aws_subnet.foo.id}"
   270  }
   271  
   272  resource "aws_route_table" "foo" {
   273  	vpc_id = "${aws_vpc.foo.id}"
   274  
   275  	route {
   276  		cidr_block = "10.2.0.0/16"
   277  		instance_id = "${aws_instance.foo.id}"
   278  	}
   279  }
   280  `
   281  
   282  const testAccRouteTableConfigTags = `
   283  resource "aws_vpc" "foo" {
   284  	cidr_block = "10.1.0.0/16"
   285  }
   286  
   287  resource "aws_route_table" "foo" {
   288  	vpc_id = "${aws_vpc.foo.id}"
   289  
   290  	tags {
   291  		foo = "bar"
   292  	}
   293  }
   294  `
   295  
   296  const testAccRouteTableConfigTagsUpdate = `
   297  resource "aws_vpc" "foo" {
   298  	cidr_block = "10.1.0.0/16"
   299  }
   300  
   301  resource "aws_route_table" "foo" {
   302  	vpc_id = "${aws_vpc.foo.id}"
   303  
   304  	tags {
   305  		bar = "baz"
   306  	}
   307  }
   308  `