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