test_utils: handle non-idempotent handlers on reload

A pattern across our charms is to run
``charms_openstack.charm.use_defaults()`` at the global level
on top of the module encapsulating the reactive handlers
(i.e. ``src/reactive/charm_handlers.py``).

An example of an non-idempotent handler is the
``charm.default-select-release`` default handler, it keeps
global state and throws a RuntimeError on subsequent calls.

The way TestRegisteredHooks reloads the module makes it
impossible to mock this out, and I think the only solution
would be to mock this out in the test helper library itself.

Closes-Bug: #1864088
Change-Id: I9ebbda895a7a748d8efa4e23eb0c3bdf660fa759
This commit is contained in:
Frode Nordahl 2020-02-20 18:46:06 +01:00
parent d0f6150d4d
commit 000b59c96f
No known key found for this signature in database
GPG Key ID: 6A5D59A3BA48373F
1 changed files with 9 additions and 0 deletions

View File

@ -18,6 +18,8 @@ import unittest
import charmhelpers.core.unitdata as unitdata
import charms_openstack.charm.core as chm_core
class PatchHelper(unittest.TestCase):
"""Helper Test Class based on unittest.TestCase which provides an easy way
@ -155,6 +157,9 @@ class TestRegisteredHooks(PatchHelper):
def tearDownClass(cls):
# and fix any breakage we did to the module
if cls._module:
# protect against charm code calling use_defaults in global
# scope referencing non-idempotent handlers
chm_core._release_selector_function = None
try:
reload(cls._module)
except NameError:
@ -193,6 +198,10 @@ class TestRegisteredHooks(PatchHelper):
# force requires to rerun the mock_hook decorator:
# try except is Python2/Python3 compatibility as Python3 has moved
# reload to importlib.
# protect against charm code calling use_defaults in global
# scope referencing non-idempotent handlers
chm_core._release_selector_function = None
try:
reload(module)
except NameError: