2015-02-21 20:52:55 +01:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
2017-05-25 17:01:25 +02:00
|
|
|
"sync"
|
2015-02-21 20:52:55 +01:00
|
|
|
"testing"
|
|
|
|
|
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
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/states"
|
2019-06-20 02:30:34 +02:00
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
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
|
|
|
"github.com/hashicorp/terraform/version"
|
2015-02-21 20:52:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestState_impl(t *testing.T) {
|
2019-06-20 02:30:34 +02:00
|
|
|
var _ statemgr.Reader = new(State)
|
|
|
|
var _ statemgr.Writer = new(State)
|
|
|
|
var _ statemgr.Persister = new(State)
|
|
|
|
var _ statemgr.Refresher = new(State)
|
|
|
|
var _ statemgr.Locker = new(State)
|
2015-02-21 20:52:55 +01:00
|
|
|
}
|
2017-05-25 17:01:25 +02:00
|
|
|
|
|
|
|
func TestStateRace(t *testing.T) {
|
|
|
|
s := &State{
|
|
|
|
Client: nilClient{},
|
|
|
|
}
|
|
|
|
|
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
|
|
|
current := states.NewState()
|
2017-05-25 17:01:25 +02:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
s.WriteState(current)
|
|
|
|
s.PersistState()
|
|
|
|
s.RefreshState()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
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
|
|
|
|
|
|
|
func TestStatePersist(t *testing.T) {
|
|
|
|
mgr := &State{
|
|
|
|
Client: &mockClient{
|
|
|
|
// Initial state just to give us a fixed starting point for our
|
|
|
|
// test assertions below, or else we'd need to deal with
|
|
|
|
// random lineage.
|
|
|
|
current: []byte(`
|
|
|
|
{
|
|
|
|
"version": 4,
|
|
|
|
"lineage": "mock-lineage",
|
|
|
|
"serial": 1,
|
|
|
|
"terraform_version":"0.0.0",
|
|
|
|
"outputs": {},
|
|
|
|
"resources": []
|
|
|
|
}
|
|
|
|
`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// In normal use (during a Terraform operation) we always refresh and read
|
|
|
|
// before any writes would happen, so we'll mimic that here for realism.
|
|
|
|
if err := mgr.RefreshState(); err != nil {
|
|
|
|
t.Fatalf("failed to RefreshState: %s", err)
|
|
|
|
}
|
|
|
|
s := mgr.State()
|
|
|
|
|
|
|
|
s.RootModule().SetOutputValue("foo", cty.StringVal("bar"), false)
|
|
|
|
if err := mgr.WriteState(s); err != nil {
|
|
|
|
t.Fatalf("failed to WriteState: %s", err)
|
|
|
|
}
|
|
|
|
if err := mgr.PersistState(); err != nil {
|
|
|
|
t.Fatalf("failed to PersistState: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Persisting the same state again should be a no-op: it doesn't fail,
|
|
|
|
// but it ought not to appear in the client's log either.
|
|
|
|
if err := mgr.WriteState(s); err != nil {
|
|
|
|
t.Fatalf("failed to WriteState: %s", err)
|
|
|
|
}
|
|
|
|
if err := mgr.PersistState(); err != nil {
|
|
|
|
t.Fatalf("failed to PersistState: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...but if we _do_ change something in the state then we should see
|
|
|
|
// it re-persist.
|
|
|
|
s.RootModule().SetOutputValue("foo", cty.StringVal("baz"), false)
|
|
|
|
if err := mgr.WriteState(s); err != nil {
|
|
|
|
t.Fatalf("failed to WriteState: %s", err)
|
|
|
|
}
|
|
|
|
if err := mgr.PersistState(); err != nil {
|
|
|
|
t.Fatalf("failed to PersistState: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := mgr.Client.(*mockClient).log
|
|
|
|
want := []mockClientRequest{
|
|
|
|
// The initial fetch from mgr.RefreshState above.
|
|
|
|
{
|
|
|
|
Method: "Get",
|
|
|
|
Content: map[string]interface{}{
|
|
|
|
"version": 4.0, // encoding/json decodes this as float64 by default
|
|
|
|
"lineage": "mock-lineage",
|
|
|
|
"serial": 1.0, // encoding/json decodes this as float64 by default
|
|
|
|
"terraform_version": "0.0.0",
|
|
|
|
"outputs": map[string]interface{}{},
|
|
|
|
"resources": []interface{}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// First call to PersistState, with output "foo" set to "bar".
|
|
|
|
{
|
|
|
|
Method: "Put",
|
|
|
|
Content: map[string]interface{}{
|
|
|
|
"version": 4.0,
|
|
|
|
"lineage": "mock-lineage",
|
|
|
|
"serial": 2.0, // serial increases because the outputs changed
|
|
|
|
"terraform_version": version.Version,
|
|
|
|
"outputs": map[string]interface{}{
|
|
|
|
"foo": map[string]interface{}{
|
|
|
|
"type": "string",
|
|
|
|
"value": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"resources": []interface{}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Second call to PersistState generates no client requests, because
|
|
|
|
// nothing changed in the state itself.
|
|
|
|
|
|
|
|
// Third call to PersistState, with the "foo" output value updated
|
|
|
|
// to "baz".
|
|
|
|
{
|
|
|
|
Method: "Put",
|
|
|
|
Content: map[string]interface{}{
|
|
|
|
"version": 4.0,
|
|
|
|
"lineage": "mock-lineage",
|
|
|
|
"serial": 3.0, // serial increases because the outputs changed
|
|
|
|
"terraform_version": version.Version,
|
|
|
|
"outputs": map[string]interface{}{
|
|
|
|
"foo": map[string]interface{}{
|
|
|
|
"type": "string",
|
|
|
|
"value": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"resources": []interface{}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(want, got); len(diff) > 0 {
|
|
|
|
t.Errorf("incorrect client requests\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|