Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add host param to url function for wait.ForSQL #524

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions e2e/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func TestContainerWithWaitForSQL(t *testing.T) {
"POSTGRES_DB": dbname,
}
var port = "5432/tcp"
dbURL := func(port nat.Port) string {
return fmt.Sprintf("postgres://postgres:password@localhost:%s/%s?sslmode=disable", port.Port(), dbname)
dbURL := func(host string, port nat.Port) string {
return fmt.Sprintf("postgres://postgres:password@%s:%s/%s?sslmode=disable", host, port.Port(), dbname)
}

t.Run("default query", func(t *testing.T) {
Expand Down
11 changes: 8 additions & 3 deletions wait/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
const defaultForSqlQuery = "SELECT 1"

//ForSQL constructs a new waitForSql strategy for the given driver
func ForSQL(port nat.Port, driver string, url func(nat.Port) string) *waitForSql {
func ForSQL(port nat.Port, driver string, url func(string, nat.Port) string) *waitForSql {
return &waitForSql{
Port: port,
URL: url,
Expand All @@ -24,7 +24,7 @@ func ForSQL(port nat.Port, driver string, url func(nat.Port) string) *waitForSql
}

type waitForSql struct {
URL func(port nat.Port) string
URL func(host string, port nat.Port) string
Driver string
Port nat.Port
startupTimeout time.Duration
Expand Down Expand Up @@ -57,6 +57,11 @@ func (w *waitForSql) WaitUntilReady(ctx context.Context, target StrategyTarget)
ctx, cancel := context.WithTimeout(ctx, w.startupTimeout)
defer cancel()

host, err := target.Host(ctx)
if err != nil {
return
}

ticker := time.NewTicker(w.PollInterval)
defer ticker.Stop()

Expand All @@ -72,7 +77,7 @@ func (w *waitForSql) WaitUntilReady(ctx context.Context, target StrategyTarget)
}
}

db, err := sql.Open(w.Driver, w.URL(port))
db, err := sql.Open(w.Driver, w.URL(host, port))
if err != nil {
return fmt.Errorf("sql.Open: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions wait/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func Test_waitForSql_WithQuery(t *testing.T) {
t.Run("default query", func(t *testing.T) {
w := ForSQL("5432/tcp", "postgres", func(port nat.Port) string {
w := ForSQL("5432/tcp", "postgres", func(host string, port nat.Port) string {
return "fake-url"
})

Expand All @@ -19,7 +19,7 @@ func Test_waitForSql_WithQuery(t *testing.T) {
t.Run("custom query", func(t *testing.T) {
const q = "SELECT 100;"

w := ForSQL("5432/tcp", "postgres", func(port nat.Port) string {
w := ForSQL("5432/tcp", "postgres", func(host string, port nat.Port) string {
return "fake-url"
}).WithQuery(q)

Expand Down