Skip to content

Commit

Permalink
Set units to blocked status when scaling beyond 1 (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielArndt authored Oct 16, 2023
1 parent 4ef21c7 commit 0e5a4f5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ def __init__(self, *args):
"""Initialize charm."""
super().__init__(*args)
if not self.unit.is_leader():
raise NotImplementedError("Scaling is not implemented for this charm")
# NOTE: In cases where leader status is lost before the charm is
# finished processing all teardown events, this prevents teardown
# event code from running. Luckily, for this charm, none of the
# teardown code is necessary to preform if we're removing the
# charm.
self.unit.status = BlockedStatus("Scaling is not implemented for this charm")
return
self._container_name = self._service_name = "nrf"
self._container = self.unit.get_container(self._container_name)
self._database = DatabaseRequires(
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@


import logging
from collections import Counter
from pathlib import Path

import pytest
import yaml
from juju.application import Application
from pytest_operator.plugin import OpsTest

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -94,3 +96,21 @@ async def test_restore_tls_and_wait_for_active_status(ops_test: OpsTest, build_a
)
await ops_test.model.integrate(relation1=APP_NAME, relation2=TLS_APPLICATION_NAME)
await ops_test.model.wait_for_idle(apps=[APP_NAME], status="active", timeout=1000)


@pytest.mark.abort_on_fail
async def test_when_scale_nrf_beyond_1_then_only_one_unit_is_active(
ops_test: OpsTest, build_and_deploy
):
assert ops_test.model
assert isinstance(app := ops_test.model.applications[APP_NAME], Application)
await app.scale(3)
await ops_test.model.wait_for_idle(apps=[APP_NAME], timeout=1000)
unit_statuses = Counter(unit.workload_status for unit in app.units)
assert unit_statuses.get("active") == 1
assert unit_statuses.get("blocked") == 2


async def test_remove_nrf(ops_test: OpsTest, build_and_deploy):
assert ops_test.model
await ops_test.model.remove_application(APP_NAME, block_until_done=True)

0 comments on commit 0e5a4f5

Please sign in to comment.