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