Files
opentf/internal/tofu/eval_context_builtin_test.go
Martin Atkins 9d93b939f5 tofu: EvalContext provider methods take context.Context
Continuing the ongoing work of getting context.Context wired in everywhere
we might want to generate OpenTelemetry traces, this completes all of the
provider-related methods of EvalContext.

Unfortunately there is still one remaining path not included here: the
EvalContext.EvaluationScope method needs to somehow arrange for contexts
to reach the provider-defined functions so that we can pass the context
to providers.Interface.CallFunction, which is tricky because that has to
get through the cty function API that wasn't designed for functions that
are backed by network calls. We'll deal with that in a subsequent commit
because it's likely to be a more invasive change than the
relatively-mechanical wiring updates included here.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-06-17 06:38:53 -07:00

94 lines
2.8 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tofu
import (
"reflect"
"sync"
"testing"
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/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(t.Context(), providerAddr1, expected1)
try2 := map[string]cty.Value{"value": cty.StringVal("bar")}
ctx2.SetProviderInput(t.Context(), providerAddr2, try2) // ignored because not a root module
actual1 := ctx1.ProviderInput(t.Context(), providerAddr1)
actual2 := ctx2.ProviderInput(t.Context(), 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]map[addrs.InstanceKey]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(t.Context(), providerAddrDefault, addrs.NoKey)
if err != nil {
t.Fatalf("error initializing provider test: %s", err)
}
_, err = ctx.InitProvider(t.Context(), providerAddrAlias, addrs.NoKey)
if err != nil {
t.Fatalf("error initializing provider test.foo: %s", err)
}
}
func testBuiltinEvalContext(t *testing.T) *BuiltinEvalContext {
return &BuiltinEvalContext{}
}