terraform/terraform/eval_local_test.go

87 lines
1.7 KiB
Go
Raw Normal View History

package terraform
import (
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/addrs"
2019-08-07 01:58:58 +02:00
"github.com/hashicorp/terraform/configs/hcl2shim"
"github.com/hashicorp/terraform/states"
)
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,
},
{
"Hello, ${local.foo}",
nil,
true, // self-referencing
},
}
for _, test := range tests {
t.Run(test.Value, func(t *testing.T) {
expr, diags := hclsyntax.ParseTemplate([]byte(test.Value), "", hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
t.Fatal(diags.Error())
}
n := &EvalLocal{
Addr: addrs.LocalValue{Name: "foo"},
Expr: expr,
}
ctx := &MockEvalContext{
StateState: states.NewState().SyncWrapper(),
EvaluateExprResult: hcl2shim.HCL2ValueFromConfigValue(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.Module(addrs.RootModuleInstance)
gotLocals := ms.LocalValues
wantLocals := map[string]cty.Value{}
if test.Want != nil {
wantLocals["foo"] = hcl2shim.HCL2ValueFromConfigValue(test.Want)
}
if !reflect.DeepEqual(gotLocals, wantLocals) {
t.Errorf(
"wrong locals after Eval\ngot: %swant: %s",
spew.Sdump(gotLocals), spew.Sdump(wantLocals),
)
}
})
}
}