2022-01-05 11:29:20 -06:00
|
|
|
package states
|
|
|
|
|
|
|
|
import (
|
2022-04-28 10:16:27 -05:00
|
|
|
"sync"
|
2022-01-05 11:29:20 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceInstanceObject_encode(t *testing.T) {
|
|
|
|
value := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"foo": cty.True,
|
|
|
|
})
|
2022-01-05 16:38:53 -06:00
|
|
|
// The in-memory order of resource dependencies is random, since they're an
|
|
|
|
// unordered set.
|
|
|
|
depsOne := []addrs.ConfigResource{
|
2022-01-05 11:29:20 -06:00
|
|
|
addrs.RootModule.Resource(addrs.ManagedResourceMode, "test", "honk"),
|
|
|
|
addrs.RootModule.Child("child").Resource(addrs.ManagedResourceMode, "test", "flub"),
|
|
|
|
addrs.RootModule.Resource(addrs.ManagedResourceMode, "test", "boop"),
|
|
|
|
}
|
2022-01-05 16:38:53 -06:00
|
|
|
depsTwo := []addrs.ConfigResource{
|
2022-01-05 11:29:20 -06:00
|
|
|
addrs.RootModule.Child("child").Resource(addrs.ManagedResourceMode, "test", "flub"),
|
|
|
|
addrs.RootModule.Resource(addrs.ManagedResourceMode, "test", "boop"),
|
|
|
|
addrs.RootModule.Resource(addrs.ManagedResourceMode, "test", "honk"),
|
|
|
|
}
|
2022-04-28 10:16:27 -05:00
|
|
|
|
|
|
|
// multiple instances may have been assigned the same deps slice
|
|
|
|
objs := []*ResourceInstanceObject{
|
|
|
|
&ResourceInstanceObject{
|
|
|
|
Value: value,
|
|
|
|
Status: ObjectPlanned,
|
|
|
|
Dependencies: depsOne,
|
|
|
|
},
|
|
|
|
&ResourceInstanceObject{
|
|
|
|
Value: value,
|
|
|
|
Status: ObjectPlanned,
|
|
|
|
Dependencies: depsTwo,
|
|
|
|
},
|
|
|
|
&ResourceInstanceObject{
|
|
|
|
Value: value,
|
|
|
|
Status: ObjectPlanned,
|
|
|
|
Dependencies: depsOne,
|
|
|
|
},
|
|
|
|
&ResourceInstanceObject{
|
|
|
|
Value: value,
|
|
|
|
Status: ObjectPlanned,
|
|
|
|
Dependencies: depsOne,
|
|
|
|
},
|
2022-01-05 16:38:53 -06:00
|
|
|
}
|
2022-04-28 10:16:27 -05:00
|
|
|
|
|
|
|
var encoded []*ResourceInstanceObjectSrc
|
|
|
|
|
|
|
|
// Encoding can happen concurrently, so we need to make sure the shared
|
|
|
|
// Dependencies are safely handled
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var mu sync.Mutex
|
|
|
|
|
|
|
|
for _, obj := range objs {
|
|
|
|
obj := obj
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
rios, err := obj.Encode(value.Type(), 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
mu.Lock()
|
|
|
|
encoded = append(encoded, rios)
|
|
|
|
mu.Unlock()
|
|
|
|
}()
|
2022-01-05 11:29:20 -06:00
|
|
|
}
|
2022-04-28 10:16:27 -05:00
|
|
|
wg.Wait()
|
|
|
|
|
2022-01-05 16:38:53 -06:00
|
|
|
// 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.
|
2022-04-28 10:16:27 -05:00
|
|
|
for i := 0; i < len(encoded)-1; i++ {
|
|
|
|
if diff := cmp.Diff(encoded[i].Dependencies, encoded[i+1].Dependencies); diff != "" {
|
|
|
|
t.Errorf("identical dependencies got encoded in different orders:\n%s", diff)
|
|
|
|
}
|
2022-01-05 11:29:20 -06:00
|
|
|
}
|
|
|
|
}
|