github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_route_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/service/ec2" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccAWSRoute_basic(t *testing.T) { 13 var route ec2.Route 14 15 //aws creates a default route 16 testCheck := func(s *terraform.State) error { 17 if *route.DestinationCidrBlock != "10.3.0.0/16" { 18 return fmt.Errorf("Destination Cidr (Expected=%s, Actual=%s)\n", "10.3.0.0/16", *route.DestinationCidrBlock) 19 } 20 21 name := "aws_internet_gateway.foo" 22 gwres, ok := s.RootModule().Resources[name] 23 if !ok { 24 return fmt.Errorf("Not found: %s\n", name) 25 } 26 27 if *route.GatewayId != gwres.Primary.ID { 28 return fmt.Errorf("Internet Gateway Id (Expected=%s, Actual=%s)\n", gwres.Primary.ID, *route.GatewayId) 29 } 30 31 return nil 32 } 33 34 resource.Test(t, resource.TestCase{ 35 PreCheck: func() { 36 testAccPreCheck(t) 37 }, 38 Providers: testAccProviders, 39 CheckDestroy: testAccCheckAWSRouteDestroy, 40 Steps: []resource.TestStep{ 41 { 42 Config: testAccAWSRouteBasicConfig, 43 Check: resource.ComposeTestCheckFunc( 44 testAccCheckAWSRouteExists("aws_route.bar", &route), 45 testCheck, 46 ), 47 }, 48 }, 49 }) 50 } 51 52 func TestAccAWSRoute_ipv6Support(t *testing.T) { 53 var route ec2.Route 54 55 //aws creates a default route 56 testCheck := func(s *terraform.State) error { 57 58 name := "aws_egress_only_internet_gateway.foo" 59 gwres, ok := s.RootModule().Resources[name] 60 if !ok { 61 return fmt.Errorf("Not found: %s\n", name) 62 } 63 64 if *route.EgressOnlyInternetGatewayId != gwres.Primary.ID { 65 return fmt.Errorf("Egress Only Internet Gateway Id (Expected=%s, Actual=%s)\n", gwres.Primary.ID, *route.EgressOnlyInternetGatewayId) 66 } 67 68 return nil 69 } 70 71 resource.Test(t, resource.TestCase{ 72 PreCheck: func() { 73 testAccPreCheck(t) 74 }, 75 Providers: testAccProviders, 76 CheckDestroy: testAccCheckAWSRouteDestroy, 77 Steps: []resource.TestStep{ 78 { 79 Config: testAccAWSRouteConfigIpv6, 80 Check: resource.ComposeTestCheckFunc( 81 testAccCheckAWSRouteExists("aws_route.bar", &route), 82 testCheck, 83 ), 84 }, 85 }, 86 }) 87 } 88 89 func TestAccAWSRoute_changeCidr(t *testing.T) { 90 var route ec2.Route 91 var routeTable ec2.RouteTable 92 93 //aws creates a default route 94 testCheck := func(s *terraform.State) error { 95 if *route.DestinationCidrBlock != "10.3.0.0/16" { 96 return fmt.Errorf("Destination Cidr (Expected=%s, Actual=%s)\n", "10.3.0.0/16", *route.DestinationCidrBlock) 97 } 98 99 name := "aws_internet_gateway.foo" 100 gwres, ok := s.RootModule().Resources[name] 101 if !ok { 102 return fmt.Errorf("Not found: %s\n", name) 103 } 104 105 if *route.GatewayId != gwres.Primary.ID { 106 return fmt.Errorf("Internet Gateway Id (Expected=%s, Actual=%s)\n", gwres.Primary.ID, *route.GatewayId) 107 } 108 109 return nil 110 } 111 112 testCheckChange := func(s *terraform.State) error { 113 if *route.DestinationCidrBlock != "10.2.0.0/16" { 114 return fmt.Errorf("Destination Cidr (Expected=%s, Actual=%s)\n", "10.2.0.0/16", *route.DestinationCidrBlock) 115 } 116 117 name := "aws_internet_gateway.foo" 118 gwres, ok := s.RootModule().Resources[name] 119 if !ok { 120 return fmt.Errorf("Not found: %s\n", name) 121 } 122 123 if *route.GatewayId != gwres.Primary.ID { 124 return fmt.Errorf("Internet Gateway Id (Expected=%s, Actual=%s)\n", gwres.Primary.ID, *route.GatewayId) 125 } 126 127 if rtlen := len(routeTable.Routes); rtlen != 2 { 128 return fmt.Errorf("Route Table has too many routes (Expected=%d, Actual=%d)\n", rtlen, 2) 129 } 130 131 return nil 132 } 133 134 resource.Test(t, resource.TestCase{ 135 PreCheck: func() { 136 testAccPreCheck(t) 137 }, 138 Providers: testAccProviders, 139 CheckDestroy: testAccCheckAWSRouteDestroy, 140 Steps: []resource.TestStep{ 141 { 142 Config: testAccAWSRouteBasicConfig, 143 Check: resource.ComposeTestCheckFunc( 144 testAccCheckAWSRouteExists("aws_route.bar", &route), 145 testCheck, 146 ), 147 }, 148 { 149 Config: testAccAWSRouteBasicConfigChangeCidr, 150 Check: resource.ComposeTestCheckFunc( 151 testAccCheckAWSRouteExists("aws_route.bar", &route), 152 testAccCheckRouteTableExists("aws_route_table.foo", &routeTable), 153 testCheckChange, 154 ), 155 }, 156 }, 157 }) 158 } 159 160 func TestAccAWSRoute_noopdiff(t *testing.T) { 161 var route ec2.Route 162 var routeTable ec2.RouteTable 163 164 testCheck := func(s *terraform.State) error { 165 return nil 166 } 167 168 testCheckChange := func(s *terraform.State) error { 169 return nil 170 } 171 172 resource.Test(t, resource.TestCase{ 173 PreCheck: func() { 174 testAccPreCheck(t) 175 }, 176 Providers: testAccProviders, 177 CheckDestroy: testAccCheckAWSRouteDestroy, 178 Steps: []resource.TestStep{ 179 { 180 Config: testAccAWSRouteNoopChange, 181 Check: resource.ComposeTestCheckFunc( 182 testAccCheckAWSRouteExists("aws_route.test", &route), 183 testCheck, 184 ), 185 }, 186 { 187 Config: testAccAWSRouteNoopChange, 188 Check: resource.ComposeTestCheckFunc( 189 testAccCheckAWSRouteExists("aws_route.test", &route), 190 testAccCheckRouteTableExists("aws_route_table.test", &routeTable), 191 testCheckChange, 192 ), 193 }, 194 }, 195 }) 196 } 197 198 func TestAccAWSRoute_doesNotCrashWithVPCEndpoint(t *testing.T) { 199 var route ec2.Route 200 201 resource.Test(t, resource.TestCase{ 202 PreCheck: func() { testAccPreCheck(t) }, 203 Providers: testAccProviders, 204 CheckDestroy: testAccCheckAWSRouteDestroy, 205 Steps: []resource.TestStep{ 206 { 207 Config: testAccAWSRouteWithVPCEndpoint, 208 Check: resource.ComposeTestCheckFunc( 209 testAccCheckAWSRouteExists("aws_route.bar", &route), 210 ), 211 }, 212 }, 213 }) 214 } 215 216 func testAccCheckAWSRouteExists(n string, res *ec2.Route) resource.TestCheckFunc { 217 return func(s *terraform.State) error { 218 rs, ok := s.RootModule().Resources[n] 219 if !ok { 220 return fmt.Errorf("Not found: %s\n", n) 221 } 222 223 if rs.Primary.ID == "" { 224 return fmt.Errorf("No ID is set") 225 } 226 227 conn := testAccProvider.Meta().(*AWSClient).ec2conn 228 r, err := findResourceRoute( 229 conn, 230 rs.Primary.Attributes["route_table_id"], 231 rs.Primary.Attributes["destination_cidr_block"], 232 rs.Primary.Attributes["destination_ipv6_cidr_block"], 233 ) 234 235 if err != nil { 236 return err 237 } 238 239 if r == nil { 240 return fmt.Errorf("Route not found") 241 } 242 243 *res = *r 244 245 return nil 246 } 247 } 248 249 func testAccCheckAWSRouteDestroy(s *terraform.State) error { 250 for _, rs := range s.RootModule().Resources { 251 if rs.Type != "aws_route" { 252 continue 253 } 254 255 conn := testAccProvider.Meta().(*AWSClient).ec2conn 256 route, err := findResourceRoute( 257 conn, 258 rs.Primary.Attributes["route_table_id"], 259 rs.Primary.Attributes["destination_cidr_block"], 260 rs.Primary.Attributes["destination_ipv6_cidr_block"], 261 ) 262 263 if route == nil && err == nil { 264 return nil 265 } 266 } 267 268 return nil 269 } 270 271 var testAccAWSRouteBasicConfig = fmt.Sprint(` 272 resource "aws_vpc" "foo" { 273 cidr_block = "10.1.0.0/16" 274 } 275 276 resource "aws_internet_gateway" "foo" { 277 vpc_id = "${aws_vpc.foo.id}" 278 } 279 280 resource "aws_route_table" "foo" { 281 vpc_id = "${aws_vpc.foo.id}" 282 } 283 284 resource "aws_route" "bar" { 285 route_table_id = "${aws_route_table.foo.id}" 286 destination_cidr_block = "10.3.0.0/16" 287 gateway_id = "${aws_internet_gateway.foo.id}" 288 } 289 `) 290 291 var testAccAWSRouteConfigIpv6 = fmt.Sprintf(` 292 resource "aws_vpc" "foo" { 293 cidr_block = "10.1.0.0/16" 294 assign_generated_ipv6_cidr_block = true 295 } 296 297 resource "aws_egress_only_internet_gateway" "foo" { 298 vpc_id = "${aws_vpc.foo.id}" 299 } 300 301 resource "aws_route_table" "foo" { 302 vpc_id = "${aws_vpc.foo.id}" 303 } 304 305 resource "aws_route" "bar" { 306 route_table_id = "${aws_route_table.foo.id}" 307 destination_ipv6_cidr_block = "::/0" 308 egress_only_gateway_id = "${aws_egress_only_internet_gateway.foo.id}" 309 } 310 311 312 `) 313 314 var testAccAWSRouteBasicConfigChangeCidr = fmt.Sprint(` 315 resource "aws_vpc" "foo" { 316 cidr_block = "10.1.0.0/16" 317 } 318 319 resource "aws_internet_gateway" "foo" { 320 vpc_id = "${aws_vpc.foo.id}" 321 } 322 323 resource "aws_route_table" "foo" { 324 vpc_id = "${aws_vpc.foo.id}" 325 } 326 327 resource "aws_route" "bar" { 328 route_table_id = "${aws_route_table.foo.id}" 329 destination_cidr_block = "10.2.0.0/16" 330 gateway_id = "${aws_internet_gateway.foo.id}" 331 } 332 `) 333 334 // Acceptance test if mixed inline and external routes are implemented 335 var testAccAWSRouteMixConfig = fmt.Sprint(` 336 resource "aws_vpc" "foo" { 337 cidr_block = "10.1.0.0/16" 338 } 339 340 resource "aws_internet_gateway" "foo" { 341 vpc_id = "${aws_vpc.foo.id}" 342 } 343 344 resource "aws_route_table" "foo" { 345 vpc_id = "${aws_vpc.foo.id}" 346 347 route { 348 cidr_block = "10.2.0.0/16" 349 gateway_id = "${aws_internet_gateway.foo.id}" 350 } 351 } 352 353 resource "aws_route" "bar" { 354 route_table_id = "${aws_route_table.foo.id}" 355 destination_cidr_block = "0.0.0.0/0" 356 gateway_id = "${aws_internet_gateway.foo.id}" 357 } 358 `) 359 360 var testAccAWSRouteNoopChange = fmt.Sprint(` 361 resource "aws_vpc" "test" { 362 cidr_block = "10.10.0.0/16" 363 } 364 365 resource "aws_route_table" "test" { 366 vpc_id = "${aws_vpc.test.id}" 367 } 368 369 resource "aws_subnet" "test" { 370 vpc_id = "${aws_vpc.test.id}" 371 cidr_block = "10.10.10.0/24" 372 } 373 374 resource "aws_route" "test" { 375 route_table_id = "${aws_route_table.test.id}" 376 destination_cidr_block = "0.0.0.0/0" 377 instance_id = "${aws_instance.nat.id}" 378 } 379 380 resource "aws_instance" "nat" { 381 ami = "ami-9abea4fb" 382 instance_type = "t2.nano" 383 subnet_id = "${aws_subnet.test.id}" 384 } 385 `) 386 387 var testAccAWSRouteWithVPCEndpoint = fmt.Sprint(` 388 resource "aws_vpc" "foo" { 389 cidr_block = "10.1.0.0/16" 390 } 391 392 resource "aws_internet_gateway" "foo" { 393 vpc_id = "${aws_vpc.foo.id}" 394 } 395 396 resource "aws_route_table" "foo" { 397 vpc_id = "${aws_vpc.foo.id}" 398 } 399 400 resource "aws_route" "bar" { 401 route_table_id = "${aws_route_table.foo.id}" 402 destination_cidr_block = "10.3.0.0/16" 403 gateway_id = "${aws_internet_gateway.foo.id}" 404 405 # Forcing endpoint to create before route - without this the crash is a race. 406 depends_on = ["aws_vpc_endpoint.baz"] 407 } 408 409 resource "aws_vpc_endpoint" "baz" { 410 vpc_id = "${aws_vpc.foo.id}" 411 service_name = "com.amazonaws.us-west-2.s3" 412 route_table_ids = ["${aws_route_table.foo.id}"] 413 } 414 `)