mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-29 10:21:01 -06:00
80b3fcf93e
In the v0.12 timeframe we made contextComponentFactory an interface with the expectation that we'd write mocks of it for tests, but in practice we ended up just always using the same "basicComponentFactory" implementation throughout. In the interests of simplification then, here we replace that interface and its sole implementation with a new concrete struct type contextPlugins. Along with the general benefit that this removes an unneeded indirection, this also means that we can add additional methods to the struct type without the usual restriction that interface types prefer to be small. In particular, in a future commit I'm planning to add methods for loading provider and provisioner schemas, working with the currently-unused new fields this commit has included in contextPlugins, as compared to its predecessor basicComponentFactory.
89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package terraform
|
|
|
|
import (
|
|
"reflect"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
"github.com/hashicorp/terraform/internal/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 = ctx1.WithPath(addrs.RootModuleInstance).(*BuiltinEvalContext)
|
|
ctx1.ProviderInputConfig = cache
|
|
ctx1.ProviderLock = &lock
|
|
|
|
ctx2 := testBuiltinEvalContext(t)
|
|
ctx2 = ctx2.WithPath(addrs.RootModuleInstance.Child("child", addrs.NoKey)).(*BuiltinEvalContext)
|
|
ctx2.ProviderInputConfig = cache
|
|
ctx2.ProviderLock = &lock
|
|
|
|
providerAddr1 := addrs.AbsProviderConfig{
|
|
Module: addrs.RootModule,
|
|
Provider: addrs.NewDefaultProvider("foo"),
|
|
}
|
|
providerAddr2 := addrs.AbsProviderConfig{
|
|
Module: addrs.RootModule.Child("child"),
|
|
Provider: addrs.NewDefaultProvider("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 = ctx.WithPath(addrs.RootModuleInstance).(*BuiltinEvalContext)
|
|
ctx.ProviderLock = &lock
|
|
ctx.ProviderCache = make(map[string]providers.Interface)
|
|
ctx.Plugins = newContextPlugins(map[addrs.Provider]providers.Factory{
|
|
addrs.NewDefaultProvider("test"): providers.FactoryFixed(testP),
|
|
}, nil)
|
|
|
|
providerAddrDefault := addrs.AbsProviderConfig{
|
|
Module: addrs.RootModule,
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
}
|
|
providerAddrAlias := addrs.AbsProviderConfig{
|
|
Module: addrs.RootModule,
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
Alias: "foo",
|
|
}
|
|
|
|
_, err := ctx.InitProvider(providerAddrDefault)
|
|
if err != nil {
|
|
t.Fatalf("error initializing provider test: %s", err)
|
|
}
|
|
_, err = ctx.InitProvider(providerAddrAlias)
|
|
if err != nil {
|
|
t.Fatalf("error initializing provider test.foo: %s", err)
|
|
}
|
|
}
|
|
|
|
func testBuiltinEvalContext(t *testing.T) *BuiltinEvalContext {
|
|
return &BuiltinEvalContext{}
|
|
}
|