From 01d510f3cbb7a2e6736d64de11c54ca52072cf28 Mon Sep 17 00:00:00 2001 From: Brandon Croft Date: Fri, 22 Jul 2022 11:25:22 -0600 Subject: [PATCH] test(cloud): ensure state version is created when saving state --- internal/cloud/backend_state_test.go | 46 +++++++++++++++++++++++++++- internal/cloud/tfe_client_mock.go | 3 ++ internal/command/jsonstate/state.go | 3 +- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/internal/cloud/backend_state_test.go b/internal/cloud/backend_state_test.go index 63c970438a..f62ef28469 100644 --- a/internal/cloud/backend_state_test.go +++ b/internal/cloud/backend_state_test.go @@ -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() diff --git a/internal/cloud/tfe_client_mock.go b/internal/cloud/tfe_client_mock.go index 9888599fae..f6f026b62b 100644 --- a/internal/cloud/tfe_client_mock.go +++ b/internal/cloud/tfe_client_mock.go @@ -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) diff --git a/internal/command/jsonstate/state.go b/internal/command/jsonstate/state.go index 5b91c9c148..20491fdbce 100644 --- a/internal/command/jsonstate/state.go +++ b/internal/command/jsonstate/state.go @@ -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