Skip to content

Commit

Permalink
Relax test to focus on the behavior we care about (encoded == encoded)
Browse files Browse the repository at this point in the history
The specific output order is meaningless, but it should always be the same after
two Encode() calls with identical (ignoring in-memory order) dependency sets.
  • Loading branch information
nfagerlund committed Jan 5, 2022
1 parent df36a03 commit 05d0feb
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions internal/states/instance_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,39 @@ func TestResourceInstanceObject_encode(t *testing.T) {
value := cty.ObjectVal(map[string]cty.Value{
"foo": cty.True,
})
deps := []addrs.ConfigResource{
// The in-memory order of resource dependencies is random, since they're an
// unordered set.
depsOne := []addrs.ConfigResource{
addrs.RootModule.Resource(addrs.ManagedResourceMode, "test", "honk"),
addrs.RootModule.Child("child").Resource(addrs.ManagedResourceMode, "test", "flub"),
addrs.RootModule.Resource(addrs.ManagedResourceMode, "test", "boop"),
}
wantDeps := []addrs.ConfigResource{
depsTwo := []addrs.ConfigResource{
addrs.RootModule.Child("child").Resource(addrs.ManagedResourceMode, "test", "flub"),
addrs.RootModule.Resource(addrs.ManagedResourceMode, "test", "boop"),
addrs.RootModule.Resource(addrs.ManagedResourceMode, "test", "honk"),
}
rio := &ResourceInstanceObject{
rioOne := &ResourceInstanceObject{
Value: value,
Status: ObjectPlanned,
Dependencies: deps,
Dependencies: depsOne,
}
rios, err := rio.Encode(value.Type(), 0)
rioTwo := &ResourceInstanceObject{
Value: value,
Status: ObjectPlanned,
Dependencies: depsTwo,
}
riosOne, err := rioOne.Encode(value.Type(), 0)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
riosTwo, err := rioTwo.Encode(value.Type(), 0)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if diff := cmp.Diff(wantDeps, rios.Dependencies); diff != "" {
t.Errorf("wrong result for deps\n%s", diff)
// However, identical sets of dependencies should always be written to state
// in an identical order, so we don't do meaningless state updates on refresh.
if diff := cmp.Diff(riosOne.Dependencies, riosTwo.Dependencies); diff != "" {
t.Errorf("identical dependencies got encoded in different orders:\n%s", diff)
}
}

0 comments on commit 05d0feb

Please sign in to comment.