mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
test(cloud): ensure state version is created when saving state
This commit is contained in:
parent
d72911a640
commit
01d510f3cb
@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
tfe "github.com/hashicorp/go-tfe"
|
||||
"github.com/hashicorp/terraform/internal/states"
|
||||
"github.com/hashicorp/terraform/internal/states/remote"
|
||||
"github.com/hashicorp/terraform/internal/states/statefile"
|
||||
@ -19,7 +20,50 @@ func TestRemoteClient(t *testing.T) {
|
||||
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)
|
||||
defer bCleanup()
|
||||
|
||||
|
@ -923,6 +923,7 @@ type MockStateVersions struct {
|
||||
states map[string][]byte
|
||||
stateVersions map[string]*tfe.StateVersion
|
||||
workspaces map[string][]string
|
||||
outputStates map[string][]byte
|
||||
}
|
||||
|
||||
func newMockStateVersions(client *MockClient) *MockStateVersions {
|
||||
@ -931,6 +932,7 @@ func newMockStateVersions(client *MockClient) *MockStateVersions {
|
||||
states: make(map[string][]byte),
|
||||
stateVersions: make(map[string]*tfe.StateVersion),
|
||||
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.outputStates[sv.ID] = []byte(*options.JSONStateOutputs)
|
||||
m.stateVersions[sv.ID] = sv
|
||||
m.workspaces[workspaceID] = append(m.workspaces[workspaceID], sv.ID)
|
||||
|
||||
|
@ -174,7 +174,8 @@ func (jsonstate *state) marshalStateValues(s *states.State, schemas *terraform.S
|
||||
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) {
|
||||
if outputs == nil {
|
||||
return nil, nil
|
||||
|
Loading…
Reference in New Issue
Block a user