Support for static variables used with encrypted plans (#1998)

Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Signed-off-by: Andrei Ciobanu <andrei.ciobanu@opentofu.org>
Co-authored-by: Andrei Ciobanu <andrei.ciobanu@opentofu.org>
This commit is contained in:
Christian Mesh
2025-07-28 12:11:43 -04:00
committed by GitHub
parent 3c76c5f419
commit be433be96f
13 changed files with 189 additions and 140 deletions

View File

@@ -9,6 +9,9 @@ import (
"testing"
"github.com/go-test/deep"
"github.com/opentofu/opentofu/internal/configs"
"github.com/zclconf/go-cty/cty"
ctymsgpack "github.com/zclconf/go-cty/cty/msgpack"
"github.com/opentofu/opentofu/internal/addrs"
)
@@ -98,3 +101,56 @@ func TestModuleOutputChangesEmpty(t *testing.T) {
t.Fatal("plan has no visible changes")
}
}
// TestVariableMapper checks that the mapper is decoding types correctly from the plan
func TestVariableMapper(t *testing.T) {
val1 := cty.StringVal("string value")
val2 := cty.ObjectVal(map[string]cty.Value{"foo": cty.StringVal("bar")})
val3 := cty.MapVal(map[string]cty.Value{
"inner": cty.SetVal([]cty.Value{cty.StringVal("baz")}),
})
val4 := cty.ListVal([]cty.Value{cty.BoolVal(false)})
val5 := cty.SetVal([]cty.Value{
cty.ObjectVal(
map[string]cty.Value{
"inner": cty.ObjectVal(map[string]cty.Value{"foo": cty.NumberIntVal(25)}),
},
),
})
p := Plan{VariableValues: map[string]DynamicValue{
"raw_string": encodeDynamicValueWithType(t, val1, cty.DynamicPseudoType),
"object_of_strings": encodeDynamicValueWithType(t, val2, cty.DynamicPseudoType),
"map_of_sets_of_strings": encodeDynamicValueWithType(t, val3, cty.DynamicPseudoType),
"list_of_bools": encodeDynamicValueWithType(t, val4, cty.DynamicPseudoType),
"set_of_obj_of_obj_of_number": encodeDynamicValueWithType(t, val5, cty.DynamicPseudoType),
}}
vm := p.VariableMapper()
cases := map[string]cty.Value{
"raw_string": val1,
"object_of_strings": val2,
"map_of_sets_of_strings": val3,
"list_of_bools": val4,
"set_of_obj_of_obj_of_number": val5,
}
for varName, wantVal := range cases {
t.Run(varName, func(t *testing.T) {
val, diag := vm(&configs.Variable{Name: varName})
if diag.HasErrors() {
t.Fatalf("unexpected diagnostics from the variable mapper: %s", diag)
}
if !val.RawEquals(wantVal) {
t.Fatalf("returned value is not equal with the expected one.\n\twant:%s\n\tgot:%s\n", wantVal, val)
}
})
}
}
func encodeDynamicValueWithType(t *testing.T, value cty.Value, ty cty.Type) []byte {
data, err := ctymsgpack.Marshal(value, ty)
if err != nil {
t.Fatalf("failed to marshal JSON: %s", err)
}
return data
}