-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcmd_migrate_db_postgres_test.go
144 lines (118 loc) · 3.33 KB
/
cmd_migrate_db_postgres_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//go:build kvdb_postgres
package main
import (
"context"
"crypto/rand"
"database/sql"
"encoding/hex"
"fmt"
"testing"
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/kvdb/postgres"
"github.com/lightningnetwork/lnd/kvdb/sqlbase"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/stretchr/testify/require"
)
const (
testMaxConnections = 100
testDsnTemplate = "postgres://postgres:[email protected]:9877/%s?sslmode=disable"
)
// PostgresTestSetup holds the test configuration for Postgres.
type PostgresTestSetup struct {
tempDir string
dbName string
postgres *embeddedpostgres.EmbeddedPostgres
stopFunc func() error
}
// setupEmbeddedPostgres initializes and starts the embedded postgres instance.
func setupEmbeddedPostgres(t *testing.T) *PostgresTestSetup {
sqlbase.Init(testMaxConnections)
setup := &PostgresTestSetup{}
// Initialize embedded postgres
setup.postgres = embeddedpostgres.NewDatabase(
embeddedpostgres.DefaultConfig().
Port(9877).
StartParameters(map[string]string{
"max_connections": fmt.Sprintf("%d", testMaxConnections),
}),
)
// Start postgres
err := setup.postgres.Start()
require.NoError(t, err, "failed to start postgres")
setup.stopFunc = setup.postgres.Stop
return setup
}
// createTestDatabase creates a new random test database.
func (p *PostgresTestSetup) createTestDatabase(t *testing.T) {
// Generate random database name
randBytes := make([]byte, 8)
_, err := rand.Read(randBytes)
require.NoError(t, err)
p.dbName = "test_" + hex.EncodeToString(randBytes)
// Create the database
dbConn, err := sql.Open("pgx", p.getDsn("postgres"))
require.NoError(t, err)
defer dbConn.Close()
_, err = dbConn.ExecContext(
context.Background(),
"CREATE DATABASE "+p.dbName,
)
require.NoError(t, err)
}
// setupTestDir creates and sets up the temporary test directory.
func (p *PostgresTestSetup) setupTestDir(t *testing.T) {
p.tempDir = setupTestData(t)
}
// getDsn returns the DSN for the specified database.
func (p *PostgresTestSetup) getDsn(dbName string) string {
return fmt.Sprintf(testDsnTemplate, dbName)
}
// cleanup performs necessary cleanup.
func (p *PostgresTestSetup) cleanup() error {
if p.stopFunc != nil {
return p.stopFunc()
}
return nil
}
// getDBConfigs returns the source and destination DB configs.
func (p *PostgresTestSetup) getDBConfigs() (*SourceDB, *DestDB) {
sourceDB := &SourceDB{
Backend: lncfg.BoltBackend,
Bolt: &Bolt{
DBTimeout: kvdb.DefaultDBTimeout,
DataDir: p.tempDir,
TowerDir: p.tempDir,
},
}
destDB := &DestDB{
Backend: lncfg.PostgresBackend,
Postgres: &postgres.Config{
Dsn: p.getDsn(p.dbName),
},
}
return sourceDB, destDB
}
// TestMigrateDBPostgres tests the migration of a database from Bolt to
// Postgres.
func TestMigrateDBPostgres(t *testing.T) {
t.Parallel()
// Setup postgres.
setup := setupEmbeddedPostgres(t)
defer func() {
require.NoError(t, setup.cleanup())
}()
// Setup test environment.
setup.setupTestDir(t)
setup.createTestDatabase(t)
sourceDB, destDB := setup.getDBConfigs()
// Create and run migration command.
cmd := &migrateDBCommand{
Source: sourceDB,
Dest: destDB,
Network: "regtest",
ChunkSize: 1024,
}
err := cmd.Execute(nil)
require.NoError(t, err, "failed to execute migration")
}