while True:
logger.info("deploy charm")
await ops_test.model.deploy(
charm_under_test, application_name=app_name, resources=resources, num_units=10
)
await block_until_leader_elected(ops_test, app_name)
if await get_leader_unit_num(ops_test, app_name) > 0:
break
# we're unlucky: unit/0 is the leader, which means no scale down could trigger a
# leadership change event - repeat
logger.info("Elected leader is unit/0 - resetting and repeating")
await ops_test.model.applications[app_name].remove()
await ops_test.model.block_until(lambda: len(ops_test.model.applications) == 0)
await ops_test.model.reset()
Currently my method for testing leadership change is:
https://github.com/canonical/alertmanager-k8s-operator/blob/main/tests/integration/test_rescale_charm.py
Is there a better way?