From d997bbf6b3a2853f19358f05f9e19ddceacb037d Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Fri, 16 Jul 2021 18:07:30 +0200 Subject: [PATCH] channeldb/test: test with postgres --- .github/workflows/main.yml | 3 +- channeldb/db_test.go | 5 ++++ channeldb/setup_test.go | 11 +++++++ contractcourt/setup_test.go | 11 +++++++ docs/release-notes/release-notes-0.14.0.md | 2 ++ kvdb/backend.go | 21 +++++++++++-- kvdb/kvdb_no_postgres.go | 19 ++++++++++++ kvdb/kvdb_postgres.go | 15 ++++++++++ kvdb/postgres/fixture.go | 4 +++ kvdb/postgres/fixture_interface.go | 8 +++++ kvdb/test_utils.go | 34 ++++++++++++++++++++++ routing/setup_test.go | 11 +++++++ sweep/setup_test.go | 11 +++++++ 13 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 channeldb/setup_test.go create mode 100644 contractcourt/setup_test.go create mode 100644 kvdb/kvdb_no_postgres.go create mode 100644 kvdb/kvdb_postgres.go create mode 100644 kvdb/postgres/fixture_interface.go create mode 100644 kvdb/test_utils.go create mode 100644 routing/setup_test.go create mode 100644 sweep/setup_test.go diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6f35225ab..81331b740 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -206,7 +206,8 @@ jobs: matrix: unit_type: - btcd unit-cover - - unit tags="kvdb_etcd kvdb_postgres" + - unit tags="kvdb_etcd" + - unit tags="kvdb_postgres" - travis-race steps: - name: git checkout diff --git a/channeldb/db_test.go b/channeldb/db_test.go index 5731c03a8..c0b9bd79c 100644 --- a/channeldb/db_test.go +++ b/channeldb/db_test.go @@ -25,6 +25,11 @@ import ( func TestOpenWithCreate(t *testing.T) { t.Parallel() + // Checking for db file existence is not possible with postgres. + if kvdb.PostgresBackend { + t.Skip() + } + // First, create a temporary directory to be used for the duration of // this test. tempDirName, err := ioutil.TempDir("", "channeldb") diff --git a/channeldb/setup_test.go b/channeldb/setup_test.go new file mode 100644 index 000000000..91d3bfb47 --- /dev/null +++ b/channeldb/setup_test.go @@ -0,0 +1,11 @@ +package channeldb + +import ( + "testing" + + "github.com/lightningnetwork/lnd/kvdb" +) + +func TestMain(m *testing.M) { + kvdb.RunTests(m) +} diff --git a/contractcourt/setup_test.go b/contractcourt/setup_test.go new file mode 100644 index 000000000..f9925ce04 --- /dev/null +++ b/contractcourt/setup_test.go @@ -0,0 +1,11 @@ +package contractcourt + +import ( + "testing" + + "github.com/lightningnetwork/lnd/kvdb" +) + +func TestMain(m *testing.M) { + kvdb.RunTests(m) +} diff --git a/docs/release-notes/release-notes-0.14.0.md b/docs/release-notes/release-notes-0.14.0.md index eb013ee94..4e782686e 100644 --- a/docs/release-notes/release-notes-0.14.0.md +++ b/docs/release-notes/release-notes-0.14.0.md @@ -460,6 +460,8 @@ messages directly. There is no routing/path finding involved. * [Fixed flake that occurred with the switch dust forwarding test under the data race unit build.](https://github.com/lightningnetwork/lnd/pull/5828) +* [Run channeldb tests on postgres](https://github.com/lightningnetwork/lnd/pull/5550) + ## Database * [Ensure single writer for legacy diff --git a/kvdb/backend.go b/kvdb/backend.go index 014260b2f..7d4cbc36f 100644 --- a/kvdb/backend.go +++ b/kvdb/backend.go @@ -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") diff --git a/kvdb/kvdb_no_postgres.go b/kvdb/kvdb_no_postgres.go new file mode 100644 index 000000000..d581c2b55 --- /dev/null +++ b/kvdb/kvdb_no_postgres.go @@ -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") +} diff --git a/kvdb/kvdb_postgres.go b/kvdb/kvdb_postgres.go new file mode 100644 index 000000000..023a51840 --- /dev/null +++ b/kvdb/kvdb_postgres.go @@ -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() +} diff --git a/kvdb/postgres/fixture.go b/kvdb/postgres/fixture.go index 929563220..9257f49aa 100644 --- a/kvdb/postgres/fixture.go +++ b/kvdb/postgres/fixture.go @@ -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) diff --git a/kvdb/postgres/fixture_interface.go b/kvdb/postgres/fixture_interface.go new file mode 100644 index 000000000..7a1f8b9e9 --- /dev/null +++ b/kvdb/postgres/fixture_interface.go @@ -0,0 +1,8 @@ +package postgres + +import "github.com/btcsuite/btcwallet/walletdb" + +type Fixture interface { + DB() walletdb.DB + Dump() (map[string]interface{}, error) +} diff --git a/kvdb/test_utils.go b/kvdb/test_utils.go new file mode 100644 index 000000000..adc1156e7 --- /dev/null +++ b/kvdb/test_utils.go @@ -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) + +} diff --git a/routing/setup_test.go b/routing/setup_test.go new file mode 100644 index 000000000..544fc3885 --- /dev/null +++ b/routing/setup_test.go @@ -0,0 +1,11 @@ +package routing + +import ( + "testing" + + "github.com/lightningnetwork/lnd/kvdb" +) + +func TestMain(m *testing.M) { + kvdb.RunTests(m) +} diff --git a/sweep/setup_test.go b/sweep/setup_test.go new file mode 100644 index 000000000..e2d22ce3b --- /dev/null +++ b/sweep/setup_test.go @@ -0,0 +1,11 @@ +package sweep + +import ( + "testing" + + "github.com/lightningnetwork/lnd/kvdb" +) + +func TestMain(m *testing.M) { + kvdb.RunTests(m) +}