etcd: add support for user specified ports for testing

This commit is contained in:
Andras Banki-Horvath 2021-01-06 19:40:30 +01:00
parent ce4ca86ca6
commit f1831f0581
No known key found for this signature in database
GPG Key ID: 80E5375C094198D8
8 changed files with 43 additions and 12 deletions

@ -253,7 +253,7 @@ func GetTestBackend(path, name string) (Backend, func(), error) {
} }
return db, empty, nil return db, empty, nil
} else if TestBackend == EtcdBackendName { } else if TestBackend == EtcdBackendName {
return GetEtcdTestBackend(path, name) return GetEtcdTestBackend(path, 0, 0)
} }
return nil, nil, fmt.Errorf("unknown backend") return nil, nil, fmt.Errorf("unknown backend")

@ -36,7 +36,11 @@ type BoltConfig struct {
// EtcdConfig holds etcd configuration. // EtcdConfig holds etcd configuration.
type EtcdConfig struct { type EtcdConfig struct {
Embedded bool `long:"embedded" description:"Use embedded etcd instance instead of the external one."` Embedded bool `long:"embedded" description:"Use embedded etcd instance instead of the external one. Note: use for testing only."`
EmbeddedClientPort uint16 `long:"embedded_client_port" description:"Client port to use for the embedded instance. Note: use for testing only."`
EmbeddedPeerPort uint16 `long:"embedded_peer_port" description:"Peer port to use for the embedded instance. Note: use for testing only."`
Host string `long:"host" description:"Etcd database host."` Host string `long:"host" description:"Etcd database host."`

@ -61,7 +61,9 @@ func getFreePort() int {
// NewEmbeddedEtcdInstance creates an embedded etcd instance for testing, // NewEmbeddedEtcdInstance creates an embedded etcd instance for testing,
// listening on random open ports. Returns the backend config and a cleanup // listening on random open ports. Returns the backend config and a cleanup
// func that will stop the etcd instance. // func that will stop the etcd instance.
func NewEmbeddedEtcdInstance(path string) (*BackendConfig, func(), error) { func NewEmbeddedEtcdInstance(path string, clientPort, peerPort uint16) (
*BackendConfig, func(), error) {
cfg := embed.NewConfig() cfg := embed.NewConfig()
cfg.Dir = path cfg.Dir = path
@ -69,9 +71,17 @@ func NewEmbeddedEtcdInstance(path string) (*BackendConfig, func(), error) {
cfg.MaxTxnOps = 8192 cfg.MaxTxnOps = 8192
cfg.MaxRequestBytes = 16384 * 1024 cfg.MaxRequestBytes = 16384 * 1024
// Listen on random free ports. // Listen on random free ports if no ports were specified.
clientURL := fmt.Sprintf("127.0.0.1:%d", getFreePort()) if clientPort == 0 {
peerURL := fmt.Sprintf("127.0.0.1:%d", getFreePort()) clientPort = uint16(getFreePort())
}
if peerPort == 0 {
peerPort = uint16(getFreePort())
}
clientURL := fmt.Sprintf("127.0.0.1:%d", clientPort)
peerURL := fmt.Sprintf("127.0.0.1:%d", peerPort)
cfg.LCUrls = []url.URL{{Host: clientURL}} cfg.LCUrls = []url.URL{{Host: clientURL}}
cfg.LPUrls = []url.URL{{Host: peerURL}} cfg.LPUrls = []url.URL{{Host: peerURL}}

@ -32,7 +32,7 @@ type EtcdTestFixture struct {
func NewTestEtcdInstance(t *testing.T, path string) (*BackendConfig, func()) { func NewTestEtcdInstance(t *testing.T, path string) (*BackendConfig, func()) {
t.Helper() t.Helper()
config, cleanup, err := NewEmbeddedEtcdInstance(path) config, cleanup, err := NewEmbeddedEtcdInstance(path, 0, 0)
if err != nil { if err != nil {
t.Fatalf("error while staring embedded etcd instance: %v", err) t.Fatalf("error while staring embedded etcd instance: %v", err)
} }

@ -37,10 +37,14 @@ func GetEtcdBackend(ctx context.Context, prefix string,
// GetEtcdTestBackend creates an embedded etcd backend for testing // GetEtcdTestBackend creates an embedded etcd backend for testing
// storig the database at the passed path. // storig the database at the passed path.
func GetEtcdTestBackend(path, name string) (Backend, func(), error) { func GetEtcdTestBackend(path string, clientPort, peerPort uint16) (
Backend, func(), error) {
empty := func() {} empty := func() {}
config, cleanup, err := etcd.NewEmbeddedEtcdInstance(path) config, cleanup, err := etcd.NewEmbeddedEtcdInstance(
path, clientPort, peerPort,
)
if err != nil { if err != nil {
return nil, empty, err return nil, empty, err
} }

@ -22,6 +22,8 @@ func GetEtcdBackend(ctx context.Context, prefix string,
// GetTestEtcdBackend is a stub returning nil, an empty closure and an // GetTestEtcdBackend is a stub returning nil, an empty closure and an
// errEtcdNotAvailable error. // errEtcdNotAvailable error.
func GetEtcdTestBackend(path, name string) (Backend, func(), error) { func GetEtcdTestBackend(path string, clientPort, peerPort uint16) (
Backend, func(), error) {
return nil, func() {}, errEtcdNotAvailable return nil, func() {}, errEtcdNotAvailable
} }

@ -83,10 +83,15 @@ func (db *DB) GetBackends(ctx context.Context, dbPath string,
if db.Backend == EtcdBackend { if db.Backend == EtcdBackend {
if db.Etcd.Embedded { if db.Etcd.Embedded {
remoteDB, _, err = kvdb.GetEtcdTestBackend(dbPath, dbName) remoteDB, _, err = kvdb.GetEtcdTestBackend(
dbPath, db.Etcd.EmbeddedClientPort,
db.Etcd.EmbeddedPeerPort,
)
} else { } else {
// Prefix will separate key/values in the db. // Prefix will separate key/values in the db.
remoteDB, err = kvdb.GetEtcdBackend(ctx, networkName, db.Etcd) remoteDB, err = kvdb.GetEtcdBackend(
ctx, networkName, db.Etcd,
)
} }
if err != nil { if err != nil {
return nil, err return nil, err

@ -984,6 +984,12 @@ litecoin.node=ltcd
; Useful for testing. ; Useful for testing.
; db.etcd.embedded=false ; db.etcd.embedded=false
; If non zero, LND will use this as client port for the embedded etcd instance.
; db.etcd.embedded_client_port=1234
; If non zero, LND will use this as peer port for the embedded etcd instance.
; db.etcd.embedded_peer_port=1235
[bolt] [bolt]
; If true, prevents the database from syncing its freelist to disk. ; If true, prevents the database from syncing its freelist to disk.
; db.bolt.nofreelistsync=1 ; db.bolt.nofreelistsync=1