Fix bug in DeepCopy of backend state data that causes loss of StateStore data (#37276)

pull/37163/head
Sarah French 11 months ago committed by GitHub
parent 3617b909c1
commit a91d3aafa8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -143,9 +143,10 @@ func (f *BackendStateFile) DeepCopy() *BackendStateFile {
return nil
}
ret := &BackendStateFile{
Version: f.Version,
TFVersion: f.TFVersion,
Backend: f.Backend.DeepCopy(),
Version: f.Version,
TFVersion: f.TFVersion,
Backend: f.Backend.DeepCopy(),
StateStore: f.StateStore.DeepCopy(),
}
if f.Remote != nil {
// This shouldn't ever be present in an object held by a caller since

@ -5,6 +5,7 @@ package workdir
import (
"encoding/json"
"reflect"
"strings"
"testing"
@ -230,3 +231,46 @@ func TestEncodeBackendStateFile(t *testing.T) {
}
}
func TestBackendStateFile_DeepCopy(t *testing.T) {
tests := map[string]struct {
file *BackendStateFile
}{
"Deep copy preserves state_store data": {
file: &BackendStateFile{
StateStore: &StateStoreConfigState{
Type: "foo_bar",
Provider: getTestProviderState(t, "1.2.3", "A", "B", "C"),
ConfigRaw: json.RawMessage([]byte(`{"foo":"bar"}`)),
Hash: 123,
},
},
},
"Deep copy preserves backend data": {
file: &BackendStateFile{
Backend: &BackendConfigState{
Type: "foobar",
ConfigRaw: json.RawMessage([]byte(`{"foo":"bar"}`)),
Hash: 123,
},
},
},
"Deep copy preserves version and Terraform version data": {
file: &BackendStateFile{
Version: 3,
TFVersion: "9.9.9",
},
},
}
for tn, tc := range tests {
t.Run(tn, func(t *testing.T) {
copy := tc.file.DeepCopy()
if !reflect.DeepEqual(copy, tc.file) {
t.Fatalf("unexpected difference in backend state data:\n got %#v, want %#v", copy, tc.file)
}
})
}
}

Loading…
Cancel
Save