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

fix(clickhouse): clickhouse waiting #428

Merged
merged 4 commits into from
Mar 9, 2024
Merged
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
16 changes: 9 additions & 7 deletions modules/clickhouse/testcontainers/clickhouse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
# under the License.
import os
from typing import Optional

import clickhouse_driver
from clickhouse_driver.errors import Error
from urllib.error import HTTPError, URLError
from urllib.request import urlopen

from testcontainers.core.generic import DbContainer
from testcontainers.core.utils import raise_for_deprecated_parameter
Expand Down Expand Up @@ -48,7 +47,7 @@ def __init__(
username: Optional[str] = None,
password: Optional[str] = None,
dbname: Optional[str] = None,
**kwargs
**kwargs,
) -> None:
raise_for_deprecated_parameter(kwargs, "user", "username")
super().__init__(image=image, **kwargs)
Expand All @@ -57,11 +56,14 @@ def __init__(
self.dbname = dbname or os.environ.get("CLICKHOUSE_DB", "test")
self.port = port
self.with_exposed_ports(self.port)
self.with_exposed_ports(8123)

@wait_container_is_ready(Error, EOFError)
@wait_container_is_ready(HTTPError, URLError)
def _connect(self) -> None:
with clickhouse_driver.Client.from_url(self.get_connection_url()) as client:
client.execute("SELECT version()")
# noinspection HttpUrlsUsage
url = f"http://{self.get_container_host_ip()}:{self.get_exposed_port(8123)}"
with urlopen(url) as r:
assert b"Ok" in r.read()

def _configure(self) -> None:
self.with_env("CLICKHOUSE_USER", self.username)
Expand Down