From 8e5e0ac9570e1ff24a6d1b076c540d352a649bfa Mon Sep 17 00:00:00 2001 From: Mark Syms Date: Tue, 24 Sep 2024 09:54:34 +0100 Subject: [PATCH] CA-396655: check xapi is enabled before starting multipath reporting Signed-off-by: Mark Syms --- drivers/mpathcount.py | 9 +++++++++ tests/test_mpathcount.py | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/drivers/mpathcount.py b/drivers/mpathcount.py index a31ec1b75..3137e9099 100755 --- a/drivers/mpathcount.py +++ b/drivers/mpathcount.py @@ -199,6 +199,14 @@ def check_devconfig(devconfig, sm_config, config, remove, add, mpath_status=None else: update_config(key, i, config[key], remove, add, mpath_status) + +def check_xapi_is_enabled(session, hostref): + host = session.xenapi.host.get_record(hostref) + if not host['enabled']: + util.SMlog("Xapi is not enabled, exiting") + mpc_exit(session, 0) + + if __name__ == '__main__': try: session = util.get_localAPI_session() @@ -207,6 +215,7 @@ def check_devconfig(devconfig, sm_config, config, remove, add, mpath_status=None sys.exit(-1) localhost = session.xenapi.host.get_by_uuid(get_localhost_uuid()) + check_xapi_is_enabled(session, localhost) # Check whether multipathing is enabled (either for root dev or SRs) try: if get_root_dev_major() != get_dm_major(): diff --git a/tests/test_mpathcount.py b/tests/test_mpathcount.py index 0415dbf74..c02478057 100644 --- a/tests/test_mpathcount.py +++ b/tests/test_mpathcount.py @@ -209,3 +209,29 @@ def test_exit_log_out_error(self, mock_exit): # Assert mock_exit.assert_called_once_with(0) session.xenapi.session.logout.assert_called_once() + + @mock.patch('mpathcount.sys.exit', autospec=True) + def test_check_xapi_enabled_yes(self, mock_exit): + # Arrange + session = mock.MagicMock() + session.xenapi.host.get_record.return_value = {'enabled': True} + hostref = mock.MagicMock() + + # Act + mpathcount.check_xapi_is_enabled(session, hostref) + + # Assert + mock_exit.assert_not_called() + + @mock.patch('mpathcount.sys.exit', autospec=True) + def test_check_xapi_enabled_no(self, mock_exit): + # Arrange + session = mock.MagicMock() + session.xenapi.host.get_record.return_value = {'enabled': False} + hostref = mock.MagicMock() + + # Act + mpathcount.check_xapi_is_enabled(session, hostref) + + # Assert + mock_exit.assert_called_once_with(0)