github.com/opentofu/opentofu@v1.7.1/internal/configs/configschema/validate_traversal_test.go (about)

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