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.
This commit is contained in:
Elle Mouton
2025-05-09 11:26:37 +02:00
parent 7a348e324d
commit dc353dc50e
8 changed files with 40 additions and 84 deletions

View File

@@ -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())

11
autopilot/setup_test.go Normal file
View File

@@ -0,0 +1,11 @@
package autopilot
import (
"testing"
"github.com/lightningnetwork/lnd/kvdb"
)
func TestMain(m *testing.M) {
kvdb.RunTests(m)
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

11
peer/setup_test.go Normal file
View File

@@ -0,0 +1,11 @@
package peer
import (
"testing"
"github.com/lightningnetwork/lnd/kvdb"
)
func TestMain(m *testing.M) {
kvdb.RunTests(m)
}

View File

@@ -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)

View File

@@ -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