2015-02-22 00:13:28 +01:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
state/remote: Don't persist snapshot for unchanged state
Previously we would write to the backend for every call to PersistState,
even if nothing changed since the last write, but update the serial only
if the state had changed.
The Terraform Cloud & Enterprise state storage have a simple safety check
that any future write with an already-used lineage and serial must be
byte-for-byte identical. StatesMarshalEqual is intended to detect that,
but it only actually detects changes the state itself, and not changes
to the snapshot metadata.
Because we write the current Terraform version into the snapshot metadata
during serialization, we'd previously have an issue where if the first
state write after upgrading Terraform to a new version happened to change
nothing about the state content then we'd write a new snapshot that
differed only by Terraform version, and Terraform Cloud/Enterprise would
then reject it.
The snapshot header is discarded immediately after decoding, so we can't
use information from it when deciding whether to increment the serial.
The next best thing is to skip sending no-op snapshot updates to the state
client in the first place.
These writes are unnecessary anyway, and state storage owners have asked
us in the past to elide these to avoid generating noise in their version
logs, so we'll also finally meet those requests as a nice side-effect of
this change.
We didn't previously have tests for the full flow of retrieving and then
successively updating persisted state snapshots, so this includes a test
which covers that logic and includes an assertion that a no-op update does
not get written to the state client.
2019-06-20 02:42:09 +02:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/json"
|
2015-02-22 00:13:28 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-07-02 00:37:38 +02:00
|
|
|
func TestRemoteClient_noPayload(t *testing.T) {
|
|
|
|
s := &State{
|
|
|
|
Client: nilClient{},
|
|
|
|
}
|
2016-07-06 16:48:52 +02:00
|
|
|
if err := s.RefreshState(); err != nil {
|
|
|
|
t.Fatal("error refreshing empty remote state")
|
2016-07-02 00:37:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }
|
state/remote: Don't persist snapshot for unchanged state
Previously we would write to the backend for every call to PersistState,
even if nothing changed since the last write, but update the serial only
if the state had changed.
The Terraform Cloud & Enterprise state storage have a simple safety check
that any future write with an already-used lineage and serial must be
byte-for-byte identical. StatesMarshalEqual is intended to detect that,
but it only actually detects changes the state itself, and not changes
to the snapshot metadata.
Because we write the current Terraform version into the snapshot metadata
during serialization, we'd previously have an issue where if the first
state write after upgrading Terraform to a new version happened to change
nothing about the state content then we'd write a new snapshot that
differed only by Terraform version, and Terraform Cloud/Enterprise would
then reject it.
The snapshot header is discarded immediately after decoding, so we can't
use information from it when deciding whether to increment the serial.
The next best thing is to skip sending no-op snapshot updates to the state
client in the first place.
These writes are unnecessary anyway, and state storage owners have asked
us in the past to elide these to avoid generating noise in their version
logs, so we'll also finally meet those requests as a nice side-effect of
this change.
We didn't previously have tests for the full flow of retrieving and then
successively updating persisted state snapshots, so this includes a test
which covers that logic and includes an assertion that a no-op update does
not get written to the state client.
2019-06-20 02:42:09 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-09-09 21:25:23 +02:00
|
|
|
c.appendLog("Put", data)
|
state/remote: Don't persist snapshot for unchanged state
Previously we would write to the backend for every call to PersistState,
even if nothing changed since the last write, but update the serial only
if the state had changed.
The Terraform Cloud & Enterprise state storage have a simple safety check
that any future write with an already-used lineage and serial must be
byte-for-byte identical. StatesMarshalEqual is intended to detect that,
but it only actually detects changes the state itself, and not changes
to the snapshot metadata.
Because we write the current Terraform version into the snapshot metadata
during serialization, we'd previously have an issue where if the first
state write after upgrading Terraform to a new version happened to change
nothing about the state content then we'd write a new snapshot that
differed only by Terraform version, and Terraform Cloud/Enterprise would
then reject it.
The snapshot header is discarded immediately after decoding, so we can't
use information from it when deciding whether to increment the serial.
The next best thing is to skip sending no-op snapshot updates to the state
client in the first place.
These writes are unnecessary anyway, and state storage owners have asked
us in the past to elide these to avoid generating noise in their version
logs, so we'll also finally meet those requests as a nice side-effect of
this change.
We didn't previously have tests for the full flow of retrieving and then
successively updating persisted state snapshots, so this includes a test
which covers that logic and includes an assertion that a no-op update does
not get written to the state client.
2019-06-20 02:42:09 +02:00
|
|
|
c.current = data
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockClient) Delete() error {
|
|
|
|
c.appendLog("Delete", c.current)
|
|
|
|
c.current = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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})
|
|
|
|
}
|
2020-09-09 21:25:23 +02:00
|
|
|
|
|
|
|
// mockClientForcePusher is like mockClient, but also implements
|
|
|
|
// EnableForcePush, allowing testing for this behavior
|
|
|
|
type mockClientForcePusher struct {
|
|
|
|
current []byte
|
|
|
|
force bool
|
|
|
|
log []mockClientRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockClientForcePusher) 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 *mockClientForcePusher) Put(data []byte) error {
|
|
|
|
if c.force {
|
|
|
|
c.appendLog("Force Put", data)
|
|
|
|
} else {
|
|
|
|
c.appendLog("Put", data)
|
|
|
|
}
|
|
|
|
c.current = data
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements remote.ClientForcePusher
|
|
|
|
func (c *mockClientForcePusher) EnableForcePush() {
|
|
|
|
c.force = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockClientForcePusher) Delete() error {
|
|
|
|
c.appendLog("Delete", c.current)
|
|
|
|
c.current = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (c *mockClientForcePusher) appendLog(method string, content []byte) {
|
|
|
|
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})
|
|
|
|
}
|