|
| 1 | +package rules |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/hashicorp/hcl/v2" |
| 7 | + "github.com/terraform-linters/tflint-plugin-sdk/helper" |
| 8 | +) |
| 9 | + |
| 10 | +func Test_TerraformMapDuplicateValues(t *testing.T) { |
| 11 | + cases := []struct { |
| 12 | + Name string |
| 13 | + Content string |
| 14 | + Expected helper.Issues |
| 15 | + Fixed string |
| 16 | + }{ |
| 17 | + { |
| 18 | + Name: "No duplicates", |
| 19 | + Content: ` |
| 20 | +resource "null_resource" "test" { |
| 21 | + test = { |
| 22 | + a = 1 |
| 23 | + b = 2 |
| 24 | + c = 3 |
| 25 | + } |
| 26 | +}`, |
| 27 | + Expected: helper.Issues{}, |
| 28 | + }, |
| 29 | + { |
| 30 | + Name: "duplicate values in map literal", |
| 31 | + Content: ` |
| 32 | +resource "null_resource" "test" { |
| 33 | + triggers = { |
| 34 | + a = "b" |
| 35 | + c = "b" |
| 36 | + } |
| 37 | +}`, |
| 38 | + Expected: helper.Issues{ |
| 39 | + { |
| 40 | + Rule: NewTerraformMapDuplicateValuesRule(), |
| 41 | + Message: `Duplicate value: "b", first defined at module.tf:4,13-16`, |
| 42 | + Range: hcl.Range{ |
| 43 | + Filename: "module.tf", |
| 44 | + Start: hcl.Pos{Line: 5, Column: 13}, |
| 45 | + End: hcl.Pos{Line: 5, Column: 16}, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + Name: "duplicate values with quoting", |
| 52 | + Content: ` |
| 53 | +resource "null_resource" "test" { |
| 54 | + triggers = { |
| 55 | + a = "b" |
| 56 | + c = "b" |
| 57 | + } |
| 58 | +}`, |
| 59 | + Expected: helper.Issues{ |
| 60 | + { |
| 61 | + Rule: NewTerraformMapDuplicateValuesRule(), |
| 62 | + Message: `Duplicate value: "b", first defined at module.tf:4,13-16`, |
| 63 | + Range: hcl.Range{ |
| 64 | + Filename: "module.tf", |
| 65 | + Start: hcl.Pos{Line: 5, Column: 13}, |
| 66 | + End: hcl.Pos{Line: 5, Column: 16}, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + Name: "Using variables as values", |
| 73 | + Content: ` |
| 74 | +variable "a" { |
| 75 | + type = string |
| 76 | + default = "b" |
| 77 | +} |
| 78 | +
|
| 79 | +resource "null_resource" "test" { |
| 80 | + map = { |
| 81 | + key1 = var.a |
| 82 | + key2 = "b" |
| 83 | + } |
| 84 | +}`, |
| 85 | + Expected: helper.Issues{ |
| 86 | + { |
| 87 | + Rule: NewTerraformMapDuplicateValuesRule(), |
| 88 | + Message: `Duplicate value: "b", first defined at module.tf:9,11-16`, |
| 89 | + Range: hcl.Range{ |
| 90 | + Filename: "module.tf", |
| 91 | + Start: hcl.Pos{Line: 10, Column: 11}, |
| 92 | + End: hcl.Pos{Line: 10, Column: 14}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + Name: "Using a variable as a value without a default", |
| 99 | + Content: ` |
| 100 | +variable "unknown" { |
| 101 | + type = string |
| 102 | +} |
| 103 | +
|
| 104 | +resource "null_resource" "test" { |
| 105 | + map = { |
| 106 | + key1 = "x" |
| 107 | + key2 = var.unknown |
| 108 | + } |
| 109 | +}`, |
| 110 | + Expected: helper.Issues{}, |
| 111 | + }, |
| 112 | + { |
| 113 | + Name: "Multiple duplicates in same map", |
| 114 | + Content: ` |
| 115 | +resource "null_resource" "test" { |
| 116 | + map = { |
| 117 | + key1 = "a" |
| 118 | + key2 = "a" |
| 119 | + key3 = "a" |
| 120 | + } |
| 121 | +}`, |
| 122 | + Expected: helper.Issues{ |
| 123 | + { |
| 124 | + Rule: NewTerraformMapDuplicateValuesRule(), |
| 125 | + Message: `Duplicate value: "a", first defined at module.tf:4,11-14`, |
| 126 | + Range: hcl.Range{ |
| 127 | + Filename: "module.tf", |
| 128 | + Start: hcl.Pos{Line: 5, Column: 11}, |
| 129 | + End: hcl.Pos{Line: 5, Column: 14}, |
| 130 | + }, |
| 131 | + }, |
| 132 | + { |
| 133 | + Rule: NewTerraformMapDuplicateValuesRule(), |
| 134 | + Message: `Duplicate value: "a", first defined at module.tf:4,11-14`, |
| 135 | + Range: hcl.Range{ |
| 136 | + Filename: "module.tf", |
| 137 | + Start: hcl.Pos{Line: 6, Column: 11}, |
| 138 | + End: hcl.Pos{Line: 6, Column: 14}, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + Name: "Using same value in different maps is okay", |
| 145 | + Content: ` |
| 146 | +resource "null_resource" "test" { |
| 147 | + map1 = { |
| 148 | + key1 = "x" |
| 149 | + } |
| 150 | + map2 = { |
| 151 | + key2 = "x" |
| 152 | + } |
| 153 | +}`, |
| 154 | + Expected: helper.Issues{}, |
| 155 | + }, |
| 156 | + { |
| 157 | + Name: "Using sensitive variable values", |
| 158 | + Content: ` |
| 159 | +variable "sensitive" { |
| 160 | + default = "secret" |
| 161 | + sensitive = true |
| 162 | +} |
| 163 | +
|
| 164 | +resource "null_resource" "test" { |
| 165 | + map = { |
| 166 | + key1 = var.sensitive |
| 167 | + key2 = "secret" |
| 168 | + } |
| 169 | +}`, |
| 170 | + // Do not report sensitive duplicate values to prevent unintentional exposure of sensitive values |
| 171 | + Expected: helper.Issues{}, |
| 172 | + }, |
| 173 | + { |
| 174 | + Name: "Using non-string values", |
| 175 | + Content: ` |
| 176 | +resource "null_resource" "test" { |
| 177 | + map = { |
| 178 | + key1 = 1 |
| 179 | + key2 = 1 |
| 180 | + key3 = {} |
| 181 | + } |
| 182 | +}`, |
| 183 | + Expected: helper.Issues{ |
| 184 | + { |
| 185 | + Rule: NewTerraformMapDuplicateValuesRule(), |
| 186 | + Message: `Duplicate value: "1", first defined at module.tf:4,12-13`, |
| 187 | + Range: hcl.Range{ |
| 188 | + Filename: "module.tf", |
| 189 | + Start: hcl.Pos{Line: 5, Column: 12}, |
| 190 | + End: hcl.Pos{Line: 5, Column: 13}, |
| 191 | + }, |
| 192 | + }, |
| 193 | + }, |
| 194 | + }, |
| 195 | + { |
| 196 | + Name: "values in for expressions", |
| 197 | + Content: ` |
| 198 | +resource "null_resource" "test" { |
| 199 | + list = [for a in ["foo", "bar"] : { |
| 200 | + key1 = "${a}_baz" |
| 201 | + key2 = "foo_baz" |
| 202 | + }] |
| 203 | +}`, |
| 204 | + // The current implementation cannot find duplicate values in for expressions. |
| 205 | + Expected: helper.Issues{}, |
| 206 | + }, |
| 207 | + { |
| 208 | + Name: "ignore boolean string values", |
| 209 | + Content: ` |
| 210 | +resource "null_resource" "test" { |
| 211 | + map = { |
| 212 | + key1 = true |
| 213 | + key2 = true |
| 214 | + } |
| 215 | +}`, |
| 216 | + Expected: helper.Issues{}, |
| 217 | + }, |
| 218 | + } |
| 219 | + |
| 220 | + rule := NewTerraformMapDuplicateValuesRule() |
| 221 | + |
| 222 | + for _, tc := range cases { |
| 223 | + t.Run(tc.Name, func(t *testing.T) { |
| 224 | + runner := testRunner(t, map[string]string{"module.tf": tc.Content}) |
| 225 | + |
| 226 | + if err := rule.Check(runner); err != nil { |
| 227 | + t.Fatalf("Unexpected error occurred: %s", err) |
| 228 | + } |
| 229 | + |
| 230 | + helper.AssertIssues(t, tc.Expected, runner.Runner.(*helper.Runner).Issues) |
| 231 | + }) |
| 232 | + } |
| 233 | +} |
0 commit comments