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:
parent
0d80a74539
commit
a456d030db
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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",
|
||||||
|
|
Loading…
Reference in New Issue