Skip to content

Commit d3ed261

Browse files
committed
sqldb: gate postgres fixtures behind build tag
1 parent 8df972d commit d3ed261

7 files changed

Lines changed: 166 additions & 32 deletions

File tree

docs/release-notes/release-notes-0.22.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353

5454
## Code Health
5555

56+
* [Limited the Docker-backed Postgres test fixtures to `test_db_postgres`
57+
builds](https://github.com/lightningnetwork/lnd/pull/10792), keeping
58+
test-only Docker dependencies out of normal `sqldb` builds.
59+
5660
## Tooling and Documentation
5761

5862
# Contributors (Alphabetical Order)

sqldb/postgres_fixture.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd)
1+
//go:build test_db_postgres && !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd)
22

33
package sqldb
44

sqldb/postgres_fixture_stub.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//go:build !test_db_postgres
2+
3+
package sqldb
4+
5+
import (
6+
"database/sql"
7+
"testing"
8+
"time"
9+
)
10+
11+
const PostgresTag = "11"
12+
13+
// TestPgFixture is only available when built with the test_db_postgres tag.
14+
type TestPgFixture struct {
15+
db *sql.DB
16+
}
17+
18+
// NewTestPgFixture constructs a new Postgres fixture when test_db_postgres is
19+
// enabled.
20+
func NewTestPgFixture(t testing.TB, _ time.Duration) *TestPgFixture {
21+
postgresFixtureUnavailable(t)
22+
return nil
23+
}
24+
25+
// GetConfig returns the full config of the Postgres node.
26+
func (*TestPgFixture) GetConfig(string) *PostgresConfig {
27+
panic("Postgres test fixture requires the test_db_postgres build tag")
28+
}
29+
30+
// TearDown stops the underlying docker container.
31+
func (*TestPgFixture) TearDown(t testing.TB) {
32+
t.Helper()
33+
}
34+
35+
// randomDBName generates a random database name.
36+
func randomDBName(t testing.TB) string {
37+
postgresFixtureUnavailable(t)
38+
return ""
39+
}
40+
41+
// NewTestPostgresDB is a helper function that creates a Postgres database for
42+
// testing using the given fixture.
43+
func NewTestPostgresDB(t testing.TB, _ *TestPgFixture) *PostgresStore {
44+
postgresFixtureUnavailable(t)
45+
return nil
46+
}
47+
48+
// NewTestPostgresDBWithVersion is a helper function that creates a Postgres
49+
// database for testing and migrates it to the given version.
50+
func NewTestPostgresDBWithVersion(t *testing.T, _ *TestPgFixture,
51+
_ uint) *PostgresStore {
52+
53+
postgresFixtureUnavailable(t)
54+
return nil
55+
}
56+
57+
func postgresFixtureUnavailable(t testing.TB) {
58+
t.Helper()
59+
t.Skip("Postgres test fixture requires the test_db_postgres build tag")
60+
}

sqldb/v2/docker_name.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//go:build test_db_postgres && !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd)
2+
3+
package sqldb
4+
5+
import "strings"
6+
7+
// sanitizeDockerName returns a Docker-safe container name.
8+
func sanitizeDockerName(name string) string {
9+
sanitized := strings.Map(func(r rune) rune {
10+
switch {
11+
case r >= 'a' && r <= 'z':
12+
return r
13+
14+
case r >= 'A' && r <= 'Z':
15+
return r
16+
17+
case r >= '0' && r <= '9':
18+
return r
19+
20+
case r == '_', r == '-':
21+
return r
22+
23+
default:
24+
return '_'
25+
}
26+
}, name)
27+
28+
sanitized = strings.Trim(sanitized, "_.-")
29+
if sanitized == "" {
30+
return "postgresql-container"
31+
}
32+
33+
return sanitized
34+
}

sqldb/v2/postgres_fixture.go

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd)
1+
//go:build test_db_postgres && !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd)
22

33
package sqldb
44

@@ -116,35 +116,6 @@ func NewTestPgFixture(t testing.TB, expiry time.Duration) *TestPgFixture {
116116
return fixture
117117
}
118118

119-
// sanitizeDockerName returns a Docker-safe container name.
120-
func sanitizeDockerName(name string) string {
121-
sanitized := strings.Map(func(r rune) rune {
122-
switch {
123-
case r >= 'a' && r <= 'z':
124-
return r
125-
126-
case r >= 'A' && r <= 'Z':
127-
return r
128-
129-
case r >= '0' && r <= '9':
130-
return r
131-
132-
case r == '_', r == '-':
133-
return r
134-
135-
default:
136-
return '_'
137-
}
138-
}, name)
139-
140-
sanitized = strings.Trim(sanitized, "_.-")
141-
if sanitized == "" {
142-
return "postgresql-container"
143-
}
144-
145-
return sanitized
146-
}
147-
148119
// GetConfig returns the full config of the Postgres node.
149120
func (f *TestPgFixture) GetConfig(dbName string) *PostgresConfig {
150121
return &PostgresConfig{

sqldb/v2/postgres_fixture_stub.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//go:build !test_db_postgres
2+
3+
package sqldb
4+
5+
import (
6+
"database/sql"
7+
"testing"
8+
"time"
9+
)
10+
11+
const PostgresTag = "15"
12+
13+
// TestPgFixture is only available when built with the test_db_postgres tag.
14+
type TestPgFixture struct{}
15+
16+
// NewTestPgFixture constructs a new Postgres fixture when test_db_postgres is
17+
// enabled.
18+
func NewTestPgFixture(t testing.TB, _ time.Duration) *TestPgFixture {
19+
postgresFixtureUnavailable(t)
20+
return nil
21+
}
22+
23+
// GetConfig returns the full config of the Postgres node.
24+
func (*TestPgFixture) GetConfig(string) *PostgresConfig {
25+
panic("Postgres test fixture requires the test_db_postgres build tag")
26+
}
27+
28+
// TearDown stops the underlying docker container.
29+
func (*TestPgFixture) TearDown(t testing.TB) {
30+
t.Helper()
31+
}
32+
33+
// DB returns the fixture database.
34+
func (*TestPgFixture) DB() *sql.DB {
35+
panic("Postgres test fixture requires the test_db_postgres build tag")
36+
}
37+
38+
// RandomDBName generates a random database name.
39+
func RandomDBName(t testing.TB) string {
40+
postgresFixtureUnavailable(t)
41+
return ""
42+
}
43+
44+
// NewTestPostgresDB is a helper function that creates a Postgres database for
45+
// testing using the given fixture.
46+
func NewTestPostgresDB(t testing.TB, _ *TestPgFixture,
47+
_ []MigrationSet) *PostgresStore {
48+
49+
postgresFixtureUnavailable(t)
50+
return nil
51+
}
52+
53+
// NewTestPostgresDBWithVersion is a helper function that creates a Postgres
54+
// database for testing and migrates it to the given version.
55+
func NewTestPostgresDBWithVersion(t testing.TB, _ *TestPgFixture,
56+
_ MigrationSet, _ uint) *PostgresStore {
57+
58+
postgresFixtureUnavailable(t)
59+
return nil
60+
}
61+
62+
func postgresFixtureUnavailable(t testing.TB) {
63+
t.Helper()
64+
t.Skip("Postgres test fixture requires the test_db_postgres build tag")
65+
}

sqldb/v2/postgres_fixture_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd)
1+
//go:build test_db_postgres && !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd)
22

33
package sqldb
44

0 commit comments

Comments
 (0)