github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/evaluate_valid_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/hcl/v2"
     7  	"github.com/hashicorp/hcl/v2/hclsyntax"
     8  
     9  	"github.com/hashicorp/terraform-plugin-sdk/internal/configs/configschema"
    10  	"github.com/hashicorp/terraform-plugin-sdk/internal/lang"
    11  )
    12  
    13  func TestStaticValidateReferences(t *testing.T) {
    14  	tests := []struct {
    15  		Ref     string
    16  		WantErr string
    17  	}{
    18  		{
    19  			"aws_instance.no_count",
    20  			``,
    21  		},
    22  		{
    23  			"aws_instance.count",
    24  			``,
    25  		},
    26  		{
    27  			"aws_instance.count[0]",
    28  			``,
    29  		},
    30  		{
    31  			"aws_instance.nonexist",
    32  			`Reference to undeclared resource: A managed resource "aws_instance" "nonexist" has not been declared in the root module.`,
    33  		},
    34  		{
    35  			"aws_instance.no_count[0]",
    36  			`Unexpected resource instance key: Because aws_instance.no_count does not have "count" or "for_each" set, references to it must not include an index key. Remove the bracketed index to refer to the single instance of this resource.`,
    37  		},
    38  		{
    39  			"aws_instance.count.foo",
    40  			// In this case we return two errors that are somewhat redundant with
    41  			// one another, but we'll accept that because they both report the
    42  			// problem from different perspectives and so give the user more
    43  			// opportunity to understand what's going on here.
    44  			`2 problems:
    45  
    46  - Missing resource instance key: Because aws_instance.count has "count" set, its attributes must be accessed on specific instances.
    47  
    48  For example, to correlate with indices of a referring resource, use:
    49      aws_instance.count[count.index]
    50  - Unsupported attribute: This object has no argument, nested block, or exported attribute named "foo".`,
    51  		},
    52  	}
    53  
    54  	cfg := testModule(t, "static-validate-refs")
    55  	evaluator := &Evaluator{
    56  		Config: cfg,
    57  		Schemas: &Schemas{
    58  			Providers: map[string]*ProviderSchema{
    59  				"aws": {
    60  					ResourceTypes: map[string]*configschema.Block{
    61  						"aws_instance": {},
    62  					},
    63  				},
    64  			},
    65  		},
    66  	}
    67  
    68  	for _, test := range tests {
    69  		t.Run(test.Ref, func(t *testing.T) {
    70  			traversal, hclDiags := hclsyntax.ParseTraversalAbs([]byte(test.Ref), "", hcl.Pos{Line: 1, Column: 1})
    71  			if hclDiags.HasErrors() {
    72  				t.Fatal(hclDiags.Error())
    73  			}
    74  
    75  			refs, diags := lang.References([]hcl.Traversal{traversal})
    76  			if diags.HasErrors() {
    77  				t.Fatal(diags.Err())
    78  			}
    79  
    80  			data := &evaluationStateData{
    81  				Evaluator: evaluator,
    82  			}
    83  
    84  			diags = data.StaticValidateReferences(refs, nil)
    85  			if diags.HasErrors() {
    86  				if test.WantErr == "" {
    87  					t.Fatalf("Unexpected diagnostics: %s", diags.Err())
    88  				}
    89  
    90  				gotErr := diags.Err().Error()
    91  				if gotErr != test.WantErr {
    92  					t.Fatalf("Wrong diagnostics\ngot:  %s\nwant: %s", gotErr, test.WantErr)
    93  				}
    94  				return
    95  			}
    96  
    97  			if test.WantErr != "" {
    98  				t.Fatalf("Expected diagnostics, but got none\nwant: %s", test.WantErr)
    99  			}
   100  		})
   101  	}
   102  }