diff --git a/docs/release-notes/release-notes-0.22.0.md b/docs/release-notes/release-notes-0.22.0.md index c3367af9cc7..82e16f6eaf5 100644 --- a/docs/release-notes/release-notes-0.22.0.md +++ b/docs/release-notes/release-notes-0.22.0.md @@ -53,6 +53,10 @@ ## Code Health +* [Limited the Docker-backed Postgres test fixtures to `test_db_postgres` + builds](https://github.com/lightningnetwork/lnd/pull/10792), keeping + test-only Docker dependencies out of normal `sqldb` builds. + ## Tooling and Documentation # Contributors (Alphabetical Order) diff --git a/sqldb/postgres_fixture.go b/sqldb/postgres_fixture.go index 6cae3e07575..2c6364694a4 100644 --- a/sqldb/postgres_fixture.go +++ b/sqldb/postgres_fixture.go @@ -1,4 +1,4 @@ -//go:build !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd) +//go:build test_db_postgres && !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd) package sqldb diff --git a/sqldb/postgres_fixture_stub.go b/sqldb/postgres_fixture_stub.go new file mode 100644 index 00000000000..875c4193259 --- /dev/null +++ b/sqldb/postgres_fixture_stub.go @@ -0,0 +1,60 @@ +//go:build !test_db_postgres + +package sqldb + +import ( + "database/sql" + "testing" + "time" +) + +const PostgresTag = "11" + +// TestPgFixture is only available when built with the test_db_postgres tag. +type TestPgFixture struct { + db *sql.DB +} + +// NewTestPgFixture constructs a new Postgres fixture when test_db_postgres is +// enabled. +func NewTestPgFixture(t testing.TB, _ time.Duration) *TestPgFixture { + postgresFixtureUnavailable(t) + return nil +} + +// GetConfig returns the full config of the Postgres node. +func (*TestPgFixture) GetConfig(string) *PostgresConfig { + panic("Postgres test fixture requires the test_db_postgres build tag") +} + +// TearDown stops the underlying docker container. +func (*TestPgFixture) TearDown(t testing.TB) { + t.Helper() +} + +// randomDBName generates a random database name. +func randomDBName(t testing.TB) string { + postgresFixtureUnavailable(t) + return "" +} + +// NewTestPostgresDB is a helper function that creates a Postgres database for +// testing using the given fixture. +func NewTestPostgresDB(t testing.TB, _ *TestPgFixture) *PostgresStore { + postgresFixtureUnavailable(t) + return nil +} + +// NewTestPostgresDBWithVersion is a helper function that creates a Postgres +// database for testing and migrates it to the given version. +func NewTestPostgresDBWithVersion(t *testing.T, _ *TestPgFixture, + _ uint) *PostgresStore { + + postgresFixtureUnavailable(t) + return nil +} + +func postgresFixtureUnavailable(t testing.TB) { + t.Helper() + t.Skip("Postgres test fixture requires the test_db_postgres build tag") +} diff --git a/sqldb/v2/docker_name.go b/sqldb/v2/docker_name.go new file mode 100644 index 00000000000..15df157b787 --- /dev/null +++ b/sqldb/v2/docker_name.go @@ -0,0 +1,34 @@ +//go:build test_db_postgres && !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd) + +package sqldb + +import "strings" + +// sanitizeDockerName returns a Docker-safe container name. +func sanitizeDockerName(name string) string { + sanitized := strings.Map(func(r rune) rune { + switch { + case r >= 'a' && r <= 'z': + return r + + case r >= 'A' && r <= 'Z': + return r + + case r >= '0' && r <= '9': + return r + + case r == '_', r == '-': + return r + + default: + return '_' + } + }, name) + + sanitized = strings.Trim(sanitized, "_.-") + if sanitized == "" { + return "postgresql-container" + } + + return sanitized +} diff --git a/sqldb/v2/postgres_fixture.go b/sqldb/v2/postgres_fixture.go index 6918e3cbdad..aec23f538b6 100644 --- a/sqldb/v2/postgres_fixture.go +++ b/sqldb/v2/postgres_fixture.go @@ -1,4 +1,4 @@ -//go:build !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd) +//go:build test_db_postgres && !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd) package sqldb @@ -116,35 +116,6 @@ func NewTestPgFixture(t testing.TB, expiry time.Duration) *TestPgFixture { return fixture } -// sanitizeDockerName returns a Docker-safe container name. -func sanitizeDockerName(name string) string { - sanitized := strings.Map(func(r rune) rune { - switch { - case r >= 'a' && r <= 'z': - return r - - case r >= 'A' && r <= 'Z': - return r - - case r >= '0' && r <= '9': - return r - - case r == '_', r == '-': - return r - - default: - return '_' - } - }, name) - - sanitized = strings.Trim(sanitized, "_.-") - if sanitized == "" { - return "postgresql-container" - } - - return sanitized -} - // GetConfig returns the full config of the Postgres node. func (f *TestPgFixture) GetConfig(dbName string) *PostgresConfig { return &PostgresConfig{ diff --git a/sqldb/v2/postgres_fixture_stub.go b/sqldb/v2/postgres_fixture_stub.go new file mode 100644 index 00000000000..b67c96357b2 --- /dev/null +++ b/sqldb/v2/postgres_fixture_stub.go @@ -0,0 +1,65 @@ +//go:build !test_db_postgres + +package sqldb + +import ( + "database/sql" + "testing" + "time" +) + +const PostgresTag = "15" + +// TestPgFixture is only available when built with the test_db_postgres tag. +type TestPgFixture struct{} + +// NewTestPgFixture constructs a new Postgres fixture when test_db_postgres is +// enabled. +func NewTestPgFixture(t testing.TB, _ time.Duration) *TestPgFixture { + postgresFixtureUnavailable(t) + return nil +} + +// GetConfig returns the full config of the Postgres node. +func (*TestPgFixture) GetConfig(string) *PostgresConfig { + panic("Postgres test fixture requires the test_db_postgres build tag") +} + +// TearDown stops the underlying docker container. +func (*TestPgFixture) TearDown(t testing.TB) { + t.Helper() +} + +// DB returns the fixture database. +func (*TestPgFixture) DB() *sql.DB { + panic("Postgres test fixture requires the test_db_postgres build tag") +} + +// RandomDBName generates a random database name. +func RandomDBName(t testing.TB) string { + postgresFixtureUnavailable(t) + return "" +} + +// NewTestPostgresDB is a helper function that creates a Postgres database for +// testing using the given fixture. +func NewTestPostgresDB(t testing.TB, _ *TestPgFixture, + _ []MigrationSet) *PostgresStore { + + postgresFixtureUnavailable(t) + return nil +} + +// NewTestPostgresDBWithVersion is a helper function that creates a Postgres +// database for testing and migrates it to the given version. +func NewTestPostgresDBWithVersion(t testing.TB, _ *TestPgFixture, + _ MigrationSet, _ uint) *PostgresStore { + + postgresFixtureUnavailable(t) + return nil +} + +func postgresFixtureUnavailable(t testing.TB) { + t.Helper() + t.Skip("Postgres test fixture requires the test_db_postgres build tag") +} diff --git a/sqldb/v2/postgres_fixture_test.go b/sqldb/v2/postgres_fixture_test.go index 4cc086cc9ec..775ce20a845 100644 --- a/sqldb/v2/postgres_fixture_test.go +++ b/sqldb/v2/postgres_fixture_test.go @@ -1,4 +1,4 @@ -//go:build !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd) +//go:build test_db_postgres && !js && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) && !(netbsd || openbsd) package sqldb