From 62f0b4769b72b6acd93184a0cadad5e7ea3126a8 Mon Sep 17 00:00:00 2001 From: Alex Kavanagh Date: Wed, 6 Sep 2017 20:38:34 +0100 Subject: [PATCH] Move application version set to charms.openstack It comes from layer-openstack in the default assess_status handler. The version of the application deployed should be set on all assess_status calls, not just during the 'update-status' hook execution. Change-Id: I1ca762875f307db9bb15dd80b0c6a01a9e17b71e Partial-Bug: 1715429 --- charms_openstack/charm/core.py | 6 ++++++ unit_tests/charms_openstack/charm/test_core.py | 17 +++++++++++++++-- unit_tests/charms_openstack/charm/utils.py | 5 +++-- unit_tests/utils.py | 7 ++++--- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/charms_openstack/charm/core.py b/charms_openstack/charm/core.py index 5de9dcc..6c5f8bb 100644 --- a/charms_openstack/charm/core.py +++ b/charms_openstack/charm/core.py @@ -772,6 +772,12 @@ class BaseOpenStackCharmAssessStatus(object): SIDE EFFECT: this function calls status_set(state, message) to set the workload status in juju. """ + # set the application version when we set the status (always) + # NOTE(tinwood) this is not, strictly speaking, good code organisation, + # as the 'application_version' property is in the classes.py file. + # However, as this is ALWAYS a mixin on that class, we can get away + # with this. + hookenv.application_version_set(self.application_version) for f in [self.check_if_paused, self.custom_assess_status_check, self.check_interfaces, diff --git a/unit_tests/charms_openstack/charm/test_core.py b/unit_tests/charms_openstack/charm/test_core.py index b0f7135..af2cbd0 100644 --- a/unit_tests/charms_openstack/charm/test_core.py +++ b/unit_tests/charms_openstack/charm/test_core.py @@ -151,11 +151,19 @@ class TestProvideCharmInstance(utils.BaseTestCase): self.assertEqual(charm, 'the-charm') +class AssessStatusCharm(MyOpenStackCharm): + release = 'juno' + + @property + def application_version(self): + return None + + class TestBaseOpenStackCharmAssessStatus(BaseOpenStackCharmTest): def setUp(self): def make_open_stack_charm(): - return MyOpenStackCharm(['interface1', 'interface2']) + return AssessStatusCharm(['interface1', 'interface2']) super().setUp(make_open_stack_charm, TEST_CONFIG) @@ -180,8 +188,13 @@ class TestBaseOpenStackCharmAssessStatus(BaseOpenStackCharmTest): self.patch_target('custom_assess_status_check', return_value=(None, None)) self.patch_target('check_services_running', return_value=(None, None)) - self.target._assess_status() + self.patch_object(chm_core.hookenv, 'application_version_set') + with mock.patch.object(AssessStatusCharm, 'application_version', + new_callable=mock.PropertyMock, + return_value="abc"): + self.target._assess_status() self.status_set.assert_called_once_with('active', 'Unit is ready') + self.application_version_set.assert_called_once_with("abc") # check all the check functions got called self.check_if_paused.assert_called_once_with() self.check_interfaces.assert_called_once_with() diff --git a/unit_tests/charms_openstack/charm/utils.py b/unit_tests/charms_openstack/charm/utils.py index 9446c82..348f7b8 100644 --- a/unit_tests/charms_openstack/charm/utils.py +++ b/unit_tests/charms_openstack/charm/utils.py @@ -31,6 +31,7 @@ class BaseOpenStackCharmTest(unit_tests.utils.BaseTestCase): chm_core._singleton = None super().tearDown() - def patch_target(self, attr, return_value=None, name=None, new=None): + def patch_target(self, attr, return_value=None, name=None, new=None, + **kwargs): # uses BaseTestCase.patch_object() to patch targer. - self.patch_object(self.target, attr, return_value, name, new) + self.patch_object(self.target, attr, return_value, name, new, **kwargs) diff --git a/unit_tests/utils.py b/unit_tests/utils.py index f3ef09c..12315ef 100644 --- a/unit_tests/utils.py +++ b/unit_tests/utils.py @@ -52,13 +52,14 @@ class BaseTestCase(unittest.TestCase): self._patches = None self._patches_start = None - def patch_object(self, obj, attr, return_value=None, name=None, new=None): + def patch_object(self, obj, attr, return_value=None, name=None, new=None, + **kwargs): if name is None: name = attr if new is not None: - mocked = mock.patch.object(obj, attr, new=new) + mocked = mock.patch.object(obj, attr, new=new, **kwargs) else: - mocked = mock.patch.object(obj, attr) + mocked = mock.patch.object(obj, attr, **kwargs) self._patches[name] = mocked started = mocked.start() if new is None: