2020-02-11 19:17:37 +01:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestNewModule_provider_fqns exercises module.gatherProviderLocalNames()
|
|
|
|
func TestNewModule_provider_local_name(t *testing.T) {
|
|
|
|
mod, diags := testModuleFromDir("testdata/providers-explicit-fqn")
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatal(diags.Error())
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:43:57 +01:00
|
|
|
p := addrs.NewProvider(addrs.DefaultRegistryHost, "foo", "test")
|
2020-02-11 19:17:37 +01:00
|
|
|
if name, exists := mod.ProviderLocalNames[p]; !exists {
|
|
|
|
t.Fatal("provider FQN foo/test not found")
|
|
|
|
} else {
|
2020-02-15 03:10:03 +01:00
|
|
|
if name != "foo-test" {
|
|
|
|
t.Fatalf("provider localname mismatch: got %s, want foo-test", name)
|
2020-02-11 19:17:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure the reverse lookup (fqn to local name) works as well
|
|
|
|
localName := mod.LocalNameForProvider(p)
|
2020-02-15 03:10:03 +01:00
|
|
|
if localName != "foo-test" {
|
2020-02-11 19:17:37 +01:00
|
|
|
t.Fatal("provider local name not found")
|
|
|
|
}
|
2020-03-23 14:50:35 +01:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
2020-02-11 19:17:37 +01:00
|
|
|
}
|
2020-03-12 17:00:00 +01:00
|
|
|
|
2020-03-16 19:36:16 +01:00
|
|
|
// This test validates the provider FQNs set in each Resource
|
|
|
|
func TestNewModule_resource_providers(t *testing.T) {
|
|
|
|
cfg, diags := testNestedModuleConfigFromDir(t, "testdata/valid-modules/nested-providers-fqns")
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatal(diags.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// both the root and child module have two resources, one which should use
|
|
|
|
// the default implied provider and one explicitly using a provider set in
|
|
|
|
// required_providers
|
2020-03-31 20:03:33 +02:00
|
|
|
wantImplicit := addrs.NewDefaultProvider("test")
|
2020-03-16 19:36:16 +01:00
|
|
|
wantFoo := addrs.NewProvider(addrs.DefaultRegistryHost, "foo", "test")
|
|
|
|
wantBar := addrs.NewProvider(addrs.DefaultRegistryHost, "bar", "test")
|
|
|
|
|
|
|
|
// root module
|
|
|
|
if !cfg.Module.ManagedResources["test_instance.explicit"].Provider.Equals(wantFoo) {
|
|
|
|
t.Fatalf("wrong provider for \"test_instance.explicit\"\ngot: %s\nwant: %s",
|
|
|
|
cfg.Module.ManagedResources["test_instance.explicit"].Provider,
|
|
|
|
wantFoo,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if !cfg.Module.ManagedResources["test_instance.implicit"].Provider.Equals(wantImplicit) {
|
|
|
|
t.Fatalf("wrong provider for \"test_instance.implicit\"\ngot: %s\nwant: %s",
|
|
|
|
cfg.Module.ManagedResources["test_instance.implicit"].Provider,
|
|
|
|
wantImplicit,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-18 13:58:20 +01:00
|
|
|
// a data source
|
|
|
|
if !cfg.Module.DataResources["data.test_resource.explicit"].Provider.Equals(wantFoo) {
|
|
|
|
t.Fatalf("wrong provider for \"module.child.test_instance.explicit\"\ngot: %s\nwant: %s",
|
|
|
|
cfg.Module.ManagedResources["test_instance.explicit"].Provider,
|
|
|
|
wantBar,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-16 19:36:16 +01:00
|
|
|
// child module
|
|
|
|
cm := cfg.Children["child"].Module
|
|
|
|
if !cm.ManagedResources["test_instance.explicit"].Provider.Equals(wantBar) {
|
|
|
|
t.Fatalf("wrong provider for \"module.child.test_instance.explicit\"\ngot: %s\nwant: %s",
|
|
|
|
cfg.Module.ManagedResources["test_instance.explicit"].Provider,
|
|
|
|
wantBar,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if !cm.ManagedResources["test_instance.implicit"].Provider.Equals(wantImplicit) {
|
|
|
|
t.Fatalf("wrong provider for \"module.child.test_instance.implicit\"\ngot: %s\nwant: %s",
|
|
|
|
cfg.Module.ManagedResources["test_instance.implicit"].Provider,
|
|
|
|
wantImplicit,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 17:00:00 +01:00
|
|
|
func TestProviderForLocalConfig(t *testing.T) {
|
|
|
|
mod, diags := testModuleFromDir("testdata/providers-explicit-fqn")
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatal(diags.Error())
|
|
|
|
}
|
|
|
|
lc := addrs.LocalProviderConfig{LocalName: "foo-test"}
|
|
|
|
got := mod.ProviderForLocalConfig(lc)
|
|
|
|
want := addrs.NewProvider(addrs.DefaultRegistryHost, "foo", "test")
|
|
|
|
if !got.Equals(want) {
|
|
|
|
t.Fatalf("wrong result! got %#v, want %#v\n", got, want)
|
|
|
|
}
|
|
|
|
}
|