Commit 10ae444e authored by Alisdair McDiarmid's avatar Alisdair McDiarmid
Browse files

Upgrade HCL to fix optional attr default crash

Also add regression test coverage of the crash. This would occur when
objects with optional attributes had default values of different type
from the attribute type, and the objects were members of a collection.

For example:

list(object({
  a = optional(set(string), [])
}))

If this type constraint is applied to a variable value where one object
has a set(string) value for a, and the other object applies the empty
tuple default, Terraform would crash.
parent 1e6f0919
Showing with 67 additions and 3 deletions
+67 -3
......@@ -42,7 +42,7 @@ require (
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f
github.com/hashicorp/hcl/v2 v2.14.0
github.com/hashicorp/hcl/v2 v2.14.1
github.com/hashicorp/terraform-config-inspect v0.0.0-20210209133302-4fd17a0faac2
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
......
......@@ -12165,6 +12165,70 @@ output "out" {
}
}
func TestContext2Apply_moduleVariableOptionalAttributesDefaultChild(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
variable "in" {
type = list(object({
a = optional(set(string))
}))
default = [
{ a = [ "foo" ] },
{ },
]
}
module "child" {
source = "./child"
in = var.in
}
output "out" {
value = module.child.out
}
`,
"child/main.tf": `
variable "in" {
type = list(object({
a = optional(set(string), [])
}))
default = []
}
output "out" {
value = var.in
}
`,
})
ctx := testContext2(t, &ContextOpts{})
// We don't specify a value for the variable here, relying on its defined
// default.
plan, diags := ctx.Plan(m, states.NewState(), SimplePlanOpts(plans.NormalMode, testInputValuesUnset(m.Module.Variables)))
if diags.HasErrors() {
t.Fatal(diags.ErrWithWarnings())
}
state, diags := ctx.Apply(plan, m)
if diags.HasErrors() {
t.Fatal(diags.ErrWithWarnings())
}
got := state.RootModule().OutputValues["out"].Value
want := cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"a": cty.SetVal([]cty.Value{cty.StringVal("foo")}),
}),
cty.ObjectVal(map[string]cty.Value{
"a": cty.SetValEmpty(cty.String),
}),
})
if !want.RawEquals(got) {
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
}
func TestContext2Apply_provisionerSensitive(t *testing.T) {
m := testModule(t, "apply-provisioner-sensitive")
p := testProvider("aws")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment