github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  func testAccCheckAWSRouteExists(n string, res *ec2.Route) resource.TestCheckFunc {
   180  	return func(s *terraform.State) error {
   181  		rs, ok := s.RootModule().Resources[n]
   182  		if !ok {
   183  			return fmt.Errorf("Not found: %s\n", n)
   184  		}
   185  
   186  		if rs.Primary.ID == "" {
   187  			return fmt.Errorf("No ID is set")
   188  		}
   189  
   190  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   191  		r, err := findResourceRoute(
   192  			conn,
   193  			rs.Primary.Attributes["route_table_id"],
   194  			rs.Primary.Attributes["destination_cidr_block"],
   195  		)
   196  
   197  		if err != nil {
   198  			return err
   199  		}
   200  
   201  		if r == nil {
   202  			return fmt.Errorf("Route not found")
   203  		}
   204  
   205  		*res = *r
   206  
   207  		return nil
   208  	}
   209  }
   210  
   211  func testAccCheckAWSRouteDestroy(s *terraform.State) error {
   212  	for _, rs := range s.RootModule().Resources {
   213  		if rs.Type != "aws_route" {
   214  			continue
   215  		}
   216  
   217  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   218  		route, err := findResourceRoute(
   219  			conn,
   220  			rs.Primary.Attributes["route_table_id"],
   221  			rs.Primary.Attributes["destination_cidr_block"],
   222  		)
   223  
   224  		if route == nil && err == nil {
   225  			return nil
   226  		}
   227  	}
   228  
   229  	return nil
   230  }
   231  
   232  var testAccAWSRouteBasicConfig = fmt.Sprint(`
   233  resource "aws_vpc" "foo" {
   234  	cidr_block = "10.1.0.0/16"
   235  }
   236  
   237  resource "aws_internet_gateway" "foo" {
   238  	vpc_id = "${aws_vpc.foo.id}"
   239  }
   240  
   241  resource "aws_route_table" "foo" {
   242  	vpc_id = "${aws_vpc.foo.id}"
   243  }
   244  
   245  resource "aws_route" "bar" {
   246  	route_table_id = "${aws_route_table.foo.id}"
   247  	destination_cidr_block = "10.3.0.0/16"
   248  	gateway_id = "${aws_internet_gateway.foo.id}"
   249  }
   250  `)
   251  
   252  var testAccAWSRouteBasicConfigChangeCidr = fmt.Sprint(`
   253  resource "aws_vpc" "foo" {
   254  	cidr_block = "10.1.0.0/16"
   255  }
   256  
   257  resource "aws_internet_gateway" "foo" {
   258  	vpc_id = "${aws_vpc.foo.id}"
   259  }
   260  
   261  resource "aws_route_table" "foo" {
   262  	vpc_id = "${aws_vpc.foo.id}"
   263  }
   264  
   265  resource "aws_route" "bar" {
   266  	route_table_id = "${aws_route_table.foo.id}"
   267  	destination_cidr_block = "10.2.0.0/16"
   268  	gateway_id = "${aws_internet_gateway.foo.id}"
   269  }
   270  `)
   271  
   272  // Acceptance test if mixed inline and external routes are implemented
   273  var testAccAWSRouteMixConfig = fmt.Sprint(`
   274  resource "aws_vpc" "foo" {
   275  	cidr_block = "10.1.0.0/16"
   276  }
   277  
   278  resource "aws_internet_gateway" "foo" {
   279  	vpc_id = "${aws_vpc.foo.id}"
   280  }
   281  
   282  resource "aws_route_table" "foo" {
   283  	vpc_id = "${aws_vpc.foo.id}"
   284  
   285  	route {
   286  		cidr_block = "10.2.0.0/16"
   287  		gateway_id = "${aws_internet_gateway.foo.id}"
   288  	}
   289  }
   290  
   291  resource "aws_route" "bar" {
   292  	route_table_id = "${aws_route_table.foo.id}"
   293  	destination_cidr_block = "0.0.0.0/0"
   294  	gateway_id = "${aws_internet_gateway.foo.id}"
   295  }
   296  `)
   297  
   298  var testAccAWSRouteNoopChange = fmt.Sprint(`
   299  resource "aws_vpc" "test" {
   300    cidr_block = "10.10.0.0/16"
   301  }
   302  
   303  resource "aws_route_table" "test" {
   304    vpc_id = "${aws_vpc.test.id}"
   305  }
   306  
   307  resource "aws_subnet" "test" {
   308    vpc_id = "${aws_vpc.test.id}"
   309    cidr_block = "10.10.10.0/24"
   310  }
   311  
   312  resource "aws_route" "test" {
   313    route_table_id = "${aws_route_table.test.id}"
   314    destination_cidr_block = "0.0.0.0/0"
   315    instance_id = "${aws_instance.nat.id}"
   316  }
   317  
   318  resource "aws_instance" "nat" {
   319    ami = "ami-9abea4fb"
   320    instance_type = "t2.nano"
   321    subnet_id = "${aws_subnet.test.id}"
   322  }
   323  `)
   324  
   325  var testAccAWSRouteWithVPCEndpoint = fmt.Sprint(`
   326  resource "aws_vpc" "foo" {
   327    cidr_block = "10.1.0.0/16"
   328  }
   329  
   330  resource "aws_internet_gateway" "foo" {
   331    vpc_id = "${aws_vpc.foo.id}"
   332  }
   333  
   334  resource "aws_route_table" "foo" {
   335    vpc_id = "${aws_vpc.foo.id}"
   336  }
   337  
   338  resource "aws_route" "bar" {
   339    route_table_id         = "${aws_route_table.foo.id}"
   340    destination_cidr_block = "10.3.0.0/16"
   341    gateway_id             = "${aws_internet_gateway.foo.id}"
   342  
   343    # Forcing endpoint to create before route - without this the crash is a race.
   344    depends_on = ["aws_vpc_endpoint.baz"]
   345  }
   346  
   347  resource "aws_vpc_endpoint" "baz" {
   348    vpc_id          = "${aws_vpc.foo.id}"
   349    service_name    = "com.amazonaws.us-west-2.s3"
   350    route_table_ids = ["${aws_route_table.foo.id}"]
   351  }
   352  `)