stacks: sort diags in test before comparison (#35550)

pull/35549/head^2
Liam Cervante 2 years ago committed by GitHub
parent 7342f9af3c
commit 38033b5c45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1931,36 +1931,16 @@ func TestApplyWithChangedInputValues(t *testing.T) {
t.Fatalf("expected exactly two diagnostics, got %s", applyDiags.ErrWithWarnings())
}
gotSeverity, wantSeverity := applyDiags[0].Severity(), tfdiags.Error
gotSummary, wantSummary := applyDiags[0].Description().Summary, "Inconsistent value for input variable during apply"
gotDetail, wantDetail := applyDiags[0].Description().Detail, "The value for non-ephemeral input variable \"input\" was set to a different value during apply than was set during plan. Only ephemeral input variables can change between the plan and apply phases."
if gotSeverity != wantSeverity {
t.Errorf("expected severity %q, got %q", wantSeverity, gotSeverity)
}
if gotSummary != wantSummary {
t.Errorf("expected summary %q, got %q", wantSummary, gotSummary)
}
if gotDetail != wantDetail {
t.Errorf("expected detail %q, got %q", wantDetail, gotDetail)
}
// We'll also receive a second error message here, since the invalid input
// has affected the component as well.
gotSeverity, wantSeverity = applyDiags[1].Severity(), tfdiags.Error
gotSummary, wantSummary = applyDiags[1].Description().Summary, "Planned input variable value changed"
gotDetail, wantDetail = applyDiags[1].Description().Detail, "The planned value for input variable \"input\" has changed between the planning and apply phases for component.self. This is a bug in Terraform - please report it."
if gotSeverity != wantSeverity {
t.Errorf("expected severity %q, got %q", wantSeverity, gotSeverity)
}
if gotSummary != wantSummary {
t.Errorf("expected summary %q, got %q", wantSummary, gotSummary)
}
if gotDetail != wantDetail {
t.Errorf("expected detail %q, got %q", wantDetail, gotDetail)
}
sort.SliceStable(applyDiags, diagnosticSortFunc(applyDiags))
expectDiagnosticsForTest(t, applyDiags,
expectDiagnostic(
tfdiags.Error,
"Inconsistent value for input variable during apply",
"The value for non-ephemeral input variable \"input\" was set to a different value during apply than was set during plan. Only ephemeral input variables can change between the plan and apply phases."),
expectDiagnostic(
tfdiags.Error,
"Planned input variable value changed",
"The planned value for input variable \"input\" has changed between the planning and apply phases for component.self. This is a bug in Terraform - please report it."))
wantChanges := []stackstate.AppliedChange{
&stackstate.AppliedChangeComponentInstance{

@ -9,6 +9,7 @@ import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-slug/sourceaddrs"
"github.com/hashicorp/go-slug/sourcebundle"
"github.com/zclconf/go-cty/cty"
@ -212,6 +213,43 @@ func plannedChangeSortKey(change stackplan.PlannedChange) string {
}
}
func diagnosticSortFunc(diags tfdiags.Diagnostics) func(i, j int) bool {
sortDescription := func(i, j tfdiags.Description) bool {
if i.Summary != j.Summary {
return i.Summary < j.Summary
}
return i.Detail < j.Detail
}
sortPos := func(i, j tfdiags.SourcePos) bool {
if i.Line != j.Line {
return i.Line < j.Line
}
return i.Column < j.Column
}
sortRange := func(i, j *tfdiags.SourceRange) bool {
if i.Filename != j.Filename {
return i.Filename < j.Filename
}
if !cmp.Equal(i.Start, j.Start) {
return sortPos(i.Start, j.Start)
}
return sortPos(i.End, j.End)
}
return func(i, j int) bool {
id, jd := diags[i], diags[j]
if id.Severity() != jd.Severity() {
return id.Severity() == tfdiags.Error
}
if !cmp.Equal(id.Description(), jd.Description()) {
return sortDescription(id.Description(), jd.Description())
}
return sortRange(id.Source().Subject, jd.Source().Subject)
}
}
func mustDefaultRootProvider(provider string) addrs.AbsProviderConfig {
return addrs.AbsProviderConfig{
Module: addrs.RootModule,

@ -3350,9 +3350,10 @@ func TestPlanInvalidProvidersFailGracefully(t *testing.T) {
go Plan(ctx, &req, &resp)
changes, diags := collectPlanOutput(changesCh, diagsCh)
sort.SliceStable(diags, diagnosticSortFunc(diags))
expectDiagnosticsForTest(t, diags,
expectDiagnostic(tfdiags.Error, "invalid configuration", "configure_error attribute was set"),
expectDiagnostic(tfdiags.Error, "Provider configuration is invalid", "Cannot plan changes for this resource because its associated provider configuration is invalid."))
expectDiagnostic(tfdiags.Error, "Provider configuration is invalid", "Cannot plan changes for this resource because its associated provider configuration is invalid."),
expectDiagnostic(tfdiags.Error, "invalid configuration", "configure_error attribute was set"))
sort.SliceStable(changes, func(i, j int) bool {
return plannedChangeSortKey(changes[i]) < plannedChangeSortKey(changes[j])

Loading…
Cancel
Save