Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform: Error message for unknown error_message in variable validation #35400

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion internal/terraform/eval_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,23 @@ func evalVariableValidation(validation *configs.CheckRule, hclCtx *hcl.EvalConte
return checkResult{Status: status}, diags
}

if !errorValue.IsKnown() {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid error message",
Detail: "Unsuitable value for error message: expression refers to values that won't be known until the apply phase.",
Subject: validation.ErrorMessage.Range().Ptr(),
Expression: validation.ErrorMessage,
EvalContext: hclCtx,
Extra: diagnosticCausedByUnknown(true),
})
return checkResult{
Status: checks.StatusError,
}, diags
}

var errorMessage string
if !errorDiags.HasErrors() && errorValue.IsKnown() && !errorValue.IsNull() {
if !errorDiags.HasErrors() && !errorValue.IsNull() {
var err error
errorValue, err = convert.Convert(errorValue, cty.String)
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions internal/terraform/eval_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"testing"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hcltest"
"github.com/zclconf/go-cty/cty"

"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/checks"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/lang"
"github.com/hashicorp/terraform/internal/lang/marks"
"github.com/hashicorp/terraform/internal/namedvals"
Expand Down Expand Up @@ -1374,3 +1376,47 @@ variable "bar" {
})
}
}

func TestEvalVariableValidation_unknownValues(t *testing.T) {
t.Run("known condition, unknown error_message", func(t *testing.T) {
rule := &configs.CheckRule{
Condition: hcltest.MockExprLiteral(cty.False),
ErrorMessage: hcltest.MockExprLiteral(cty.UnknownVal(cty.String)),
}
hclCtx := &hcl.EvalContext{}
varAddr := addrs.AbsInputVariableInstance{
Module: addrs.RootModuleInstance,
Variable: addrs.InputVariable{Name: "foo"},
}

result, diags := evalVariableValidation(rule, hclCtx, hcl.Range{}, varAddr, 0)
if got, want := result.Status, checks.StatusError; got != want {
t.Errorf("wrong result.Status\ngot: %s\nwant: %s", got, want)
}
if !diags.HasErrors() {
t.Fatalf("unexpected success; want error")
}
found := false
hasCorrectExtra := false
wantDesc := tfdiags.Description{
Summary: "Invalid error message",
Detail: "Unsuitable value for error message: expression refers to values that won't be known until the apply phase.",
}
for _, diag := range diags {
gotDesc := diag.Description()
if diag.Severity() == tfdiags.Error && gotDesc.Summary == wantDesc.Summary && gotDesc.Detail == wantDesc.Detail {
found = true
hasCorrectExtra = tfdiags.DiagnosticCausedByUnknown(diag)
break
}
}
if !found {
t.Errorf("missing expected error diagnostic\nwant: %s: %s\ngot: %s",
wantDesc.Summary, wantDesc.Detail,
diags.Err().Error(),
)
} else if !hasCorrectExtra {
t.Errorf("diagnostic is not marked as being 'caused by unknown'")
}
})
}
Loading