addrs: Provider.IsLegacy and IsDefault functions
* add IsLegacy and IsDefault funcs to addrs.Provider * add some test coverage
This commit is contained in:
parent
4f85a1a6ba
commit
5b427ec648
|
@ -126,6 +126,25 @@ func (pt Provider) LessThan(other Provider) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsLegacy returns true if the provider is a legacy-style provider
|
||||||
|
func (pt Provider) IsLegacy() bool {
|
||||||
|
if pt.IsZero() {
|
||||||
|
panic("called IsLegacy() on zero-value addrs.Provider")
|
||||||
|
}
|
||||||
|
|
||||||
|
return pt.Hostname == DefaultRegistryHost && pt.Namespace == LegacyProviderNamespace
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsDefault returns true if the provider is a default hashicorp provider
|
||||||
|
func (pt Provider) IsDefault() bool {
|
||||||
|
if pt.IsZero() {
|
||||||
|
panic("called IsDefault() on zero-value addrs.Provider")
|
||||||
|
}
|
||||||
|
|
||||||
|
return pt.Hostname == DefaultRegistryHost && pt.Namespace == "hashicorp"
|
||||||
|
}
|
||||||
|
|
||||||
// Equals returns true if the receiver and other provider have the same attributes.
|
// Equals returns true if the receiver and other provider have the same attributes.
|
||||||
func (pt Provider) Equals(other Provider) bool {
|
func (pt Provider) Equals(other Provider) bool {
|
||||||
return pt == other
|
return pt == other
|
||||||
|
|
|
@ -7,6 +7,84 @@ import (
|
||||||
svchost "github.com/hashicorp/terraform-svchost"
|
svchost "github.com/hashicorp/terraform-svchost"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestProviderIsDefault(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
Input Provider
|
||||||
|
Want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Provider{
|
||||||
|
Type: "test",
|
||||||
|
Hostname: DefaultRegistryHost,
|
||||||
|
Namespace: "hashicorp",
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Provider{
|
||||||
|
Type: "test",
|
||||||
|
Hostname: "registry.terraform.com",
|
||||||
|
Namespace: "hashicorp",
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Provider{
|
||||||
|
Type: "test",
|
||||||
|
Hostname: DefaultRegistryHost,
|
||||||
|
Namespace: "othercorp",
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
got := test.Input.IsDefault()
|
||||||
|
if got != test.Want {
|
||||||
|
t.Errorf("wrong result for %s\n", test.Input.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProviderIsLegacy(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
Input Provider
|
||||||
|
Want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Provider{
|
||||||
|
Type: "test",
|
||||||
|
Hostname: DefaultRegistryHost,
|
||||||
|
Namespace: LegacyProviderNamespace,
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Provider{
|
||||||
|
Type: "test",
|
||||||
|
Hostname: "registry.terraform.com",
|
||||||
|
Namespace: LegacyProviderNamespace,
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Provider{
|
||||||
|
Type: "test",
|
||||||
|
Hostname: DefaultRegistryHost,
|
||||||
|
Namespace: "hashicorp",
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
got := test.Input.IsLegacy()
|
||||||
|
if got != test.Want {
|
||||||
|
t.Errorf("wrong result for %s\n", test.Input.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseProviderSourceStr(t *testing.T) {
|
func TestParseProviderSourceStr(t *testing.T) {
|
||||||
tests := map[string]struct {
|
tests := map[string]struct {
|
||||||
Want Provider
|
Want Provider
|
||||||
|
|
|
@ -9,12 +9,18 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestConfigProviderTypes(t *testing.T) {
|
func TestConfigProviderTypes(t *testing.T) {
|
||||||
|
// nil cfg should return an empty map
|
||||||
|
got := NewEmptyConfig().ProviderTypes()
|
||||||
|
if len(got) != 0 {
|
||||||
|
t.Fatal("expected empty result from empty config")
|
||||||
|
}
|
||||||
|
|
||||||
cfg, diags := testModuleConfigFromFile("testdata/valid-files/providers-explicit-implied.tf")
|
cfg, diags := testModuleConfigFromFile("testdata/valid-files/providers-explicit-implied.tf")
|
||||||
if diags.HasErrors() {
|
if diags.HasErrors() {
|
||||||
t.Fatal(diags.Error())
|
t.Fatal(diags.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
got := cfg.ProviderTypes()
|
got = cfg.ProviderTypes()
|
||||||
want := []addrs.Provider{
|
want := []addrs.Provider{
|
||||||
addrs.NewLegacyProvider("aws"),
|
addrs.NewLegacyProvider("aws"),
|
||||||
addrs.NewLegacyProvider("null"),
|
addrs.NewLegacyProvider("null"),
|
||||||
|
|
|
@ -297,6 +297,27 @@ func TestMergeProviderVersionConstraints(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"merge with source constraint": {
|
||||||
|
map[string]ProviderRequirements{
|
||||||
|
"random": ProviderRequirements{
|
||||||
|
Type: addrs.Provider{Type: "random"},
|
||||||
|
VersionConstraints: []VersionConstraint{vc1},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
[]*RequiredProvider{
|
||||||
|
&RequiredProvider{
|
||||||
|
Name: "random",
|
||||||
|
Source: Source{SourceStr: "hashicorp/random"},
|
||||||
|
Requirement: vc2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
map[string]ProviderRequirements{
|
||||||
|
"random": ProviderRequirements{
|
||||||
|
Type: addrs.NewDefaultProvider("random"),
|
||||||
|
VersionConstraints: []VersionConstraint{vc2},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, test := range tests {
|
for name, test := range tests {
|
||||||
|
|
|
@ -27,6 +27,12 @@ func TestNewModule_provider_local_name(t *testing.T) {
|
||||||
if localName != "foo-test" {
|
if localName != "foo-test" {
|
||||||
t.Fatal("provider local name not found")
|
t.Fatal("provider local name not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if there is not a local name for a provider, it should return the type name
|
||||||
|
localName = mod.LocalNameForProvider(addrs.NewLegacyProvider("nonexist"))
|
||||||
|
if localName != "nonexist" {
|
||||||
|
t.Error("wrong local name returned for a non-local provider")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This test validates the provider FQNs set in each Resource
|
// This test validates the provider FQNs set in each Resource
|
||||||
|
|
Loading…
Reference in New Issue