Skip to content

Commit

Permalink
feat: add host param to url function for wait.ForSQL (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
frozenbonito committed Sep 15, 2022
1 parent f684e45 commit 769d576
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
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

0 comments on commit 769d576

Please sign in to comment.