opentofu/terraform/eval_local_test.go
Martin Atkins 3a30bfe845 core: evaluate locals and return them for interpolation
We stash the locals in the module state in a map that is ignored for JSON
serialization. We don't include locals in the persisted state because they
can be trivially recomputed and this allows us to assume that they will
pass through verbatim, without any normalization or other transforms
caused by the JSON serialization.

From a user standpoint a local is just a named alias for an expression,
so it's desirable that the result passes through here in as raw a form
as possible, so it behaves as closely as possible to simply using the
given expression directly.
2017-08-21 15:15:25 -07:00

81 lines
1.4 KiB
Go

package terraform
import (
"reflect"
"sync"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/terraform/config"
)
func TestEvalLocal_impl(t *testing.T) {
var _ EvalNode = new(EvalLocal)
}
func TestEvalLocal(t *testing.T) {
tests := []struct {
Value string
Want interface{}
Err bool
}{
{
"hello!",
"hello!",
false,
},
{
"",
"",
false,
},
}
for _, test := range tests {
t.Run(test.Value, func(t *testing.T) {
rawConfig, err := config.NewRawConfig(map[string]interface{}{
"value": test.Value,
})
if err != nil {
t.Fatal(err)
}
n := &EvalLocal{
Name: "foo",
Value: rawConfig,
}
ctx := &MockEvalContext{
StateState: &State{},
StateLock: &sync.RWMutex{},
InterpolateConfigResult: testResourceConfig(t, map[string]interface{}{
"value": test.Want,
}),
}
_, err = n.Eval(ctx)
if (err != nil) != test.Err {
if err != nil {
t.Errorf("unexpected error: %s", err)
} else {
t.Errorf("successful Eval; want error")
}
}
ms := ctx.StateState.ModuleByPath([]string{})
gotLocals := ms.Locals
wantLocals := map[string]interface{}{
"foo": test.Want,
}
if !reflect.DeepEqual(gotLocals, wantLocals) {
t.Errorf(
"wrong locals after Eval\ngot: %swant: %s",
spew.Sdump(gotLocals), spew.Sdump(wantLocals),
)
}
})
}
}