2017-08-03 19:10:50 +02:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRemoteClient_impl(t *testing.T) {
|
|
|
|
var _ remote.Client = new(RemoteClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteClient(t *testing.T) {
|
|
|
|
prepareEtcdv3(t)
|
2017-09-08 23:49:23 +02:00
|
|
|
defer cleanupEtcdv3(t)
|
2017-08-03 19:10:50 +02:00
|
|
|
|
2017-09-08 23:34:15 +02:00
|
|
|
prefix := fmt.Sprintf("%s/%s/", keyPrefix, time.Now().Format(time.RFC3339))
|
|
|
|
|
2017-08-03 19:10:50 +02:00
|
|
|
// Get the backend
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-09-08 18:46:39 +02:00
|
|
|
"endpoints": etcdv3Endpoints,
|
2017-09-08 23:34:15 +02:00
|
|
|
"prefix": prefix,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-08-03 19:10:50 +02:00
|
|
|
|
|
|
|
// Grab the client
|
2018-10-01 23:35:23 +02:00
|
|
|
state, err := b.StateMgr(backend.DefaultStateName)
|
2017-08-03 19:10:50 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error: %s.", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
remote.TestClient(t, state.(*remote.State).Client)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdv3_stateLock(t *testing.T) {
|
|
|
|
prepareEtcdv3(t)
|
2017-09-08 23:49:23 +02:00
|
|
|
defer cleanupEtcdv3(t)
|
2017-08-03 19:10:50 +02:00
|
|
|
|
2017-09-08 23:34:15 +02:00
|
|
|
prefix := fmt.Sprintf("%s/%s/", keyPrefix, time.Now().Format(time.RFC3339))
|
2017-08-03 19:10:50 +02:00
|
|
|
|
|
|
|
// Get the backend
|
2018-03-21 02:43:02 +01:00
|
|
|
s1, err := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-09-08 18:46:39 +02:00
|
|
|
"endpoints": etcdv3Endpoints,
|
2017-09-08 23:34:15 +02:00
|
|
|
"prefix": prefix,
|
2018-10-01 23:35:23 +02:00
|
|
|
})).StateMgr(backend.DefaultStateName)
|
2017-08-03 19:10:50 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
s2, err := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-09-08 18:46:39 +02:00
|
|
|
"endpoints": etcdv3Endpoints,
|
2017-09-08 23:34:15 +02:00
|
|
|
"prefix": prefix,
|
2018-10-01 23:35:23 +02:00
|
|
|
})).StateMgr(backend.DefaultStateName)
|
2017-08-03 19:10:50 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
remote.TestRemoteLocks(t, s1.(*remote.State).Client, s2.(*remote.State).Client)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdv3_destroyLock(t *testing.T) {
|
|
|
|
prepareEtcdv3(t)
|
2017-09-08 23:49:23 +02:00
|
|
|
defer cleanupEtcdv3(t)
|
2017-08-03 19:10:50 +02:00
|
|
|
|
2017-09-08 23:34:15 +02:00
|
|
|
prefix := fmt.Sprintf("%s/%s/", keyPrefix, time.Now().Format(time.RFC3339))
|
|
|
|
|
2017-08-03 19:10:50 +02:00
|
|
|
// Get the backend
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-09-08 18:46:39 +02:00
|
|
|
"endpoints": etcdv3Endpoints,
|
2017-09-08 23:34:15 +02:00
|
|
|
"prefix": prefix,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-08-03 19:10:50 +02:00
|
|
|
|
|
|
|
// Grab the client
|
2018-10-01 23:35:23 +02:00
|
|
|
s, err := b.StateMgr(backend.DefaultStateName)
|
2017-08-03 19:10:50 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Unlock(id); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-09-08 18:46:39 +02:00
|
|
|
res, err := c.Client.KV.Get(context.TODO(), c.info.Path)
|
2017-08-03 19:10:50 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if res.Count != 0 {
|
|
|
|
t.Fatalf("lock key not cleaned up at: %s", string(res.Kvs[0].Key))
|
|
|
|
}
|
|
|
|
}
|