github.com/jpreese/tflint@v0.19.2-0.20200908152133-b01686250fb6/rules/terraformrules/terraform_workspace_remote_test.go (about)

     1  package terraformrules
     2  
     3  import (
     4  	"testing"
     5  
     6  	hcl "github.com/hashicorp/hcl/v2"
     7  	"github.com/terraform-linters/tflint/tflint"
     8  )
     9  
    10  func Test_TerraformWorkspaceRemoteRule(t *testing.T) {
    11  	cases := []struct {
    12  		Name     string
    13  		Content  string
    14  		Expected tflint.Issues
    15  	}{
    16  		{
    17  			Name: "terraform.workspace in resource with remote backend",
    18  			Content: `
    19  terraform {
    20  	backend "remote" {}
    21  }
    22  resource "null_resource" "a" {
    23  	triggers = {
    24  		w = terraform.workspace
    25  	}
    26  }`,
    27  			Expected: tflint.Issues{
    28  				{
    29  					Rule:    NewTerraformWorkspaceRemoteRule(),
    30  					Message: "terraform.workspace should not be used with a 'remote' backend",
    31  					Range: hcl.Range{
    32  						Filename: "config.tf",
    33  						Start:    hcl.Pos{Line: 6, Column: 13},
    34  						End:      hcl.Pos{Line: 8, Column: 3},
    35  					},
    36  				},
    37  			},
    38  		},
    39  		{
    40  			Name: "terraform.workspace with no backend",
    41  			Content: `
    42  resource "null_resource" "a" {
    43  	triggers = {
    44  		w = terraform.workspace
    45  	}
    46  }`,
    47  			Expected: tflint.Issues{},
    48  		},
    49  		{
    50  			Name: "terraform.workspace with non-remote backend",
    51  			Content: `
    52  terraform {
    53  	backend "local" {}
    54  }
    55  
    56  resource "null_resource" "a" {
    57  	triggers = {
    58  		w = terraform.workspace
    59  	}
    60  }`,
    61  			Expected: tflint.Issues{},
    62  		},
    63  		{
    64  			Name: "terraform.workspace in data source with remote backend",
    65  			Content: `
    66  terraform {
    67  	backend "remote" {}
    68  }
    69  data "null_data_source" "a" {
    70  	inputs = {
    71  		w = terraform.workspace
    72  	}
    73  }`,
    74  			Expected: tflint.Issues{
    75  				{
    76  					Rule:    NewTerraformWorkspaceRemoteRule(),
    77  					Message: "terraform.workspace should not be used with a 'remote' backend",
    78  					Range: hcl.Range{
    79  						Filename: "config.tf",
    80  						Start:    hcl.Pos{Line: 6, Column: 11},
    81  						End:      hcl.Pos{Line: 8, Column: 3},
    82  					},
    83  				},
    84  			},
    85  		},
    86  		{
    87  			Name: "terraform.workspace in module call with remote backend",
    88  			Content: `
    89  terraform {
    90  	backend "remote" {}
    91  }
    92  module "a" {
    93  	source = "."
    94  	w = terraform.workspace
    95  }`,
    96  			Expected: tflint.Issues{
    97  				{
    98  					Rule:    NewTerraformWorkspaceRemoteRule(),
    99  					Message: "terraform.workspace should not be used with a 'remote' backend",
   100  					Range: hcl.Range{
   101  						Filename: "config.tf",
   102  						Start:    hcl.Pos{Line: 7, Column: 6},
   103  						End:      hcl.Pos{Line: 7, Column: 25},
   104  					},
   105  				},
   106  			},
   107  		},
   108  		{
   109  			Name: "terraform.workspace in provider config with remote backend",
   110  			Content: `
   111  terraform {
   112  	backend "remote" {}
   113  }
   114  provider "aws" {
   115  	assume_role	{
   116  		role_arn = terraform.workspace
   117  	}
   118  }`,
   119  			Expected: tflint.Issues{
   120  				{
   121  					Rule:    NewTerraformWorkspaceRemoteRule(),
   122  					Message: "terraform.workspace should not be used with a 'remote' backend",
   123  					Range: hcl.Range{
   124  						Filename: "config.tf",
   125  						Start:    hcl.Pos{Line: 7, Column: 14},
   126  						End:      hcl.Pos{Line: 7, Column: 33},
   127  					},
   128  				},
   129  			},
   130  		},
   131  		{
   132  			Name: "terraform.workspace in locals with remote backend",
   133  			Content: `
   134  terraform {
   135  	backend "remote" {}
   136  }
   137  locals {
   138  	w = terraform.workspace
   139  }`,
   140  			Expected: tflint.Issues{
   141  				{
   142  					Rule:    NewTerraformWorkspaceRemoteRule(),
   143  					Message: "terraform.workspace should not be used with a 'remote' backend",
   144  					Range: hcl.Range{
   145  						Filename: "config.tf",
   146  						Start:    hcl.Pos{Line: 6, Column: 6},
   147  						End:      hcl.Pos{Line: 6, Column: 25},
   148  					},
   149  				},
   150  			},
   151  		},
   152  		{
   153  			Name: "terraform.workspace in output with remote backend",
   154  			Content: `
   155  terraform {
   156  	backend "remote" {}
   157  }
   158  output "o" {
   159  	value = terraform.workspace
   160  }`,
   161  			Expected: tflint.Issues{
   162  				{
   163  					Rule:    NewTerraformWorkspaceRemoteRule(),
   164  					Message: "terraform.workspace should not be used with a 'remote' backend",
   165  					Range: hcl.Range{
   166  						Filename: "config.tf",
   167  						Start:    hcl.Pos{Line: 6, Column: 10},
   168  						End:      hcl.Pos{Line: 6, Column: 29},
   169  					},
   170  				},
   171  			},
   172  		},
   173  		{
   174  			Name: "nonmatching expressions with remote backend",
   175  			Content: `
   176  terraform {
   177  	backend "remote" {}
   178  }
   179  locals {
   180  	a = "terraform.workspace"
   181  	b = path.module
   182  }`,
   183  			Expected: tflint.Issues{},
   184  		},
   185  		{
   186  			Name: "meta-arguments",
   187  			Content: `
   188  terraform {
   189  	backend "remote" {}
   190  }
   191  resource "aws_instance" "foo" {
   192    instance_type = "t3.nano"
   193  
   194    lifecycle {
   195      ignore_changes = [instance_type]
   196    }
   197  
   198    providers = {
   199      aws = aws
   200    }
   201  
   202    depends_on = [aws_instance.bar]
   203  }`,
   204  			Expected: tflint.Issues{},
   205  		},
   206  	}
   207  
   208  	rule := NewTerraformWorkspaceRemoteRule()
   209  
   210  	for _, tc := range cases {
   211  		t.Run(tc.Name, func(t *testing.T) {
   212  			runner := tflint.TestRunner(t, map[string]string{"config.tf": tc.Content})
   213  
   214  			if err := rule.Check(runner); err != nil {
   215  				t.Fatalf("Unexpected error occurred: %s", err)
   216  			}
   217  
   218  			tflint.AssertIssues(t, tc.Expected, runner.Issues)
   219  		})
   220  	}
   221  }