github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/configs/provider_test.go (about)

     1  package configs
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/go-test/deep"
     8  	"github.com/hashicorp/hcl/v2"
     9  	"github.com/hashicorp/hcl/v2/hclsyntax"
    10  	"github.com/hashicorp/terraform/addrs"
    11  )
    12  
    13  func TestProviderReservedNames(t *testing.T) {
    14  	src, err := ioutil.ReadFile("testdata/invalid-files/provider-reserved.tf")
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	parser := testParser(map[string]string{
    19  		"config.tf": string(src),
    20  	})
    21  	_, diags := parser.LoadConfigFile("config.tf")
    22  
    23  	assertExactDiagnostics(t, diags, []string{
    24  		`config.tf:10,3-8: Reserved argument name in provider block; The provider argument name "count" is reserved for use by Terraform in a future version.`,
    25  		`config.tf:11,3-13: Reserved argument name in provider block; The provider argument name "depends_on" is reserved for use by Terraform in a future version.`,
    26  		`config.tf:12,3-11: Reserved argument name in provider block; The provider argument name "for_each" is reserved for use by Terraform in a future version.`,
    27  		`config.tf:14,3-12: Reserved block type name in provider block; The block type name "lifecycle" is reserved for use by Terraform in a future version.`,
    28  		`config.tf:15,3-9: Reserved block type name in provider block; The block type name "locals" is reserved for use by Terraform in a future version.`,
    29  		`config.tf:13,3-9: Reserved argument name in provider block; The provider argument name "source" is reserved for use by Terraform in a future version.`,
    30  	})
    31  }
    32  
    33  func TestParseProviderConfigCompact(t *testing.T) {
    34  	tests := []struct {
    35  		Input    string
    36  		Want     addrs.ProviderConfig
    37  		WantDiag string
    38  	}{
    39  		{
    40  			`aws`,
    41  			addrs.ProviderConfig{
    42  				Type: addrs.NewLegacyProvider("aws"),
    43  			},
    44  			``,
    45  		},
    46  		{
    47  			`aws.foo`,
    48  			addrs.ProviderConfig{
    49  				Type:  addrs.NewLegacyProvider("aws"),
    50  				Alias: "foo",
    51  			},
    52  			``,
    53  		},
    54  		{
    55  			`aws["foo"]`,
    56  			addrs.ProviderConfig{},
    57  			`The provider type name must either stand alone or be followed by an alias name separated with a dot.`,
    58  		},
    59  	}
    60  
    61  	for _, test := range tests {
    62  		t.Run(test.Input, func(t *testing.T) {
    63  			traversal, parseDiags := hclsyntax.ParseTraversalAbs([]byte(test.Input), "", hcl.Pos{})
    64  			if len(parseDiags) != 0 {
    65  				t.Errorf("unexpected diagnostics during parse")
    66  				for _, diag := range parseDiags {
    67  					t.Logf("- %s", diag)
    68  				}
    69  				return
    70  			}
    71  
    72  			got, diags := ParseProviderConfigCompact(traversal)
    73  
    74  			if test.WantDiag != "" {
    75  				if len(diags) != 1 {
    76  					t.Fatalf("got %d diagnostics; want 1", len(diags))
    77  				}
    78  				gotDetail := diags[0].Description().Detail
    79  				if gotDetail != test.WantDiag {
    80  					t.Fatalf("wrong diagnostic detail\ngot:  %s\nwant: %s", gotDetail, test.WantDiag)
    81  				}
    82  				return
    83  			} else {
    84  				if len(diags) != 0 {
    85  					t.Fatalf("got %d diagnostics; want 0", len(diags))
    86  				}
    87  			}
    88  
    89  			for _, problem := range deep.Equal(got, test.Want) {
    90  				t.Error(problem)
    91  			}
    92  		})
    93  	}
    94  }