Skip to content

Commit 71bd0f4

Browse files
authored
fix(trino): resolve the mapped host port in get_connection_url() (#1112)
Fixes #1111 ### What's wrong `TrinoContainer.get_connection_url()` builds the URL as `trino://{user}@{host}:{self.port}`, and `self.port` is the fixed container-internal port set in `__init__` (8080 by default) — never resolved through `get_exposed_port()`. Since Docker maps that to a random host port, the URL this method returns can't actually connect to anything. The existing test (`test_docker_run_trino`) didn't catch this because it bypasses `get_connection_url()` entirely, connecting via `trino.dbapi.connect()` with an explicitly resolved `get_exposed_port(trino.port)` instead. ### The fix One-line change, matching the pattern `CockroachDBContainer` and `CrateDBContainer` already use via `_create_connection_url()`: ```diff def get_connection_url(self): - return f"trino://{self.user}@{self.get_container_host_ip()}:{self.port}" + return f"trino://{self.user}@{self.get_container_host_ip()}:{self.get_exposed_port(self.port)}" ``` Also added `test_get_connection_url`, which actually exercises `get_connection_url()` via SQLAlchemy (the way a real user would use it), matching the sibling `cockroachdb`/`cratedb` tests' pattern. Ran locally against a real container — passes with the fix, and the bug is reproducible by inspection: `trino.port` and `trino.get_exposed_port(trino.port)` return different values whenever Docker doesn't happen to map the container port 1:1 to the host, which is the common case. ### Where this was found Building a nightly CI job in `apache/superset` that tests its Trino `db_engine_specs` module against a real Trino instance via this library — more context in the linked issue. # PR Checklist - [x] PR title follows Conventional Commits (`fix(trino):`), scoped to the community module per the community-modules note in the PR template. - [x] Maintainer edits are allowed on this branch. - [x] Change is in `src/testcontainers/community/trino/` with a corresponding test in `tests/community/trino/`. - [x] No new dependencies added. - [x] Branch is up to date with `main`.
1 parent 8d81011 commit 71bd0f4

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎src/testcontainers/community/trino/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(
3838
)
3939

4040
def get_connection_url(self):
41-
return f"trino://{self.user}@{self.get_container_host_ip()}:{self.port}"
41+
return f"trino://{self.user}@{self.get_container_host_ip()}:{self.get_exposed_port(self.port)}"
4242

4343
def _configure(self):
4444
pass

‎tests/community/trino/test_trino.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sqlalchemy
12
from trino.dbapi import connect
23

34
from testcontainers.community.trino import TrinoContainer
@@ -16,3 +17,12 @@ def test_docker_run_trino():
1617
rows = cur.fetchall()
1718
assert rows[0][0] == "451"
1819
conn.close()
20+
21+
22+
def test_get_connection_url():
23+
container = TrinoContainer("trinodb/trino:451")
24+
with container as trino:
25+
engine = sqlalchemy.create_engine(trino.get_connection_url())
26+
with engine.connect() as connection:
27+
result = connection.execute(sqlalchemy.text("SELECT version()"))
28+
assert result.fetchone()[0] == "451"

0 commit comments

Comments
 (0)