mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-08 17:22:05 +02:00
etcd: extract NewEtcdClient into exported function
In order for us to be able to re-use the code for initializing an etcd client connection, we extract and export the NewEtcdClient function.
This commit is contained in:
@@ -133,9 +133,10 @@ type db struct {
|
|||||||
// Enforce db implements the walletdb.DB interface.
|
// Enforce db implements the walletdb.DB interface.
|
||||||
var _ walletdb.DB = (*db)(nil)
|
var _ walletdb.DB = (*db)(nil)
|
||||||
|
|
||||||
// newEtcdBackend returns a db object initialized with the passed backend
|
// NewEtcdClient creates a new etcd v3 API client.
|
||||||
// config. If etcd connection cannot be established, then returns error.
|
func NewEtcdClient(ctx context.Context, cfg Config) (*clientv3.Client,
|
||||||
func newEtcdBackend(ctx context.Context, cfg Config) (*db, error) {
|
context.Context, func(), error) {
|
||||||
|
|
||||||
clientCfg := clientv3.Config{
|
clientCfg := clientv3.Config{
|
||||||
Endpoints: []string{cfg.Host},
|
Endpoints: []string{cfg.Host},
|
||||||
DialTimeout: etcdConnectionTimeout,
|
DialTimeout: etcdConnectionTimeout,
|
||||||
@@ -153,7 +154,7 @@ func newEtcdBackend(ctx context.Context, cfg Config) (*db, error) {
|
|||||||
|
|
||||||
tlsConfig, err := tlsInfo.ClientConfig()
|
tlsConfig, err := tlsInfo.ClientConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
clientCfg.TLS = tlsConfig
|
clientCfg.TLS = tlsConfig
|
||||||
@@ -164,7 +165,7 @@ func newEtcdBackend(ctx context.Context, cfg Config) (*db, error) {
|
|||||||
cli, err := clientv3.New(clientCfg)
|
cli, err := clientv3.New(clientCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply the namespace.
|
// Apply the namespace.
|
||||||
@@ -172,6 +173,17 @@ func newEtcdBackend(ctx context.Context, cfg Config) (*db, error) {
|
|||||||
cli.Watcher = namespace.NewWatcher(cli.Watcher, cfg.Namespace)
|
cli.Watcher = namespace.NewWatcher(cli.Watcher, cfg.Namespace)
|
||||||
cli.Lease = namespace.NewLease(cli.Lease, cfg.Namespace)
|
cli.Lease = namespace.NewLease(cli.Lease, cfg.Namespace)
|
||||||
|
|
||||||
|
return cli, ctx, cancel, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// newEtcdBackend returns a db object initialized with the passed backend
|
||||||
|
// config. If etcd connection cannot be established, then returns error.
|
||||||
|
func newEtcdBackend(ctx context.Context, cfg Config) (*db, error) {
|
||||||
|
cli, ctx, cancel, err := NewEtcdClient(ctx, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
backend := &db{
|
backend := &db{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
@@ -12,7 +12,9 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCopy(t *testing.T) {
|
// TestDump tests that the Dump() method creates a one-to-one copy of the
|
||||||
|
// database content.
|
||||||
|
func TestDump(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
f := NewEtcdTestFixture(t)
|
f := NewEtcdTestFixture(t)
|
||||||
@@ -45,6 +47,8 @@ func TestCopy(t *testing.T) {
|
|||||||
require.Equal(t, expected, f.Dump())
|
require.Equal(t, expected, f.Dump())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestAbortContext tests that an update on the database is aborted if the
|
||||||
|
// database's main context in cancelled.
|
||||||
func TestAbortContext(t *testing.T) {
|
func TestAbortContext(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -73,3 +77,26 @@ func TestAbortContext(t *testing.T) {
|
|||||||
// No changes in the DB.
|
// No changes in the DB.
|
||||||
require.Equal(t, map[string]string{}, f.Dump())
|
require.Equal(t, map[string]string{}, f.Dump())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestNewEtcdClient tests that an etcd v3 client can be created correctly.
|
||||||
|
func TestNewEtcdClient(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
f := NewEtcdTestFixture(t)
|
||||||
|
defer f.Cleanup()
|
||||||
|
|
||||||
|
client, ctx, cancel, err := NewEtcdClient(
|
||||||
|
context.Background(), f.BackendConfig(),
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Cleanup(cancel)
|
||||||
|
|
||||||
|
_, err = client.Put(ctx, "foo/bar", "baz")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
resp, err := client.Get(ctx, "foo/bar")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.Len(t, resp.Kvs, 1)
|
||||||
|
require.Equal(t, "baz", string(resp.Kvs[0].Value))
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user