github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_route_table_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 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 IDRefreshName: "aws_route_table.foo", 64 Providers: testAccProviders, 65 CheckDestroy: testAccCheckRouteTableDestroy, 66 Steps: []resource.TestStep{ 67 { 68 Config: testAccRouteTableConfig, 69 Check: resource.ComposeTestCheckFunc( 70 testAccCheckRouteTableExists( 71 "aws_route_table.foo", &v), 72 testCheck, 73 ), 74 }, 75 76 { 77 Config: testAccRouteTableConfigChange, 78 Check: resource.ComposeTestCheckFunc( 79 testAccCheckRouteTableExists( 80 "aws_route_table.foo", &v), 81 testCheckChange, 82 ), 83 }, 84 }, 85 }) 86 } 87 88 func TestAccAWSRouteTable_instance(t *testing.T) { 89 var v ec2.RouteTable 90 91 testCheck := func(*terraform.State) error { 92 if len(v.Routes) != 2 { 93 return fmt.Errorf("bad routes: %#v", v.Routes) 94 } 95 96 routes := make(map[string]*ec2.Route) 97 for _, r := range v.Routes { 98 routes[*r.DestinationCidrBlock] = r 99 } 100 101 if _, ok := routes["10.1.0.0/16"]; !ok { 102 return fmt.Errorf("bad routes: %#v", v.Routes) 103 } 104 if _, ok := routes["10.2.0.0/16"]; !ok { 105 return fmt.Errorf("bad routes: %#v", v.Routes) 106 } 107 108 return nil 109 } 110 111 resource.Test(t, resource.TestCase{ 112 PreCheck: func() { testAccPreCheck(t) }, 113 IDRefreshName: "aws_route_table.foo", 114 Providers: testAccProviders, 115 CheckDestroy: testAccCheckRouteTableDestroy, 116 Steps: []resource.TestStep{ 117 { 118 Config: testAccRouteTableConfigInstance, 119 Check: resource.ComposeTestCheckFunc( 120 testAccCheckRouteTableExists( 121 "aws_route_table.foo", &v), 122 testCheck, 123 ), 124 }, 125 }, 126 }) 127 } 128 129 func TestAccAWSRouteTable_ipv6(t *testing.T) { 130 var v ec2.RouteTable 131 132 testCheck := func(*terraform.State) error { 133 // Expect 3: 2 IPv6 (local + all outbound) + 1 IPv4 134 if len(v.Routes) != 3 { 135 return fmt.Errorf("bad routes: %#v", v.Routes) 136 } 137 138 return nil 139 } 140 141 resource.Test(t, resource.TestCase{ 142 PreCheck: func() { testAccPreCheck(t) }, 143 IDRefreshName: "aws_route_table.foo", 144 Providers: testAccProviders, 145 CheckDestroy: testAccCheckRouteTableDestroy, 146 Steps: []resource.TestStep{ 147 { 148 Config: testAccRouteTableConfigIpv6, 149 Check: resource.ComposeTestCheckFunc( 150 testAccCheckRouteTableExists("aws_route_table.foo", &v), 151 testCheck, 152 ), 153 }, 154 }, 155 }) 156 } 157 158 func TestAccAWSRouteTable_tags(t *testing.T) { 159 var route_table ec2.RouteTable 160 161 resource.Test(t, resource.TestCase{ 162 PreCheck: func() { testAccPreCheck(t) }, 163 IDRefreshName: "aws_route_table.foo", 164 Providers: testAccProviders, 165 CheckDestroy: testAccCheckRouteTableDestroy, 166 Steps: []resource.TestStep{ 167 { 168 Config: testAccRouteTableConfigTags, 169 Check: resource.ComposeTestCheckFunc( 170 testAccCheckRouteTableExists("aws_route_table.foo", &route_table), 171 testAccCheckTags(&route_table.Tags, "foo", "bar"), 172 ), 173 }, 174 175 { 176 Config: testAccRouteTableConfigTagsUpdate, 177 Check: resource.ComposeTestCheckFunc( 178 testAccCheckRouteTableExists("aws_route_table.foo", &route_table), 179 testAccCheckTags(&route_table.Tags, "foo", ""), 180 testAccCheckTags(&route_table.Tags, "bar", "baz"), 181 ), 182 }, 183 }, 184 }) 185 } 186 187 // For GH-13545, Fixes panic on an empty route config block 188 func TestAccAWSRouteTable_panicEmptyRoute(t *testing.T) { 189 resource.Test(t, resource.TestCase{ 190 PreCheck: func() { testAccPreCheck(t) }, 191 IDRefreshName: "aws_route_table.foo", 192 Providers: testAccProviders, 193 CheckDestroy: testAccCheckRouteTableDestroy, 194 Steps: []resource.TestStep{ 195 { 196 Config: testAccRouteTableConfigPanicEmptyRoute, 197 ExpectError: regexp.MustCompile("The request must contain the parameter destinationCidrBlock or destinationIpv6CidrBlock"), 198 }, 199 }, 200 }) 201 } 202 203 func testAccCheckRouteTableDestroy(s *terraform.State) error { 204 conn := testAccProvider.Meta().(*AWSClient).ec2conn 205 206 for _, rs := range s.RootModule().Resources { 207 if rs.Type != "aws_route_table" { 208 continue 209 } 210 211 // Try to find the resource 212 resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{ 213 RouteTableIds: []*string{aws.String(rs.Primary.ID)}, 214 }) 215 if err == nil { 216 if len(resp.RouteTables) > 0 { 217 return fmt.Errorf("still exist.") 218 } 219 220 return nil 221 } 222 223 // Verify the error is what we want 224 ec2err, ok := err.(awserr.Error) 225 if !ok { 226 return err 227 } 228 if ec2err.Code() != "InvalidRouteTableID.NotFound" { 229 return err 230 } 231 } 232 233 return nil 234 } 235 236 func testAccCheckRouteTableExists(n string, v *ec2.RouteTable) resource.TestCheckFunc { 237 return func(s *terraform.State) error { 238 rs, ok := s.RootModule().Resources[n] 239 if !ok { 240 return fmt.Errorf("Not found: %s", n) 241 } 242 243 if rs.Primary.ID == "" { 244 return fmt.Errorf("No ID is set") 245 } 246 247 conn := testAccProvider.Meta().(*AWSClient).ec2conn 248 resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{ 249 RouteTableIds: []*string{aws.String(rs.Primary.ID)}, 250 }) 251 if err != nil { 252 return err 253 } 254 if len(resp.RouteTables) == 0 { 255 return fmt.Errorf("RouteTable not found") 256 } 257 258 *v = *resp.RouteTables[0] 259 260 return nil 261 } 262 } 263 264 // VPC Peering connections are prefixed with pcx 265 // Right now there is no VPC Peering resource 266 func TestAccAWSRouteTable_vpcPeering(t *testing.T) { 267 var v ec2.RouteTable 268 269 testCheck := func(*terraform.State) error { 270 if len(v.Routes) != 2 { 271 return fmt.Errorf("bad routes: %#v", v.Routes) 272 } 273 274 routes := make(map[string]*ec2.Route) 275 for _, r := range v.Routes { 276 routes[*r.DestinationCidrBlock] = r 277 } 278 279 if _, ok := routes["10.1.0.0/16"]; !ok { 280 return fmt.Errorf("bad routes: %#v", v.Routes) 281 } 282 if _, ok := routes["10.2.0.0/16"]; !ok { 283 return fmt.Errorf("bad routes: %#v", v.Routes) 284 } 285 286 return nil 287 } 288 resource.Test(t, resource.TestCase{ 289 PreCheck: func() { testAccPreCheck(t) }, 290 Providers: testAccProviders, 291 CheckDestroy: testAccCheckRouteTableDestroy, 292 Steps: []resource.TestStep{ 293 { 294 Config: testAccRouteTableVpcPeeringConfig, 295 Check: resource.ComposeTestCheckFunc( 296 testAccCheckRouteTableExists( 297 "aws_route_table.foo", &v), 298 testCheck, 299 ), 300 }, 301 }, 302 }) 303 } 304 305 func TestAccAWSRouteTable_vgwRoutePropagation(t *testing.T) { 306 var v ec2.RouteTable 307 var vgw ec2.VpnGateway 308 309 testCheck := func(*terraform.State) error { 310 if len(v.PropagatingVgws) != 1 { 311 return fmt.Errorf("bad propagating vgws: %#v", v.PropagatingVgws) 312 } 313 314 propagatingVGWs := make(map[string]*ec2.PropagatingVgw) 315 for _, gw := range v.PropagatingVgws { 316 propagatingVGWs[*gw.GatewayId] = gw 317 } 318 319 if _, ok := propagatingVGWs[*vgw.VpnGatewayId]; !ok { 320 return fmt.Errorf("bad propagating vgws: %#v", v.PropagatingVgws) 321 } 322 323 return nil 324 325 } 326 resource.Test(t, resource.TestCase{ 327 PreCheck: func() { testAccPreCheck(t) }, 328 Providers: testAccProviders, 329 CheckDestroy: resource.ComposeTestCheckFunc( 330 testAccCheckVpnGatewayDestroy, 331 testAccCheckRouteTableDestroy, 332 ), 333 Steps: []resource.TestStep{ 334 { 335 Config: testAccRouteTableVgwRoutePropagationConfig, 336 Check: resource.ComposeTestCheckFunc( 337 testAccCheckRouteTableExists( 338 "aws_route_table.foo", &v), 339 testAccCheckVpnGatewayExists( 340 "aws_vpn_gateway.foo", &vgw), 341 testCheck, 342 ), 343 }, 344 }, 345 }) 346 } 347 348 const testAccRouteTableConfig = ` 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 gateway_id = "${aws_internet_gateway.foo.id}" 363 } 364 } 365 ` 366 367 const testAccRouteTableConfigChange = ` 368 resource "aws_vpc" "foo" { 369 cidr_block = "10.1.0.0/16" 370 } 371 372 resource "aws_internet_gateway" "foo" { 373 vpc_id = "${aws_vpc.foo.id}" 374 } 375 376 resource "aws_route_table" "foo" { 377 vpc_id = "${aws_vpc.foo.id}" 378 379 route { 380 cidr_block = "10.3.0.0/16" 381 gateway_id = "${aws_internet_gateway.foo.id}" 382 } 383 384 route { 385 cidr_block = "10.4.0.0/16" 386 gateway_id = "${aws_internet_gateway.foo.id}" 387 } 388 } 389 ` 390 391 const testAccRouteTableConfigIpv6 = ` 392 resource "aws_vpc" "foo" { 393 cidr_block = "10.1.0.0/16" 394 assign_generated_ipv6_cidr_block = true 395 } 396 397 resource "aws_egress_only_internet_gateway" "foo" { 398 vpc_id = "${aws_vpc.foo.id}" 399 } 400 401 resource "aws_route_table" "foo" { 402 vpc_id = "${aws_vpc.foo.id}" 403 404 route { 405 ipv6_cidr_block = "::/0" 406 egress_only_gateway_id = "${aws_egress_only_internet_gateway.foo.id}" 407 } 408 } 409 ` 410 411 const testAccRouteTableConfigInstance = ` 412 resource "aws_vpc" "foo" { 413 cidr_block = "10.1.0.0/16" 414 } 415 416 resource "aws_subnet" "foo" { 417 cidr_block = "10.1.1.0/24" 418 vpc_id = "${aws_vpc.foo.id}" 419 } 420 421 resource "aws_instance" "foo" { 422 # us-west-2 423 ami = "ami-4fccb37f" 424 instance_type = "m1.small" 425 subnet_id = "${aws_subnet.foo.id}" 426 } 427 428 resource "aws_route_table" "foo" { 429 vpc_id = "${aws_vpc.foo.id}" 430 431 route { 432 cidr_block = "10.2.0.0/16" 433 instance_id = "${aws_instance.foo.id}" 434 } 435 } 436 ` 437 438 const testAccRouteTableConfigTags = ` 439 resource "aws_vpc" "foo" { 440 cidr_block = "10.1.0.0/16" 441 } 442 443 resource "aws_route_table" "foo" { 444 vpc_id = "${aws_vpc.foo.id}" 445 446 tags { 447 foo = "bar" 448 } 449 } 450 ` 451 452 const testAccRouteTableConfigTagsUpdate = ` 453 resource "aws_vpc" "foo" { 454 cidr_block = "10.1.0.0/16" 455 } 456 457 resource "aws_route_table" "foo" { 458 vpc_id = "${aws_vpc.foo.id}" 459 460 tags { 461 bar = "baz" 462 } 463 } 464 ` 465 466 // VPC Peering connections are prefixed with pcx 467 const testAccRouteTableVpcPeeringConfig = ` 468 resource "aws_vpc" "foo" { 469 cidr_block = "10.1.0.0/16" 470 } 471 472 resource "aws_internet_gateway" "foo" { 473 vpc_id = "${aws_vpc.foo.id}" 474 } 475 476 resource "aws_vpc" "bar" { 477 cidr_block = "10.3.0.0/16" 478 } 479 480 resource "aws_internet_gateway" "bar" { 481 vpc_id = "${aws_vpc.bar.id}" 482 } 483 484 resource "aws_vpc_peering_connection" "foo" { 485 vpc_id = "${aws_vpc.foo.id}" 486 peer_vpc_id = "${aws_vpc.bar.id}" 487 tags { 488 foo = "bar" 489 } 490 } 491 492 resource "aws_route_table" "foo" { 493 vpc_id = "${aws_vpc.foo.id}" 494 495 route { 496 cidr_block = "10.2.0.0/16" 497 vpc_peering_connection_id = "${aws_vpc_peering_connection.foo.id}" 498 } 499 } 500 ` 501 502 const testAccRouteTableVgwRoutePropagationConfig = ` 503 resource "aws_vpc" "foo" { 504 cidr_block = "10.1.0.0/16" 505 } 506 507 resource "aws_vpn_gateway" "foo" { 508 vpc_id = "${aws_vpc.foo.id}" 509 } 510 511 resource "aws_route_table" "foo" { 512 vpc_id = "${aws_vpc.foo.id}" 513 514 propagating_vgws = ["${aws_vpn_gateway.foo.id}"] 515 } 516 ` 517 518 // For GH-13545 519 const testAccRouteTableConfigPanicEmptyRoute = ` 520 resource "aws_vpc" "foo" { 521 cidr_block = "10.2.0.0/16" 522 } 523 524 resource "aws_route_table" "foo" { 525 vpc_id = "${aws_vpc.foo.id}" 526 527 route { 528 } 529 } 530 `