github.com/jpreese/tflint@v0.19.2-0.20200908152133-b01686250fb6/rules/terraformrules/terraform_unused_declarations_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_TerraformUnusedDeclarationsRule(t *testing.T) { 11 cases := []struct { 12 Name string 13 Content string 14 JSON bool 15 Expected tflint.Issues 16 }{ 17 { 18 Name: "unused variable", 19 Content: ` 20 variable "not_used" {} 21 22 variable "used" {} 23 output "u" { value = var.used } 24 `, 25 Expected: tflint.Issues{ 26 { 27 Rule: NewTerraformUnusedDeclarationsRule(), 28 Message: `variable "not_used" is declared but not used`, 29 Range: hcl.Range{ 30 Filename: "config.tf", 31 Start: hcl.Pos{Line: 2, Column: 1}, 32 End: hcl.Pos{Line: 2, Column: 20}, 33 }, 34 }, 35 }, 36 }, 37 { 38 Name: "unused data source", 39 Content: ` 40 data "null_data_source" "not_used" {} 41 42 data "null_data_source" "used" {} 43 output "u" { value = data.null_data_source.used } 44 `, 45 Expected: tflint.Issues{ 46 { 47 Rule: NewTerraformUnusedDeclarationsRule(), 48 Message: `data "null_data_source" "not_used" is declared but not used`, 49 Range: hcl.Range{ 50 Filename: "config.tf", 51 Start: hcl.Pos{Line: 2, Column: 1}, 52 End: hcl.Pos{Line: 2, Column: 35}, 53 }, 54 }, 55 }, 56 }, 57 { 58 Name: "unused local source", 59 Content: ` 60 locals { 61 not_used = "" 62 used = "" 63 } 64 65 output "u" { value = local.used } 66 `, 67 Expected: tflint.Issues{ 68 { 69 Rule: NewTerraformUnusedDeclarationsRule(), 70 Message: `local.not_used is declared but not used`, 71 Range: hcl.Range{ 72 Filename: "config.tf", 73 Start: hcl.Pos{Line: 3, Column: 2}, 74 End: hcl.Pos{Line: 3, Column: 15}, 75 }, 76 }, 77 }, 78 }, 79 { 80 Name: "variable used in resource", 81 Content: ` 82 variable "used" {} 83 resource "null_resource" "n" { 84 triggers = { 85 u = var.used 86 } 87 } 88 `, 89 Expected: tflint.Issues{}, 90 }, 91 { 92 Name: "variable used in module", 93 Content: ` 94 variable "used" {} 95 module "m" { 96 source = "." 97 u = var.used 98 } 99 `, 100 Expected: tflint.Issues{}, 101 }, 102 { 103 Name: "variable used in module", 104 Content: ` 105 variable "used" {} 106 module "m" { 107 source = "." 108 u = var.used 109 } 110 `, 111 Expected: tflint.Issues{}, 112 }, 113 { 114 Name: "variable used in provider", 115 Content: ` 116 variable "aws_region" {} 117 provider "aws" { 118 region = var.aws_region 119 } 120 `, 121 Expected: tflint.Issues{}, 122 }, 123 { 124 Name: "meta-arguments", 125 Content: ` 126 variable "used" {} 127 resource "null_resource" "n" { 128 triggers = { 129 u = var.used 130 } 131 132 lifecycle { 133 ignore_changes = [triggers] 134 } 135 136 providers = { 137 null = null 138 } 139 140 depends_on = [aws_instance.foo] 141 } 142 `, 143 Expected: tflint.Issues{}, 144 }, 145 { 146 Name: "additional traversal", 147 Content: ` 148 variable "v" { 149 type = object({ foo = string }) 150 } 151 output "v" { 152 value = var.v.foo 153 } 154 155 data "terraform_remote_state" "d" {} 156 output "d" { 157 value = data.terraform_remote_state.d.outputs.foo 158 } 159 `, 160 Expected: tflint.Issues{}, 161 }, 162 { 163 Name: "json", 164 JSON: true, 165 Content: ` 166 { 167 "resource": { 168 "foo": { 169 "bar": { 170 "nested": [{ 171 "${var.again}": [] 172 }] 173 } 174 } 175 }, 176 "variable": { 177 "again": {} 178 } 179 }`, 180 Expected: tflint.Issues{}, 181 }, 182 } 183 184 rule := NewTerraformUnusedDeclarationsRule() 185 186 for _, tc := range cases { 187 filename := "config.tf" 188 if tc.JSON { 189 filename += ".json" 190 } 191 192 t.Run(tc.Name, func(t *testing.T) { 193 runner := tflint.TestRunner(t, map[string]string{filename: tc.Content}) 194 195 if err := rule.Check(runner); err != nil { 196 t.Fatalf("Unexpected error occurred: %s", err) 197 } 198 199 tflint.AssertIssues(t, tc.Expected, runner.Issues) 200 }) 201 } 202 }