From a456d030db92a827df6e2bb78db3089acecc99c2 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Thu, 15 Jul 2021 13:30:11 -0400 Subject: [PATCH] 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. --- internal/command/views/operation.go | 12 +++++++- internal/command/views/operation_test.go | 38 ++++++++++++------------ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/internal/command/views/operation.go b/internal/command/views/operation.go index d564ea9fc..c617dd187 100644 --- a/internal/command/views/operation.go +++ b/internal/command/views/operation.go @@ -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 } diff --git a/internal/command/views/operation_test.go b/internal/command/views/operation_test.go index 7d2964443..e642dbfab 100644 --- a/internal/command/views/operation_test.go +++ b/internal/command/views/operation_test.go @@ -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",