From f0d91ae31b13ce1dcbbda282fc18fa7694ebd494 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Tue, 12 Jun 2018 14:30:09 +0100 Subject: [PATCH] Fix caching of model name and remove debug lines --- unit_tests/test_zaza_model.py | 11 ++++++++++- unit_tests/utils.py | 2 -- zaza/model.py | 7 +++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/unit_tests/test_zaza_model.py b/unit_tests/test_zaza_model.py index b038c68..e7e315b 100644 --- a/unit_tests/test_zaza_model.py +++ b/unit_tests/test_zaza_model.py @@ -10,6 +10,11 @@ import zaza.model as model class TestModel(ut_utils.BaseTestCase): + def tearDown(self): + super(TestModel, self).tearDown() + # Clear cached model name + model.CURRENT_MODEL = None + def setUp(self): super(TestModel, self).setUp() @@ -126,7 +131,6 @@ class TestModel(ut_utils.BaseTestCase): def test_get_juju_model(self): self.patch_object(model.os, 'environ') self.patch_object(model, 'get_current_model') - model.CURRENT_MODEL = None self.get_current_model.return_value = 'modelsmodel' def _get_env(key): @@ -138,6 +142,11 @@ class TestModel(ut_utils.BaseTestCase): self.assertEqual(model.get_juju_model(), 'envmodel') self.get_current_model.assert_not_called() + def test_get_juju_model_noenv(self): + self.patch_object(model.os, 'environ') + self.patch_object(model, 'get_current_model') + self.get_current_model.return_value = 'modelsmodel' + # No envirnment variable self.environ.__getitem__.side_effect = KeyError self.assertEqual(model.get_juju_model(), 'modelsmodel') diff --git a/unit_tests/utils.py b/unit_tests/utils.py index 370262c..e1d0fb6 100644 --- a/unit_tests/utils.py +++ b/unit_tests/utils.py @@ -47,7 +47,6 @@ class BaseTestCase(unittest.TestCase): def tearDown(self): for k, v in self._patches.items(): - print("Stopping patch {} {}".format(k, v)) v.stop() setattr(self, k, None) self._patches = None @@ -66,7 +65,6 @@ class BaseTestCase(unittest.TestCase): if new is None: started.return_value = return_value self._patches_start[name] = started - print("Starting patch {}".format(name)) setattr(self, name, started) def patch(self, item, return_value=None, name=None, new=None, **kwargs): diff --git a/zaza/model.py b/zaza/model.py index b5ddba2..7e1058a 100644 --- a/zaza/model.py +++ b/zaza/model.py @@ -15,6 +15,7 @@ from zaza import sync_wrapper CURRENT_MODEL = None + def set_juju_model(model_name): """Point environment at the given model @@ -33,6 +34,7 @@ def get_juju_model(): :returns: In focus model name :rtype: str """ + global CURRENT_MODEL if CURRENT_MODEL: return CURRENT_MODEL # LY: I think we should remove the KeyError handling. I don't think we @@ -41,10 +43,11 @@ def get_juju_model(): # zaza will loose the ability to do concurrent runs. try: # Check the environment - return os.environ["JUJU_MODEL"] + CURRENT_MODEL = os.environ["JUJU_MODEL"] except KeyError: # If unset connect get the current active model - return get_current_model() + CURRENT_MODEL = get_current_model() + return CURRENT_MODEL async def deployed(filter=None):