charmed-kubernetes / pytest-operator

Apache License 2.0
6 stars 13 forks source link

Means to test leadership change #42

Open sed-i opened 2 years ago

sed-i commented 2 years ago

Currently my method for testing leadership change is:

  1. deploy 10 units and hope the leader is not unit/0
  2. scale down to below the current leader

https://github.com/canonical/alertmanager-k8s-operator/blob/main/tests/integration/test_rescale_charm.py

Is there a better way?

    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()