Skip to content

Commit

Permalink
Remove existing tests for remote state persist
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Trout committed May 5, 2020
1 parent 9eb9080 commit 7fa1016
Showing 1 changed file with 1 addition and 171 deletions.
172 changes: 1 addition & 171 deletions state/remote/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (tc testCase) isRequested(t *testing.T) bool {
return !tc.noRequest
}

func TestStatePersistNew(t *testing.T) {
func TestStatePersist(t *testing.T) {
testCases := []testCase{
// Refreshing state before we run the test loop causes a GET
testCase{
Expand Down Expand Up @@ -241,173 +241,3 @@ func TestStatePersistNew(t *testing.T) {
cleanup()
}
}

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)
}

// We also don't persist state if the lineage or the serial change
originalSerial := mgr.serial
mgr.serial++
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)
}
mgr.serial = originalSerial

originalLineage := mgr.lineage
mgr.lineage = "behold-a-wild-lineage-appears"
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)
}
mgr.lineage = originalLineage

// ...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 serial changed
{
Method: "Put",
Content: map[string]interface{}{
"version": 4.0,
"lineage": "mock-lineage",
"serial": 4.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{}{},
},
},

// Fourth call to PersistState, with the lineage changed
{
Method: "Put",
Content: map[string]interface{}{
"version": 4.0,
"lineage": "behold-a-wild-lineage-appears",
"serial": 3.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{}{},
},
},

// Fifth call to PersistState, with the "foo" output value updated
// to "baz".
{
Method: "Put",
Content: map[string]interface{}{
"version": 4.0,
"lineage": "mock-lineage",
"serial": 4.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)
}
}

0 comments on commit 7fa1016

Please sign in to comment.