core: Correctly ensure that State() is a copy
The previous mechanism for testing state threw away the mutation made on the state by calling State() twice - this commit corrects the test to match the comment. In addition, we replace the custom copying logic with the copystructure library to simplify the code.
This commit is contained in:
parent
b190aa05a5
commit
d60365af02
|
@ -118,9 +118,11 @@ func TestState(t *testing.T, s interface{}) {
|
||||||
t.Fatalf("bad: expected %d, got %d", serial, reader.State().Serial)
|
t.Fatalf("bad: expected %d, got %d", serial, reader.State().Serial)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that State() returns a copy
|
// Check that State() returns a copy by modifying the copy and comparing
|
||||||
reader.State().Serial++
|
// to the current state.
|
||||||
if reflect.DeepEqual(reader.State(), current) {
|
stateCopy := reader.State()
|
||||||
|
stateCopy.Serial++
|
||||||
|
if reflect.DeepEqual(stateCopy, current) {
|
||||||
t.Fatal("State() should return a copy")
|
t.Fatal("State() should return a copy")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -458,23 +458,12 @@ func (s *State) SameLineage(other *State) bool {
|
||||||
// DeepCopy performs a deep copy of the state structure and returns
|
// DeepCopy performs a deep copy of the state structure and returns
|
||||||
// a new structure.
|
// a new structure.
|
||||||
func (s *State) DeepCopy() *State {
|
func (s *State) DeepCopy() *State {
|
||||||
if s == nil {
|
copy, err := copystructure.Copy(s)
|
||||||
return nil
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
n := &State{
|
|
||||||
Version: s.Version,
|
return copy.(*State)
|
||||||
Lineage: s.Lineage,
|
|
||||||
TFVersion: s.TFVersion,
|
|
||||||
Serial: s.Serial,
|
|
||||||
Modules: make([]*ModuleState, 0, len(s.Modules)),
|
|
||||||
}
|
|
||||||
for _, mod := range s.Modules {
|
|
||||||
n.Modules = append(n.Modules, mod.deepcopy())
|
|
||||||
}
|
|
||||||
if s.Remote != nil {
|
|
||||||
n.Remote = s.Remote.deepcopy()
|
|
||||||
}
|
|
||||||
return n
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IncrementSerialMaybe increments the serial number of this state
|
// IncrementSerialMaybe increments the serial number of this state
|
||||||
|
@ -1187,28 +1176,12 @@ func (r *ResourceState) init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ResourceState) deepcopy() *ResourceState {
|
func (r *ResourceState) deepcopy() *ResourceState {
|
||||||
if r == nil {
|
copy, err := copystructure.Copy(r)
|
||||||
return nil
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
n := &ResourceState{
|
return copy.(*ResourceState)
|
||||||
Type: r.Type,
|
|
||||||
Dependencies: nil,
|
|
||||||
Primary: r.Primary.DeepCopy(),
|
|
||||||
Provider: r.Provider,
|
|
||||||
}
|
|
||||||
if r.Dependencies != nil {
|
|
||||||
n.Dependencies = make([]string, len(r.Dependencies))
|
|
||||||
copy(n.Dependencies, r.Dependencies)
|
|
||||||
}
|
|
||||||
if r.Deposed != nil {
|
|
||||||
n.Deposed = make([]*InstanceState, 0, len(r.Deposed))
|
|
||||||
for _, inst := range r.Deposed {
|
|
||||||
n.Deposed = append(n.Deposed, inst.DeepCopy())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return n
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// prune is used to remove any instances that are no longer required
|
// prune is used to remove any instances that are no longer required
|
||||||
|
@ -1278,27 +1251,12 @@ func (i *InstanceState) init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InstanceState) DeepCopy() *InstanceState {
|
func (i *InstanceState) DeepCopy() *InstanceState {
|
||||||
if i == nil {
|
copy, err := copystructure.Copy(i)
|
||||||
return nil
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
n := &InstanceState{
|
|
||||||
ID: i.ID,
|
return copy.(*InstanceState)
|
||||||
Ephemeral: *i.Ephemeral.DeepCopy(),
|
|
||||||
Tainted: i.Tainted,
|
|
||||||
}
|
|
||||||
if i.Attributes != nil {
|
|
||||||
n.Attributes = make(map[string]string, len(i.Attributes))
|
|
||||||
for k, v := range i.Attributes {
|
|
||||||
n.Attributes[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if i.Meta != nil {
|
|
||||||
n.Meta = make(map[string]string, len(i.Meta))
|
|
||||||
for k, v := range i.Meta {
|
|
||||||
n.Meta[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return n
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *InstanceState) Empty() bool {
|
func (s *InstanceState) Empty() bool {
|
||||||
|
@ -1446,17 +1404,12 @@ func (e *EphemeralState) init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EphemeralState) DeepCopy() *EphemeralState {
|
func (e *EphemeralState) DeepCopy() *EphemeralState {
|
||||||
if e == nil {
|
copy, err := copystructure.Copy(e)
|
||||||
return nil
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
n := &EphemeralState{}
|
|
||||||
if e.ConnInfo != nil {
|
return copy.(*EphemeralState)
|
||||||
n.ConnInfo = make(map[string]string, len(e.ConnInfo))
|
|
||||||
for k, v := range e.ConnInfo {
|
|
||||||
n.ConnInfo[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return n
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type jsonStateVersionIdentifier struct {
|
type jsonStateVersionIdentifier struct {
|
||||||
|
|
Loading…
Reference in New Issue