From 000b59c96f0e203789d876f55c436d40b5077adf Mon Sep 17 00:00:00 2001 From: Frode Nordahl Date: Thu, 20 Feb 2020 18:46:06 +0100 Subject: [PATCH] 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 --- charms_openstack/test_utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/charms_openstack/test_utils.py b/charms_openstack/test_utils.py index ff07118..2c4d99c 100644 --- a/charms_openstack/test_utils.py +++ b/charms_openstack/test_utils.py @@ -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: