Skip to content

Commit

Permalink
Merge pull request #12 from canonical/DPE-4167_no_active_wo_db
Browse files Browse the repository at this point in the history
[DPE-4167] NO active state before Opensearch connection is established
  • Loading branch information
juditnovak authored Apr 25, 2024
2 parents 8d82b44 + 9364a5e commit 5a216b0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from literals import (
CHARM_KEY,
CHARM_USERS,
MSG_DB_MISSING,
MSG_INSTALLING,
MSG_STARTING,
MSG_STARTING_SERVER,
Expand Down Expand Up @@ -135,6 +136,9 @@ def reconcile(self, event: EventBase) -> None:
):
self.on[f"{self.restart.name}"].acquire_lock.emit()

if self.unit.is_leader() and self.state.opensearch_server:
clear_status(self.app, MSG_DB_MISSING)

def _on_secret_changed(self, event: SecretChangedEvent):
"""Reconfigure services on a secret changed event."""
if not event.secret.label:
Expand Down Expand Up @@ -205,13 +209,12 @@ def init_server(self):
logger.info(f"{self.unit.name} started")

# added here in case a `restart` was missed
self.state.unit_server.update(
{
"state": "started",
}
)
self.state.unit_server.update({"state": "started"})
clear_status(self.unit, MSG_STARTING_SERVER)

if self.unit.is_leader() and not self.state.opensearch_server:
self.app.status = BlockedStatus(MSG_DB_MISSING)


if __name__ == "__main__":
main(OpensearchDasboardsCharm)
5 changes: 5 additions & 0 deletions src/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

"""Charmed Machine Operator for Apache Opensearch Dashboards."""

import logging

from ops.model import ActiveStatus, Application, Unit

logger = logging.getLogger(__name__)


def clear_status(scope_obj: Unit | Application, messages: str | list[str]) -> None:
"""Clear status if set."""
if not isinstance(messages, list):
messages = [messages]

if any([scope_obj.status.message == message for message in messages]):
logger.debug(f"Clearing status {messages} from {scope_obj}.")
scope_obj.status = ActiveStatus()
1 change: 1 addition & 0 deletions src/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@
MSG_STARTING_SERVER = "starting Opensearch Dashboards server..."
MSG_WAITING_FOR_USER_CREDENTIALS = "waiting for passwords to be created"
MSG_WAITING_FOR_PEER = "waiting for peer relation"
MSG_DB_MISSING = "Opensearch connection is missing"
6 changes: 2 additions & 4 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,9 @@ async def test_deploy_active(ops_test: OpsTest):
await ops_test.model.block_until(
lambda: len(ops_test.model.applications[APP_NAME].units) == NUM_UNITS_APP
)
await ops_test.model.wait_for_idle(
apps=[APP_NAME], status="active", timeout=1000, idle_period=30
)
await ops_test.model.wait_for_idle(apps=[APP_NAME], timeout=1000, idle_period=30)

assert ops_test.model.applications[APP_NAME].status == "active"
assert ops_test.model.applications[APP_NAME].status == "blocked"

pytest.relation = await ops_test.model.relate(OPENSEARCH_CHARM, APP_NAME)
await ops_test.model.wait_for_idle(
Expand Down
23 changes: 22 additions & 1 deletion tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def test_restart_restarts_with_sleep(harness):
assert patched_sleep.call_count >= 1


def test_init_server_calls_necessary_methods(harness):
def test_init_server_calls_necessary_methods_non_leader(harness):
with harness.hooks_disabled():
peer_rel_id = harness.add_relation(PEER, CHARM_KEY)
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/0")
Expand All @@ -269,6 +269,27 @@ def test_init_server_calls_necessary_methods(harness):
assert isinstance(harness.charm.unit.status, ActiveStatus)


def test_init_server_calls_necessary_methods_leader(harness):
with harness.hooks_disabled():
peer_rel_id = harness.add_relation(PEER, CHARM_KEY)
harness.set_leader(True)
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/0")
harness.update_relation_data(peer_rel_id, f"{CHARM_KEY}", {"monitor-password": "bla"})

with (
patch("managers.config.ConfigManager.set_dashboard_properties") as dashboard_properties,
patch("workload.ODWorkload.start") as start,
):
harness.charm.init_server()

dashboard_properties.assert_called_once()
start.assert_called_once()

assert harness.charm.state.unit_server.started
assert isinstance(harness.charm.app.status, BlockedStatus)
assert isinstance(harness.charm.unit.status, ActiveStatus)


def test_config_changed_applies_relation_data(harness):
with harness.hooks_disabled():
_ = harness.add_relation(PEER, CHARM_KEY)
Expand Down

0 comments on commit 5a216b0

Please sign in to comment.