core: Use the new checks package for condition tracking

The "checks" package is an expansion what we previously called
plans.Conditions to accommodate a new requirement that we be able to track
which checks we're expecting to run even if we don't actually get around
to running them, which will be helpful when we start using checks as part
of our module testing story because test reporting tools appreciate there
being a relatively consistent set of test cases from one run to the next.

So far this should be essentially a no-op change from an external
functionality standpoint, aside from some minor adjustments to how we
report some of the error and warning cases from condition evaluation in
light of the fact that the "checks" package can now track errors as a
different outcome than a failure of a valid check.

As is often the case with anything which changes what we track
in the EvalContext and persist between plan and apply, Terraform Core is
pretty brittle and so this had knock-on effects elsewhere too. Again, the
goal is for these changes to not create any material externally-visible
difference, and just to accommodate the new assumption that there will
always be a "checks" object available for tracking during a graph walk.
This commit is contained in:
Martin Atkins
2022-06-15 18:00:20 -07:00
parent 9dea19807f
commit 3785619f93
29 changed files with 982 additions and 582 deletions

View File

@@ -9,6 +9,7 @@ import (
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/checks"
"github.com/hashicorp/terraform/internal/command/jsonconfig"
"github.com/hashicorp/terraform/internal/command/jsonstate"
"github.com/hashicorp/terraform/internal/configs"
@@ -180,9 +181,9 @@ func Marshal(
}
// output.Conditions
err = output.marshalConditionResults(p.Conditions)
err = output.marshalCheckResults(p.Checks)
if err != nil {
return nil, fmt.Errorf("error in marshaling condition results: %s", err)
return nil, fmt.Errorf("error in marshaling check results: %s", err)
}
// output.PriorState
@@ -501,21 +502,30 @@ func (p *plan) marshalOutputChanges(changes *plans.Changes) error {
return nil
}
func (p *plan) marshalConditionResults(conditions plans.Conditions) error {
for addr, c := range conditions {
func (p *plan) marshalCheckResults(results *states.CheckResults) error {
if results == nil {
return nil
}
for _, result := range results.Results {
cr := conditionResult{
checkAddress: addr,
Address: c.Address.String(),
Type: c.Type.String(),
ErrorMessage: c.ErrorMessage,
checkAddress: result.CheckAddr.String(),
Address: result.CheckAddr.Container.String(),
Type: result.CheckAddr.Type.String(),
ErrorMessage: result.ErrorMessage,
}
if c.Result.IsKnown() {
cr.Result = c.Result.True()
} else {
switch result.Status {
case checks.StatusPass:
cr.Result = true
case checks.StatusFail:
cr.Result = false
case checks.StatusError:
cr.Result = false
case checks.StatusUnknown:
cr.Unknown = true
}
p.Conditions = append(p.Conditions, cr)
}
sort.Slice(p.Conditions, func(i, j int) bool {
return p.Conditions[i].checkAddress < p.Conditions[j].checkAddress
})