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 | 24 ++++++++++++------------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/internal/command/views/operation.go b/internal/command/views/operation.go index d564ea9fced1..c617dd187c31 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 7d29644434b9..e642dbfabeb5 100644 --- a/internal/command/views/operation_test.go +++ b/internal/command/views/operation_test.go @@ -707,40 +707,40 @@ func TestOperationJSON_planDrift(t *testing.T) { v.Plan(plan, testSchemas()) want := []map[string]interface{}{ - // Drift detected: update + // Drift detected: delete { "@level": "info", - "@message": "test_resource.boop: Drift detected (update)", + "@message": "test_resource.beep: Drift detected (delete)", "@module": "terraform.ui", "type": "resource_drift", "change": map[string]interface{}{ - "action": "update", + "action": "delete", "resource": map[string]interface{}{ - "addr": "test_resource.boop", + "addr": "test_resource.beep", "implied_provider": "test", "module": "", - "resource": "test_resource.boop", + "resource": "test_resource.beep", "resource_key": nil, - "resource_name": "boop", + "resource_name": "beep", "resource_type": "test_resource", }, }, }, - // Drift detected: delete + // Drift detected: update { "@level": "info", - "@message": "test_resource.beep: Drift detected (delete)", + "@message": "test_resource.boop: Drift detected (update)", "@module": "terraform.ui", "type": "resource_drift", "change": map[string]interface{}{ - "action": "delete", + "action": "update", "resource": map[string]interface{}{ - "addr": "test_resource.beep", + "addr": "test_resource.boop", "implied_provider": "test", "module": "", - "resource": "test_resource.beep", + "resource": "test_resource.boop", "resource_key": nil, - "resource_name": "beep", + "resource_name": "boop", "resource_type": "test_resource", }, },