mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-15 19:22:46 -06:00
47a16b0937
a large refactor to addrs.AbsProviderConfig, embedding the addrs.Provider instead of a Type string. I've added and updated tests, added some Legacy functions to support older state formats and shims, and added a normalization step when reading v4 (current) state files (not the added tests under states/statefile/roundtrip which work with both current and legacy-style AbsProviderConfig strings). The remaining 'fixme' and 'todo' comments are mostly going to be addressed in a subsequent PR and involve looking up a given local provider config's FQN. This is fine for now as we are only working with default assumption.
94 lines
2.7 KiB
Go
94 lines
2.7 KiB
Go
package terraform
|
|
|
|
import (
|
|
"reflect"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
"github.com/hashicorp/terraform/providers"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
func TestBuiltinEvalContextProviderInput(t *testing.T) {
|
|
var lock sync.Mutex
|
|
cache := make(map[string]map[string]cty.Value)
|
|
|
|
ctx1 := testBuiltinEvalContext(t)
|
|
ctx1.PathValue = addrs.RootModuleInstance
|
|
ctx1.ProviderInputConfig = cache
|
|
ctx1.ProviderLock = &lock
|
|
|
|
ctx2 := testBuiltinEvalContext(t)
|
|
ctx2.PathValue = addrs.RootModuleInstance.Child("child", addrs.NoKey)
|
|
ctx2.ProviderInputConfig = cache
|
|
ctx2.ProviderLock = &lock
|
|
|
|
providerAddr1 := addrs.AbsProviderConfig{
|
|
Module: addrs.RootModuleInstance,
|
|
Provider: addrs.NewLegacyProvider("foo"),
|
|
}
|
|
providerAddr2 := addrs.AbsProviderConfig{
|
|
Module: addrs.RootModuleInstance.Child("child", addrs.NoKey),
|
|
Provider: addrs.NewLegacyProvider("foo"),
|
|
}
|
|
|
|
expected1 := map[string]cty.Value{"value": cty.StringVal("foo")}
|
|
ctx1.SetProviderInput(providerAddr1, expected1)
|
|
|
|
try2 := map[string]cty.Value{"value": cty.StringVal("bar")}
|
|
ctx2.SetProviderInput(providerAddr2, try2) // ignored because not a root module
|
|
|
|
actual1 := ctx1.ProviderInput(providerAddr1)
|
|
actual2 := ctx2.ProviderInput(providerAddr2)
|
|
|
|
if !reflect.DeepEqual(actual1, expected1) {
|
|
t.Errorf("wrong result 1\ngot: %#v\nwant: %#v", actual1, expected1)
|
|
}
|
|
if actual2 != nil {
|
|
t.Errorf("wrong result 2\ngot: %#v\nwant: %#v", actual2, nil)
|
|
}
|
|
}
|
|
|
|
func TestBuildingEvalContextInitProvider(t *testing.T) {
|
|
var lock sync.Mutex
|
|
|
|
testP := &MockProvider{}
|
|
|
|
ctx := testBuiltinEvalContext(t)
|
|
ctx.ProviderLock = &lock
|
|
ctx.ProviderCache = make(map[string]providers.Interface)
|
|
ctx.Components = &basicComponentFactory{
|
|
providers: map[addrs.Provider]providers.Factory{
|
|
addrs.NewLegacyProvider("test"): providers.FactoryFixed(testP),
|
|
},
|
|
}
|
|
|
|
// FIXME: Once AbsProviderConfig has a provider FQN instead of an
|
|
// embedded LocalProviderConfig, use a legacy or default provider address
|
|
// here depending on whether we've moved away from legacy provider
|
|
// addresses in general yet.
|
|
providerAddrDefault := addrs.AbsProviderConfig{
|
|
Module: addrs.RootModuleInstance,
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
}
|
|
providerAddrAlias := addrs.AbsProviderConfig{
|
|
Module: addrs.RootModuleInstance,
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
Alias: "foo",
|
|
}
|
|
|
|
_, err := ctx.InitProvider("test", providerAddrDefault)
|
|
if err != nil {
|
|
t.Fatalf("error initializing provider test: %s", err)
|
|
}
|
|
_, err = ctx.InitProvider("test", providerAddrAlias)
|
|
if err != nil {
|
|
t.Fatalf("error initializing provider test.foo: %s", err)
|
|
}
|
|
}
|
|
|
|
func testBuiltinEvalContext(t *testing.T) *BuiltinEvalContext {
|
|
return &BuiltinEvalContext{}
|
|
}
|