From fd042afdf186ab419ee35ea9678735895c62b723 Mon Sep 17 00:00:00 2001 From: "Erlon R. Cruz" Date: Tue, 21 Mar 2023 16:45:30 -0300 Subject: [PATCH] Skip version caching for subordinate charms Caching the charm OpenStack version will cause undesired behaviors if the charm is a subordinate. The charm local cache is firstly populated during the charm install. If the charm is a subordinate, the version will remain the same regardless of future OpenStack upgrades because today, the hook that updates the cache to the new version in only called for principal charms. Related-bug: #1949074 Change-Id: I76abbd29ca910fe4c4d62da09e2d2dd3b5c798a6 --- charms_openstack/charm/defaults.py | 17 +++++++++++++++-- .../charms_openstack/charm/test_defaults.py | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/charms_openstack/charm/defaults.py b/charms_openstack/charm/defaults.py index 6fdcbd0..baccf91 100644 --- a/charms_openstack/charm/defaults.py +++ b/charms_openstack/charm/defaults.py @@ -1,4 +1,5 @@ import charmhelpers.contrib.openstack.utils as os_utils +import charmhelpers.core.hookenv as hookenv import charmhelpers.core.unitdata as unitdata import charms.reactive as reactive @@ -97,7 +98,15 @@ def make_default_select_release_handler(): that it doesn't need to keep going and getting it from the package information. """ - release_version = unitdata.kv().get(OPENSTACK_RELEASE_KEY, None) + release_version = None + # Using the cached OpenStack version will cause undesired behaviors + # if the charm is a subordinate. The charm local cache is firstly + # populated during the charm install and after that, only during the + # openstack upgrades. If the charm is a subordinate, the version will + # always remain the same. + if not hookenv.is_subordinate(): + release_version = unitdata.kv().get(OPENSTACK_RELEASE_KEY, None) + if release_version is None: try: # First make an attempt of determining release from a charm @@ -125,7 +134,11 @@ def make_default_select_release_handler(): pkg = 'dummy-package' release_version = os_utils.os_release( pkg, source_key=singleton.source_config_key) - unitdata.kv().set(OPENSTACK_RELEASE_KEY, release_version) + + # Skip caching the release if the charm is a subordinate. + if not hookenv.is_subordinate(): + unitdata.kv().set(OPENSTACK_RELEASE_KEY, release_version) + return release_version diff --git a/unit_tests/charms_openstack/charm/test_defaults.py b/unit_tests/charms_openstack/charm/test_defaults.py index 8a2fbe0..0b5a1ce 100644 --- a/unit_tests/charms_openstack/charm/test_defaults.py +++ b/unit_tests/charms_openstack/charm/test_defaults.py @@ -113,6 +113,7 @@ class TestDefaults(BaseOpenStackCharmTest): singleton.source_config_key = 'fake-config-key' singleton.get_os_codename_package.return_value = None self.patch_object(chm, 'get_charm_instance', return_value=singleton) + self.patch_object(chm.hookenv, 'is_subordinate', return_value=False) # set a release kv.get.return_value = 'one' release = h.map['function']() @@ -153,6 +154,19 @@ class TestDefaults(BaseOpenStackCharmTest): self.assertEqual(release, 'four') singleton.get_os_codename_package.assert_called_once_with( mock.ANY, mock.ANY, apt_cache_sufficient=True) + # Test subordinate charm + kv.reset_mock() + kv.get.return_value = None + singleton.get_os_codename_package.reset_mock() + singleton.get_os_codename_package.return_value = 'five' + chm.hookenv.is_subordinate.reset_mock() + chm.hookenv.is_subordinate.return_value = True + self.os_release.reset_mock() + self.os_release.return_value = 'five' + release = h.map['function']() + self.assertEqual(release, 'five') + kv.set.assert_not_called() + kv.get.assert_not_called() def test_default_select_package_type_handler(self): self.assertIn('charm.default-select-package-type',