Fix flapping JSON output test properly

This commit makes the output order of the resource drift messages
stable, by building a slice of changes and sorting it by address.
This commit is contained in:
Alisdair McDiarmid 2021-07-15 13:30:11 -04:00
parent 0d80a74539
commit a456d030db
2 changed files with 30 additions and 20 deletions

View File

@ -3,6 +3,7 @@ package views
import (
"bytes"
"fmt"
"sort"
"strings"
"github.com/hashicorp/terraform/internal/addrs"
@ -202,6 +203,7 @@ func (v *OperationJSON) resourceDrift(oldState, newState *states.State, schemas
// resource instances.
return nil
}
var changes []*json.ResourceInstanceChange
for _, ms := range oldState.Modules {
for _, rs := range ms.Resources {
if rs.Addr.Resource.Mode != addrs.ManagedResourceMode {
@ -266,10 +268,18 @@ func (v *OperationJSON) resourceDrift(oldState, newState *states.State, schemas
Action: action,
},
}
v.view.ResourceDrift(json.NewResourceInstanceChange(change))
changes = append(changes, json.NewResourceInstanceChange(change))
}
}
}
// Sort the change structs lexically by address to give stable output
sort.Slice(changes, func(i, j int) bool { return changes[i].Resource.Addr < changes[j].Resource.Addr })
for _, change := range changes {
v.view.ResourceDrift(change)
}
return nil
}

View File

@ -707,25 +707,6 @@ func TestOperationJSON_planDrift(t *testing.T) {
v.Plan(plan, testSchemas())
want := []map[string]interface{}{
// Drift detected: update
{
"@level": "info",
"@message": "test_resource.boop: Drift detected (update)",
"@module": "terraform.ui",
"type": "resource_drift",
"change": map[string]interface{}{
"action": "update",
"resource": map[string]interface{}{
"addr": "test_resource.boop",
"implied_provider": "test",
"module": "",
"resource": "test_resource.boop",
"resource_key": nil,
"resource_name": "boop",
"resource_type": "test_resource",
},
},
},
// Drift detected: delete
{
"@level": "info",
@ -745,6 +726,25 @@ func TestOperationJSON_planDrift(t *testing.T) {
},
},
},
// Drift detected: update
{
"@level": "info",
"@message": "test_resource.boop: Drift detected (update)",
"@module": "terraform.ui",
"type": "resource_drift",
"change": map[string]interface{}{
"action": "update",
"resource": map[string]interface{}{
"addr": "test_resource.boop",
"implied_provider": "test",
"module": "",
"resource": "test_resource.boop",
"resource_key": nil,
"resource_name": "boop",
"resource_type": "test_resource",
},
},
},
// No changes
{
"@level": "info",