From 59efce8bc81fc7800d2cb6ba696028832063a6e0 Mon Sep 17 00:00:00 2001 From: Alan Baghumian Date: Mon, 6 Nov 2023 16:06:45 -0800 Subject: [PATCH] Restore original paste files if glance-common re-installation does not restore them. Implemented a unit test function for the above scenario. Call apt_update from upgrade_charm(). Closes-Bug: #2042792 Change-Id: I57c5fa293b421483b0879f24ddac260e47c7cbef --- hooks/glance_relations.py | 1 + hooks/glance_utils.py | 11 ++++++++++- unit_tests/test_glance_utils.py | 30 ++++++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/hooks/glance_relations.py b/hooks/glance_relations.py index 97a71d02..5659b1c7 100755 --- a/hooks/glance_relations.py +++ b/hooks/glance_relations.py @@ -487,6 +487,7 @@ def cluster_changed(): @harden() def upgrade_charm(): resolve_CONFIGS() + apt_update() apt_install(filter_installed_packages(determine_packages()), fatal=True) packages_removed = remove_old_packages() backup_deprecated_configurations() diff --git a/hooks/glance_utils.py b/hooks/glance_utils.py index d1f5a02d..90e755a7 100644 --- a/hooks/glance_utils.py +++ b/hooks/glance_utils.py @@ -636,7 +636,7 @@ def reinstall_paste_ini(force_reinstall=False): for paste_file in [GLANCE_REGISTRY_PASTE, GLANCE_API_PASTE]: if os.path.exists(paste_file): - os.remove(paste_file) + os.replace(paste_file, f'{paste_file}.charm.upgrade') # glance-registry is deprecated at queens but still # installed. if cmp_release < 'rocky': @@ -647,6 +647,15 @@ def reinstall_paste_ini(force_reinstall=False): apt_install(packages=pkg_list, options=REINSTALL_OPTIONS, fatal=True) + # LP: #2042792 Restore original paste files + for paste_file in [GLANCE_REGISTRY_PASTE, + GLANCE_API_PASTE]: + if not os.path.exists(paste_file): + if os.path.exists(f'{paste_file}.charm.upgrade'): + os.replace(f'{paste_file}.charm.upgrade', paste_file) + else: + if os.path.exists(f'{paste_file}.charm.upgrade'): + os.remove(f'{paste_file}.charm.upgrade') db.set(PASTE_INI_MARKER, True) db.flush() diff --git a/unit_tests/test_glance_utils.py b/unit_tests/test_glance_utils.py index bcacc0af..ee2de9ee 100644 --- a/unit_tests/test_glance_utils.py +++ b/unit_tests/test_glance_utils.py @@ -369,10 +369,6 @@ class TestGlanceUtils(CharmTestCase): call(utils.GLANCE_REGISTRY_PASTE), call(utils.GLANCE_API_PASTE), ]) - _os.remove.assert_has_calls([ - call(utils.GLANCE_REGISTRY_PASTE), - call(utils.GLANCE_API_PASTE), - ]) self.assertTrue(test_kv.get(utils.PASTE_INI_MARKER)) self.assertTrue(test_kv.flushed) @@ -439,6 +435,32 @@ class TestGlanceUtils(CharmTestCase): utils.reinstall_paste_ini() self.assertTrue(self.apt_install.called) + @patch.object(utils, 'os_release') + @patch.object(utils, 'os') + @patch.object(utils, 'kv') + def test_reinstall_paste_ini_rocky_restore(self, kv, _os, mock_os_release): + """Ensure that paste.ini files are restored""" + mock_os_release.return_value = 'rocky' + _os.path.exists.return_value = True + test_kv = SimpleKV() + test_kv.set(utils.PASTE_INI_MARKER, False) + kv.return_value = test_kv + + self.apt_install.reset_mock() + utils.reinstall_paste_ini() + + self.apt_install.assert_called_with( + packages=['glance-common'], + options=utils.REINSTALL_OPTIONS, + fatal=True + ) + reg_paste_backup = f'{utils.GLANCE_REGISTRY_PASTE}.charm.upgrade' + api_paste_backup = f'{utils.GLANCE_API_PASTE}.charm.upgrade' + _os.remove.assert_has_calls([ + call(reg_paste_backup), + call(api_paste_backup), + ]) + def _test_is_api_ready(self, tgt): fake_config = MagicMock() with patch.object(utils, 'incomplete_relation_data') as ird: