github.com/wata727/tflint@v0.12.2-0.20191013070026-96dd0d36f385/rules/awsrules/aws_route_not_specified_target_test.go (about)

     1  package awsrules
     2  
     3  import (
     4  	"testing"
     5  
     6  	hcl "github.com/hashicorp/hcl/v2"
     7  	"github.com/wata727/tflint/tflint"
     8  )
     9  
    10  func Test_AwsRouteNotSpecifiedTarget(t *testing.T) {
    11  	cases := []struct {
    12  		Name     string
    13  		Content  string
    14  		Expected tflint.Issues
    15  	}{
    16  		{
    17  			Name: "route target is not specified",
    18  			Content: `
    19  resource "aws_route" "foo" {
    20      route_table_id = "rtb-1234abcd"
    21  }`,
    22  			Expected: tflint.Issues{
    23  				{
    24  					Rule:    NewAwsRouteNotSpecifiedTargetRule(),
    25  					Message: "The routing target is not specified, each aws_route must contain either egress_only_gateway_id, gateway_id, instance_id, nat_gateway_id, network_interface_id, transit_gateway_id, or vpc_peering_connection_id.",
    26  					Range: hcl.Range{
    27  						Filename: "resource.tf",
    28  						Start:    hcl.Pos{Line: 2, Column: 1},
    29  						End:      hcl.Pos{Line: 2, Column: 27},
    30  					},
    31  				},
    32  			},
    33  		},
    34  		{
    35  			Name: "gateway_id is specified",
    36  			Content: `
    37  resource "aws_route" "foo" {
    38      route_table_id = "rtb-1234abcd"
    39      gateway_id = "igw-1234abcd"
    40  }`,
    41  			Expected: tflint.Issues{},
    42  		},
    43  		{
    44  			Name: "egress_only_gateway_id is specified",
    45  			Content: `
    46  resource "aws_route" "foo" {
    47      route_table_id = "rtb-1234abcd"
    48      egress_only_gateway_id = "eigw-1234abcd"
    49  }`,
    50  			Expected: tflint.Issues{},
    51  		},
    52  		{
    53  			Name: "nat_gateway_id is specified",
    54  			Content: `
    55  resource "aws_route" "foo" {
    56      route_table_id = "rtb-1234abcd"
    57      nat_gateway_id = "nat-1234abcd"
    58  }`,
    59  			Expected: tflint.Issues{},
    60  		},
    61  		{
    62  			Name: "instance_id is specified",
    63  			Content: `
    64  resource "aws_route" "foo" {
    65      route_table_id = "rtb-1234abcd"
    66      instance_id = "i-1234abcd"
    67  }`,
    68  			Expected: tflint.Issues{},
    69  		},
    70  		{
    71  			Name: "vpc_peering_connection_id is specified",
    72  			Content: `
    73  resource "aws_route" "foo" {
    74      route_table_id = "rtb-1234abcd"
    75      vpc_peering_connection_id = "pcx-1234abcd"
    76  }`,
    77  			Expected: tflint.Issues{},
    78  		},
    79  		{
    80  			Name: "network_interface_id is specified",
    81  			Content: `
    82  resource "aws_route" "foo" {
    83      route_table_id = "rtb-1234abcd"
    84      network_interface_id = "eni-1234abcd"
    85  }`,
    86  			Expected: tflint.Issues{},
    87  		},
    88  		{
    89  			Name: "transit_gateway_id is specified",
    90  			Content: `
    91  resource "aws_route" "foo" {
    92  	route_table_id = "rtb-1234abcd"
    93  	transit_gateway_id = "tgw-1234abcd"
    94  }`,
    95  			Expected: tflint.Issues{},
    96  		},
    97  		{
    98  			Name: "transit_gateway_id is specified, but the value is null",
    99  			Content: `
   100  variable "transit_gateway_id" {
   101  	type    = string
   102  	default = null
   103  }
   104  
   105  resource "aws_route" "foo" {
   106  	route_table_id = "rtb-1234abcd"
   107  	transit_gateway_id = var.transit_gateway_id
   108  }`,
   109  			Expected: tflint.Issues{
   110  				{
   111  					Rule:    NewAwsRouteNotSpecifiedTargetRule(),
   112  					Message: "The routing target is not specified, each aws_route must contain either egress_only_gateway_id, gateway_id, instance_id, nat_gateway_id, network_interface_id, transit_gateway_id, or vpc_peering_connection_id.",
   113  					Range: hcl.Range{
   114  						Filename: "resource.tf",
   115  						Start:    hcl.Pos{Line: 7, Column: 1},
   116  						End:      hcl.Pos{Line: 7, Column: 27},
   117  					},
   118  				},
   119  			},
   120  		},
   121  	}
   122  
   123  	rule := NewAwsRouteNotSpecifiedTargetRule()
   124  
   125  	for _, tc := range cases {
   126  		runner := tflint.TestRunner(t, map[string]string{"resource.tf": tc.Content})
   127  
   128  		if err := rule.Check(runner); err != nil {
   129  			t.Fatalf("Unexpected error occurred: %s", err)
   130  		}
   131  
   132  		tflint.AssertIssues(t, tc.Expected, runner.Issues)
   133  	}
   134  }