Skip to content

Commit

Permalink
Stub in state tests with loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Trout committed May 4, 2020
1 parent 80aaa94 commit 5f06a69
Showing 1 changed file with 143 additions and 0 deletions.
143 changes: 143 additions & 0 deletions state/remote/state_test.go
Expand Up @@ -41,6 +41,149 @@ func TestStateRace(t *testing.T) {
wg.Wait()
}

func TestStatePersistNew(t *testing.T) {
type testCase struct {
name string
// A function to mutate state and return a cleanup function
mutationFunc func(*State) func()
// The expected request to have taken place
expectedRequest mockClientRequest
}

testCases := []testCase{
// Refreshing state before we run the test loop causes a GET
testCase{
name: "refresh state",
mutationFunc: func(mgr *State) func() {
return func() {}
},
expectedRequest: mockClientRequest{
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{}{},
},
},
},
testCase{
name: "change lineage",
mutationFunc: func(mgr *State) func() {
originalLineage := mgr.lineage
mgr.lineage = "some-new-lineage"
return func() {
mgr.lineage = originalLineage
}
},
expectedRequest: mockClientRequest{
Method: "Put",
Content: map[string]interface{}{
"version": 4.0, // encoding/json decodes this as float64 by default
"lineage": "some-new-lineage",
"serial": 2.0, // encoding/json decodes this as float64 by default
"terraform_version": version.Version,
"outputs": map[string]interface{}{},
"resources": []interface{}{},
},
},
},
testCase{
name: "change serial",
mutationFunc: func(mgr *State) func() {
originalSerial := mgr.serial
mgr.serial++
return func() {
mgr.serial = originalSerial
}
},
expectedRequest: mockClientRequest{
Method: "Put",
Content: map[string]interface{}{
"version": 4.0, // encoding/json decodes this as float64 by default
"lineage": "mock-lineage",
"serial": 4.0, // encoding/json decodes this as float64 by default
"terraform_version": version.Version,
"outputs": map[string]interface{}{},
"resources": []interface{}{},
},
},
},
testCase{
name: "change a state value",
mutationFunc: func(mgr *State) func() {
mgr.State().RootModule().SetOutputValue("foo", cty.StringVal("bar"), false)
return func() {}
},
expectedRequest: mockClientRequest{
Method: "Put",
Content: map[string]interface{}{
"version": 4.0, // encoding/json decodes this as float64 by default
"lineage": "mock-lineage",
"serial": 3.0, // encoding/json decodes this as float64 by default
"terraform_version": version.Version,
"outputs": map[string]interface{}{
"foo": map[string]interface{}{
"type": "string",
"value": "baz",
},
},
"resources": []interface{}{},
},
},
},
}

// Initial setup of state just to give us a fixed starting point for our
// test assertions below, or else we'd need to deal with
// random lineage.
mgr := &State{
Client: &mockClient{
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.
// NB This causes a GET to be logged so the first item in the test cases
// must account for this
if err := mgr.RefreshState(); err != nil {
t.Fatalf("failed to RefreshState: %s", err)
}

// Run tests in order.
for i, tc := range testCases {
cleanup := tc.mutationFunc(mgr)

s := mgr.State()
if err := mgr.WriteState(s); err != nil {
t.Fatalf("failed to WriteState for %s: %s", tc.name, err)
}
if err := mgr.PersistState(); err != nil {
t.Fatalf("failed to PersistState for %s: %s", tc.name, err)
}

// Get captured request from the mock client log
// based on the index of the current test
loggedRequest := mgr.Client.(*mockClient).log[i]
if diff := cmp.Diff(tc.expectedRequest, loggedRequest); len(diff) > 0 {
t.Fatalf("incorrect client requests for %s:\n%s", tc.name, diff)
}
cleanup()
}
}

func TestStatePersist(t *testing.T) {
mgr := &State{
Client: &mockClient{
Expand Down

0 comments on commit 5f06a69

Please sign in to comment.