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