Fix opentf test crash on nil output (#267)

Co-authored-by: Elbaz <eran.elbaz@env0.com>
This commit is contained in:
Elbaz
2023-09-06 13:40:12 +03:00
committed by GitHub
parent 78c7f75639
commit 4d9404a59e
6 changed files with 42 additions and 9 deletions

View File

@@ -152,6 +152,10 @@ func TestTest(t *testing.T) {
expected: "3 passed, 0 failed.",
code: 0,
},
"null_output": {
expected: "1 passed, 0 failed.",
code: 0,
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
@@ -227,6 +231,11 @@ func TestTest_Full_Output(t *testing.T) {
expected: "Blocks of type \"check\" are not expected here.",
code: 1,
},
"not_exists_output": {
expected: "Error: Reference to undeclared output value",
args: []string{"-no-color"},
code: 1,
},
"refresh_conflicting_config": {
expected: "Incompatible plan options",
code: 1,

View File

@@ -0,0 +1,2 @@
resource "test_resource" "resource" {
}

View File

@@ -0,0 +1,6 @@
run "not_exists" {
assert {
condition = output.something_that_does_not_exist == null
error_message = "Should fail for Reference to undeclared output value"
}
}

View File

@@ -0,0 +1,3 @@
output "my_null_output" {
value = null
}

View File

@@ -0,0 +1,6 @@
run "null" {
assert {
condition = output.my_null_output == null
error_message = "Should work"
}
}

View File

@@ -975,17 +975,24 @@ func (d *evaluationStateData) GetOutput(addr addrs.OutputValue, rng tfdiags.Sour
output := d.Evaluator.State.OutputValue(addr.Absolute(d.ModulePath))
val := output.Value
if val == cty.NilVal {
// Not evaluated yet?
val = cty.DynamicVal
}
// https://github.com/opentffoundation/opentf/issues/257
// If the output is null - it does not serialize as part of the node_output state https://github.com/opentffoundation/opentf/blob/4b623c56ffe9e6c1dc345e54470b71b0f261297a/internal/opentf/node_output.go#L592-L596
// In such a case, we should simply return a nil value because OpenTF test crash to evaluate for invalid memory address or nil pointer dereference
if output == nil {
return cty.NilVal, diags
} else {
val := output.Value
if val == cty.NilVal {
// Not evaluated yet?
val = cty.DynamicVal
}
if output.Sensitive {
val = val.Mark(marks.Sensitive)
}
if output.Sensitive {
val = val.Mark(marks.Sensitive)
}
return val, diags
return val, diags
}
}
func (d *evaluationStateData) GetCheckBlock(addr addrs.Check, rng tfdiags.SourceRange) (cty.Value, tfdiags.Diagnostics) {