export Reset()

This package is used for testing, so there needs to be an easy method
for reinitializing the stored data between tests.
This commit is contained in:
James Bardin 2017-08-01 16:15:52 -04:00
parent ac60ddcd40
commit 5deef9621d
3 changed files with 23 additions and 21 deletions

View File

@ -17,13 +17,27 @@ 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{
var (
states stateMap
locks lockMap
)
func init() {
Reset()
}
// 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{},
}
var locks = lockMap{
locks = lockMap{
m: map[string]*state.LockInfo{},
}
}
// New creates a new backend for Inmem remote state.
func New() backend.Backend {

View File

@ -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)

View File

@ -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)