github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/configs/configschema/validate_traversal_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package configschema 5 6 import ( 7 "testing" 8 9 "github.com/hashicorp/hcl/v2" 10 "github.com/hashicorp/hcl/v2/hclsyntax" 11 "github.com/zclconf/go-cty/cty" 12 ) 13 14 func TestStaticValidateTraversal(t *testing.T) { 15 attrs := map[string]*Attribute{ 16 "str": {Type: cty.String, Optional: true}, 17 "list": {Type: cty.List(cty.String), Optional: true}, 18 "dyn": {Type: cty.DynamicPseudoType, Optional: true}, 19 "deprecated": {Type: cty.String, Computed: true, Deprecated: true}, 20 "nested_single": { 21 Optional: true, 22 NestedType: &Object{ 23 Nesting: NestingSingle, 24 Attributes: map[string]*Attribute{ 25 "optional": {Type: cty.String, Optional: true}, 26 }, 27 }, 28 }, 29 "nested_list": { 30 Optional: true, 31 NestedType: &Object{ 32 Nesting: NestingList, 33 Attributes: map[string]*Attribute{ 34 "optional": {Type: cty.String, Optional: true}, 35 }, 36 }, 37 }, 38 "nested_set": { 39 Optional: true, 40 NestedType: &Object{ 41 Nesting: NestingSet, 42 Attributes: map[string]*Attribute{ 43 "optional": {Type: cty.String, Optional: true}, 44 }, 45 }, 46 }, 47 "nested_map": { 48 Optional: true, 49 NestedType: &Object{ 50 Nesting: NestingMap, 51 Attributes: map[string]*Attribute{ 52 "optional": {Type: cty.String, Optional: true}, 53 }, 54 }, 55 }, 56 } 57 schema := &Block{ 58 Attributes: attrs, 59 BlockTypes: map[string]*NestedBlock{ 60 "single_block": { 61 Nesting: NestingSingle, 62 Block: Block{ 63 Attributes: attrs, 64 }, 65 }, 66 "list_block": { 67 Nesting: NestingList, 68 Block: Block{ 69 Attributes: attrs, 70 }, 71 }, 72 "set_block": { 73 Nesting: NestingSet, 74 Block: Block{ 75 Attributes: attrs, 76 }, 77 }, 78 "map_block": { 79 Nesting: NestingMap, 80 Block: Block{ 81 Attributes: attrs, 82 }, 83 }, 84 }, 85 } 86 87 tests := []struct { 88 Traversal string 89 WantError string 90 }{ 91 { 92 `obj`, 93 ``, 94 }, 95 { 96 `obj.str`, 97 ``, 98 }, 99 { 100 `obj.str.nonexist`, 101 `Unsupported attribute: Can't access attributes on a primitive-typed value (string).`, 102 }, 103 { 104 `obj.list`, 105 ``, 106 }, 107 { 108 `obj.list[0]`, 109 ``, 110 }, 111 { 112 `obj.list.nonexist`, 113 `Unsupported attribute: This value does not have any attributes.`, 114 }, 115 { 116 `obj.dyn`, 117 ``, 118 }, 119 { 120 `obj.dyn.anything_goes`, 121 ``, 122 }, 123 { 124 `obj.dyn[0]`, 125 ``, 126 }, 127 { 128 `obj.nonexist`, 129 `Unsupported attribute: This object has no argument, nested block, or exported attribute named "nonexist".`, 130 }, 131 { 132 `obj[1]`, 133 `Invalid index operation: Only attribute access is allowed here, using the dot operator.`, 134 }, 135 { 136 `obj["str"]`, // we require attribute access for the first step to avoid ambiguity with resource instance indices 137 `Invalid index operation: Only attribute access is allowed here. Did you mean to access attribute "str" using the dot operator?`, 138 }, 139 { 140 `obj.atr`, 141 `Unsupported attribute: This object has no argument, nested block, or exported attribute named "atr". Did you mean "str"?`, 142 }, 143 { 144 `obj.single_block`, 145 ``, 146 }, 147 { 148 `obj.single_block.str`, 149 ``, 150 }, 151 { 152 `obj.single_block.nonexist`, 153 `Unsupported attribute: This object has no argument, nested block, or exported attribute named "nonexist".`, 154 }, 155 { 156 `obj.list_block`, 157 ``, 158 }, 159 { 160 `obj.list_block[0]`, 161 ``, 162 }, 163 { 164 `obj.list_block[0].str`, 165 ``, 166 }, 167 { 168 `obj.list_block[0].nonexist`, 169 `Unsupported attribute: This object has no argument, nested block, or exported attribute named "nonexist".`, 170 }, 171 { 172 `obj.list_block.str`, 173 `Invalid operation: Block type "list_block" is represented by a list of objects, so it must be indexed using a numeric key, like .list_block[0].`, 174 }, 175 { 176 `obj.set_block`, 177 ``, 178 }, 179 { 180 `obj.set_block[0]`, 181 `Cannot index a set value: Block type "set_block" is represented by a set of objects, and set elements do not have addressable keys. To find elements matching specific criteria, use a "for" expression with an "if" clause.`, 182 }, 183 { 184 `obj.set_block.str`, 185 `Cannot index a set value: Block type "set_block" is represented by a set of objects, and set elements do not have addressable keys. To find elements matching specific criteria, use a "for" expression with an "if" clause.`, 186 }, 187 { 188 `obj.map_block`, 189 ``, 190 }, 191 { 192 `obj.map_block.anything`, 193 ``, 194 }, 195 { 196 `obj.map_block["anything"]`, 197 ``, 198 }, 199 { 200 `obj.map_block.anything.str`, 201 ``, 202 }, 203 { 204 `obj.map_block["anything"].str`, 205 ``, 206 }, 207 { 208 `obj.map_block.anything.nonexist`, 209 `Unsupported attribute: This object has no argument, nested block, or exported attribute named "nonexist".`, 210 }, 211 { 212 `obj.nested_single.optional`, 213 ``, 214 }, 215 { 216 `obj.nested_list[0].optional`, 217 ``, 218 }, 219 { 220 `obj.nested_set[0].optional`, 221 `Invalid index: Elements of a set are identified only by their value and don't have any separate index or key to select with, so it's only possible to perform operations across all elements of the set.`, 222 }, 223 { 224 `obj.nested_map["key"].optional`, 225 ``, 226 }, 227 { 228 `obj.deprecated`, 229 `Deprecated attribute: The attribute "deprecated" is deprecated. Refer to the provider documentation for details.`, 230 }, 231 } 232 233 for _, test := range tests { 234 t.Run(test.Traversal, func(t *testing.T) { 235 traversal, parseDiags := hclsyntax.ParseTraversalAbs([]byte(test.Traversal), "", hcl.Pos{Line: 1, Column: 1}) 236 for _, diag := range parseDiags { 237 t.Error(diag.Error()) 238 } 239 240 // We trim the "obj." portion from the front since StaticValidateTraversal 241 // only works with relative traversals. 242 traversal = traversal[1:] 243 244 diags := schema.StaticValidateTraversal(traversal) 245 if test.WantError == "" { 246 if diags.HasErrors() { 247 t.Errorf("unexpected error: %s", diags.Err().Error()) 248 } 249 } else { 250 err := diags.ErrWithWarnings() 251 if err != nil { 252 if got := err.Error(); got != test.WantError { 253 t.Errorf("wrong error\ngot: %s\nwant: %s", got, test.WantError) 254 } 255 } else { 256 t.Errorf("wrong error\ngot: <no error>\nwant: %s", test.WantError) 257 } 258 } 259 }) 260 } 261 }