2017-01-19 05:51:15 +01:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestClient is a generic function to test any client.
|
|
|
|
func TestClient(t *testing.T, c Client) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
s := state.TestStateInitial()
|
|
|
|
if err := terraform.WriteState(s, &buf); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
data := buf.Bytes()
|
|
|
|
|
|
|
|
if err := c.Put(data); err != nil {
|
|
|
|
t.Fatalf("put: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := c.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("get: %s", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(p.Data, data) {
|
|
|
|
t.Fatalf("bad: %#v", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Delete(); err != nil {
|
|
|
|
t.Fatalf("delete: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err = c.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("get: %s", err)
|
|
|
|
}
|
|
|
|
if p != nil {
|
|
|
|
t.Fatalf("bad: %#v", p)
|
|
|
|
}
|
|
|
|
}
|
2017-02-07 16:33:05 +01:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:25:04 +01:00
|
|
|
infoA := state.NewLockInfo()
|
|
|
|
infoA.Operation = "test"
|
|
|
|
infoA.Who = "clientA"
|
|
|
|
|
|
|
|
infoB := state.NewLockInfo()
|
|
|
|
infoB.Operation = "test"
|
|
|
|
infoB.Who = "clientB"
|
2017-02-14 23:48:57 +01:00
|
|
|
|
2017-02-15 16:39:35 +01:00
|
|
|
lockIDA, err := lockerA.Lock(infoA)
|
|
|
|
if err != nil {
|
2017-02-07 16:33:05 +01:00
|
|
|
t.Fatal("unable to get initial lock:", err)
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:39:35 +01:00
|
|
|
_, err = lockerB.Lock(infoB)
|
|
|
|
if err == nil {
|
|
|
|
lockerA.Unlock(lockIDA)
|
2017-02-07 16:33:05 +01:00
|
|
|
t.Fatal("client B obtained lock while held by client A")
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:39:35 +01:00
|
|
|
if err := lockerA.Unlock(lockIDA); err != nil {
|
2017-02-07 16:33:05 +01:00
|
|
|
t.Fatal("error unlocking client A", err)
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:39:35 +01:00
|
|
|
lockIDB, err := lockerB.Lock(infoB)
|
|
|
|
if err != nil {
|
2017-02-07 16:33:05 +01:00
|
|
|
t.Fatal("unable to obtain lock from client B")
|
|
|
|
}
|
|
|
|
|
2017-02-15 18:02:37 +01:00
|
|
|
if lockIDB == lockIDA {
|
|
|
|
t.Fatalf("duplicate lock IDs: %q", lockIDB)
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:39:35 +01:00
|
|
|
if err = lockerB.Unlock(lockIDB); err != nil {
|
2017-02-07 16:33:05 +01:00
|
|
|
t.Fatal("error unlocking client B:", err)
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:39:35 +01:00
|
|
|
// TODO: Should we enforce that Unlock requires the correct ID?
|
2017-02-07 16:33:05 +01:00
|
|
|
}
|