mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-01-19 05:45:21 +01: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:
parent
98f359c5b7
commit
d576184fdb
@ -133,9 +133,10 @@ type db struct {
|
||||
// Enforce db implements the walletdb.DB interface.
|
||||
var _ walletdb.DB = (*db)(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) {
|
||||
// NewEtcdClient creates a new etcd v3 API client.
|
||||
func NewEtcdClient(ctx context.Context, cfg Config) (*clientv3.Client,
|
||||
context.Context, func(), error) {
|
||||
|
||||
clientCfg := clientv3.Config{
|
||||
Endpoints: []string{cfg.Host},
|
||||
DialTimeout: etcdConnectionTimeout,
|
||||
@ -153,7 +154,7 @@ func newEtcdBackend(ctx context.Context, cfg Config) (*db, error) {
|
||||
|
||||
tlsConfig, err := tlsInfo.ClientConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
clientCfg.TLS = tlsConfig
|
||||
@ -164,7 +165,7 @@ func newEtcdBackend(ctx context.Context, cfg Config) (*db, error) {
|
||||
cli, err := clientv3.New(clientCfg)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, err
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
// 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.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{
|
||||
cfg: cfg,
|
||||
ctx: ctx,
|
||||
|
@ -12,7 +12,9 @@ import (
|
||||
"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()
|
||||
|
||||
f := NewEtcdTestFixture(t)
|
||||
@ -45,6 +47,8 @@ func TestCopy(t *testing.T) {
|
||||
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) {
|
||||
t.Parallel()
|
||||
|
||||
@ -73,3 +77,26 @@ func TestAbortContext(t *testing.T) {
|
||||
// No changes in the DB.
|
||||
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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user