From 818ba631105fdbda3766612018079cebd401b46f Mon Sep 17 00:00:00 2001 From: Lucian Petrut Date: Wed, 20 Dec 2017 16:27:21 +0200 Subject: [PATCH] Remove deprecated methods This change removes some methods that have been deprecated in Pike. For this reason, we should merge this in Rocky. Change-Id: I7ccd54865920f28add085509e14c86efadfc4e56 --- .../unit/utils/compute/test_clusterutils.py | 24 +++++---- .../tests/unit/utils/storage/test_smbutils.py | 53 ------------------- os_win/utils/compute/clusterutils.py | 18 +++---- os_win/utils/storage/smbutils.py | 31 ----------- 4 files changed, 22 insertions(+), 104 deletions(-) diff --git a/os_win/tests/unit/utils/compute/test_clusterutils.py b/os_win/tests/unit/utils/compute/test_clusterutils.py index 075cb366..114f316c 100644 --- a/os_win/tests/unit/utils/compute/test_clusterutils.py +++ b/os_win/tests/unit/utils/compute/test_clusterutils.py @@ -742,7 +742,7 @@ class ClusterUtilsTestCase(test_base.OsWinBaseTestCase): @mock.patch.object(clusterutils, 'tpool') @mock.patch.object(clusterutils, 'patcher') def test_monitor_vm_failover_no_vm(self, mock_patcher, mock_tpool): - self._clusterutils._watcher = mock.MagicMock() + mock_watcher = mock.MagicMock() fake_prev = mock.MagicMock(OwnerNode=self._FAKE_PREV_HOST) fake_wmi_object = mock.MagicMock(OwnerNode=self._FAKE_HOST, Name='Virtual Machine', @@ -750,18 +750,19 @@ class ClusterUtilsTestCase(test_base.OsWinBaseTestCase): mock_tpool.execute.return_value = fake_wmi_object fake_callback = mock.MagicMock() - self._clusterutils.monitor_vm_failover(fake_callback, - mock.sentinel.event_timeout_ms) + self._clusterutils._monitor_vm_failover(mock_watcher, + fake_callback, + mock.sentinel.event_timeout_ms) mock_tpool.execute.assert_called_once_with( - self._clusterutils._watcher, + mock_watcher, mock.sentinel.event_timeout_ms) fake_callback.assert_not_called() @mock.patch.object(clusterutils, 'tpool') @mock.patch.object(clusterutils, 'patcher') def test_monitor_vm_failover(self, mock_patcher, mock_tpool): - self._clusterutils._watcher = mock.MagicMock() + mock_watcher = mock.MagicMock() fake_prev = mock.MagicMock(OwnerNode=self._FAKE_PREV_HOST) fake_wmi_object = mock.MagicMock(OwnerNode=self._FAKE_HOST, Name=self._FAKE_RESOURCEGROUP_NAME, @@ -769,18 +770,20 @@ class ClusterUtilsTestCase(test_base.OsWinBaseTestCase): mock_tpool.execute.return_value = fake_wmi_object fake_callback = mock.MagicMock() - self._clusterutils.monitor_vm_failover(fake_callback) + self._clusterutils._monitor_vm_failover(mock_watcher, fake_callback) mock_tpool.execute.assert_called_once_with( - self._clusterutils._watcher, + mock_watcher, self._clusterutils._WMI_EVENT_TIMEOUT_MS) fake_callback.assert_called_once_with(self._FAKE_VM_NAME, self._FAKE_PREV_HOST, self._FAKE_HOST) - @mock.patch.object(clusterutils.ClusterUtils, 'monitor_vm_failover') + @mock.patch.object(clusterutils.ClusterUtils, '_get_failover_watcher') + @mock.patch.object(clusterutils.ClusterUtils, '_monitor_vm_failover') @mock.patch.object(clusterutils, 'time') - def test_get_vm_owner_change_listener(self, mock_time, mock_monitor): + def test_get_vm_owner_change_listener(self, mock_time, + mock_monitor, mock_get_watcher): mock_monitor.side_effect = [None, exceptions.OSWinException, KeyboardInterrupt] @@ -790,7 +793,8 @@ class ClusterUtilsTestCase(test_base.OsWinBaseTestCase): mock.sentinel.callback) mock_monitor.assert_has_calls( - [mock.call(mock.sentinel.callback, + [mock.call(mock_get_watcher.return_value, + mock.sentinel.callback, constants.DEFAULT_WMI_EVENT_TIMEOUT_MS)] * 3) mock_time.sleep.assert_called_once_with( constants.DEFAULT_WMI_EVENT_TIMEOUT_MS / 1000) diff --git a/os_win/tests/unit/utils/storage/test_smbutils.py b/os_win/tests/unit/utils/storage/test_smbutils.py index 72e8fb32..8870fdce 100644 --- a/os_win/tests/unit/utils/storage/test_smbutils.py +++ b/os_win/tests/unit/utils/storage/test_smbutils.py @@ -108,59 +108,6 @@ class SMBUtilsTestCase(test_base.OsWinBaseTestCase): self._smbutils.unmount_smb_share, mock.sentinel.share_path, force=True) - @mock.patch.object(smbutils, 'ctypes') - @mock.patch.object(smbutils, 'kernel32', create=True) - @mock.patch('os.path.abspath') - def _test_get_share_capacity_info(self, mock_abspath, - mock_kernel32, mock_ctypes, - raised_exc=None, ignore_errors=False): - expected_values = ('total_bytes', 'free_bytes') - - mock_params = [mock.Mock(value=value) for value in expected_values] - mock_ctypes.c_ulonglong.side_effect = mock_params - mock_ctypes.c_wchar_p = lambda x: (x, 'c_wchar_p') - - self._mock_run.side_effect = raised_exc( - func_name='fake_func_name', - error_code='fake_error_code', - error_message='fake_error_message') if raised_exc else None - - if raised_exc and not ignore_errors: - self.assertRaises(raised_exc, - self._smbutils.get_share_capacity_info, - mock.sentinel.share_path, - ignore_errors=ignore_errors) - else: - ret_val = self._smbutils.get_share_capacity_info( - mock.sentinel.share_path, - ignore_errors=ignore_errors) - expected_ret_val = (0, 0) if raised_exc else expected_values - - self.assertEqual(expected_ret_val, ret_val) - - mock_abspath.assert_called_once_with(mock.sentinel.share_path) - mock_ctypes.pointer.assert_has_calls( - [mock.call(param) for param in mock_params]) - self._mock_run.assert_called_once_with( - mock_kernel32.GetDiskFreeSpaceExW, - mock_ctypes.c_wchar_p(mock_abspath.return_value), - None, - mock_ctypes.pointer.return_value, - mock_ctypes.pointer.return_value, - kernel32_lib_func=True) - - def test_get_share_capacity_info_successfully(self): - self._test_get_share_capacity_info() - - def test_get_share_capacity_info_ignored_error(self): - self._test_get_share_capacity_info( - raised_exc=exceptions.Win32Exception, - ignore_errors=True) - - def test_get_share_capacity_info_raised_exc(self): - self._test_get_share_capacity_info( - raised_exc=exceptions.Win32Exception) - def test_get_smb_share_path(self): fake_share = mock.Mock(Path=mock.sentinel.share_path) self._smb_conn.Msft_SmbShare.return_value = [fake_share] diff --git a/os_win/utils/compute/clusterutils.py b/os_win/utils/compute/clusterutils.py index 1efc56a3..9271a19b 100644 --- a/os_win/utils/compute/clusterutils.py +++ b/os_win/utils/compute/clusterutils.py @@ -69,7 +69,6 @@ class ClusterUtils(baseutils.BaseUtils): if sys.platform == 'win32': self._init_hyperv_conn(host) - self._watcher = self._get_failover_watcher() def _init_hyperv_conn(self, host): try: @@ -456,8 +455,8 @@ class ClusterUtils(baseutils.BaseUtils): state_info['status_info'] = status_info return state_info - def monitor_vm_failover(self, callback, - event_timeout_ms=_WMI_EVENT_TIMEOUT_MS): + def _monitor_vm_failover(self, watcher, callback, + event_timeout_ms=_WMI_EVENT_TIMEOUT_MS): """Creates a monitor to check for new WMI MSCluster_Resource events. @@ -469,19 +468,15 @@ class ClusterUtils(baseutils.BaseUtils): Any event object caught will then be processed. """ - # TODO(lpetrut): mark this method as private once compute-hyperv - # stops using it. We should also remove the instance '_watcher' - # attribute since we end up spawning unused event listeners. - vm_name = None new_host = None try: # wait for new event for _WMI_EVENT_TIMEOUT_MS milliseconds. if patcher.is_monkey_patched('thread'): - wmi_object = tpool.execute(self._watcher, + wmi_object = tpool.execute(watcher, event_timeout_ms) else: - wmi_object = self._watcher(event_timeout_ms) + wmi_object = watcher(event_timeout_ms) old_host = wmi_object.previous.OwnerNode new_host = wmi_object.OwnerNode @@ -504,13 +499,16 @@ class ClusterUtils(baseutils.BaseUtils): def get_vm_owner_change_listener(self): def listener(callback): + watcher = self._get_failover_watcher() + while True: # We avoid setting an infinite timeout in order to let # the process gracefully stop. Note that the os-win WMI # event listeners are meant to be used as long running # daemons, so no stop API is provided ATM. try: - self.monitor_vm_failover( + self._monitor_vm_failover( + watcher, callback, constants.DEFAULT_WMI_EVENT_TIMEOUT_MS) except Exception: diff --git a/os_win/utils/storage/smbutils.py b/os_win/utils/storage/smbutils.py index c05c5bb5..1c879053 100644 --- a/os_win/utils/storage/smbutils.py +++ b/os_win/utils/storage/smbutils.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import ctypes import os import socket @@ -24,9 +23,6 @@ from os_win import _utils from os_win import exceptions from os_win.utils import baseutils from os_win.utils import win32utils -from os_win.utils.winapi import libs as w_lib - -kernel32 = w_lib.get_shared_lib_handle(w_lib.KERNEL32) LOG = logging.getLogger(__name__) @@ -88,33 +84,6 @@ class SMBUtils(baseutils.BaseUtils): raise exceptions.SMBException( _("Could not unmount share: %s") % share_path) - # TODO(atuvenie) This method should be removed once all the callers - # have changed to using the get_disk_capacity method from diskutils - def get_share_capacity_info(self, share_path, ignore_errors=False): - norm_path = os.path.abspath(share_path) - - total_bytes = ctypes.c_ulonglong(0) - free_bytes = ctypes.c_ulonglong(0) - - try: - self._win32_utils.run_and_check_output( - kernel32.GetDiskFreeSpaceExW, - ctypes.c_wchar_p(norm_path), - None, - ctypes.pointer(total_bytes), - ctypes.pointer(free_bytes), - kernel32_lib_func=True) - return total_bytes.value, free_bytes.value - except exceptions.Win32Exception as exc: - LOG.error("Could not get share %(share_path)s capacity info. " - "Exception: %(exc)s", - dict(share_path=share_path, - exc=exc)) - if ignore_errors: - return 0, 0 - else: - raise exc - def get_smb_share_path(self, share_name): shares = self._smb_conn.Msft_SmbShare(Name=share_name) share_path = shares[0].Path if shares else None