github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  			resource.TestStep{
    42  				Config: testAccAWSRouteBasicConfig,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckAWSRouteExists("aws_route.bar", &route),
    45  					testCheck,
    46  				),
    47  			},
    48  		},
    49  	})
    50  }
    51  
    52  func TestAccAWSRoute_changeCidr(t *testing.T) {
    53  	var route ec2.Route
    54  	var routeTable ec2.RouteTable
    55  
    56  	//aws creates a default route
    57  	testCheck := func(s *terraform.State) error {
    58  		if *route.DestinationCidrBlock != "10.3.0.0/16" {
    59  			return fmt.Errorf("Destination Cidr (Expected=%s, Actual=%s)\n", "10.3.0.0/16", *route.DestinationCidrBlock)
    60  		}
    61  
    62  		name := "aws_internet_gateway.foo"
    63  		gwres, ok := s.RootModule().Resources[name]
    64  		if !ok {
    65  			return fmt.Errorf("Not found: %s\n", name)
    66  		}
    67  
    68  		if *route.GatewayId != gwres.Primary.ID {
    69  			return fmt.Errorf("Internet Gateway Id (Expected=%s, Actual=%s)\n", gwres.Primary.ID, *route.GatewayId)
    70  		}
    71  
    72  		return nil
    73  	}
    74  
    75  	testCheckChange := func(s *terraform.State) error {
    76  		if *route.DestinationCidrBlock != "10.2.0.0/16" {
    77  			return fmt.Errorf("Destination Cidr (Expected=%s, Actual=%s)\n", "10.2.0.0/16", *route.DestinationCidrBlock)
    78  		}
    79  
    80  		name := "aws_internet_gateway.foo"
    81  		gwres, ok := s.RootModule().Resources[name]
    82  		if !ok {
    83  			return fmt.Errorf("Not found: %s\n", name)
    84  		}
    85  
    86  		if *route.GatewayId != gwres.Primary.ID {
    87  			return fmt.Errorf("Internet Gateway Id (Expected=%s, Actual=%s)\n", gwres.Primary.ID, *route.GatewayId)
    88  		}
    89  
    90  		if rtlen := len(routeTable.Routes); rtlen != 2 {
    91  			return fmt.Errorf("Route Table has too many routes (Expected=%d, Actual=%d)\n", rtlen, 2)
    92  		}
    93  
    94  		return nil
    95  	}
    96  
    97  	resource.Test(t, resource.TestCase{
    98  		PreCheck: func() {
    99  			testAccPreCheck(t)
   100  		},
   101  		Providers:    testAccProviders,
   102  		CheckDestroy: testAccCheckAWSRouteDestroy,
   103  		Steps: []resource.TestStep{
   104  			resource.TestStep{
   105  				Config: testAccAWSRouteBasicConfig,
   106  				Check: resource.ComposeTestCheckFunc(
   107  					testAccCheckAWSRouteExists("aws_route.bar", &route),
   108  					testCheck,
   109  				),
   110  			},
   111  			resource.TestStep{
   112  				Config: testAccAWSRouteBasicConfigChangeCidr,
   113  				Check: resource.ComposeTestCheckFunc(
   114  					testAccCheckAWSRouteExists("aws_route.bar", &route),
   115  					testAccCheckRouteTableExists("aws_route_table.foo", &routeTable),
   116  					testCheckChange,
   117  				),
   118  			},
   119  		},
   120  	})
   121  }
   122  
   123  func TestAccAWSRoute_noopdiff(t *testing.T) {
   124  	var route ec2.Route
   125  	var routeTable ec2.RouteTable
   126  
   127  	testCheck := func(s *terraform.State) error {
   128  		return nil
   129  	}
   130  
   131  	testCheckChange := func(s *terraform.State) error {
   132  		return nil
   133  	}
   134  
   135  	resource.Test(t, resource.TestCase{
   136  		PreCheck: func() {
   137  			testAccPreCheck(t)
   138  		},
   139  		Providers:    testAccProviders,
   140  		CheckDestroy: testAccCheckAWSRouteDestroy,
   141  		Steps: []resource.TestStep{
   142  			resource.TestStep{
   143  				Config: testAccAWSRouteNoopChange,
   144  				Check: resource.ComposeTestCheckFunc(
   145  					testAccCheckAWSRouteExists("aws_route.test", &route),
   146  					testCheck,
   147  				),
   148  			},
   149  			resource.TestStep{
   150  				Config: testAccAWSRouteNoopChange,
   151  				Check: resource.ComposeTestCheckFunc(
   152  					testAccCheckAWSRouteExists("aws_route.test", &route),
   153  					testAccCheckRouteTableExists("aws_route_table.test", &routeTable),
   154  					testCheckChange,
   155  				),
   156  			},
   157  		},
   158  	})
   159  }
   160  
   161  func TestAccAWSRoute_doesNotCrashWithVPCEndpoint(t *testing.T) {
   162  	var route ec2.Route
   163  
   164  	resource.Test(t, resource.TestCase{
   165  		PreCheck:     func() { testAccPreCheck(t) },
   166  		Providers:    testAccProviders,
   167  		CheckDestroy: testAccCheckAWSRouteDestroy,
   168  		Steps: []resource.TestStep{
   169  			resource.TestStep{
   170  				Config: testAccAWSRouteWithVPCEndpoint,
   171  				Check: resource.ComposeTestCheckFunc(
   172  					testAccCheckAWSRouteExists("aws_route.bar", &route),
   173  				),
   174  			},
   175  		},
   176  	})
   177  }
   178  
   179  // Acceptance test if mixed inline and external routes are implemented
   180  /*
   181  func TestAccAWSRoute_mix(t *testing.T) {
   182  	var rt ec2.RouteTable
   183  	var route ec2.Route
   184  
   185  	//aws creates a default route
   186  	testCheck := func(s *terraform.State) error {
   187  		if *route.DestinationCidrBlock != "0.0.0.0/0" {
   188  			return fmt.Errorf("Destination Cidr (Expected=%s, Actual=%s)\n", "0.0.0.0/0", *route.DestinationCidrBlock)
   189  		}
   190  
   191  		name := "aws_internet_gateway.foo"
   192  		gwres, ok := s.RootModule().Resources[name]
   193  		if !ok {
   194  			return fmt.Errorf("Not found: %s\n", name)
   195  		}
   196  
   197  		if *route.GatewayId != gwres.Primary.ID {
   198  			return fmt.Errorf("Internet Gateway Id (Expected=%s, Actual=%s)\n", gwres.Primary.ID, *route.GatewayId)
   199  		}
   200  
   201  		if len(rt.Routes) != 3 {
   202  			return fmt.Errorf("bad routes: %#v", rt.Routes)
   203  		}
   204  
   205  		routes := make(map[string]*ec2.Route)
   206  		for _, r := range rt.Routes {
   207  			routes[*r.DestinationCidrBlock] = r
   208  		}
   209  
   210  		if _, ok := routes["10.1.0.0/16"]; !ok {
   211  			return fmt.Errorf("Missing route %s: %#v", "10.1.0.0/16", rt.Routes)
   212  		}
   213  		if _, ok := routes["10.2.0.0/16"]; !ok {
   214  			return fmt.Errorf("Missing route %s: %#v", "10.2.0.0/16", rt.Routes)
   215  		}
   216  		if _, ok := routes["0.0.0.0/0"]; !ok {
   217  			return fmt.Errorf("Missing route %s: %#v", "0.0.0.0/0", rt.Routes)
   218  		}
   219  
   220  		return nil
   221  	}
   222  
   223  	resource.Test(t, resource.TestCase{
   224  		PreCheck:     func() { testAccPreCheck(t) },
   225  		Providers:    testAccProviders,
   226  		CheckDestroy: testAccCheckAWSRouteDestroy,
   227  		Steps: []resource.TestStep{
   228  			resource.TestStep{
   229  				Config: testAccAWSRouteMixConfig,
   230  				Check: resource.ComposeTestCheckFunc(
   231  					testAccCheckRouteTableExists("aws_route_table.foo", &rt),
   232  					testAccCheckAWSRouteExists("aws_route.bar", &route),
   233  					testCheck,
   234  				),
   235  			},
   236  		},
   237  	})
   238  }
   239  */
   240  
   241  func testAccCheckAWSRouteExists(n string, res *ec2.Route) resource.TestCheckFunc {
   242  	return func(s *terraform.State) error {
   243  		rs, ok := s.RootModule().Resources[n]
   244  		if !ok {
   245  			return fmt.Errorf("Not found: %s\n", n)
   246  		}
   247  
   248  		if rs.Primary.ID == "" {
   249  			return fmt.Errorf("No ID is set")
   250  		}
   251  
   252  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   253  		r, err := findResourceRoute(
   254  			conn,
   255  			rs.Primary.Attributes["route_table_id"],
   256  			rs.Primary.Attributes["destination_cidr_block"],
   257  		)
   258  
   259  		if err != nil {
   260  			return err
   261  		}
   262  
   263  		if r == nil {
   264  			return fmt.Errorf("Route not found")
   265  		}
   266  
   267  		*res = *r
   268  
   269  		return nil
   270  	}
   271  }
   272  
   273  func testAccCheckAWSRouteDestroy(s *terraform.State) error {
   274  	for _, rs := range s.RootModule().Resources {
   275  		if rs.Type != "aws_route" {
   276  			continue
   277  		}
   278  
   279  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   280  		route, err := findResourceRoute(
   281  			conn,
   282  			rs.Primary.Attributes["route_table_id"],
   283  			rs.Primary.Attributes["destination_cidr_block"],
   284  		)
   285  
   286  		if route == nil && err == nil {
   287  			return nil
   288  		}
   289  	}
   290  
   291  	return nil
   292  }
   293  
   294  var testAccAWSRouteBasicConfig = fmt.Sprint(`
   295  resource "aws_vpc" "foo" {
   296  	cidr_block = "10.1.0.0/16"
   297  }
   298  
   299  resource "aws_internet_gateway" "foo" {
   300  	vpc_id = "${aws_vpc.foo.id}"
   301  }
   302  
   303  resource "aws_route_table" "foo" {
   304  	vpc_id = "${aws_vpc.foo.id}"
   305  }
   306  
   307  resource "aws_route" "bar" {
   308  	route_table_id = "${aws_route_table.foo.id}"
   309  	destination_cidr_block = "10.3.0.0/16"
   310  	gateway_id = "${aws_internet_gateway.foo.id}"
   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  `)