-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathconfig_test.go
More file actions
151 lines (127 loc) · 4.16 KB
/
Copy pathconfig_test.go
File metadata and controls
151 lines (127 loc) · 4.16 KB
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
145
146
147
148
149
150
151
package terminal
import (
"database/sql"
"os"
"path/filepath"
"testing"
"github.com/lightninglabs/lightning-terminal/db"
"github.com/stretchr/testify/require"
)
// TestBlockStartupForPostgresIfSqliteDBExists verifies that a Postgres startup
// is rejected when the default SQLite database file still exists for the
// selected network.
func TestBlockStartupForPostgresIfSqliteDBExists(t *testing.T) {
litDir := t.TempDir()
sqlitePath := filepath.Join(
litDir, "regtest", defaultSqliteDatabaseFileName,
)
fixture := db.NewTestPgFixture(
t, db.DefaultPostgresFixtureLifetime, true,
)
t.Cleanup(func() {
fixture.TearDown(t)
})
require.NoError(t, os.MkdirAll(filepath.Dir(sqlitePath), 0700))
require.NoError(t, os.WriteFile(sqlitePath, []byte("sqlite"), 0600))
cfg := &Config{
DatabaseBackend: DatabaseBackendPostgres,
LitDir: litDir,
Network: "regtest",
Postgres: fixture.GetConfig(),
}
err := validateExclusiveSQLBackends(t.Context(), cfg, litDir)
require.Error(t, err)
require.Contains(t, err.Error(), "sqlite database file already exists")
require.Contains(t, err.Error(), sqlitePath)
}
// TestBlockStartupForSqliteIfPostgresDBExists verifies that a SQLite startup is
// rejected when the configured Postgres database already exists.
func TestBlockStartupForSqliteIfPostgresDBExists(t *testing.T) {
fixture := db.NewTestPgFixture(
t, db.DefaultPostgresFixtureLifetime, true,
)
t.Cleanup(func() {
fixture.TearDown(t)
})
cfg := &Config{
DatabaseBackend: DatabaseBackendSqlite,
Postgres: fixture.GetConfig(),
}
err := validateExclusiveSQLBackends(t.Context(), cfg, "")
require.Error(t, err)
require.Contains(t, err.Error(), "postgres database")
require.Contains(t, err.Error(), cfg.Postgres.DBName)
}
// TestDontBlockSqliteOnlyStartup verifies that SQLite startup is allowed when
// no concrete Postgres database is configured.
func TestDontBlockSqliteOnlyStartup(t *testing.T) {
cfg := &Config{
DatabaseBackend: DatabaseBackendSqlite,
}
require.NoError(
t, validateExclusiveSQLBackends(t.Context(), cfg, ""),
)
}
// TestDontBlockSqliteStartupIfConfiguredPostgresDoesntExist verifies that a
// SQLite startup is allowed when the configured Postgres database does not
// exist.
func TestDontBlockSqliteStartupIfConfiguredPostgresDoesntExist(t *testing.T) {
fixture := db.NewTestPgFixture(
t, db.DefaultPostgresFixtureLifetime, true,
)
t.Cleanup(func() {
fixture.TearDown(t)
})
pgCfg := fixture.GetConfig()
pgCfg.DBName = "does_not_exist_for_config_validation"
cfg := &Config{
DatabaseBackend: DatabaseBackendSqlite,
Postgres: pgCfg,
}
require.NoError(
t, validateExclusiveSQLBackends(t.Context(), cfg, ""),
)
// We also validate that the validateExclusiveSQLBackends passed because
// the db with the configured DBName doesn't exist and not because the
// connection parameters are wrong. This proves that we're ok with a
// postgres setup existing, as long as the specific database doesn't
// exist.
dbConn, err := sql.Open("postgres", pgCfg.DSN(false))
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, dbConn.Close())
})
err = dbConn.Ping()
require.Error(t, err)
require.True(t, isMissingPostgresDatabase(err))
}
// TestDontBlockPostgresOnlyStartup verifies that a Postgres is allowed when
// no sqlite database file exists for the selected network, despite the actual
// folder where the file would be placed exists.
func TestDontBlockPostgresOnlyStartup(t *testing.T) {
litDir := t.TempDir()
sqlitePath := filepath.Join(
litDir, "regtest", defaultSqliteDatabaseFileName,
)
fixture := db.NewTestPgFixture(
t, db.DefaultPostgresFixtureLifetime, true,
)
t.Cleanup(func() {
fixture.TearDown(t)
})
require.NoError(t, os.MkdirAll(filepath.Dir(sqlitePath), 0700))
// NOTE: we don't write any file to the sqlite path here, so the sqlite
// database file never exists, only the default directory where it would
// be located.
cfg := &Config{
DatabaseBackend: DatabaseBackendPostgres,
LitDir: litDir,
Network: "regtest",
Postgres: fixture.GetConfig(),
}
require.NoError(
t, validateExclusiveSQLBackends(
t.Context(), cfg, litDir,
),
)
}