2017-01-19 05:49:01 +01:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2017-10-08 18:57:11 +02:00
|
|
|
"context"
|
2017-01-19 05:49:01 +01:00
|
|
|
"fmt"
|
2017-10-08 18:57:11 +02:00
|
|
|
"net"
|
|
|
|
"sync"
|
2017-01-19 05:49:01 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
2017-04-06 20:04:50 +02:00
|
|
|
"github.com/hashicorp/terraform/state"
|
2017-01-19 05:49:01 +01:00
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRemoteClient_impl(t *testing.T) {
|
|
|
|
var _ remote.Client = new(RemoteClient)
|
2017-02-15 23:20:59 +01:00
|
|
|
var _ remote.ClientLocker = new(RemoteClient)
|
2017-01-19 05:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteClient(t *testing.T) {
|
|
|
|
// Get the backend
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-13 23:25:58 +01:00
|
|
|
"address": srv.HTTPAddr,
|
2017-01-19 05:49:01 +01:00
|
|
|
"path": fmt.Sprintf("tf-unit/%s", time.Now().String()),
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-01-19 05:49:01 +01:00
|
|
|
|
2017-03-02 07:15:08 +01:00
|
|
|
// Grab the client
|
|
|
|
state, err := b.State(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:49:01 +01:00
|
|
|
// Test
|
2017-03-02 07:15:08 +01:00
|
|
|
remote.TestClient(t, state.(*remote.State).Client)
|
2017-01-19 05:49:01 +01:00
|
|
|
}
|
2017-02-07 16:05:53 +01:00
|
|
|
|
2017-03-14 20:24:50 +01:00
|
|
|
// test the gzip functionality of the client
|
|
|
|
func TestRemoteClient_gzipUpgrade(t *testing.T) {
|
|
|
|
statePath := fmt.Sprintf("tf-unit/%s", time.Now().String())
|
|
|
|
|
|
|
|
// Get the backend
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-14 20:24:50 +01:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": statePath,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-03-14 20:24:50 +01:00
|
|
|
|
|
|
|
// Grab the client
|
|
|
|
state, err := b.State(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
remote.TestClient(t, state.(*remote.State).Client)
|
|
|
|
|
|
|
|
// create a new backend with gzip
|
2018-03-21 02:43:02 +01:00
|
|
|
b = backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-14 20:24:50 +01:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": statePath,
|
|
|
|
"gzip": true,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-03-14 20:24:50 +01:00
|
|
|
|
|
|
|
// Grab the client
|
|
|
|
state, err = b.State(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
remote.TestClient(t, state.(*remote.State).Client)
|
|
|
|
}
|
|
|
|
|
2017-02-07 16:05:53 +01:00
|
|
|
func TestConsul_stateLock(t *testing.T) {
|
2017-02-07 16:33:05 +01:00
|
|
|
path := fmt.Sprintf("tf-unit/%s", time.Now().String())
|
2017-02-07 16:05:53 +01:00
|
|
|
|
|
|
|
// create 2 instances to get 2 remote.Clients
|
2018-03-21 02:43:02 +01:00
|
|
|
sA, err := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-13 23:25:58 +01:00
|
|
|
"address": srv.HTTPAddr,
|
2017-02-07 16:05:53 +01:00
|
|
|
"path": path,
|
2018-03-21 02:43:02 +01:00
|
|
|
})).State(backend.DefaultStateName)
|
2017-02-07 16:33:05 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
sB, err := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-13 23:25:58 +01:00
|
|
|
"address": srv.HTTPAddr,
|
2017-02-07 16:05:53 +01:00
|
|
|
"path": path,
|
2018-03-21 02:43:02 +01:00
|
|
|
})).State(backend.DefaultStateName)
|
2017-02-07 16:33:05 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-07 16:05:53 +01:00
|
|
|
|
2017-02-07 16:33:05 +01:00
|
|
|
remote.TestRemoteLocks(t, sA.(*remote.State).Client, sB.(*remote.State).Client)
|
2017-02-07 16:05:53 +01:00
|
|
|
}
|
2017-04-06 20:04:50 +02:00
|
|
|
|
|
|
|
func TestConsul_destroyLock(t *testing.T) {
|
|
|
|
// Get the backend
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-04-06 20:04:50 +02:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": fmt.Sprintf("tf-unit/%s", time.Now().String()),
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-04-06 20:04:50 +02:00
|
|
|
|
|
|
|
// Grab the client
|
|
|
|
s, err := b.State(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := s.(*remote.State).Client.(*RemoteClient)
|
|
|
|
|
|
|
|
info := state.NewLockInfo()
|
|
|
|
id, err := c.Lock(info)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lockPath := c.Path + lockSuffix
|
|
|
|
|
|
|
|
if err := c.Unlock(id); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the lock val
|
|
|
|
pair, _, err := c.Client.KV().Get(lockPath, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if pair != nil {
|
|
|
|
t.Fatalf("lock key not cleaned up at: %s", pair.Key)
|
|
|
|
}
|
|
|
|
}
|
2017-05-30 17:07:56 +02:00
|
|
|
|
|
|
|
func TestConsul_lostLock(t *testing.T) {
|
|
|
|
path := fmt.Sprintf("tf-unit/%s", time.Now().String())
|
|
|
|
|
|
|
|
// create 2 instances to get 2 remote.Clients
|
2018-03-21 02:43:02 +01:00
|
|
|
sA, err := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-05-30 17:07:56 +02:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path,
|
2018-03-21 02:43:02 +01:00
|
|
|
})).State(backend.DefaultStateName)
|
2017-05-30 17:07:56 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
sB, err := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-05-30 17:07:56 +02:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path + "-not-used",
|
2018-03-21 02:43:02 +01:00
|
|
|
})).State(backend.DefaultStateName)
|
2017-05-30 17:07:56 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info := state.NewLockInfo()
|
|
|
|
info.Operation = "test-lost-lock"
|
|
|
|
id, err := sA.Lock(info)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reLocked := make(chan struct{})
|
|
|
|
testLockHook = func() {
|
|
|
|
close(reLocked)
|
2017-10-08 22:24:45 +02:00
|
|
|
testLockHook = nil
|
2017-05-30 17:07:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// now we use the second client to break the lock
|
|
|
|
kv := sB.(*remote.State).Client.(*RemoteClient).Client.KV()
|
|
|
|
_, err = kv.Delete(path+lockSuffix, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-reLocked
|
|
|
|
|
|
|
|
if err := sA.Unlock(id); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2017-10-08 18:57:11 +02:00
|
|
|
|
|
|
|
func TestConsul_lostLockConnection(t *testing.T) {
|
|
|
|
// create an "unreliable" network by closing all the consul client's
|
|
|
|
// network connections
|
|
|
|
conns := &unreliableConns{}
|
|
|
|
origDialFn := dialContext
|
|
|
|
defer func() {
|
|
|
|
dialContext = origDialFn
|
|
|
|
}()
|
|
|
|
dialContext = conns.DialContext
|
|
|
|
|
|
|
|
path := fmt.Sprintf("tf-unit/%s", time.Now().String())
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-10-08 18:57:11 +02:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-10-08 18:57:11 +02:00
|
|
|
|
|
|
|
s, err := b.State(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info := state.NewLockInfo()
|
|
|
|
info.Operation = "test-lost-lock-connection"
|
|
|
|
id, err := s.Lock(info)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-29 01:45:10 +02:00
|
|
|
// kill the connection a few times
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
dialed := conns.dialedDone()
|
|
|
|
// kill any open connections
|
|
|
|
conns.Kill()
|
|
|
|
// wait for a new connection to be dialed, and kill it again
|
|
|
|
<-dialed
|
2017-10-08 18:57:11 +02:00
|
|
|
}
|
|
|
|
|
2017-10-29 01:45:10 +02:00
|
|
|
if err := s.Unlock(id); err != nil {
|
|
|
|
t.Fatal("unlock error:", err)
|
2017-10-08 18:57:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type unreliableConns struct {
|
|
|
|
sync.Mutex
|
|
|
|
conns []net.Conn
|
|
|
|
dialCallback func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *unreliableConns) DialContext(ctx context.Context, netw, addr string) (net.Conn, error) {
|
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
|
|
|
|
dialer := &net.Dialer{}
|
|
|
|
conn, err := dialer.DialContext(ctx, netw, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.conns = append(u.conns, conn)
|
|
|
|
|
|
|
|
if u.dialCallback != nil {
|
|
|
|
u.dialCallback()
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2017-10-29 01:45:10 +02:00
|
|
|
func (u *unreliableConns) dialedDone() chan struct{} {
|
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
dialed := make(chan struct{})
|
|
|
|
u.dialCallback = func() {
|
|
|
|
defer close(dialed)
|
|
|
|
u.dialCallback = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return dialed
|
|
|
|
}
|
|
|
|
|
2017-10-08 18:57:11 +02:00
|
|
|
// Kill these with a deadline, just to make sure we don't end up with any EOFs
|
|
|
|
// that get ignored.
|
|
|
|
func (u *unreliableConns) Kill() {
|
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
|
|
|
|
for _, conn := range u.conns {
|
|
|
|
conn.(*net.TCPConn).SetDeadline(time.Now())
|
|
|
|
}
|
|
|
|
u.conns = nil
|
|
|
|
}
|