Skip to content

Commit

Permalink
Merge pull request #29178 from hashicorp/alisdair/fix-flapping-json-o…
Browse files Browse the repository at this point in the history
…utput-test-for-real

Fix flapping JSON output test properly
  • Loading branch information
alisdair authored Jul 16, 2021
2 parents 0d80a74 + a456d03 commit 6d65c19
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
12 changes: 11 additions & 1 deletion internal/command/views/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package views
import (
"bytes"
"fmt"
"sort"
"strings"

"github.com/hashicorp/terraform/internal/addrs"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
24 changes: 12 additions & 12 deletions internal/command/views/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
Expand Down

0 comments on commit 6d65c19

Please sign in to comment.