github.com/kevinklinger/open_terraform@v1.3.6/noninternal/configs/configschema/validate_traversal_test.go (about)

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