Skip to content

Commit

Permalink
collect-status is now a lifecycle event
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti committed Nov 23, 2023
1 parent 1517bde commit d8f4e3c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
11 changes: 9 additions & 2 deletions ops/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@

from ops import model
from ops._private import yaml
from ops.framework import EventBase, EventSource, Framework, Object, ObjectEvents
from ops.framework import (
EventBase,
EventSource,
Framework,
LifecycleEvent,
Object,
ObjectEvents,
)

if TYPE_CHECKING:
from typing_extensions import Required, TypedDict
Expand Down Expand Up @@ -830,7 +837,7 @@ def defer(self) -> None:
'this event until you create a new revision.')


class CollectStatusEvent(EventBase):
class CollectStatusEvent(LifecycleEvent):
"""Event triggered at the end of every hook to collect statuses for evaluation.
If the charm wants to provide application or unit status in a consistent
Expand Down
4 changes: 2 additions & 2 deletions ops/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,11 +906,11 @@ def _reemit(self, single_event_path: Optional[str] = None):
if single_event_path is None:
logger.debug("Re-emitting deferred event %s.", event)
elif isinstance(event, LifecycleEvent):
# Ignore Lifecycle events: they are "private" and not interesting.
pass
logger.debug("Emitting lifecycle event %s.", event.handle.kind)
elif self._event_name and self._event_name != event.handle.kind:
# if the event we are emitting now is not the event being
# dispatched, and it also is not an event we have deferred,
# and is also not a lifecycle (framework-emitted) event,
# it must be a custom event
logger.debug("Emitting custom event %s.", event)

Expand Down
1 change: 1 addition & 0 deletions ops/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def setup_root_logging(model_backend: "_ModelBackend", debug: bool = False):
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(JujuLogHandler(model_backend))
logger.handlers
if debug:
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
Expand Down
1 change: 1 addition & 0 deletions test/charms/test_main/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __init__(self, *args):
self.framework.observe(self.on.mon_relation_departed, self._on_mon_relation_departed)
self.framework.observe(self.on.ha_relation_broken, self._on_ha_relation_broken)
self.framework.observe(self.on.test_pebble_ready, self._on_test_pebble_ready)
self.on

self.framework.observe(self.on.secret_remove, self._on_secret_remove)
self.framework.observe(self.on.secret_rotate, self._on_secret_rotate)
Expand Down
21 changes: 15 additions & 6 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ def test_collect_metrics(self):
['juju-log', '--log-level', 'DEBUG', '--', 'Emitting Juju event collect_metrics.'],
['add-metric', '--labels', 'bar=4.2', 'foo=42'],
['is-leader', '--format=json'],
['juju-log', '--log-level', 'DEBUG', '--', 'Emitting lifecycle event commit.']
]
calls = fake_script_calls(self)

Expand All @@ -645,6 +646,7 @@ def test_custom_event(self):
['juju-log', '--log-level', 'DEBUG', '--', 'Emitting Juju event update_status.'],
['juju-log', '--log-level', 'DEBUG', '--', custom_event_prefix],
['is-leader', '--format=json'],
['juju-log', '--log-level', 'DEBUG', '--', 'Emitting lifecycle event commit.']
]
# Remove the "[key]>" suffix from the end of the event string
self.assertRegex(calls[2][-1], re.escape(custom_event_prefix) + '.*')
Expand Down Expand Up @@ -791,9 +793,13 @@ def _call_event(self, rel_path, env):

def test_setup_event_links(self):
"""Test auto-creation of symlinks caused by initial events."""
all_event_hooks = [f"hooks/{name.replace('_', '-')}"
for name, event_source in self.charm_module.Charm.on.events().items()
if issubclass(event_source.event_type, ops.LifecycleEvent)]
# Fixme: this is always an empty list, is this test outdated?
all_event_hooks = [
f"hooks/{name.replace('_', '-')}" for name,
event_source in self.charm_module.Charm.on.events().items() if issubclass(
event_source.event_type,
(ops.CommitEvent,
ops.PreCommitEvent))]

initial_events = {
EventSpec(ops.InstallEvent, 'install'),
Expand Down Expand Up @@ -909,9 +915,10 @@ def test_hook_and_dispatch(self):
['juju-log', '--log-level', 'DEBUG', '--',
'Emitting Juju event install.'],
['is-leader', '--format=json'],
['juju-log', '--log-level', 'DEBUG', '--', 'Emitting lifecycle event commit.']
]
calls = fake_script_calls(self)
self.assertRegex(' '.join(calls.pop(-3)), 'Initializing SQLite local storage: ')
self.assertRegex(' '.join(calls.pop(-4)), 'Initializing SQLite local storage: ')
self.assertEqual(calls, expected)

def test_non_executable_hook_and_dispatch(self):
Expand All @@ -929,9 +936,10 @@ def test_non_executable_hook_and_dispatch(self):
['juju-log', '--log-level', 'DEBUG', '--',
'Emitting Juju event install.'],
['is-leader', '--format=json'],
['juju-log', '--log-level', 'DEBUG', '--', 'Emitting lifecycle event commit.']
]
calls = fake_script_calls(self)
self.assertRegex(' '.join(calls.pop(-3)), 'Initializing SQLite local storage: ')
self.assertRegex(' '.join(calls.pop(-4)), 'Initializing SQLite local storage: ')
self.assertEqual(calls, expected)

def test_hook_and_dispatch_with_failing_hook(self):
Expand Down Expand Up @@ -1012,9 +1020,10 @@ def test_hook_and_dispatch_but_hook_is_dispatch_copy(self):
['juju-log', '--log-level', 'DEBUG', '--',
'Emitting Juju event install.'],
['is-leader', '--format=json'],
['juju-log', '--log-level', 'DEBUG', '--', 'Emitting lifecycle event commit.']
]
calls = fake_script_calls(self)
self.assertRegex(' '.join(calls.pop(-3)), 'Initializing SQLite local storage: ')
self.assertRegex(' '.join(calls.pop(-4)), 'Initializing SQLite local storage: ')

self.assertEqual(calls, expected)

Expand Down

0 comments on commit d8f4e3c

Please sign in to comment.