From dd41c56d864a0ecde74bf1981f4f70dec2189a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20C=2E=20Mass=C3=B3n?= <939888+Abuelodelanada@users.noreply.github.com> Date: Fri, 27 Sep 2024 08:48:16 -0300 Subject: [PATCH] observe on.start event to make start up process more robust (#292) * observe on.start event to avoid intermediate erros status * fix scenario tests --- src/charm.py | 5 +++++ tests/scenario/conftest.py | 2 ++ tests/scenario/helpers.py | 13 +++++++++++-- tox.ini | 2 +- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/charm.py b/src/charm.py index 8758a6ff..40a7a3cf 100755 --- a/src/charm.py +++ b/src/charm.py @@ -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}" @@ -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() diff --git a/tests/scenario/conftest.py b/tests/scenario/conftest.py index e241438f..8193a4a8 100644 --- a/tests/scenario/conftest.py +++ b/tests/scenario/conftest.py @@ -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 diff --git a/tests/scenario/helpers.py b/tests/scenario/helpers.py index 06609e6a..caf19a37 100644 --- a/tests/scenario/helpers.py +++ b/tests/scenario/helpers.py @@ -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") diff --git a/tox.ini b/tox.ini index b406e637..2d744f55 100644 --- a/tox.ini +++ b/tox.ini @@ -100,7 +100,7 @@ commands = description = Scenario tests deps = pytest - ops-scenario>=6 + ops-scenario<7 cosl -r{toxinidir}/requirements.txt commands =