Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/release-notes/release-notes-0.22.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion sqldb/postgres_fixture.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
60 changes: 60 additions & 0 deletions sqldb/postgres_fixture_stub.go
Original file line number Diff line number Diff line change
@@ -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
}
Comment on lines +14 to +16
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The db field in the TestPgFixture stub is unused, as the stubbed functions either skip the test or panic before accessing any fields. For consistency with the v2 stub (which uses an empty struct) and to keep the stub implementation minimal, consider removing this field. Note that if removed, the database/sql import should also be removed if it becomes unused.

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

// 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")
}
34 changes: 34 additions & 0 deletions sqldb/v2/docker_name.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 1 addition & 30 deletions sqldb/v2/postgres_fixture.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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{
Expand Down
65 changes: 65 additions & 0 deletions sqldb/v2/postgres_fixture_stub.go
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 1 addition & 1 deletion sqldb/v2/postgres_fixture_test.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Loading