|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2025 Canonical Ltd. |
| 3 | +# See LICENSE file for licensing details. |
| 4 | + |
| 5 | + |
| 6 | +import logging |
| 7 | +import subprocess |
| 8 | +from types import SimpleNamespace |
| 9 | + |
| 10 | +from pytest_operator.plugin import OpsTest |
| 11 | +from tenacity import Retrying, stop_after_delay, wait_fixed |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +K8S_DB_MODEL_NAME = "database" |
| 17 | +MICROK8S_CLOUD_NAME = "cloudk8s" |
| 18 | + |
| 19 | + |
| 20 | +CONFIG_OPTS = {"workload_name": "test_mode", "parallel_processes": 1} |
| 21 | +SERIES = "jammy" |
| 22 | +KAFKA = "kafka" |
| 23 | +APP_NAME = "kafka-benchmark" |
| 24 | +KAFKA_CHANNEL = "3/edge" |
| 25 | +DEFAULT_NUM_UNITS = 2 |
| 26 | + |
| 27 | + |
| 28 | +KRAFT_CONFIG = { |
| 29 | + "profile": "testing", |
| 30 | + "roles": "broker,controller", |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +MODEL_CONFIG = { |
| 35 | + "logging-config": "<root>=INFO;unit=DEBUG", |
| 36 | + "update-status-hook-interval": "1m", |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +def check_service(svc_name: str, unit_id: int = 0, retry_if_fail: bool = True) -> bool | None: |
| 41 | + def __check(): |
| 42 | + try: |
| 43 | + return ( |
| 44 | + subprocess.check_output( |
| 45 | + [ |
| 46 | + "juju", |
| 47 | + "ssh", |
| 48 | + f"{APP_NAME}/{unit_id}", |
| 49 | + "--", |
| 50 | + "sudo", |
| 51 | + "systemctl", |
| 52 | + "is-active", |
| 53 | + svc_name, |
| 54 | + ], |
| 55 | + text=True, |
| 56 | + ).rstrip() |
| 57 | + == "active" |
| 58 | + ) |
| 59 | + except Exception: |
| 60 | + return False |
| 61 | + |
| 62 | + if not retry_if_fail: |
| 63 | + return __check() |
| 64 | + for attempt in Retrying(stop=stop_after_delay(150), wait=wait_fixed(15)): |
| 65 | + with attempt: |
| 66 | + return __check() |
| 67 | + |
| 68 | + |
| 69 | +async def run_action( |
| 70 | + ops_test, action_name: str, unit_name: str, timeout: int = 30, **action_kwargs |
| 71 | +): |
| 72 | + """Runs the given action on the given unit.""" |
| 73 | + client_unit = ops_test.model.units.get(unit_name) |
| 74 | + action = await client_unit.run_action(action_name, **action_kwargs) |
| 75 | + result = await action.wait() |
| 76 | + logging.info(f"request results: {result.results}") |
| 77 | + return SimpleNamespace(status=result.status or "completed", response=result.results) |
| 78 | + |
| 79 | + |
| 80 | +async def get_leader_unit_id(ops_test: OpsTest, app: str = APP_NAME) -> int: |
| 81 | + """Helper function that retrieves the leader unit ID.""" |
| 82 | + leader_unit = None |
| 83 | + for unit in ops_test.model.applications[app].units: |
| 84 | + if await unit.is_leader_from_status(): |
| 85 | + leader_unit = unit |
| 86 | + break |
| 87 | + |
| 88 | + if not leader_unit: |
| 89 | + raise ValueError("Leader unit not found") |
| 90 | + |
| 91 | + return int(leader_unit.name.split("/")[1]) |
0 commit comments