Skip to content

Commit

Permalink
adds Harness.hooks_disabled, a context manager (#393)
Browse files Browse the repository at this point in the history
* adds Harness.hooks_disabled, a context manager
  • Loading branch information
chipaca authored Sep 2, 2020
1 parent fc63ee5 commit 5c698d7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
21 changes: 20 additions & 1 deletion ops/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import inspect
import pathlib
import random
from textwrap import dedent
import tempfile
import typing
import yaml
from contextlib import contextmanager
from textwrap import dedent

from ops import (
charm,
Expand Down Expand Up @@ -321,6 +322,24 @@ def enable_hooks(self) -> None:
"""
self._hooks_enabled = True

@contextmanager
def hooks_disabled(self):
"""A context manager to run code with hooks disabled.
Example::
with harness.hooks_disabled():
# things in here don't fire events
harness.set_leader(True)
harness.update_config(unset=['foo', 'bar'])
# things here will again fire events
"""
self.disable_hooks()
try:
yield None
finally:
self.enable_hooks()

def _next_relation_id(self):
rel_id = self._relation_id_counter
self._relation_id_counter += 1
Expand Down
22 changes: 22 additions & 0 deletions test/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,28 @@ def test_hooks_enabled_and_disabled(self):
harness.charm.get_changes(reset=True),
[{'name': 'config-changed', 'data': {'value': 'fourth', 'third': '3'}}])

def test_hooks_disabled_contextmanager(self):
harness = Harness(RecordingCharm, meta='''
name: test-charm
''')
self.addCleanup(harness.cleanup)
# Before begin() there are no events.
harness.update_config({'value': 'first'})
# By default, after begin the charm is set up to receive events.
harness.begin()
harness.update_config({'value': 'second'})
self.assertEqual(
harness.charm.get_changes(reset=True),
[{'name': 'config-changed', 'data': {'value': 'second'}}])
# Once disabled, we won't see config-changed when we make an update
with harness.hooks_disabled():
harness.update_config({'third': '3'})
self.assertEqual(harness.charm.get_changes(reset=True), [])
harness.update_config({'value': 'fourth'})
self.assertEqual(
harness.charm.get_changes(reset=True),
[{'name': 'config-changed', 'data': {'value': 'fourth', 'third': '3'}}])

def test_metadata_from_directory(self):
tmp = pathlib.Path(tempfile.mkdtemp())
self.addCleanup(shutil.rmtree, str(tmp))
Expand Down

0 comments on commit 5c698d7

Please sign in to comment.