|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2025 Canonical Ltd. |
| 3 | +# See LICENSE file for licensing details. |
| 4 | + |
| 5 | +import logging |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import jubilant |
| 9 | +import yaml |
| 10 | + |
| 11 | +from integration.helpers.cassandra import OPERATOR_PASSWORD |
| 12 | +from integration.helpers.continuous_writes import ContinuousWrites |
| 13 | +from integration.helpers.juju import ( |
| 14 | + app_secret_extract, |
| 15 | + get_hosts, |
| 16 | + get_leader_unit, |
| 17 | + get_non_leader_units, |
| 18 | + scale_sequentially_to, |
| 19 | +) |
| 20 | + |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +def test_deploy(juju: jubilant.Juju, cassandra_charm: Path, app_name: str) -> None: |
| 25 | + juju.deploy( |
| 26 | + cassandra_charm, |
| 27 | + app=app_name, |
| 28 | + config={"profile": "testing"}, |
| 29 | + num_units=3, |
| 30 | + ) |
| 31 | + juju.wait( |
| 32 | + ready=lambda status: jubilant.all_agents_idle(status) and jubilant.all_active(status), |
| 33 | + delay=20, |
| 34 | + timeout=2400, |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +def test_seed_primary(juju: jubilant.Juju, app_name: str) -> None: |
| 39 | + _, leader_status = get_leader_unit(juju, app_name) |
| 40 | + for unit, _ in juju.status().apps[app_name].units.items(): |
| 41 | + assert f"{leader_status.public_address}:7000" in get_seeds(juju, unit) |
| 42 | + |
| 43 | + |
| 44 | +def test_kill_primary( |
| 45 | + juju: jubilant.Juju, app_name: str, continuous_writes: ContinuousWrites |
| 46 | +) -> None: |
| 47 | + continuous_writes.start( |
| 48 | + hosts=get_hosts(juju, app_name), |
| 49 | + password=app_secret_extract(juju, app_name, OPERATOR_PASSWORD), |
| 50 | + replication_factor=3, |
| 51 | + ) |
| 52 | + |
| 53 | + leader, _ = get_leader_unit(juju, app_name) |
| 54 | + kill_unit(juju, leader) |
| 55 | + |
| 56 | + juju.wait( |
| 57 | + ready=lambda status: jubilant.all_agents_idle(status) and jubilant.all_active(status), |
| 58 | + delay=20, |
| 59 | + timeout=1800, |
| 60 | + ) |
| 61 | + continuous_writes.assert_new_writes() |
| 62 | + |
| 63 | + new_leader, _ = get_leader_unit(juju, app_name) |
| 64 | + assert leader != new_leader |
| 65 | + |
| 66 | + |
| 67 | +def test_seed_new_primary(juju: jubilant.Juju, app_name: str) -> None: |
| 68 | + _, leader_status = get_leader_unit(juju, app_name) |
| 69 | + for unit, _ in juju.status().apps[app_name].units.items(): |
| 70 | + assert f"{leader_status.public_address}:7000" in get_seeds(juju, unit) |
| 71 | + |
| 72 | + |
| 73 | +def test_kill_subordinate( |
| 74 | + juju: jubilant.Juju, app_name: str, continuous_writes: ContinuousWrites |
| 75 | +) -> None: |
| 76 | + scale_sequentially_to(juju, app_name, 3) |
| 77 | + |
| 78 | + subordinate = get_non_leader_units(juju, app_name)[0] |
| 79 | + kill_unit(juju, subordinate) |
| 80 | + |
| 81 | + juju.wait( |
| 82 | + ready=lambda status: jubilant.all_agents_idle(status) and jubilant.all_active(status), |
| 83 | + delay=20, |
| 84 | + timeout=1200, |
| 85 | + ) |
| 86 | + continuous_writes.stop_and_assert_writes() |
| 87 | + |
| 88 | + |
| 89 | +def kill_unit(juju: jubilant.Juju, unit: str) -> None: |
| 90 | + juju.cli("remove-unit", "--force", "--no-wait", "--no-prompt", "--destroy-storage", unit) |
| 91 | + |
| 92 | + |
| 93 | +def get_seeds(juju: jubilant.Juju, unit: str) -> list[str]: |
| 94 | + config_file = juju.ssh( |
| 95 | + unit, "cat /var/snap/charmed-cassandra/current/etc/cassandra/cassandra.yaml" |
| 96 | + ) |
| 97 | + config = yaml.load(config_file, yaml.Loader) |
| 98 | + return config["seed_provider"][0]["parameters"][0]["seeds"].split(",") |
0 commit comments