From dc353dc50efb9d9b4c1b477db826c38a77bae280 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Fri, 9 May 2025 11:26:37 +0200 Subject: [PATCH] multi: use MakeTestGraph everywhere for test graph creation In this commit, we unify how all unit tests that make use of the graph create their test ChannelGraph instance. This will make it easier to ensure that once we plug in different graph DB implementations, that all unit tests are run against all variants of the graph DB. With this commit, `NewChannelGraph` is mainly only called via `MakeTestGraph` for all tests _except_ for `TestGraphLoading` which needs to be able to reload a ChannelGraph with the same backend. This will be addressed in a follow-up commit once more helpers are defined. Note that in all previous packages where we created a test graph using `kvdb.GetBoltBackend`, we now need to add a `TestMain` function with a call to `kvdb.RunTest` since the `MakeTestGraph` helper uses `GetTestBackend` instead of `kvdb.GetBoltBackend` which requires an embedded postgres instance to be running. --- autopilot/prefattach_test.go | 18 +----------------- autopilot/setup_test.go | 11 +++++++++++ graph/builder_test.go | 4 ++-- graph/db/kv_store.go | 2 ++ graph/notifications_test.go | 30 +----------------------------- peer/setup_test.go | 11 +++++++++++ peer/test_utils.go | 21 ++------------------- routing/pathfind_test.go | 27 ++++++++++----------------- 8 files changed, 40 insertions(+), 84 deletions(-) create mode 100644 autopilot/setup_test.go create mode 100644 peer/setup_test.go diff --git a/autopilot/prefattach_test.go b/autopilot/prefattach_test.go index de7b6a7d6..e0baf4971 100644 --- a/autopilot/prefattach_test.go +++ b/autopilot/prefattach_test.go @@ -14,7 +14,6 @@ import ( "github.com/btcsuite/btcd/btcutil" graphdb "github.com/lightningnetwork/lnd/graph/db" "github.com/lightningnetwork/lnd/graph/db/models" - "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing/route" "github.com/stretchr/testify/require" @@ -37,22 +36,7 @@ type testDBGraph struct { } func newDiskChanGraph(t *testing.T) (testGraph, error) { - backend, err := kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{ - DBPath: t.TempDir(), - DBFileName: "graph.db", - NoFreelistSync: true, - AutoCompact: false, - AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge, - DBTimeout: kvdb.DefaultDBTimeout, - }) - require.NoError(t, err) - - graphStore, err := graphdb.NewKVStore(backend) - require.NoError(t, err) - - graphDB, err := graphdb.NewChannelGraph(graphStore) - require.NoError(t, err) - + graphDB := graphdb.MakeTestGraph(t) require.NoError(t, graphDB.Start()) t.Cleanup(func() { require.NoError(t, graphDB.Stop()) diff --git a/autopilot/setup_test.go b/autopilot/setup_test.go new file mode 100644 index 000000000..c9c050a8e --- /dev/null +++ b/autopilot/setup_test.go @@ -0,0 +1,11 @@ +package autopilot + +import ( + "testing" + + "github.com/lightningnetwork/lnd/kvdb" +) + +func TestMain(m *testing.M) { + kvdb.RunTests(m) +} diff --git a/graph/builder_test.go b/graph/builder_test.go index aed30e097..fa7fc722d 100644 --- a/graph/builder_test.go +++ b/graph/builder_test.go @@ -1352,7 +1352,7 @@ func parseTestGraph(t *testing.T, useCache bool, path string) ( testAddrs = append(testAddrs, testAddr) // Next, create a temporary graph database for usage within the test. - graph := makeTestGraph(t, useCache) + graph := graphdb.MakeTestGraph(t, graphdb.WithUseGraphCache(useCache)) aliasMap := make(map[string]route.Vertex) privKeyMap := make(map[string]*btcec.PrivateKey) @@ -1726,7 +1726,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool, testAddrs = append(testAddrs, testAddr) // Next, create a temporary graph database for usage within the test. - graph := makeTestGraph(t, useCache) + graph := graphdb.MakeTestGraph(t, graphdb.WithUseGraphCache(useCache)) aliasMap := make(map[string]route.Vertex) privKeyMap := make(map[string]*btcec.PrivateKey) diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go index 4e2bf7365..ff221a94d 100644 --- a/graph/db/kv_store.go +++ b/graph/db/kv_store.go @@ -4710,6 +4710,8 @@ func (c *chanGraphNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo, // MakeTestGraph creates a new instance of the ChannelGraph for testing // purposes. func MakeTestGraph(t testing.TB, opts ...ChanGraphOption) *ChannelGraph { + t.Helper() + // Next, create KVStore for the first time. backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr") t.Cleanup(backendCleanup) diff --git a/graph/notifications_test.go b/graph/notifications_test.go index 33bac64dd..71c6f4a34 100644 --- a/graph/notifications_test.go +++ b/graph/notifications_test.go @@ -22,7 +22,6 @@ import ( "github.com/lightningnetwork/lnd/graph/db/models" "github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/input" - "github.com/lightningnetwork/lnd/kvdb" lnmock "github.com/lightningnetwork/lnd/lntest/mock" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" @@ -1034,8 +1033,7 @@ type testCtx struct { func createTestCtxSingleNode(t *testing.T, startingHeight uint32) *testCtx { - graph := makeTestGraph(t, true) - + graph := graphdb.MakeTestGraph(t) sourceNode := createTestNode(t) require.NoError(t, @@ -1082,32 +1080,6 @@ func (c *testCtx) RestartBuilder(t *testing.T) { c.builder = builder } -// makeTestGraph creates a new instance of a channeldb.ChannelGraph for testing -// purposes. -func makeTestGraph(t *testing.T, useCache bool) *graphdb.ChannelGraph { - t.Helper() - - // Create channelgraph for the first time. - backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr") - require.NoError(t, err) - - t.Cleanup(backendCleanup) - - graphStore, err := graphdb.NewKVStore(backend) - require.NoError(t, err) - - graph, err := graphdb.NewChannelGraph( - graphStore, graphdb.WithUseGraphCache(useCache), - ) - require.NoError(t, err) - require.NoError(t, graph.Start()) - t.Cleanup(func() { - require.NoError(t, graph.Stop()) - }) - - return graph -} - type testGraphInstance struct { graph *graphdb.ChannelGraph diff --git a/peer/setup_test.go b/peer/setup_test.go new file mode 100644 index 000000000..db41fd531 --- /dev/null +++ b/peer/setup_test.go @@ -0,0 +1,11 @@ +package peer + +import ( + "testing" + + "github.com/lightningnetwork/lnd/kvdb" +) + +func TestMain(m *testing.M) { + kvdb.RunTests(m) +} diff --git a/peer/test_utils.go b/peer/test_utils.go index 4d2bae8c7..673eceed8 100644 --- a/peer/test_utils.go +++ b/peer/test_utils.go @@ -23,7 +23,6 @@ import ( "github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/keychain" - "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/lntest/channels" "github.com/lightningnetwork/lnd/lntest/mock" "github.com/lightningnetwork/lnd/lntypes" @@ -603,29 +602,13 @@ func createTestPeer(t *testing.T) *peerTestCtx { const chanActiveTimeout = time.Minute - dbPath := t.TempDir() - - graphBackend, err := kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{ - DBPath: dbPath, - DBFileName: "graph.db", - NoFreelistSync: true, - AutoCompact: false, - AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge, - DBTimeout: kvdb.DefaultDBTimeout, - }) - require.NoError(t, err) - - graphStore, err := graphdb.NewKVStore(graphBackend) - require.NoError(t, err) - - dbAliceGraph, err := graphdb.NewChannelGraph(graphStore) - require.NoError(t, err) + dbAliceGraph := graphdb.MakeTestGraph(t) require.NoError(t, dbAliceGraph.Start()) t.Cleanup(func() { require.NoError(t, dbAliceGraph.Stop()) }) - dbAliceChannel := channeldb.OpenForTesting(t, dbPath) + dbAliceChannel := channeldb.OpenForTesting(t, t.TempDir()) nodeSignerAlice := netann.NewNodeSigner(aliceKeySigner) diff --git a/routing/pathfind_test.go b/routing/pathfind_test.go index 42ba75019..c497978c9 100644 --- a/routing/pathfind_test.go +++ b/routing/pathfind_test.go @@ -159,28 +159,21 @@ func makeTestGraph(t *testing.T, useCache bool) (*graphdb.ChannelGraph, kvdb.Backend, error) { // Create channelgraph for the first time. - backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr") - if err != nil { - return nil, nil, err - } - - t.Cleanup(backendCleanup) - - graphStore, err := graphdb.NewKVStore(backend) - require.NoError(t, err) - - graph, err := graphdb.NewChannelGraph( - graphStore, graphdb.WithUseGraphCache(useCache), - ) - if err != nil { - return nil, nil, err - } + graph := graphdb.MakeTestGraph(t, graphdb.WithUseGraphCache(useCache)) require.NoError(t, graph.Start()) t.Cleanup(func() { require.NoError(t, graph.Stop()) }) - return graph, backend, nil + mcBackend, backendCleanup, err := kvdb.GetTestBackend( + t.TempDir(), "mission_control", + ) + if err != nil { + return nil, nil, err + } + t.Cleanup(backendCleanup) + + return graph, mcBackend, nil } // parseTestGraph returns a fully populated ChannelGraph given a path to a JSON