Files
opentf/internal/lang/eval/module_instance_test.go
Martin Atkins 507055bc2f lang/eval: Shuffle resource instance result handling a little more
The separation of concerns here still doesn't feel _quite_ right since
a resource instance node needs to know what provider it belongs to even
though that package doesn't otherwise interact with providers directly
at all, but this at least clarifies exactly one location that's responsible
for getting the config value for the result value callback to use.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-10-27 10:15:41 -07:00

64 lines
1.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 eval
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs"
"github.com/opentofu/opentofu/internal/lang/grapheval"
"github.com/opentofu/opentofu/internal/providers"
)
func TestCompileModuleInstance_valuesOnly(t *testing.T) {
// This is a relatively straightforward test of compiling a module
// instance containing only inert values (no resources, etc) and
// then pulling values out of it to make sure that it's wired together
// correctly. This is far from exhaustive but covers some of
// the fundamentals that more complex situations rely on.
ctx := grapheval.ContextWithNewWorker(t.Context())
module := configs.ModuleFromStringForTesting(t, `
variable "a" {
type = string
}
locals {
b = "${var.a}:${var.a}"
}
output "c" {
value = "${local.b}/${local.b}"
}
`)
evalCtx := &EvalContext{}
evalCtx.init()
call := &moduleInstanceCall{
inputValues: InputValuesForTesting(map[string]cty.Value{
"a": cty.True,
}),
evalContext: evalCtx,
evaluationGlue: &validationGlue{
providers: ProvidersForTesting(map[addrs.Provider]*providers.GetProviderSchemaResponse{}),
},
}
inst := compileModuleInstance(ctx, module, addrs.ModuleSourceLocal("."), call)
got, diags := inst.Value(ctx)
if diags.HasErrors() {
t.Fatalf("unexpected errors: %s", diags.Err().Error())
}
want := cty.ObjectVal(map[string]cty.Value{
"c": cty.StringVal("true:true/true:true"),
})
if diff := cmp.Diff(want, got, ctydebug.CmpOptions); diff != "" {
t.Error("wrong result\n" + diff)
}
}