Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions internal/backend/remote-state/inmem/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,27 @@ func (b *Backend) Configure(configVal cty.Value) tfdiags.Diagnostics {
states.Lock()
defer states.Unlock()

defaultClient := &RemoteClient{
Name: backend.DefaultStateName,
}
// Some tests may configure this backend multiple times
// and expect the same state from memory afterwards.
_, ok := states.m[backend.DefaultStateName]
if !ok {
defaultClient := &RemoteClient{
Name: backend.DefaultStateName,
}

states.m[backend.DefaultStateName] = &remote.State{
Client: defaultClient,
}
states.m[backend.DefaultStateName] = &remote.State{
Client: defaultClient,
}

// set the default client lock info per the test config
if v := configVal.GetAttr("lock_id"); !v.IsNull() {
info := statemgr.NewLockInfo()
info.ID = v.AsString()
info.Operation = "test"
info.Info = "test config"
// set the default client lock info per the test config
if v := configVal.GetAttr("lock_id"); !v.IsNull() {
info := statemgr.NewLockInfo()
info.ID = v.AsString()
info.Operation = "test"
info.Info = "test config"

locks.lock(backend.DefaultStateName, info)
locks.lock(backend.DefaultStateName, info)
}
}

return nil
Expand Down
34 changes: 34 additions & 0 deletions internal/backend/remote-state/inmem/testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package inmem

import (
"testing"

statespkg "github.com/hashicorp/terraform/internal/states"
)

func ReadState(t *testing.T, wsName string) *statespkg.State {
states.Lock()
defer states.Unlock()

stateMgr, ok := states.m[wsName]
if !ok {
t.Fatalf("state not found for workspace %s", wsName)
}

return stateMgr.State()
}

func ReadWorkspaces(t *testing.T) []string {
states.Lock()
defer states.Unlock()

workspaces := make([]string, 0, len(states.m))
for wsName := range states.m {
workspaces = append(workspaces, wsName)
}

return workspaces
}
8 changes: 4 additions & 4 deletions internal/cloud/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func TestCloud_PrepareConfig(t *testing.T) {
}

for name, tc := range cases {
s := testServer(t)
s := TestServer(t)
b := New(testDisco(s))

// Validate
Expand Down Expand Up @@ -794,7 +794,7 @@ func TestCloud_configVerifyMinimumTFEVersion(t *testing.T) {
w.Header().Set("TFP-API-Version", "2.4")
},
}
s := testServerWithHandlers(handlers)
s := TestServerWithHandlers(t, handlers)

b := New(testDisco(s))

Expand Down Expand Up @@ -831,7 +831,7 @@ func TestCloud_configVerifyMinimumTFEVersionInAutomation(t *testing.T) {
w.Header().Set("TFP-API-Version", "2.4")
},
}
s := testServerWithHandlers(handlers)
s := TestServerWithHandlers(t, handlers)

b := New(testDisco(s))
b.runningInAutomation = true
Expand Down Expand Up @@ -1680,7 +1680,7 @@ func TestCloudBackend_DeleteWorkspace_DoesNotExist(t *testing.T) {
}

func TestCloud_ServiceDiscoveryAliases(t *testing.T) {
s := testServer(t)
s := TestServer(t)
b := New(testDisco(s))

diag := b.Configure(cty.ObjectVal(map[string]cty.Value{
Expand Down
21 changes: 13 additions & 8 deletions internal/cloud/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ func testBackendWithOutputs(t *testing.T) (*Cloud, func()) {
func testBackend(t *testing.T, obj cty.Value, handlers map[string]func(http.ResponseWriter, *http.Request)) (*Cloud, *MockClient, func()) {
var s *httptest.Server
if handlers != nil {
s = testServerWithHandlers(handlers)
s = TestServerWithHandlers(t, handlers)
} else {
s = testServer(t)
s = TestServer(t)
}
b := New(testDisco(s))

Expand Down Expand Up @@ -324,7 +324,7 @@ func testBackend(t *testing.T, obj cty.Value, handlers map[string]func(http.Resp
// testUnconfiguredBackend is used for testing the configuration of the backend
// with the mock client
func testUnconfiguredBackend(t *testing.T) (*Cloud, func()) {
s := testServer(t)
s := TestServer(t)
b := New(testDisco(s))

// Normally, the client is created during configuration, but the configuration uses the
Expand Down Expand Up @@ -395,15 +395,15 @@ func testLocalBackend(t *testing.T, cloud *Cloud) backendrun.OperationsBackend {
return b
}

// testServer returns a started *httptest.Server used for local testing with the default set of
// TestServer returns a started *httptest.Server used for local testing with the default set of
// request handlers.
func testServer(t *testing.T) *httptest.Server {
return testServerWithHandlers(testDefaultRequestHandlers)
func TestServer(t *testing.T) *httptest.Server {
return TestServerWithHandlers(t, testDefaultRequestHandlers)
}

// testServerWithHandlers returns a started *httptest.Server with the given set of request handlers
// TestServerWithHandlers returns a started *httptest.Server with the given set of request handlers
// overriding any default request handlers (testDefaultRequestHandlers).
func testServerWithHandlers(handlers map[string]func(http.ResponseWriter, *http.Request)) *httptest.Server {
func TestServerWithHandlers(t *testing.T, handlers map[string]func(http.ResponseWriter, *http.Request)) *httptest.Server {
mux := http.NewServeMux()
for route, handler := range handlers {
mux.HandleFunc(route, handler)
Expand All @@ -414,6 +414,11 @@ func testServerWithHandlers(handlers map[string]func(http.ResponseWriter, *http.
}
}

mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
t.Logf("unexpected %s request received for %q", req.Method, req.URL.String())
w.WriteHeader(http.StatusBadRequest)
})

return httptest.NewServer(mux)
}

Expand Down
Loading