Skip to content

Commit

Permalink
observe on.start event to make start up process more robust (#292)
Browse files Browse the repository at this point in the history
* observe on.start event to avoid intermediate erros status

* fix scenario tests
  • Loading branch information
Abuelodelanada authored Sep 27, 2024
1 parent 7518ec8 commit dd41c56
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def __init__(self, *args):

# Core lifecycle events
self.framework.observe(self.on.config_changed, self._on_config_changed)
self.framework.observe(self.on.start, self._on_start)

peer_ha_netlocs = [
f"{hostname}:{self._ports.ha}"
Expand Down Expand Up @@ -487,6 +488,10 @@ def _on_config_changed(self, _):
"""Event handler for ConfigChangedEvent."""
self._common_exit_hook(update_ca_certs=True)

def _on_start(self, _):
"""Event handler for ConfigChangedEvent."""
self._common_exit_hook(update_ca_certs=True)

def _on_peer_relation_joined(self, _):
"""Event handler for replica's RelationChangedEvent."""
self._common_exit_hook()
Expand Down
2 changes: 2 additions & 0 deletions tests/scenario/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def alertmanager_charm():
is_ready=tautology,
), patch.object(WorkloadManager, "check_config", lambda *a, **kw: ("ok", "")), patch.object(
WorkloadManager, "_alertmanager_version", property(lambda *_: "0.0.0")
), patch(
"subprocess.run"
):
yield AlertmanagerCharm

Expand Down
13 changes: 11 additions & 2 deletions tests/scenario/helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

from scenario import Container, Context, PeerRelation, Relation, State
from scenario import Container, Context, ExecOutput, PeerRelation, Relation, State


def begin_with_initial_hooks_isolated(context: Context, *, leader: bool = True) -> State:
container = Container("alertmanager", can_connect=False)
container = Container(
"alertmanager",
can_connect=False,
exec_mock={
("update-ca-certificates", "--fresh"): ExecOutput( # this is the command we're mocking
return_code=0, # this data structure contains all we need to mock the call.
stdout="OK",
)
},
)
state = State(config={"config_file": ""}, containers=[container])
peer_rel = PeerRelation("replicas")

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ commands =
description = Scenario tests
deps =
pytest
ops-scenario>=6
ops-scenario<7
cosl
-r{toxinidir}/requirements.txt
commands =
Expand Down

0 comments on commit dd41c56

Please sign in to comment.