test(cloud): ensure state version is created when saving state

This commit is contained in:
Brandon Croft 2022-07-22 11:25:22 -06:00
parent d72911a640
commit 01d510f3cb
No known key found for this signature in database
GPG Key ID: B01E32423322EB9D
3 changed files with 50 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"testing" "testing"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform/internal/states" "github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/states/remote" "github.com/hashicorp/terraform/internal/states/remote"
"github.com/hashicorp/terraform/internal/states/statefile" "github.com/hashicorp/terraform/internal/states/statefile"
@ -19,7 +20,50 @@ func TestRemoteClient(t *testing.T) {
remote.TestClient(t, client) remote.TestClient(t, client)
} }
func TestRemoteClient_stateLock(t *testing.T) { func TestRemoteClient_stateVersionCreated(t *testing.T) {
b, bCleanup := testBackendWithName(t)
defer bCleanup()
raw, err := b.StateMgr(testBackendSingleWorkspaceName)
if err != nil {
t.Fatalf("error: %v", err)
}
client := raw.(*remote.State).Client
err = client.Put(([]byte)(`
{
"version": 4,
"terraform_version": "1.3.0",
"serial": 1,
"lineage": "backend-change",
"outputs": {
"foo": {
"type": "string",
"value": "bar"
}
}
}`))
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
stateVersionsAPI := b.client.StateVersions.(*MockStateVersions)
if got, want := len(stateVersionsAPI.stateVersions), 1; got != want {
t.Fatalf("wrong number of state versions in the mock client %d; want %d", got, want)
}
var stateVersion *tfe.StateVersion
for _, sv := range stateVersionsAPI.stateVersions {
stateVersion = sv
}
if stateVersionsAPI.outputStates[stateVersion.ID] == nil || len(stateVersionsAPI.outputStates[stateVersion.ID]) == 0 {
t.Fatal("no state version outputs in the mock client")
}
}
func TestRemoteClient_TestRemoteLocks(t *testing.T) {
b, bCleanup := testBackendWithName(t) b, bCleanup := testBackendWithName(t)
defer bCleanup() defer bCleanup()

View File

@ -923,6 +923,7 @@ type MockStateVersions struct {
states map[string][]byte states map[string][]byte
stateVersions map[string]*tfe.StateVersion stateVersions map[string]*tfe.StateVersion
workspaces map[string][]string workspaces map[string][]string
outputStates map[string][]byte
} }
func newMockStateVersions(client *MockClient) *MockStateVersions { func newMockStateVersions(client *MockClient) *MockStateVersions {
@ -931,6 +932,7 @@ func newMockStateVersions(client *MockClient) *MockStateVersions {
states: make(map[string][]byte), states: make(map[string][]byte),
stateVersions: make(map[string]*tfe.StateVersion), stateVersions: make(map[string]*tfe.StateVersion),
workspaces: make(map[string][]string), workspaces: make(map[string][]string),
outputStates: make(map[string][]byte),
} }
} }
@ -972,6 +974,7 @@ func (m *MockStateVersions) Create(ctx context.Context, workspaceID string, opti
} }
m.states[sv.DownloadURL] = state m.states[sv.DownloadURL] = state
m.outputStates[sv.ID] = []byte(*options.JSONStateOutputs)
m.stateVersions[sv.ID] = sv m.stateVersions[sv.ID] = sv
m.workspaces[workspaceID] = append(m.workspaces[workspaceID], sv.ID) m.workspaces[workspaceID] = append(m.workspaces[workspaceID], sv.ID)

View File

@ -174,7 +174,8 @@ func (jsonstate *state) marshalStateValues(s *states.State, schemas *terraform.S
return nil return nil
} }
// MarshalOutputs returns the json representation of the state output values // MarshalOutputs translates a map of states.OutputValue to a map of jsonstate.output,
// which are defined for json encoding.
func MarshalOutputs(outputs map[string]*states.OutputValue) (map[string]output, error) { func MarshalOutputs(outputs map[string]*states.OutputValue) (map[string]output, error) {
if outputs == nil { if outputs == nil {
return nil, nil return nil, nil