Skip to content

fix: strip port/proto suffix in wait.ForSQL URL callback#4

Merged
maxryl merged 2 commits into
masterfrom
fix/wait-sql-port-suffix
Apr 22, 2026
Merged

fix: strip port/proto suffix in wait.ForSQL URL callback#4
maxryl merged 2 commits into
masterfrom
fix/wait-sql-port-suffix

Conversation

@maxryl

@maxryl maxryl commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Since testcontainers-go v0.42.0 (chore!: migrate to moby modules, #3591), wait.ForSQL's URL callback receives the port as network.Port.String() ("<num>/<proto>", e.g. "32769/tcp") instead of the bare numeric port. Our callback at container.go was splicing that directly into the URL path, making pgx parse the dbname as tcp/<configured-db>; every SQL probe failed with database "tcp/<db>" does not exist until the 10 s wait timeout expired with a misleading get state: context deadline exceeded from testcontainers' own container-inspection call.
  • Fix: strip the /proto suffix via strings.Cut(port, "/") before formatting the DSN.
  • Add container_test.go with a TestMain-scoped container and three tests that cover the exact regression (container comes up, instances are usable, instances are isolated from the template).
  • Add a minimal Test workflow (go test -race on push & PR).
  • Drive-by: SlogAdapter.Printf was passing its variadic args without ..., flagged by go vet (which -race enables). Forward them correctly so fmt.Sprintf sees the intended values.

Root cause reference

github.com/moby/moby/api@v1.54.2/types/network/port.go:

func (p Port) AppendTo(b []byte) []byte {
    if p.IsZero() { return b }
    return fmt.Appendf(b, "%d/%s", p.num, p.proto.Value())
}

Port.String() uses AppendTo, so the port crossing the callback boundary always carries the proto suffix now.

maxryl added 2 commits April 22, 2026 14:33
testcontainers-go v0.42 (moby-modules migration) changed the signature of
wait.ForSQL's URL callback from (host string, port nat.Port) to
(host string, port string), and now passes port.String() which returns
"<num>/<proto>" (e.g. "5432/tcp"). Embedding that directly into the URL
made pgx parse the dbname as "tcp/<configured-db>" — every connect
attempt failed with "database tcp/<db> does not exist" and the wait
loop timed out at 10s with a misleading "get state: context deadline
exceeded" error.

Strip the suffix in the callback before composing the DSN, and add
testcontainer-backed regression tests.
Add a Test workflow that runs `go test -race` on push and PR. `go vet`
(run by `-race`) flags that SlogAdapter.Printf passed its variadic
arguments without spreading, so fmt.Sprintf saw a single []any instead
of the intended args — fix it by forwarding with `v...`.
@maxryl maxryl added the bug Something isn't working label Apr 22, 2026
@maxryl maxryl merged commit b1f7c0a into master Apr 22, 2026
1 check passed
@maxryl maxryl deleted the fix/wait-sql-port-suffix branch April 22, 2026 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant