|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | +from os import environ |
| 14 | +from typing import Optional |
| 15 | +from urllib.error import HTTPError, URLError |
| 16 | +from urllib.request import urlopen |
| 17 | + |
| 18 | +from testcontainers.core.generic import DbContainer |
| 19 | +from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs |
| 20 | + |
| 21 | + |
| 22 | +class CockroachDBContainer(DbContainer): |
| 23 | + """ |
| 24 | + CockroachDB database container. |
| 25 | +
|
| 26 | + Example: |
| 27 | +
|
| 28 | + The example will spin up a CockroachDB database to which you can connect with the credentials |
| 29 | + passed in the constructor. Alternatively, you may use the :code:`get_connection_url()` |
| 30 | + method which returns a sqlalchemy-compatible url in format |
| 31 | + :code:`dialect+driver://username:password@host:port/database`. |
| 32 | +
|
| 33 | + .. doctest:: |
| 34 | +
|
| 35 | + >>> import sqlalchemy |
| 36 | + >>> from testcontainers.cockroachdb import CockroachDBContainer |
| 37 | +
|
| 38 | + >>> with CockroachDBContainer('cockroachdb/cockroach:v24.1.1') as crdb: |
| 39 | + ... engine = sqlalchemy.create_engine(crdb.get_connection_url()) |
| 40 | + ... with engine.begin() as connection: |
| 41 | + ... result = connection.execute(sqlalchemy.text("select version()")) |
| 42 | + ... version, = result.fetchone() |
| 43 | +
|
| 44 | + """ |
| 45 | + |
| 46 | + COCKROACH_DB_PORT: int = 26257 |
| 47 | + COCKROACH_API_PORT: int = 8080 |
| 48 | + |
| 49 | + def __init__( |
| 50 | + self, |
| 51 | + image: str = "cockroachdb/cockroach:v24.1.1", |
| 52 | + username: Optional[str] = None, |
| 53 | + password: Optional[str] = None, |
| 54 | + dbname: Optional[str] = None, |
| 55 | + dialect="cockroachdb+psycopg2", |
| 56 | + **kwargs, |
| 57 | + ) -> None: |
| 58 | + super().__init__(image, **kwargs) |
| 59 | + |
| 60 | + self.with_exposed_ports(self.COCKROACH_DB_PORT, self.COCKROACH_API_PORT) |
| 61 | + self.username = username or environ.get("COCKROACH_USER", "cockroach") |
| 62 | + self.password = password or environ.get("COCKROACH_PASSWORD", "arthropod") |
| 63 | + self.dbname = dbname or environ.get("COCKROACH_DATABASE", "roach") |
| 64 | + self.dialect = dialect |
| 65 | + |
| 66 | + def _configure(self) -> None: |
| 67 | + self.with_env("COCKROACH_DATABASE", self.dbname) |
| 68 | + self.with_env("COCKROACH_USER", self.username) |
| 69 | + self.with_env("COCKROACH_PASSWORD", self.password) |
| 70 | + |
| 71 | + cmd = "start-single-node" |
| 72 | + if not self.password: |
| 73 | + cmd += " --insecure" |
| 74 | + self.with_command(cmd) |
| 75 | + |
| 76 | + @wait_container_is_ready(HTTPError, URLError) |
| 77 | + def _connect(self) -> None: |
| 78 | + host = self.get_container_host_ip() |
| 79 | + url = f"http://{host}:{self.get_exposed_port(self.COCKROACH_API_PORT)}/health" |
| 80 | + self._wait_for_health(url) |
| 81 | + wait_for_logs(self, "finished creating default user*") |
| 82 | + |
| 83 | + @staticmethod |
| 84 | + def _wait_for_health(url): |
| 85 | + with urlopen(url) as response: |
| 86 | + response.read() |
| 87 | + |
| 88 | + def get_connection_url(self) -> str: |
| 89 | + conn_str = super()._create_connection_url( |
| 90 | + dialect=self.dialect, |
| 91 | + username=self.username, |
| 92 | + password=self.password, |
| 93 | + dbname=self.dbname, |
| 94 | + port=self.COCKROACH_DB_PORT, |
| 95 | + ) |
| 96 | + |
| 97 | + if self.password: |
| 98 | + conn_str += "?sslmode=require" |
| 99 | + |
| 100 | + return conn_str |
0 commit comments