opentofu/terraform/node_output_test.go
Kristin Laemmert 069f379e75 terraform: refactor Node*Ouput
This commit refactors NodeApplyableOutput and NodeDestroyableOutput into
the new Execute() pattern, collapsing the functions in eval_output.go
into one place.

I also reverted a recent decision to have Execute take a _pointer_ to a
walkOperation: I was thinking of interfaces, not constant bytes, so all
it did was cause problems.

And finally I removed eval_lang.go, which was unused.
2020-09-09 08:45:54 -04:00

81 lines
1.8 KiB
Go

package terraform
import (
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/states"
"github.com/zclconf/go-cty/cty"
)
func TestNodeApplyableOutputExecute(t *testing.T) {
ctx := new(MockEvalContext)
ctx.StateState = states.NewState().SyncWrapper()
cases := []struct {
name string
val cty.Value
err bool
}{
{
// Eval should recognize a single map in a slice, and collapse it
// into the map value
"single-map",
cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("b"),
}),
false,
},
{
// we can't apply a multi-valued map to a variable, so this should error
"multi-map",
cty.ListVal([]cty.Value{
cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("b"),
}),
cty.MapVal(map[string]cty.Value{
"c": cty.StringVal("d"),
}),
}),
true,
},
}
for _, tc := range cases {
node := &NodeApplyableOutput{
Config: &configs.Output{},
Addr: addrs.OutputValue{Name: tc.name}.Absolute(addrs.RootModuleInstance),
}
ctx.EvaluateExprResult = tc.val
t.Run(tc.name, func(t *testing.T) {
err := node.Execute(ctx, walkApply)
if err != nil && !tc.err {
t.Fatal(err)
}
})
}
}
func TestNodeDestroyableOutputExecute(t *testing.T) {
outputAddr := addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance)
state := states.NewState()
state.Module(addrs.RootModuleInstance).SetOutputValue("foo", cty.StringVal("bar"), false)
state.OutputValue(outputAddr)
ctx := &MockEvalContext{
StateState: state.SyncWrapper(),
}
node := NodeDestroyableOutput{Addr: outputAddr}
err := node.Execute(ctx, walkApply)
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if state.OutputValue(outputAddr) != nil {
t.Fatal("Unexpected outputs in state after removal")
}
}