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

     1  package addrs
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/go-test/deep"
     7  	svchost "github.com/hashicorp/terraform-svchost"
     8  )
     9  
    10  func TestProviderString(t *testing.T) {
    11  	tests := []struct {
    12  		Input Provider
    13  		Want  string
    14  	}{
    15  		{
    16  			Provider{
    17  				Type:      "test",
    18  				Hostname:  DefaultProviderRegistryHost,
    19  				Namespace: "hashicorp",
    20  			},
    21  			NewDefaultProvider("test").String(),
    22  		},
    23  		{
    24  			Provider{
    25  				Type:      "test-beta",
    26  				Hostname:  DefaultProviderRegistryHost,
    27  				Namespace: "hashicorp",
    28  			},
    29  			NewDefaultProvider("test-beta").String(),
    30  		},
    31  		{
    32  			Provider{
    33  				Type:      "test",
    34  				Hostname:  "registry.terraform.com",
    35  				Namespace: "hashicorp",
    36  			},
    37  			"registry.terraform.com/hashicorp/test",
    38  		},
    39  		{
    40  			Provider{
    41  				Type:      "test",
    42  				Hostname:  DefaultProviderRegistryHost,
    43  				Namespace: "othercorp",
    44  			},
    45  			DefaultProviderRegistryHost.ForDisplay() + "/othercorp/test",
    46  		},
    47  	}
    48  
    49  	for _, test := range tests {
    50  		got := test.Input.String()
    51  		if got != test.Want {
    52  			t.Errorf("wrong result for %s\n", test.Input.String())
    53  		}
    54  	}
    55  }
    56  
    57  func TestProviderLegacyString(t *testing.T) {
    58  	tests := []struct {
    59  		Input Provider
    60  		Want  string
    61  	}{
    62  		{
    63  			Provider{
    64  				Type:      "test",
    65  				Hostname:  DefaultProviderRegistryHost,
    66  				Namespace: LegacyProviderNamespace,
    67  			},
    68  			"test",
    69  		},
    70  		{
    71  			Provider{
    72  				Type:      "terraform",
    73  				Hostname:  BuiltInProviderHost,
    74  				Namespace: BuiltInProviderNamespace,
    75  			},
    76  			"terraform",
    77  		},
    78  	}
    79  
    80  	for _, test := range tests {
    81  		got := test.Input.LegacyString()
    82  		if got != test.Want {
    83  			t.Errorf("wrong result for %s\ngot:  %s\nwant: %s", test.Input.String(), got, test.Want)
    84  		}
    85  	}
    86  }
    87  
    88  func TestProviderDisplay(t *testing.T) {
    89  	tests := []struct {
    90  		Input Provider
    91  		Want  string
    92  	}{
    93  		{
    94  			Provider{
    95  				Type:      "test",
    96  				Hostname:  DefaultProviderRegistryHost,
    97  				Namespace: "hashicorp",
    98  			},
    99  			"hashicorp/test",
   100  		},
   101  		{
   102  			Provider{
   103  				Type:      "test",
   104  				Hostname:  "registry.terraform.com",
   105  				Namespace: "hashicorp",
   106  			},
   107  			"registry.terraform.com/hashicorp/test",
   108  		},
   109  		{
   110  			Provider{
   111  				Type:      "test",
   112  				Hostname:  DefaultProviderRegistryHost,
   113  				Namespace: "othercorp",
   114  			},
   115  			"othercorp/test",
   116  		},
   117  	}
   118  
   119  	for _, test := range tests {
   120  		got := test.Input.ForDisplay()
   121  		if got != test.Want {
   122  			t.Errorf("wrong result for %s\n", test.Input.String())
   123  		}
   124  	}
   125  }
   126  
   127  func TestProviderIsDefaultProvider(t *testing.T) {
   128  	tests := []struct {
   129  		Input Provider
   130  		Want  bool
   131  	}{
   132  		{
   133  			Provider{
   134  				Type:      "test",
   135  				Hostname:  DefaultProviderRegistryHost,
   136  				Namespace: "hashicorp",
   137  			},
   138  			true,
   139  		},
   140  		{
   141  			Provider{
   142  				Type:      "test",
   143  				Hostname:  "registry.terraform.com",
   144  				Namespace: "hashicorp",
   145  			},
   146  			false,
   147  		},
   148  		{
   149  			Provider{
   150  				Type:      "test",
   151  				Hostname:  DefaultProviderRegistryHost,
   152  				Namespace: "othercorp",
   153  			},
   154  			false,
   155  		},
   156  	}
   157  
   158  	for _, test := range tests {
   159  		got := IsDefaultProvider(test.Input)
   160  		if got != test.Want {
   161  			t.Errorf("wrong result for %s\n", test.Input.String())
   162  		}
   163  	}
   164  }
   165  
   166  func TestProviderIsBuiltIn(t *testing.T) {
   167  	tests := []struct {
   168  		Input Provider
   169  		Want  bool
   170  	}{
   171  		{
   172  			Provider{
   173  				Type:      "test",
   174  				Hostname:  BuiltInProviderHost,
   175  				Namespace: BuiltInProviderNamespace,
   176  			},
   177  			true,
   178  		},
   179  		{
   180  			Provider{
   181  				Type:      "terraform",
   182  				Hostname:  BuiltInProviderHost,
   183  				Namespace: BuiltInProviderNamespace,
   184  			},
   185  			true,
   186  		},
   187  		{
   188  			Provider{
   189  				Type:      "test",
   190  				Hostname:  BuiltInProviderHost,
   191  				Namespace: "boop",
   192  			},
   193  			false,
   194  		},
   195  		{
   196  			Provider{
   197  				Type:      "test",
   198  				Hostname:  DefaultProviderRegistryHost,
   199  				Namespace: BuiltInProviderNamespace,
   200  			},
   201  			false,
   202  		},
   203  		{
   204  			Provider{
   205  				Type:      "test",
   206  				Hostname:  DefaultProviderRegistryHost,
   207  				Namespace: "hashicorp",
   208  			},
   209  			false,
   210  		},
   211  		{
   212  			Provider{
   213  				Type:      "test",
   214  				Hostname:  "registry.terraform.com",
   215  				Namespace: "hashicorp",
   216  			},
   217  			false,
   218  		},
   219  		{
   220  			Provider{
   221  				Type:      "test",
   222  				Hostname:  DefaultProviderRegistryHost,
   223  				Namespace: "othercorp",
   224  			},
   225  			false,
   226  		},
   227  	}
   228  
   229  	for _, test := range tests {
   230  		got := test.Input.IsBuiltIn()
   231  		if got != test.Want {
   232  			t.Errorf("wrong result for %s\ngot:  %#v\nwant: %#v", test.Input.String(), got, test.Want)
   233  		}
   234  	}
   235  }
   236  
   237  func TestProviderIsLegacy(t *testing.T) {
   238  	tests := []struct {
   239  		Input Provider
   240  		Want  bool
   241  	}{
   242  		{
   243  			Provider{
   244  				Type:      "test",
   245  				Hostname:  DefaultProviderRegistryHost,
   246  				Namespace: LegacyProviderNamespace,
   247  			},
   248  			true,
   249  		},
   250  		{
   251  			Provider{
   252  				Type:      "test",
   253  				Hostname:  "registry.terraform.com",
   254  				Namespace: LegacyProviderNamespace,
   255  			},
   256  			false,
   257  		},
   258  		{
   259  			Provider{
   260  				Type:      "test",
   261  				Hostname:  DefaultProviderRegistryHost,
   262  				Namespace: "hashicorp",
   263  			},
   264  			false,
   265  		},
   266  	}
   267  
   268  	for _, test := range tests {
   269  		got := test.Input.IsLegacy()
   270  		if got != test.Want {
   271  			t.Errorf("wrong result for %s\n", test.Input.String())
   272  		}
   273  	}
   274  }
   275  
   276  func TestParseProviderSourceStr(t *testing.T) {
   277  	tests := map[string]struct {
   278  		Want Provider
   279  		Err  bool
   280  	}{
   281  		"registry.terraform.io/hashicorp/aws": {
   282  			Provider{
   283  				Type:      "aws",
   284  				Namespace: "hashicorp",
   285  				Hostname:  DefaultProviderRegistryHost,
   286  			},
   287  			false,
   288  		},
   289  		"registry.Terraform.io/HashiCorp/AWS": {
   290  			Provider{
   291  				Type:      "aws",
   292  				Namespace: "hashicorp",
   293  				Hostname:  DefaultProviderRegistryHost,
   294  			},
   295  			false,
   296  		},
   297  		"hashicorp/aws": {
   298  			Provider{
   299  				Type:      "aws",
   300  				Namespace: "hashicorp",
   301  				Hostname:  DefaultProviderRegistryHost,
   302  			},
   303  			false,
   304  		},
   305  		"HashiCorp/AWS": {
   306  			Provider{
   307  				Type:      "aws",
   308  				Namespace: "hashicorp",
   309  				Hostname:  DefaultProviderRegistryHost,
   310  			},
   311  			false,
   312  		},
   313  		"aws": {
   314  			Provider{
   315  				Type:      "aws",
   316  				Namespace: "hashicorp",
   317  				Hostname:  DefaultProviderRegistryHost,
   318  			},
   319  			false,
   320  		},
   321  		"AWS": {
   322  			Provider{
   323  				Type:      "aws",
   324  				Namespace: "hashicorp",
   325  				Hostname:  DefaultProviderRegistryHost,
   326  			},
   327  			false,
   328  		},
   329  		"example.com/foo-bar/baz-boop": {
   330  			Provider{
   331  				Type:      "baz-boop",
   332  				Namespace: "foo-bar",
   333  				Hostname:  svchost.Hostname("example.com"),
   334  			},
   335  			false,
   336  		},
   337  		"foo-bar/baz-boop": {
   338  			Provider{
   339  				Type:      "baz-boop",
   340  				Namespace: "foo-bar",
   341  				Hostname:  DefaultProviderRegistryHost,
   342  			},
   343  			false,
   344  		},
   345  		"localhost:8080/foo/bar": {
   346  			Provider{
   347  				Type:      "bar",
   348  				Namespace: "foo",
   349  				Hostname:  svchost.Hostname("localhost:8080"),
   350  			},
   351  			false,
   352  		},
   353  		"example.com/too/many/parts/here": {
   354  			Provider{},
   355  			true,
   356  		},
   357  		"/too///many//slashes": {
   358  			Provider{},
   359  			true,
   360  		},
   361  		"///": {
   362  			Provider{},
   363  			true,
   364  		},
   365  		"/ / /": { // empty strings
   366  			Provider{},
   367  			true,
   368  		},
   369  		"badhost!/hashicorp/aws": {
   370  			Provider{},
   371  			true,
   372  		},
   373  		"example.com/badnamespace!/aws": {
   374  			Provider{},
   375  			true,
   376  		},
   377  		"example.com/bad--namespace/aws": {
   378  			Provider{},
   379  			true,
   380  		},
   381  		"example.com/-badnamespace/aws": {
   382  			Provider{},
   383  			true,
   384  		},
   385  		"example.com/badnamespace-/aws": {
   386  			Provider{},
   387  			true,
   388  		},
   389  		"example.com/bad.namespace/aws": {
   390  			Provider{},
   391  			true,
   392  		},
   393  		"example.com/hashicorp/badtype!": {
   394  			Provider{},
   395  			true,
   396  		},
   397  		"example.com/hashicorp/bad--type": {
   398  			Provider{},
   399  			true,
   400  		},
   401  		"example.com/hashicorp/-badtype": {
   402  			Provider{},
   403  			true,
   404  		},
   405  		"example.com/hashicorp/badtype-": {
   406  			Provider{},
   407  			true,
   408  		},
   409  		"example.com/hashicorp/bad.type": {
   410  			Provider{},
   411  			true,
   412  		},
   413  
   414  		// We forbid the terraform- prefix both because it's redundant to
   415  		// include "terraform" in a Terraform provider name and because we use
   416  		// the longer prefix terraform-provider- to hint for users who might be
   417  		// accidentally using the git repository name or executable file name
   418  		// instead of the provider type.
   419  		"example.com/hashicorp/terraform-provider-bad": {
   420  			Provider{},
   421  			true,
   422  		},
   423  		"example.com/hashicorp/terraform-bad": {
   424  			Provider{},
   425  			true,
   426  		},
   427  	}
   428  
   429  	for name, test := range tests {
   430  		got, diags := ParseProviderSourceString(name)
   431  		for _, problem := range deep.Equal(got, test.Want) {
   432  			t.Errorf(problem)
   433  		}
   434  		if len(diags) > 0 {
   435  			if test.Err == false {
   436  				t.Errorf("got error, expected success")
   437  			}
   438  		} else {
   439  			if test.Err {
   440  				t.Errorf("got success, expected error")
   441  			}
   442  		}
   443  	}
   444  }
   445  
   446  func TestParseProviderPart(t *testing.T) {
   447  	tests := map[string]struct {
   448  		Want  string
   449  		Error string
   450  	}{
   451  		`foo`: {
   452  			`foo`,
   453  			``,
   454  		},
   455  		`FOO`: {
   456  			`foo`,
   457  			``,
   458  		},
   459  		`Foo`: {
   460  			`foo`,
   461  			``,
   462  		},
   463  		`abc-123`: {
   464  			`abc-123`,
   465  			``,
   466  		},
   467  		`Испытание`: {
   468  			`испытание`,
   469  			``,
   470  		},
   471  		`münchen`: { // this is a precomposed u with diaeresis
   472  			`münchen`, // this is a precomposed u with diaeresis
   473  			``,
   474  		},
   475  		`münchen`: { // this is a separate u and combining diaeresis
   476  			`münchen`, // this is a precomposed u with diaeresis
   477  			``,
   478  		},
   479  		`abc--123`: {
   480  			``,
   481  			`cannot use multiple consecutive dashes`,
   482  		},
   483  		`xn--80akhbyknj4f`: { // this is the punycode form of "испытание", but we don't accept punycode here
   484  			``,
   485  			`cannot use multiple consecutive dashes`,
   486  		},
   487  		`abc.123`: {
   488  			``,
   489  			`dots are not allowed`,
   490  		},
   491  		`-abc123`: {
   492  			``,
   493  			`must contain only letters, digits, and dashes, and may not use leading or trailing dashes`,
   494  		},
   495  		`abc123-`: {
   496  			``,
   497  			`must contain only letters, digits, and dashes, and may not use leading or trailing dashes`,
   498  		},
   499  		``: {
   500  			``,
   501  			`must have at least one character`,
   502  		},
   503  	}
   504  
   505  	for given, test := range tests {
   506  		t.Run(given, func(t *testing.T) {
   507  			got, err := ParseProviderPart(given)
   508  			if test.Error != "" {
   509  				if err == nil {
   510  					t.Errorf("unexpected success\ngot:  %s\nwant: %s", err, test.Error)
   511  				} else if got := err.Error(); got != test.Error {
   512  					t.Errorf("wrong error\ngot:  %s\nwant: %s", got, test.Error)
   513  				}
   514  			} else {
   515  				if err != nil {
   516  					t.Errorf("unexpected error\ngot:  %s\nwant: <nil>", err)
   517  				} else if got != test.Want {
   518  					t.Errorf("wrong result\ngot:  %s\nwant: %s", got, test.Want)
   519  				}
   520  			}
   521  		})
   522  	}
   523  }
   524  
   525  func TestProviderEquals(t *testing.T) {
   526  	tests := []struct {
   527  		InputP Provider
   528  		OtherP Provider
   529  		Want   bool
   530  	}{
   531  		{
   532  			NewProvider(DefaultProviderRegistryHost, "foo", "test"),
   533  			NewProvider(DefaultProviderRegistryHost, "foo", "test"),
   534  			true,
   535  		},
   536  		{
   537  			NewProvider(DefaultProviderRegistryHost, "foo", "test"),
   538  			NewProvider(DefaultProviderRegistryHost, "bar", "test"),
   539  			false,
   540  		},
   541  		{
   542  			NewProvider(DefaultProviderRegistryHost, "foo", "test"),
   543  			NewProvider(DefaultProviderRegistryHost, "foo", "my-test"),
   544  			false,
   545  		},
   546  		{
   547  			NewProvider(DefaultProviderRegistryHost, "foo", "test"),
   548  			NewProvider("example.com", "foo", "test"),
   549  			false,
   550  		},
   551  	}
   552  	for _, test := range tests {
   553  		t.Run(test.InputP.String(), func(t *testing.T) {
   554  			got := test.InputP.Equals(test.OtherP)
   555  			if got != test.Want {
   556  				t.Errorf("wrong result\ngot:  %v\nwant: %v", got, test.Want)
   557  			}
   558  		})
   559  	}
   560  }