From 31b868eb209f97475ae2a37a84259bbb5ed09ff9 Mon Sep 17 00:00:00 2001 From: dongdong tao Date: Mon, 28 Mar 2022 11:50:41 +0800 Subject: [PATCH] Check if the newhost exists instead of currenthost Closes-Bug: #1956470 Signed-off-by: Dongdong Tao Change-Id: I2b9eec030a5b0f6d9a405cecea29bef04eb72f5d (cherry picked from commit 93dfc624d4325630dcbd3ba9d06c5f8d3f38845c) --- actions/cinder_manage.py | 7 ++++--- unit_tests/test_actions_cinder_manage.py | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/actions/cinder_manage.py b/actions/cinder_manage.py index d4f7fdc5..999283e5 100755 --- a/actions/cinder_manage.py +++ b/actions/cinder_manage.py @@ -143,7 +143,8 @@ def remove_services(args): def _rename_volume_host(currenthost, newhost): services = cinder_manage_service_list() - services = [s for s in services if s.host == currenthost] + services = [s for s in services if s.host == newhost or + ("#" in newhost and s.host == newhost[0:newhost.rfind('#')])] if services: try: cinder_manage_volume_update_host(currenthost, newhost) @@ -151,8 +152,8 @@ def _rename_volume_host(currenthost, newhost): action_set({'traceback': traceback.format_exc()}) action_fail("Cannot update host {}".format(currenthost)) else: - action_fail("Cannot update host attribute from {}, {} not found" - .format(currenthost, currenthost)) + action_fail("Cannot update host attribute to {}, {} not found" + .format(newhost, newhost)) def rename_volume_host(args): diff --git a/unit_tests/test_actions_cinder_manage.py b/unit_tests/test_actions_cinder_manage.py index e3a0033e..d2302e51 100644 --- a/unit_tests/test_actions_cinder_manage.py +++ b/unit_tests/test_actions_cinder_manage.py @@ -107,7 +107,7 @@ class CinderManageTestCase(CharmTestCase): self.action_get.return_value = 'myhost' svc1_mock = mock.MagicMock() svc1_mock.binary = "cinder-volume" - svc1_mock.host = "a" + svc1_mock.host = "b" cinder_manage_service_list.return_value = [svc1_mock] cinder_manage._rename_volume_host('a', 'b') cinder_manage_volume_update_host.assert_called_once_with('a', 'b') @@ -122,7 +122,7 @@ class CinderManageTestCase(CharmTestCase): cinder_manage._rename_volume_host('a', 'b') self.assertFalse(cinder_manage_volume_update_host.called) self.action_fail.assert_called_once_with( - 'Cannot update host attribute from a, a not found') + 'Cannot update host attribute to b, b not found') @patch.object(cinder_manage, 'cinder_manage_service_list') @patch.object(cinder_manage, 'cinder_manage_volume_update_host') @@ -133,7 +133,7 @@ class CinderManageTestCase(CharmTestCase): self.action_get.return_value = 'myhost' svc1_mock = mock.MagicMock() svc1_mock.binary = "cinder-volume" - svc1_mock.host = "a" + svc1_mock.host = "b" cinder_manage_service_list.return_value = [svc1_mock] cinder_manage._rename_volume_host('a', 'b') cinder_manage_volume_update_host.assert_called_once_with('a', 'b')