Merge pull request #29178 from hashicorp/alisdair/fix-flapping-json-output-test-for-real

Fix flapping JSON output test properly
This commit is contained in:
Alisdair McDiarmid 2021-07-16 10:05:48 -04:00 committed by GitHub
commit 6d65c19134
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 20 deletions

View File

@ -3,6 +3,7 @@ package views
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"sort"
"strings" "strings"
"github.com/hashicorp/terraform/internal/addrs" "github.com/hashicorp/terraform/internal/addrs"
@ -202,6 +203,7 @@ func (v *OperationJSON) resourceDrift(oldState, newState *states.State, schemas
// resource instances. // resource instances.
return nil return nil
} }
var changes []*json.ResourceInstanceChange
for _, ms := range oldState.Modules { for _, ms := range oldState.Modules {
for _, rs := range ms.Resources { for _, rs := range ms.Resources {
if rs.Addr.Resource.Mode != addrs.ManagedResourceMode { if rs.Addr.Resource.Mode != addrs.ManagedResourceMode {
@ -266,10 +268,18 @@ func (v *OperationJSON) resourceDrift(oldState, newState *states.State, schemas
Action: action, 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 return nil
} }

View File

@ -707,25 +707,6 @@ func TestOperationJSON_planDrift(t *testing.T) {
v.Plan(plan, testSchemas()) v.Plan(plan, testSchemas())
want := []map[string]interface{}{ 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 // Drift detected: delete
{ {
"@level": "info", "@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 // No changes
{ {
"@level": "info", "@level": "info",