Move TestRemoteLocks to state/remote

This was legacy remote state client and backends can use this test
function without an import cycle.
This commit is contained in:
James Bardin 2017-02-07 10:33:05 -05:00
parent 54cac349a3
commit 9b76f6e138
5 changed files with 61 additions and 97 deletions

View File

@ -32,21 +32,28 @@ func TestRemoteClient(t *testing.T) {
func TestConsul_stateLock(t *testing.T) { func TestConsul_stateLock(t *testing.T) {
addr := os.Getenv("CONSUL_HTTP_ADDR") addr := os.Getenv("CONSUL_HTTP_ADDR")
if addr == "" { if addr == "" {
t.Log("consul lock tests require a running consul instance") t.Log("consul lock tests require CONSUL_HTTP_ADDR")
t.Skip() t.Skip()
} }
path := "testing" //fmt.Sprintf("tf-unit/%s", time.Now().String()) path := fmt.Sprintf("tf-unit/%s", time.Now().String())
// create 2 instances to get 2 remote.Clients // create 2 instances to get 2 remote.Clients
a := backend.TestBackendConfig(t, New(), map[string]interface{}{ sA, err := backend.TestBackendConfig(t, New(), map[string]interface{}{
"address": addr, "address": addr,
"path": path, "path": path,
}) }).State()
b := backend.TestBackendConfig(t, New(), map[string]interface{}{ if err != nil {
"address": addr, t.Fatal(err)
"path": path, }
})
remotestate.TestRemoteLocks(t, a, b) sB, err := backend.TestBackendConfig(t, New(), map[string]interface{}{
"address": addr,
"path": path,
}).State()
if err != nil {
t.Fatal(err)
}
remote.TestRemoteLocks(t, sA.(*remote.State).Client, sB.(*remote.State).Client)
} }

View File

@ -4,7 +4,6 @@ import (
"testing" "testing"
"github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/state/remote" "github.com/hashicorp/terraform/state/remote"
) )
@ -16,57 +15,3 @@ func TestClient(t *testing.T, raw backend.Backend) {
remote.TestClient(t, b.client) remote.TestClient(t, b.client)
} }
// Test the lock implementation for a remote.Client.
// This test requires 2 backend instances, in oder to have multiple remote
// clients since some implementations may tie the client to the lock, or may
// have reentrant locks.
func TestRemoteLocks(t *testing.T, a, b backend.Backend) {
sA, err := a.State()
if err != nil {
t.Fatal("failed to get state from backend A:", err)
}
sB, err := b.State()
if err != nil {
t.Fatal("failed to get state from backend B:", err)
}
lockerA, ok := sA.(state.Locker)
if !ok {
t.Fatal("client A not a state.Locker")
}
lockerB, ok := sB.(state.Locker)
if !ok {
t.Fatal("client B not a state.Locker")
}
if err := lockerA.Lock("test client A"); err != nil {
t.Fatal("unable to get initial lock:", err)
}
if err := lockerB.Lock("test client B"); err == nil {
lockerA.Unlock()
t.Fatal("client B obtained lock while held by client A")
} else {
t.Log("lock info error:", err)
}
if err := lockerA.Unlock(); err != nil {
t.Fatal("error unlocking client A", err)
}
if err := lockerB.Lock("test client B"); err != nil {
t.Fatal("unable to obtain lock from client B")
}
if err := lockerB.Unlock(); err != nil {
t.Fatal("error unlocking client B:", err)
}
// unlock should be repeatable
if err := lockerA.Unlock(); err != nil {
t.Fatal("Unlock error from client A when state was not locked:", err)
}
}

View File

@ -44,38 +44,6 @@ func testClient(t *testing.T, c Client) {
} }
} }
func testClientLocks(t *testing.T, c Client) {
s3Client := c.(*S3Client)
// initial lock
if err := s3Client.Lock("test"); err != nil {
t.Fatal(err)
}
// second lock should fail
if err := s3Client.Lock("test"); err == nil {
t.Fatal("expected error, got nil")
}
// unlock should work
if err := s3Client.Unlock(); err != nil {
t.Fatal(err)
}
// now we should be able to lock again
if err := s3Client.Lock("test"); err != nil {
t.Fatal(err)
}
// unlock should be idempotent
if err := s3Client.Unlock(); err != nil {
t.Fatal(err)
}
if err := s3Client.Unlock(); err != nil {
t.Fatal(err)
}
}
func TestRemoteClient_noPayload(t *testing.T) { func TestRemoteClient_noPayload(t *testing.T) {
s := &State{ s := &State{
Client: nilClient{}, Client: nilClient{},

View File

@ -169,7 +169,7 @@ func TestS3ClientLocks(t *testing.T) {
createDynamoDBTable(t, s3Client, bucketName) createDynamoDBTable(t, s3Client, bucketName)
testClientLocks(t, client) TestRemoteLocks(t, client, client)
} }
// create the dynamoDB table, and wait until we can query it. // create the dynamoDB table, and wait until we can query it.

View File

@ -41,3 +41,47 @@ func TestClient(t *testing.T, c Client) {
t.Fatalf("bad: %#v", p) t.Fatalf("bad: %#v", p)
} }
} }
// Test the lock implementation for a remote.Client.
// This test requires 2 client instances, in oder to have multiple remote
// clients since some implementations may tie the client to the lock, or may
// have reentrant locks.
func TestRemoteLocks(t *testing.T, a, b Client) {
lockerA, ok := a.(state.Locker)
if !ok {
t.Fatal("client A not a state.Locker")
}
lockerB, ok := b.(state.Locker)
if !ok {
t.Fatal("client B not a state.Locker")
}
if err := lockerA.Lock("test client A"); err != nil {
t.Fatal("unable to get initial lock:", err)
}
if err := lockerB.Lock("test client B"); err == nil {
lockerA.Unlock()
t.Fatal("client B obtained lock while held by client A")
} else {
t.Log("lock info error:", err)
}
if err := lockerA.Unlock(); err != nil {
t.Fatal("error unlocking client A", err)
}
if err := lockerB.Lock("test client B"); err != nil {
t.Fatal("unable to obtain lock from client B")
}
if err := lockerB.Unlock(); err != nil {
t.Fatal("error unlocking client B:", err)
}
// unlock should be repeatable
if err := lockerA.Unlock(); err != nil {
t.Fatal("Unlock error from client A when state was not locked:", err)
}
}