github.com/jgadling/terraform@v0.3.8-0.20150227214559-abd68c2c87bc/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 func testAccCheckRouteTableDestroy(s *terraform.State) error { 153 conn := testAccProvider.Meta().(*AWSClient).ec2conn 154 155 for _, rs := range s.RootModule().Resources { 156 if rs.Type != "aws_route_table" { 157 continue 158 } 159 160 // Try to find the resource 161 resp, err := conn.DescribeRouteTables( 162 []string{rs.Primary.ID}, ec2.NewFilter()) 163 if err == nil { 164 if len(resp.RouteTables) > 0 { 165 return fmt.Errorf("still exist.") 166 } 167 168 return nil 169 } 170 171 // Verify the error is what we want 172 ec2err, ok := err.(*ec2.Error) 173 if !ok { 174 return err 175 } 176 if ec2err.Code != "InvalidRouteTableID.NotFound" { 177 return err 178 } 179 } 180 181 return nil 182 } 183 184 func testAccCheckRouteTableExists(n string, v *ec2.RouteTable) resource.TestCheckFunc { 185 return func(s *terraform.State) error { 186 rs, ok := s.RootModule().Resources[n] 187 if !ok { 188 return fmt.Errorf("Not found: %s", n) 189 } 190 191 if rs.Primary.ID == "" { 192 return fmt.Errorf("No ID is set") 193 } 194 195 conn := testAccProvider.Meta().(*AWSClient).ec2conn 196 resp, err := conn.DescribeRouteTables( 197 []string{rs.Primary.ID}, ec2.NewFilter()) 198 if err != nil { 199 return err 200 } 201 if len(resp.RouteTables) == 0 { 202 return fmt.Errorf("RouteTable not found") 203 } 204 205 *v = resp.RouteTables[0] 206 207 return nil 208 } 209 } 210 211 func TestAccAWSRouteTable_vpcPeering(t *testing.T) { 212 var v ec2.RouteTable 213 214 testCheck := func(*terraform.State) error { 215 if len(v.Routes) != 2 { 216 return fmt.Errorf("bad routes: %#v", v.Routes) 217 } 218 219 routes := make(map[string]ec2.Route) 220 for _, r := range v.Routes { 221 routes[r.DestinationCidrBlock] = r 222 } 223 224 if _, ok := routes["10.1.0.0/16"]; !ok { 225 return fmt.Errorf("bad routes: %#v", v.Routes) 226 } 227 if _, ok := routes["10.2.0.0/16"]; !ok { 228 return fmt.Errorf("bad routes: %#v", v.Routes) 229 } 230 231 return nil 232 } 233 resource.Test(t, resource.TestCase{ 234 PreCheck: func() { testAccPreCheck(t) }, 235 Providers: testAccProviders, 236 CheckDestroy: testAccCheckRouteTableDestroy, 237 Steps: []resource.TestStep{ 238 resource.TestStep{ 239 Config: testAccRouteTableVpcPeeringConfig, 240 Check: resource.ComposeTestCheckFunc( 241 testAccCheckRouteTableExists( 242 "aws_route_table.foo", &v), 243 testCheck, 244 ), 245 }, 246 }, 247 }) 248 } 249 250 const testAccRouteTableConfig = ` 251 resource "aws_vpc" "foo" { 252 cidr_block = "10.1.0.0/16" 253 } 254 255 resource "aws_internet_gateway" "foo" { 256 vpc_id = "${aws_vpc.foo.id}" 257 } 258 259 resource "aws_route_table" "foo" { 260 vpc_id = "${aws_vpc.foo.id}" 261 262 route { 263 cidr_block = "10.2.0.0/16" 264 gateway_id = "${aws_internet_gateway.foo.id}" 265 } 266 } 267 ` 268 269 const testAccRouteTableConfigChange = ` 270 resource "aws_vpc" "foo" { 271 cidr_block = "10.1.0.0/16" 272 } 273 274 resource "aws_internet_gateway" "foo" { 275 vpc_id = "${aws_vpc.foo.id}" 276 } 277 278 resource "aws_route_table" "foo" { 279 vpc_id = "${aws_vpc.foo.id}" 280 281 route { 282 cidr_block = "10.3.0.0/16" 283 gateway_id = "${aws_internet_gateway.foo.id}" 284 } 285 286 route { 287 cidr_block = "10.4.0.0/16" 288 gateway_id = "${aws_internet_gateway.foo.id}" 289 } 290 } 291 ` 292 293 const testAccRouteTableConfigInstance = ` 294 resource "aws_vpc" "foo" { 295 cidr_block = "10.1.0.0/16" 296 } 297 298 resource "aws_subnet" "foo" { 299 cidr_block = "10.1.1.0/24" 300 vpc_id = "${aws_vpc.foo.id}" 301 } 302 303 resource "aws_instance" "foo" { 304 # us-west-2 305 ami = "ami-4fccb37f" 306 instance_type = "m1.small" 307 subnet_id = "${aws_subnet.foo.id}" 308 } 309 310 resource "aws_route_table" "foo" { 311 vpc_id = "${aws_vpc.foo.id}" 312 313 route { 314 cidr_block = "10.2.0.0/16" 315 instance_id = "${aws_instance.foo.id}" 316 } 317 } 318 ` 319 320 const testAccRouteTableConfigTags = ` 321 resource "aws_vpc" "foo" { 322 cidr_block = "10.1.0.0/16" 323 } 324 325 resource "aws_route_table" "foo" { 326 vpc_id = "${aws_vpc.foo.id}" 327 328 tags { 329 foo = "bar" 330 } 331 } 332 ` 333 334 const testAccRouteTableConfigTagsUpdate = ` 335 resource "aws_vpc" "foo" { 336 cidr_block = "10.1.0.0/16" 337 } 338 339 resource "aws_route_table" "foo" { 340 vpc_id = "${aws_vpc.foo.id}" 341 342 tags { 343 bar = "baz" 344 } 345 } 346 ` 347 348 const testAccRouteTableVpcPeeringConfig = ` 349 resource "aws_vpc" "foo" { 350 cidr_block = "10.1.0.0/16" 351 } 352 353 resource "aws_internet_gateway" "foo" { 354 vpc_id = "${aws_vpc.foo.id}" 355 } 356 357 resource "aws_route_table" "foo" { 358 vpc_id = "${aws_vpc.foo.id}" 359 360 route { 361 cidr_block = "10.2.0.0/16" 362 vpc_peering_connection_id = "vpc-12345" 363 } 364 } 365 `