From 5deef9621ddd7c2fa8a9203c6fcece5608924bbd Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 1 Aug 2017 16:15:52 -0400 Subject: [PATCH] export Reset() This package is used for testing, so there needs to be an easy method for reinitializing the stored data between tests. --- backend/remote-state/inmem/backend.go | 22 ++++++++++++++++++---- backend/remote-state/inmem/backend_test.go | 18 +++--------------- backend/remote-state/inmem/client_test.go | 4 ++-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/backend/remote-state/inmem/backend.go b/backend/remote-state/inmem/backend.go index 8d434b5fb..3e66d1274 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 4398591bd..1f4ab1138 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 c040a6e72..3a0fa8f30 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)