github.com/pulumi/terraform@v1.4.0/pkg/addrs/provider_config_test.go (about)

     1  package addrs
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/go-test/deep"
     8  
     9  	"github.com/hashicorp/hcl/v2"
    10  	"github.com/hashicorp/hcl/v2/hclsyntax"
    11  )
    12  
    13  func TestParseAbsProviderConfig(t *testing.T) {
    14  	tests := []struct {
    15  		Input    string
    16  		Want     AbsProviderConfig
    17  		WantDiag string
    18  	}{
    19  		{
    20  			`provider["registry.terraform.io/hashicorp/aws"]`,
    21  			AbsProviderConfig{
    22  				Module: RootModule,
    23  				Provider: Provider{
    24  					Type:      "aws",
    25  					Namespace: "hashicorp",
    26  					Hostname:  "registry.terraform.io",
    27  				},
    28  			},
    29  			``,
    30  		},
    31  		{
    32  			`provider["registry.terraform.io/hashicorp/aws"].foo`,
    33  			AbsProviderConfig{
    34  				Module: RootModule,
    35  				Provider: Provider{
    36  					Type:      "aws",
    37  					Namespace: "hashicorp",
    38  					Hostname:  "registry.terraform.io",
    39  				},
    40  				Alias: "foo",
    41  			},
    42  			``,
    43  		},
    44  		{
    45  			`module.baz.provider["registry.terraform.io/hashicorp/aws"]`,
    46  			AbsProviderConfig{
    47  				Module: Module{"baz"},
    48  				Provider: Provider{
    49  					Type:      "aws",
    50  					Namespace: "hashicorp",
    51  					Hostname:  "registry.terraform.io",
    52  				},
    53  			},
    54  			``,
    55  		},
    56  		{
    57  			`module.baz.provider["registry.terraform.io/hashicorp/aws"].foo`,
    58  			AbsProviderConfig{
    59  				Module: Module{"baz"},
    60  				Provider: Provider{
    61  					Type:      "aws",
    62  					Namespace: "hashicorp",
    63  					Hostname:  "registry.terraform.io",
    64  				},
    65  				Alias: "foo",
    66  			},
    67  			``,
    68  		},
    69  		{
    70  			`module.baz["foo"].provider["registry.terraform.io/hashicorp/aws"]`,
    71  			AbsProviderConfig{},
    72  			`Provider address cannot contain module indexes`,
    73  		},
    74  		{
    75  			`module.baz[1].provider["registry.terraform.io/hashicorp/aws"]`,
    76  			AbsProviderConfig{},
    77  			`Provider address cannot contain module indexes`,
    78  		},
    79  		{
    80  			`module.baz[1].module.bar.provider["registry.terraform.io/hashicorp/aws"]`,
    81  			AbsProviderConfig{},
    82  			`Provider address cannot contain module indexes`,
    83  		},
    84  		{
    85  			`aws`,
    86  			AbsProviderConfig{},
    87  			`Provider address must begin with "provider.", followed by a provider type name.`,
    88  		},
    89  		{
    90  			`aws.foo`,
    91  			AbsProviderConfig{},
    92  			`Provider address must begin with "provider.", followed by a provider type name.`,
    93  		},
    94  		{
    95  			`provider`,
    96  			AbsProviderConfig{},
    97  			`Provider address must begin with "provider.", followed by a provider type name.`,
    98  		},
    99  		{
   100  			`provider.aws.foo.bar`,
   101  			AbsProviderConfig{},
   102  			`Extraneous operators after provider configuration alias.`,
   103  		},
   104  		{
   105  			`provider["aws"]["foo"]`,
   106  			AbsProviderConfig{},
   107  			`Provider type name must be followed by a configuration alias name.`,
   108  		},
   109  		{
   110  			`module.foo`,
   111  			AbsProviderConfig{},
   112  			`Provider address must begin with "provider.", followed by a provider type name.`,
   113  		},
   114  		{
   115  			`provider[0]`,
   116  			AbsProviderConfig{},
   117  			`The prefix "provider." must be followed by a provider type name.`,
   118  		},
   119  	}
   120  
   121  	for _, test := range tests {
   122  		t.Run(test.Input, func(t *testing.T) {
   123  			traversal, parseDiags := hclsyntax.ParseTraversalAbs([]byte(test.Input), "", hcl.Pos{})
   124  			if len(parseDiags) != 0 {
   125  				t.Errorf("unexpected diagnostics during parse")
   126  				for _, diag := range parseDiags {
   127  					t.Logf("- %s", diag)
   128  				}
   129  				return
   130  			}
   131  
   132  			got, diags := ParseAbsProviderConfig(traversal)
   133  
   134  			if test.WantDiag != "" {
   135  				if len(diags) != 1 {
   136  					t.Fatalf("got %d diagnostics; want 1", len(diags))
   137  				}
   138  				gotDetail := diags[0].Description().Detail
   139  				if gotDetail != test.WantDiag {
   140  					t.Fatalf("wrong diagnostic detail\ngot:  %s\nwant: %s", gotDetail, test.WantDiag)
   141  				}
   142  				return
   143  			} else {
   144  				if len(diags) != 0 {
   145  					t.Fatalf("got %d diagnostics; want 0", len(diags))
   146  				}
   147  			}
   148  
   149  			for _, problem := range deep.Equal(got, test.Want) {
   150  				t.Error(problem)
   151  			}
   152  		})
   153  	}
   154  }
   155  
   156  func TestAbsProviderConfigString(t *testing.T) {
   157  	tests := []struct {
   158  		Config AbsProviderConfig
   159  		Want   string
   160  	}{
   161  		{
   162  			AbsProviderConfig{
   163  				Module:   RootModule,
   164  				Provider: NewLegacyProvider("foo"),
   165  			},
   166  			`provider["registry.terraform.io/-/foo"]`,
   167  		},
   168  		{
   169  			AbsProviderConfig{
   170  				Module:   RootModule.Child("child_module"),
   171  				Provider: NewDefaultProvider("foo"),
   172  			},
   173  			`module.child_module.provider["registry.terraform.io/hashicorp/foo"]`,
   174  		},
   175  		{
   176  			AbsProviderConfig{
   177  				Module:   RootModule,
   178  				Alias:    "bar",
   179  				Provider: NewDefaultProvider("foo"),
   180  			},
   181  			`provider["registry.terraform.io/hashicorp/foo"].bar`,
   182  		},
   183  		{
   184  			AbsProviderConfig{
   185  				Module:   RootModule.Child("child_module"),
   186  				Alias:    "bar",
   187  				Provider: NewDefaultProvider("foo"),
   188  			},
   189  			`module.child_module.provider["registry.terraform.io/hashicorp/foo"].bar`,
   190  		},
   191  	}
   192  
   193  	for _, test := range tests {
   194  		got := test.Config.String()
   195  		if got != test.Want {
   196  			t.Errorf("wrong result. Got %s, want %s\n", got, test.Want)
   197  		}
   198  	}
   199  }
   200  
   201  func TestAbsProviderConfigLegacyString(t *testing.T) {
   202  	tests := []struct {
   203  		Config AbsProviderConfig
   204  		Want   string
   205  	}{
   206  		{
   207  			AbsProviderConfig{
   208  				Module:   RootModule,
   209  				Provider: NewLegacyProvider("foo"),
   210  			},
   211  			`provider.foo`,
   212  		},
   213  		{
   214  			AbsProviderConfig{
   215  				Module:   RootModule.Child("child_module"),
   216  				Provider: NewLegacyProvider("foo"),
   217  			},
   218  			`module.child_module.provider.foo`,
   219  		},
   220  		{
   221  			AbsProviderConfig{
   222  				Module:   RootModule,
   223  				Alias:    "bar",
   224  				Provider: NewLegacyProvider("foo"),
   225  			},
   226  			`provider.foo.bar`,
   227  		},
   228  		{
   229  			AbsProviderConfig{
   230  				Module:   RootModule.Child("child_module"),
   231  				Alias:    "bar",
   232  				Provider: NewLegacyProvider("foo"),
   233  			},
   234  			`module.child_module.provider.foo.bar`,
   235  		},
   236  	}
   237  
   238  	for _, test := range tests {
   239  		got := test.Config.LegacyString()
   240  		if got != test.Want {
   241  			t.Errorf("wrong result. Got %s, want %s\n", got, test.Want)
   242  		}
   243  	}
   244  }
   245  
   246  func TestParseLegacyAbsProviderConfigStr(t *testing.T) {
   247  	tests := []struct {
   248  		Config string
   249  		Want   AbsProviderConfig
   250  	}{
   251  		{
   252  			`provider.foo`,
   253  			AbsProviderConfig{
   254  				Module:   RootModule,
   255  				Provider: NewLegacyProvider("foo"),
   256  			},
   257  		},
   258  		{
   259  			`module.child_module.provider.foo`,
   260  			AbsProviderConfig{
   261  				Module:   RootModule.Child("child_module"),
   262  				Provider: NewLegacyProvider("foo"),
   263  			},
   264  		},
   265  		{
   266  			`provider.terraform`,
   267  			AbsProviderConfig{
   268  				Module:   RootModule,
   269  				Provider: NewBuiltInProvider("terraform"),
   270  			},
   271  		},
   272  	}
   273  
   274  	for _, test := range tests {
   275  		got, _ := ParseLegacyAbsProviderConfigStr(test.Config)
   276  		if !reflect.DeepEqual(got, test.Want) {
   277  			t.Errorf("wrong result. Got %s, want %s\n", got, test.Want)
   278  		}
   279  	}
   280  }