|
| 1 | +import tarfile |
| 2 | +import time |
| 3 | +from io import BytesIO |
| 4 | +from textwrap import dedent |
| 5 | + |
| 6 | +from testcontainers.core.container import DockerContainer |
| 7 | +from testcontainers.core.waiting_utils import wait_for_logs |
| 8 | + |
| 9 | + |
| 10 | +class RedpandaContainer(DockerContainer): |
| 11 | + """ |
| 12 | + Redpanda container. |
| 13 | +
|
| 14 | + Example: |
| 15 | +
|
| 16 | + .. doctest:: |
| 17 | +
|
| 18 | + >>> from testcontainers.redpanda import RedpandaContainer |
| 19 | +
|
| 20 | + >>> with RedpandaContainer() as redpanda: |
| 21 | + ... connection = redpanda.get_bootstrap_server() |
| 22 | + """ |
| 23 | + |
| 24 | + TC_START_SCRIPT = "/tc-start.sh" |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + image: str = "docker.redpanda.com/redpandadata/redpanda:v23.1.13", |
| 29 | + **kwargs, |
| 30 | + ) -> None: |
| 31 | + kwargs["entrypoint"] = "sh" |
| 32 | + super(RedpandaContainer, self).__init__(image, **kwargs) |
| 33 | + self.redpanda_port = 9092 |
| 34 | + self.schema_registry_port = 8081 |
| 35 | + self.with_exposed_ports(self.redpanda_port, self.schema_registry_port) |
| 36 | + |
| 37 | + def get_bootstrap_server(self) -> str: |
| 38 | + host = self.get_container_host_ip() |
| 39 | + port = self.get_exposed_port(self.redpanda_port) |
| 40 | + return f"{host}:{port}" |
| 41 | + |
| 42 | + def get_schema_registry_address(self) -> str: |
| 43 | + host = self.get_container_host_ip() |
| 44 | + port = self.get_exposed_port(self.schema_registry_port) |
| 45 | + return f"http://{host}:{port}" |
| 46 | + |
| 47 | + def tc_start(self) -> None: |
| 48 | + host = self.get_container_host_ip() |
| 49 | + port = self.get_exposed_port(self.redpanda_port) |
| 50 | + |
| 51 | + data = ( |
| 52 | + dedent( |
| 53 | + f""" |
| 54 | + #!/bin/bash |
| 55 | + /usr/bin/rpk redpanda start --mode dev-container --smp 1 --memory 1G \ |
| 56 | + --kafka-addr PLAINTEXT://0.0.0.0:29092,OUTSIDE://0.0.0.0:9092 \ |
| 57 | + --advertise-kafka-addr PLAINTEXT://127.0.0.1:29092,OUTSIDE://{host}:{port} |
| 58 | + """ |
| 59 | + ) |
| 60 | + .strip() |
| 61 | + .encode("utf-8") |
| 62 | + ) |
| 63 | + |
| 64 | + self.create_file(data, RedpandaContainer.TC_START_SCRIPT) |
| 65 | + |
| 66 | + def start(self, timeout=10) -> "RedpandaContainer": |
| 67 | + script = RedpandaContainer.TC_START_SCRIPT |
| 68 | + command = f'-c "while [ ! -f {script} ]; do sleep 0.1; done; sh {script}"' |
| 69 | + self.with_command(command) |
| 70 | + super().start() |
| 71 | + self.tc_start() |
| 72 | + wait_for_logs(self, r".*Started Kafka API server.*", timeout=timeout) |
| 73 | + return self |
| 74 | + |
| 75 | + def create_file(self, content: bytes, path: str) -> None: |
| 76 | + with BytesIO() as archive, tarfile.TarFile(fileobj=archive, mode="w") as tar: |
| 77 | + tarinfo = tarfile.TarInfo(name=path) |
| 78 | + tarinfo.size = len(content) |
| 79 | + tarinfo.mtime = time.time() |
| 80 | + tar.addfile(tarinfo, BytesIO(content)) |
| 81 | + archive.seek(0) |
| 82 | + self.get_wrapped_container().put_archive("/", archive) |
0 commit comments