diff --git a/backend/remote-state/inmem/backend.go b/backend/remote-state/inmem/backend.go index 8d434b5fb6..3e66d12749 100644 --- a/backend/remote-state/inmem/backend.go +++ b/backend/remote-state/inmem/backend.go @@ -17,12 +17,26 @@ import ( // we keep the states and locks in package-level variables, so that they can be // accessed from multiple instances of the backend. This better emulates // backend instances accessing a single remote data store. -var states = stateMap{ - m: map[string]*remote.State{}, +var ( + states stateMap + locks lockMap +) + +func init() { + Reset() } -var locks = lockMap{ - m: map[string]*state.LockInfo{}, +// Reset clears out all existing state and lock data. +// This is used to initialize the package during init, as well as between +// tests. +func Reset() { + states = stateMap{ + m: map[string]*remote.State{}, + } + + locks = lockMap{ + m: map[string]*state.LockInfo{}, + } } // New creates a new backend for Inmem remote state. diff --git a/backend/remote-state/inmem/backend_test.go b/backend/remote-state/inmem/backend_test.go index 4398591bdf..1f4ab11386 100644 --- a/backend/remote-state/inmem/backend_test.go +++ b/backend/remote-state/inmem/backend_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/hashicorp/terraform/backend" - "github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/state/remote" ) @@ -12,19 +11,8 @@ func TestBackend_impl(t *testing.T) { var _ backend.Backend = new(Backend) } -// reset the states and locks between tests -func reset() { - states = stateMap{ - m: map[string]*remote.State{}, - } - - locks = lockMap{ - m: map[string]*state.LockInfo{}, - } -} - func TestBackendConfig(t *testing.T) { - defer reset() + defer Reset() testID := "test_lock_id" config := map[string]interface{}{ @@ -49,13 +37,13 @@ func TestBackendConfig(t *testing.T) { } func TestBackend(t *testing.T) { - defer reset() + defer Reset() b := backend.TestBackendConfig(t, New(), nil).(*Backend) backend.TestBackend(t, b, nil) } func TestBackendLocked(t *testing.T) { - defer reset() + defer Reset() b1 := backend.TestBackendConfig(t, New(), nil).(*Backend) b2 := backend.TestBackendConfig(t, New(), nil).(*Backend) diff --git a/backend/remote-state/inmem/client_test.go b/backend/remote-state/inmem/client_test.go index c040a6e72f..3a0fa8f30b 100644 --- a/backend/remote-state/inmem/client_test.go +++ b/backend/remote-state/inmem/client_test.go @@ -13,7 +13,7 @@ func TestRemoteClient_impl(t *testing.T) { } func TestRemoteClient(t *testing.T) { - defer reset() + defer Reset() b := backend.TestBackendConfig(t, New(), nil) s, err := b.State(backend.DefaultStateName) @@ -25,7 +25,7 @@ func TestRemoteClient(t *testing.T) { } func TestInmemLocks(t *testing.T) { - defer reset() + defer Reset() s, err := backend.TestBackendConfig(t, New(), nil).State(backend.DefaultStateName) if err != nil { t.Fatal(err)