github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/builtin/providers/aws/resource_aws_route_table_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "os" 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 resource.TestStep{ 68 Config: testAccRouteTableConfig, 69 Check: resource.ComposeTestCheckFunc( 70 testAccCheckRouteTableExists( 71 "aws_route_table.foo", &v), 72 testCheck, 73 ), 74 }, 75 76 resource.TestStep{ 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 resource.TestStep{ 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_tags(t *testing.T) { 130 var route_table ec2.RouteTable 131 132 resource.Test(t, resource.TestCase{ 133 PreCheck: func() { testAccPreCheck(t) }, 134 IDRefreshName: "aws_route_table.foo", 135 Providers: testAccProviders, 136 CheckDestroy: testAccCheckRouteTableDestroy, 137 Steps: []resource.TestStep{ 138 resource.TestStep{ 139 Config: testAccRouteTableConfigTags, 140 Check: resource.ComposeTestCheckFunc( 141 testAccCheckRouteTableExists("aws_route_table.foo", &route_table), 142 testAccCheckTags(&route_table.Tags, "foo", "bar"), 143 ), 144 }, 145 146 resource.TestStep{ 147 Config: testAccRouteTableConfigTagsUpdate, 148 Check: resource.ComposeTestCheckFunc( 149 testAccCheckRouteTableExists("aws_route_table.foo", &route_table), 150 testAccCheckTags(&route_table.Tags, "foo", ""), 151 testAccCheckTags(&route_table.Tags, "bar", "baz"), 152 ), 153 }, 154 }, 155 }) 156 } 157 158 func testAccCheckRouteTableDestroy(s *terraform.State) error { 159 conn := testAccProvider.Meta().(*AWSClient).ec2conn 160 161 for _, rs := range s.RootModule().Resources { 162 if rs.Type != "aws_route_table" { 163 continue 164 } 165 166 // Try to find the resource 167 resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{ 168 RouteTableIds: []*string{aws.String(rs.Primary.ID)}, 169 }) 170 if err == nil { 171 if len(resp.RouteTables) > 0 { 172 return fmt.Errorf("still exist.") 173 } 174 175 return nil 176 } 177 178 // Verify the error is what we want 179 ec2err, ok := err.(awserr.Error) 180 if !ok { 181 return err 182 } 183 if ec2err.Code() != "InvalidRouteTableID.NotFound" { 184 return err 185 } 186 } 187 188 return nil 189 } 190 191 func testAccCheckRouteTableExists(n string, v *ec2.RouteTable) resource.TestCheckFunc { 192 return func(s *terraform.State) error { 193 rs, ok := s.RootModule().Resources[n] 194 if !ok { 195 return fmt.Errorf("Not found: %s", n) 196 } 197 198 if rs.Primary.ID == "" { 199 return fmt.Errorf("No ID is set") 200 } 201 202 conn := testAccProvider.Meta().(*AWSClient).ec2conn 203 resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{ 204 RouteTableIds: []*string{aws.String(rs.Primary.ID)}, 205 }) 206 if err != nil { 207 return err 208 } 209 if len(resp.RouteTables) == 0 { 210 return fmt.Errorf("RouteTable not found") 211 } 212 213 *v = *resp.RouteTables[0] 214 215 return nil 216 } 217 } 218 219 // VPC Peering connections are prefixed with pcx 220 // Right now there is no VPC Peering resource 221 func TestAccAWSRouteTable_vpcPeering(t *testing.T) { 222 var v ec2.RouteTable 223 224 testCheck := func(*terraform.State) error { 225 if len(v.Routes) != 2 { 226 return fmt.Errorf("bad routes: %#v", v.Routes) 227 } 228 229 routes := make(map[string]*ec2.Route) 230 for _, r := range v.Routes { 231 routes[*r.DestinationCidrBlock] = r 232 } 233 234 if _, ok := routes["10.1.0.0/16"]; !ok { 235 return fmt.Errorf("bad routes: %#v", v.Routes) 236 } 237 if _, ok := routes["10.2.0.0/16"]; !ok { 238 return fmt.Errorf("bad routes: %#v", v.Routes) 239 } 240 241 return nil 242 } 243 resource.Test(t, resource.TestCase{ 244 PreCheck: func() { 245 testAccPreCheck(t) 246 if os.Getenv("AWS_ACCOUNT_ID") == "" { 247 t.Fatal("Error: Test TestAccAWSRouteTable_vpcPeering requires an Account ID in AWS_ACCOUNT_ID ") 248 } 249 }, 250 Providers: testAccProviders, 251 CheckDestroy: testAccCheckRouteTableDestroy, 252 Steps: []resource.TestStep{ 253 resource.TestStep{ 254 Config: testAccRouteTableVpcPeeringConfig(os.Getenv("AWS_ACCOUNT_ID")), 255 Check: resource.ComposeTestCheckFunc( 256 testAccCheckRouteTableExists( 257 "aws_route_table.foo", &v), 258 testCheck, 259 ), 260 }, 261 }, 262 }) 263 } 264 265 func TestAccAWSRouteTable_vgwRoutePropagation(t *testing.T) { 266 var v ec2.RouteTable 267 var vgw ec2.VpnGateway 268 269 testCheck := func(*terraform.State) error { 270 if len(v.PropagatingVgws) != 1 { 271 return fmt.Errorf("bad propagating vgws: %#v", v.PropagatingVgws) 272 } 273 274 propagatingVGWs := make(map[string]*ec2.PropagatingVgw) 275 for _, gw := range v.PropagatingVgws { 276 propagatingVGWs[*gw.GatewayId] = gw 277 } 278 279 if _, ok := propagatingVGWs[*vgw.VpnGatewayId]; !ok { 280 return fmt.Errorf("bad propagating vgws: %#v", v.PropagatingVgws) 281 } 282 283 return nil 284 285 } 286 resource.Test(t, resource.TestCase{ 287 PreCheck: func() { testAccPreCheck(t) }, 288 Providers: testAccProviders, 289 CheckDestroy: resource.ComposeTestCheckFunc( 290 testAccCheckVpnGatewayDestroy, 291 testAccCheckRouteTableDestroy, 292 ), 293 Steps: []resource.TestStep{ 294 resource.TestStep{ 295 Config: testAccRouteTableVgwRoutePropagationConfig, 296 Check: resource.ComposeTestCheckFunc( 297 testAccCheckRouteTableExists( 298 "aws_route_table.foo", &v), 299 testAccCheckVpnGatewayExists( 300 "aws_vpn_gateway.foo", &vgw), 301 testCheck, 302 ), 303 }, 304 }, 305 }) 306 } 307 308 const testAccRouteTableConfig = ` 309 resource "aws_vpc" "foo" { 310 cidr_block = "10.1.0.0/16" 311 } 312 313 resource "aws_internet_gateway" "foo" { 314 vpc_id = "${aws_vpc.foo.id}" 315 } 316 317 resource "aws_route_table" "foo" { 318 vpc_id = "${aws_vpc.foo.id}" 319 320 route { 321 cidr_block = "10.2.0.0/16" 322 gateway_id = "${aws_internet_gateway.foo.id}" 323 } 324 } 325 ` 326 327 const testAccRouteTableConfigChange = ` 328 resource "aws_vpc" "foo" { 329 cidr_block = "10.1.0.0/16" 330 } 331 332 resource "aws_internet_gateway" "foo" { 333 vpc_id = "${aws_vpc.foo.id}" 334 } 335 336 resource "aws_route_table" "foo" { 337 vpc_id = "${aws_vpc.foo.id}" 338 339 route { 340 cidr_block = "10.3.0.0/16" 341 gateway_id = "${aws_internet_gateway.foo.id}" 342 } 343 344 route { 345 cidr_block = "10.4.0.0/16" 346 gateway_id = "${aws_internet_gateway.foo.id}" 347 } 348 } 349 ` 350 351 const testAccRouteTableConfigInstance = ` 352 resource "aws_vpc" "foo" { 353 cidr_block = "10.1.0.0/16" 354 } 355 356 resource "aws_subnet" "foo" { 357 cidr_block = "10.1.1.0/24" 358 vpc_id = "${aws_vpc.foo.id}" 359 } 360 361 resource "aws_instance" "foo" { 362 # us-west-2 363 ami = "ami-4fccb37f" 364 instance_type = "m1.small" 365 subnet_id = "${aws_subnet.foo.id}" 366 } 367 368 resource "aws_route_table" "foo" { 369 vpc_id = "${aws_vpc.foo.id}" 370 371 route { 372 cidr_block = "10.2.0.0/16" 373 instance_id = "${aws_instance.foo.id}" 374 } 375 } 376 ` 377 378 const testAccRouteTableConfigTags = ` 379 resource "aws_vpc" "foo" { 380 cidr_block = "10.1.0.0/16" 381 } 382 383 resource "aws_route_table" "foo" { 384 vpc_id = "${aws_vpc.foo.id}" 385 386 tags { 387 foo = "bar" 388 } 389 } 390 ` 391 392 const testAccRouteTableConfigTagsUpdate = ` 393 resource "aws_vpc" "foo" { 394 cidr_block = "10.1.0.0/16" 395 } 396 397 resource "aws_route_table" "foo" { 398 vpc_id = "${aws_vpc.foo.id}" 399 400 tags { 401 bar = "baz" 402 } 403 } 404 ` 405 406 // VPC Peering connections are prefixed with pcx 407 // This test requires an ENV var, AWS_ACCOUNT_ID, with a valid AWS Account ID 408 func testAccRouteTableVpcPeeringConfig(acc string) string { 409 cfg := `resource "aws_vpc" "foo" { 410 cidr_block = "10.1.0.0/16" 411 } 412 413 resource "aws_internet_gateway" "foo" { 414 vpc_id = "${aws_vpc.foo.id}" 415 } 416 417 resource "aws_vpc" "bar" { 418 cidr_block = "10.3.0.0/16" 419 } 420 421 resource "aws_internet_gateway" "bar" { 422 vpc_id = "${aws_vpc.bar.id}" 423 } 424 425 resource "aws_vpc_peering_connection" "foo" { 426 vpc_id = "${aws_vpc.foo.id}" 427 peer_vpc_id = "${aws_vpc.bar.id}" 428 peer_owner_id = "%s" 429 tags { 430 foo = "bar" 431 } 432 } 433 434 resource "aws_route_table" "foo" { 435 vpc_id = "${aws_vpc.foo.id}" 436 437 route { 438 cidr_block = "10.2.0.0/16" 439 vpc_peering_connection_id = "${aws_vpc_peering_connection.foo.id}" 440 } 441 } 442 ` 443 return fmt.Sprintf(cfg, acc) 444 } 445 446 const testAccRouteTableVgwRoutePropagationConfig = ` 447 resource "aws_vpc" "foo" { 448 cidr_block = "10.1.0.0/16" 449 } 450 451 resource "aws_vpn_gateway" "foo" { 452 vpc_id = "${aws_vpc.foo.id}" 453 } 454 455 resource "aws_route_table" "foo" { 456 vpc_id = "${aws_vpc.foo.id}" 457 458 propagating_vgws = ["${aws_vpn_gateway.foo.id}"] 459 } 460 `