2017-01-19 05:47:18 +01:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
2017-03-02 07:58:51 +01:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2017-01-19 05:47:18 +01:00
|
|
|
"testing"
|
|
|
|
|
2018-02-21 02:32:07 +01:00
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
2017-01-19 05:47:18 +01:00
|
|
|
"github.com/hashicorp/terraform/config"
|
2017-03-15 01:28:42 +01:00
|
|
|
"github.com/hashicorp/terraform/state"
|
2017-01-19 05:47:18 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestBackendConfig validates and configures the backend with the
|
|
|
|
// given configuration.
|
|
|
|
func TestBackendConfig(t *testing.T, b Backend, c map[string]interface{}) Backend {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2017-01-19 05:47:18 +01:00
|
|
|
// Get the proper config structure
|
|
|
|
rc, err := config.NewRawConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
conf := terraform.NewResourceConfig(rc)
|
|
|
|
|
|
|
|
// Validate
|
|
|
|
warns, errs := b.Validate(conf)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("warnings: %s", warns)
|
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("errors: %s", errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure
|
|
|
|
if err := b.Configure(conf); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
2017-03-02 07:58:51 +01:00
|
|
|
|
|
|
|
// TestBackend will test the functionality of a Backend. The backend is
|
|
|
|
// assumed to already be configured. This will test state functionality.
|
|
|
|
// If the backend reports it doesn't support multi-state by returning the
|
|
|
|
// error ErrNamedStatesNotSupported, then it will not test that.
|
2018-02-21 02:32:07 +01:00
|
|
|
func TestBackendStates(t *testing.T, b Backend) {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2017-03-02 07:58:51 +01:00
|
|
|
states, err := b.States()
|
|
|
|
if err == ErrNamedStatesNotSupported {
|
|
|
|
t.Logf("TestBackend: named states not supported in %T, skipping", b)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test it starts with only the default
|
|
|
|
if len(states) != 1 || states[0] != DefaultStateName {
|
|
|
|
t.Fatalf("should only have default to start: %#v", states)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a couple states
|
2017-03-23 14:47:36 +01:00
|
|
|
foo, err := b.State("foo")
|
2017-03-02 07:58:51 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
2017-03-23 14:47:36 +01:00
|
|
|
if err := foo.RefreshState(); err != nil {
|
2017-03-02 07:58:51 +01:00
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
2017-03-23 14:47:36 +01:00
|
|
|
if v := foo.State(); v.HasResources() {
|
2017-03-02 07:58:51 +01:00
|
|
|
t.Fatalf("should be empty: %s", v)
|
|
|
|
}
|
|
|
|
|
2017-03-23 14:47:36 +01:00
|
|
|
bar, err := b.State("bar")
|
2017-03-02 07:58:51 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
2017-03-23 14:47:36 +01:00
|
|
|
if err := bar.RefreshState(); err != nil {
|
2017-03-02 07:58:51 +01:00
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
2017-03-23 14:47:36 +01:00
|
|
|
if v := bar.State(); v.HasResources() {
|
2017-03-02 07:58:51 +01:00
|
|
|
t.Fatalf("should be empty: %s", v)
|
|
|
|
}
|
|
|
|
|
2017-03-23 14:47:36 +01:00
|
|
|
// Verify they are distinct states that can be read back from storage
|
2017-03-02 07:58:51 +01:00
|
|
|
{
|
2017-03-23 14:47:36 +01:00
|
|
|
// start with a fresh state, and record the lineage being
|
|
|
|
// written to "bar"
|
|
|
|
barState := terraform.NewState()
|
2017-07-14 19:50:26 +02:00
|
|
|
|
|
|
|
// creating the named state may have created a lineage, so use that if it exists.
|
|
|
|
if s := bar.State(); s != nil && s.Lineage != "" {
|
|
|
|
barState.Lineage = s.Lineage
|
|
|
|
}
|
2017-03-23 14:47:36 +01:00
|
|
|
barLineage := barState.Lineage
|
|
|
|
|
|
|
|
// the foo lineage should be distinct from bar, and unchanged after
|
|
|
|
// modifying bar
|
|
|
|
fooState := terraform.NewState()
|
2017-07-14 19:50:26 +02:00
|
|
|
// creating the named state may have created a lineage, so use that if it exists.
|
|
|
|
if s := foo.State(); s != nil && s.Lineage != "" {
|
|
|
|
fooState.Lineage = s.Lineage
|
|
|
|
}
|
2017-03-23 14:47:36 +01:00
|
|
|
fooLineage := fooState.Lineage
|
|
|
|
|
|
|
|
// write a known state to foo
|
|
|
|
if err := foo.WriteState(fooState); err != nil {
|
|
|
|
t.Fatal("error writing foo state:", err)
|
|
|
|
}
|
|
|
|
if err := foo.PersistState(); err != nil {
|
|
|
|
t.Fatal("error persisting foo state:", err)
|
2017-03-09 11:47:21 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 14:47:36 +01:00
|
|
|
// write a distinct known state to bar
|
|
|
|
if err := bar.WriteState(barState); err != nil {
|
2017-03-02 07:58:51 +01:00
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
2017-03-23 14:47:36 +01:00
|
|
|
if err := bar.PersistState(); err != nil {
|
2017-03-02 07:58:51 +01:00
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-03-23 14:47:36 +01:00
|
|
|
// verify that foo is unchanged with the existing state manager
|
|
|
|
if err := foo.RefreshState(); err != nil {
|
|
|
|
t.Fatal("error refreshing foo:", err)
|
|
|
|
}
|
|
|
|
fooState = foo.State()
|
|
|
|
switch {
|
|
|
|
case fooState == nil:
|
|
|
|
t.Fatal("nil state read from foo")
|
|
|
|
case fooState.Lineage == barLineage:
|
|
|
|
t.Fatalf("bar lineage read from foo: %#v", fooState)
|
|
|
|
case fooState.Lineage != fooLineage:
|
|
|
|
t.Fatal("foo lineage alterred")
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch foo again from the backend
|
|
|
|
foo, err = b.State("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("error re-fetching state:", err)
|
|
|
|
}
|
|
|
|
if err := foo.RefreshState(); err != nil {
|
|
|
|
t.Fatal("error refreshing foo:", err)
|
2017-03-02 07:58:51 +01:00
|
|
|
}
|
2017-03-23 14:47:36 +01:00
|
|
|
fooState = foo.State()
|
|
|
|
switch {
|
|
|
|
case fooState == nil:
|
|
|
|
t.Fatal("nil state read from foo")
|
|
|
|
case fooState.Lineage != fooLineage:
|
|
|
|
t.Fatal("incorrect state returned from backend")
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the bar again from the backend
|
|
|
|
bar, err = b.State("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("error re-fetching state:", err)
|
|
|
|
}
|
|
|
|
if err := bar.RefreshState(); err != nil {
|
|
|
|
t.Fatal("error refreshing bar:", err)
|
|
|
|
}
|
|
|
|
barState = bar.State()
|
|
|
|
switch {
|
|
|
|
case barState == nil:
|
|
|
|
t.Fatal("nil state read from bar")
|
|
|
|
case barState.Lineage != barLineage:
|
|
|
|
t.Fatal("incorrect state returned from backend")
|
2017-03-02 07:58:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we can now list them
|
|
|
|
{
|
2017-03-23 14:47:36 +01:00
|
|
|
// we determined that named stated are supported earlier
|
2017-03-02 07:58:51 +01:00
|
|
|
states, err := b.States()
|
2017-03-23 14:47:36 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2017-03-02 07:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(states)
|
|
|
|
expected := []string{"bar", "default", "foo"}
|
|
|
|
if !reflect.DeepEqual(states, expected) {
|
|
|
|
t.Fatalf("bad: %#v", states)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete some states
|
|
|
|
if err := b.DeleteState("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the default state can't be deleted
|
|
|
|
if err := b.DeleteState(DefaultStateName); err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
2017-06-23 16:13:03 +02:00
|
|
|
// Create and delete the foo state again.
|
|
|
|
// Make sure that there are no leftover artifacts from a deleted state
|
|
|
|
// preventing re-creation.
|
|
|
|
foo, err = b.State("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
|
|
|
if err := foo.RefreshState(); err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
if v := foo.State(); v.HasResources() {
|
|
|
|
t.Fatalf("should be empty: %s", v)
|
|
|
|
}
|
|
|
|
// and delete it again
|
|
|
|
if err := b.DeleteState("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-03-02 07:58:51 +01:00
|
|
|
// Verify deletion
|
|
|
|
{
|
|
|
|
states, err := b.States()
|
|
|
|
if err == ErrNamedStatesNotSupported {
|
|
|
|
t.Logf("TestBackend: named states not supported in %T, skipping", b)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(states)
|
|
|
|
expected := []string{"bar", "default"}
|
|
|
|
if !reflect.DeepEqual(states, expected) {
|
|
|
|
t.Fatalf("bad: %#v", states)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-15 01:28:42 +01:00
|
|
|
|
2018-02-21 02:32:07 +01:00
|
|
|
// TestBackendStateLocks will test the locking functionality of the remote
|
|
|
|
// state backend.
|
|
|
|
func TestBackendStateLocks(t *testing.T, b1, b2 Backend) {
|
|
|
|
t.Helper()
|
|
|
|
testLocks(t, b1, b2, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestBackendStateForceUnlock verifies that the lock error is the expected
|
|
|
|
// type, and the lock can be unlocked using the ID reported in the error.
|
|
|
|
// Remote state backends that support -force-unlock should call this in at
|
|
|
|
// least one of the acceptance tests.
|
|
|
|
func TestBackendStateForceUnlock(t *testing.T, b1, b2 Backend) {
|
|
|
|
t.Helper()
|
|
|
|
testLocks(t, b1, b2, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLocks(t *testing.T, b1, b2 Backend, testForceUnlock bool) {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2017-03-15 01:28:42 +01:00
|
|
|
// Get the default state for each
|
|
|
|
b1StateMgr, err := b1.State(DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
|
|
|
if err := b1StateMgr.RefreshState(); err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fast exit if this doesn't support locking at all
|
|
|
|
if _, ok := b1StateMgr.(state.Locker); !ok {
|
|
|
|
t.Logf("TestBackend: backend %T doesn't support state locking, not testing", b1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("TestBackend: testing state locking for %T", b1)
|
|
|
|
|
|
|
|
b2StateMgr, err := b2.State(DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
|
|
|
if err := b2StateMgr.RefreshState(); err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reassign so its obvious whats happening
|
|
|
|
lockerA := b1StateMgr.(state.Locker)
|
|
|
|
lockerB := b2StateMgr.(state.Locker)
|
|
|
|
|
|
|
|
infoA := state.NewLockInfo()
|
|
|
|
infoA.Operation = "test"
|
|
|
|
infoA.Who = "clientA"
|
|
|
|
|
|
|
|
infoB := state.NewLockInfo()
|
|
|
|
infoB.Operation = "test"
|
|
|
|
infoB.Who = "clientB"
|
|
|
|
|
|
|
|
lockIDA, err := lockerA.Lock(infoA)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unable to get initial lock:", err)
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:15:23 +01:00
|
|
|
// Make sure we can still get the state.State from another instance even
|
|
|
|
// when locked. This should only happen when a state is loaded via the
|
|
|
|
// backend, and as a remote state.
|
|
|
|
_, err = b2.State(DefaultStateName)
|
|
|
|
if err != nil {
|
2018-02-21 02:32:07 +01:00
|
|
|
t.Errorf("failed to read locked state from another backend instance: %s", err)
|
2017-12-06 23:15:23 +01:00
|
|
|
}
|
|
|
|
|
2017-03-15 01:28:42 +01:00
|
|
|
// If the lock ID is blank, assume locking is disabled
|
|
|
|
if lockIDA == "" {
|
|
|
|
t.Logf("TestBackend: %T: empty string returned for lock, assuming disabled", b1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = lockerB.Lock(infoB)
|
|
|
|
if err == nil {
|
|
|
|
lockerA.Unlock(lockIDA)
|
|
|
|
t.Fatal("client B obtained lock while held by client A")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := lockerA.Unlock(lockIDA); err != nil {
|
|
|
|
t.Fatal("error unlocking client A", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lockIDB, err := lockerB.Lock(infoB)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unable to obtain lock from client B")
|
|
|
|
}
|
|
|
|
|
|
|
|
if lockIDB == lockIDA {
|
2018-02-21 02:32:07 +01:00
|
|
|
t.Errorf("duplicate lock IDs: %q", lockIDB)
|
2017-03-15 01:28:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = lockerB.Unlock(lockIDB); err != nil {
|
|
|
|
t.Fatal("error unlocking client B:", err)
|
|
|
|
}
|
|
|
|
|
2018-02-21 02:32:07 +01:00
|
|
|
// test the equivalent of -force-unlock, by using the id from the error
|
|
|
|
// output.
|
|
|
|
if !testForceUnlock {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a new ID
|
|
|
|
infoA.ID, err = uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lockIDA, err = lockerA.Lock(infoA)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unable to get re lock A:", err)
|
|
|
|
}
|
|
|
|
unlock := func() {
|
|
|
|
err := lockerA.Unlock(lockIDA)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = lockerB.Lock(infoB)
|
|
|
|
if err == nil {
|
|
|
|
unlock()
|
|
|
|
t.Fatal("client B obtained lock while held by client A")
|
|
|
|
}
|
|
|
|
|
|
|
|
infoErr, ok := err.(*state.LockError)
|
|
|
|
if !ok {
|
|
|
|
unlock()
|
|
|
|
t.Fatalf("expected type *state.LockError, got : %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to unlock with the second unlocker, using the ID from the error
|
|
|
|
if err := lockerB.Unlock(infoErr.Info.ID); err != nil {
|
|
|
|
unlock()
|
|
|
|
t.Fatalf("could not unlock with the reported ID %q: %s", infoErr.Info.ID, err)
|
|
|
|
}
|
2017-03-15 01:28:42 +01:00
|
|
|
}
|