mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-28 14:40:51 +02:00
channeldb/test: test with postgres
This commit is contained in:
@@ -5,7 +5,9 @@ package kvdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -245,7 +247,20 @@ func updateLastCompactionDate(dbFile string) error {
|
||||
func GetTestBackend(path, name string) (Backend, func(), error) {
|
||||
empty := func() {}
|
||||
|
||||
if TestBackend == BoltBackendName {
|
||||
switch {
|
||||
case PostgresBackend:
|
||||
key := filepath.Join(path, name)
|
||||
keyHash := sha256.Sum256([]byte(key))
|
||||
|
||||
f, err := NewPostgresFixture("test_" + hex.EncodeToString(keyHash[:]))
|
||||
if err != nil {
|
||||
return nil, func() {}, err
|
||||
}
|
||||
return f.DB(), func() {
|
||||
_ = f.DB().Close()
|
||||
}, nil
|
||||
|
||||
case TestBackend == BoltBackendName:
|
||||
db, err := GetBoltBackend(&BoltBackendConfig{
|
||||
DBPath: path,
|
||||
DBFileName: name,
|
||||
@@ -256,7 +271,8 @@ func GetTestBackend(path, name string) (Backend, func(), error) {
|
||||
return nil, nil, err
|
||||
}
|
||||
return db, empty, nil
|
||||
} else if TestBackend == EtcdBackendName {
|
||||
|
||||
case TestBackend == EtcdBackendName:
|
||||
etcdConfig, cancel, err := StartEtcdTestBackend(path, 0, 0, "")
|
||||
if err != nil {
|
||||
return nil, empty, err
|
||||
@@ -265,6 +281,7 @@ func GetTestBackend(path, name string) (Backend, func(), error) {
|
||||
EtcdBackendName, context.TODO(), etcdConfig,
|
||||
)
|
||||
return backend, cancel, err
|
||||
|
||||
}
|
||||
|
||||
return nil, nil, fmt.Errorf("unknown backend")
|
||||
|
19
kvdb/kvdb_no_postgres.go
Normal file
19
kvdb/kvdb_no_postgres.go
Normal file
@@ -0,0 +1,19 @@
|
||||
// +build !kvdb_postgres
|
||||
|
||||
package kvdb
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/lightningnetwork/lnd/kvdb/postgres"
|
||||
)
|
||||
|
||||
const PostgresBackend = false
|
||||
|
||||
func NewPostgresFixture(dbName string) (postgres.Fixture, error) {
|
||||
return nil, errors.New("postgres backend not available")
|
||||
}
|
||||
|
||||
func StartEmbeddedPostgres() (func() error, error) {
|
||||
return nil, errors.New("postgres backend not available")
|
||||
}
|
15
kvdb/kvdb_postgres.go
Normal file
15
kvdb/kvdb_postgres.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// +build kvdb_postgres
|
||||
|
||||
package kvdb
|
||||
|
||||
import "github.com/lightningnetwork/lnd/kvdb/postgres"
|
||||
|
||||
const PostgresBackend = true
|
||||
|
||||
func NewPostgresFixture(dbName string) (postgres.Fixture, error) {
|
||||
return postgres.NewFixture(dbName)
|
||||
}
|
||||
|
||||
func StartEmbeddedPostgres() (func() error, error) {
|
||||
return postgres.StartEmbeddedPostgres()
|
||||
}
|
@@ -95,6 +95,10 @@ type fixture struct {
|
||||
Db walletdb.DB
|
||||
}
|
||||
|
||||
func (b *fixture) DB() walletdb.DB {
|
||||
return b.Db
|
||||
}
|
||||
|
||||
// Dump returns the raw contents of the database.
|
||||
func (b *fixture) Dump() (map[string]interface{}, error) {
|
||||
dbConn, err := sql.Open("pgx", b.Dsn)
|
||||
|
8
kvdb/postgres/fixture_interface.go
Normal file
8
kvdb/postgres/fixture_interface.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package postgres
|
||||
|
||||
import "github.com/btcsuite/btcwallet/walletdb"
|
||||
|
||||
type Fixture interface {
|
||||
DB() walletdb.DB
|
||||
Dump() (map[string]interface{}, error)
|
||||
}
|
34
kvdb/test_utils.go
Normal file
34
kvdb/test_utils.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package kvdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// RunTests is a helper function to run the tests in a package with
|
||||
// initialization and tear-down of a test kvdb backend.
|
||||
func RunTests(m *testing.M) {
|
||||
var close func() error
|
||||
if PostgresBackend {
|
||||
var err error
|
||||
close, err = StartEmbeddedPostgres()
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// os.Exit() does not respect defer statements
|
||||
code := m.Run()
|
||||
|
||||
if close != nil {
|
||||
err := close()
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
os.Exit(code)
|
||||
|
||||
}
|
Reference in New Issue
Block a user