mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-04-28 21:01:16 -04:00
Both differing serials and lineage protections should be bypassed with the -force flag (in addition to resources). Compared to other backends we aren’t just shipping over the state bytes in a simple payload during the persistence phase of the push command and the force flag added to the Go TFE client needs to be specified at that time. To prevent changing every method signature of PersistState of the remote client I added an optional interface that provides a hook to flag the Client as operating in a force push context. Changing the method signature would be more explicit at the cost of not being used anywhere else currently or the optional interface pattern could be applied to the state itself so it could be upgraded to support PersistState(force bool) only when needed. Prior to this only the resources of the state were checked for changes not the lineage or the serial. To bring this in line with documented behavior noted above those attributes also have a “read” counterpart just like state has. These are now checked along with state to determine if the state as a whole is unchanged. Tests were altered to table driven test format and testing was expanded to include WriteStateForMigration and its interaction with a ClientForcePusher type.
129 lines
2.8 KiB
Go
129 lines
2.8 KiB
Go
package remote
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/md5"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
"github.com/hashicorp/terraform/states/statefile"
|
|
)
|
|
|
|
// testClient is a generic function to test any client.
|
|
func testClient(t *testing.T, c Client) {
|
|
var buf bytes.Buffer
|
|
s := state.TestStateInitial()
|
|
sf := &statefile.File{State: s}
|
|
if err := statefile.Write(sf, &buf); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
data := buf.Bytes()
|
|
|
|
if err := c.Put(data); err != nil {
|
|
t.Fatalf("put: %s", err)
|
|
}
|
|
|
|
p, err := c.Get()
|
|
if err != nil {
|
|
t.Fatalf("get: %s", err)
|
|
}
|
|
if !bytes.Equal(p.Data, data) {
|
|
t.Fatalf("bad: %#v", p)
|
|
}
|
|
|
|
if err := c.Delete(); err != nil {
|
|
t.Fatalf("delete: %s", err)
|
|
}
|
|
|
|
p, err = c.Get()
|
|
if err != nil {
|
|
t.Fatalf("get: %s", err)
|
|
}
|
|
if p != nil {
|
|
t.Fatalf("bad: %#v", p)
|
|
}
|
|
}
|
|
|
|
func TestRemoteClient_noPayload(t *testing.T) {
|
|
s := &State{
|
|
Client: nilClient{},
|
|
}
|
|
if err := s.RefreshState(); err != nil {
|
|
t.Fatal("error refreshing empty remote state")
|
|
}
|
|
}
|
|
|
|
// nilClient returns nil for everything
|
|
type nilClient struct{}
|
|
|
|
func (nilClient) Get() (*Payload, error) { return nil, nil }
|
|
|
|
func (c nilClient) Put([]byte) error { return nil }
|
|
|
|
func (c nilClient) Delete() error { return nil }
|
|
|
|
// mockClient is a client that tracks persisted state snapshots only in
|
|
// memory and also logs what it has been asked to do for use in test
|
|
// assertions.
|
|
type mockClient struct {
|
|
current []byte
|
|
log []mockClientRequest
|
|
force bool
|
|
}
|
|
|
|
type mockClientRequest struct {
|
|
Method string
|
|
Content map[string]interface{}
|
|
}
|
|
|
|
func (c *mockClient) Get() (*Payload, error) {
|
|
c.appendLog("Get", c.current)
|
|
if c.current == nil {
|
|
return nil, nil
|
|
}
|
|
checksum := md5.Sum(c.current)
|
|
return &Payload{
|
|
Data: c.current,
|
|
MD5: checksum[:],
|
|
}, nil
|
|
}
|
|
|
|
func (c *mockClient) Put(data []byte) error {
|
|
if c.force {
|
|
c.appendLog("Force Put", data)
|
|
} else {
|
|
c.appendLog("Put", data)
|
|
}
|
|
c.current = data
|
|
return nil
|
|
}
|
|
|
|
func (c *mockClient) Delete() error {
|
|
c.appendLog("Delete", c.current)
|
|
c.current = nil
|
|
return nil
|
|
}
|
|
|
|
// Implements remote.ClientForcePusher
|
|
func (c *mockClient) EnableForcePush() {
|
|
c.force = true
|
|
}
|
|
|
|
func (c *mockClient) appendLog(method string, content []byte) {
|
|
// For easier test assertions, we actually log the result of decoding
|
|
// the content JSON rather than the raw bytes. Callers are in principle
|
|
// allowed to provide any arbitrary bytes here, but we know we're only
|
|
// using this to test our own State implementation here and that always
|
|
// uses the JSON state format, so this is fine.
|
|
|
|
var contentVal map[string]interface{}
|
|
if content != nil {
|
|
err := json.Unmarshal(content, &contentVal)
|
|
if err != nil {
|
|
panic(err) // should never happen because our tests control this input
|
|
}
|
|
}
|
|
c.log = append(c.log, mockClientRequest{method, contentVal})
|
|
}
|