From 9e32e304f8a8ae9a7d2f85a7f4e013cf57b55945 Mon Sep 17 00:00:00 2001 From: Alexandru Bleotu Date: Thu, 22 Sep 2016 12:17:04 +0100 Subject: [PATCH 1/3] Added VMwareRuntimeError salt exception --- salt/exceptions.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/salt/exceptions.py b/salt/exceptions.py index 06e3d1088e..22b60e926e 100644 --- a/salt/exceptions.py +++ b/salt/exceptions.py @@ -386,6 +386,13 @@ class VMwareSaltError(CommandExecutionError): ''' +class VMwareRuntimeError(VMwareSaltError): + ''' + Used when a runtime error is encountered when communicating with the + vCenter + ''' + + class VMwareConnectionError(VMwareSaltError): ''' Used when the client fails to connect to a either a VMware vCenter server or From 86fa9f45f2d42417f7122f99dcc829e504157762 Mon Sep 17 00:00:00 2001 From: Alexandru Bleotu Date: Thu, 22 Sep 2016 12:15:52 +0100 Subject: [PATCH 2/3] Added get_root_folder method in salt.utils.vmware --- salt/utils/vmware.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/salt/utils/vmware.py b/salt/utils/vmware.py index 46067451ce..7978852b46 100644 --- a/salt/utils/vmware.py +++ b/salt/utils/vmware.py @@ -551,6 +551,22 @@ def get_inventory(service_instance): return service_instance.RetrieveContent() +def get_root_folder(service_instance): + ''' + Returns the root folder of a vCenter. + + service_instance + The Service Instance Object for which to obtain the root folder. + ''' + try: + log.trace('Retrieving root folder') + return service_instance.RetrieveContent().rootFolder + except vim.fault.VimFault as exc: + raise salt.exceptions.VMwareApiError(exc.msg) + except vmodl.RuntimeFault as exc: + raise salt.exceptions.VMwareRuntimeError(exc.msg) + + def get_content(service_instance, obj_type, property_list=None, container_ref=None, traversal_spec=None, local_properties=False): From fa2ecb9f5656cf090f85367cd8a71177449451a6 Mon Sep 17 00:00:00 2001 From: Alexandru Bleotu Date: Thu, 22 Sep 2016 12:17:50 +0100 Subject: [PATCH 3/3] Added tests for get_root_folder --- tests/unit/utils/vmware_test/common_test.py | 36 ++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/unit/utils/vmware_test/common_test.py b/tests/unit/utils/vmware_test/common_test.py index 086bab7adc..d033c45a9d 100644 --- a/tests/unit/utils/vmware_test/common_test.py +++ b/tests/unit/utils/vmware_test/common_test.py @@ -15,10 +15,11 @@ from salttesting.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock, \ PropertyMock # Import Salt libraries +import salt.exceptions as excs import salt.utils.vmware # Import Third Party Libs try: - from pyVmomi import vim + from pyVmomi import vim, vmodl HAS_PYVMOMI = True except ImportError: HAS_PYVMOMI = False @@ -467,8 +468,41 @@ class GetContentTestCase(TestCase): obj=container_ref_mock, skip=False, selectSet=None) +@skipIf(NO_MOCK, NO_MOCK_REASON) +@skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing') +class GetRootFolderTestCase(TestCase): + '''Tests for salt.utils.get_root_folder''' + + def setUp(self): + self.mock_root_folder = MagicMock() + self.mock_content = MagicMock(rootFolder=self.mock_root_folder) + self.mock_si = MagicMock( + RetrieveContent=MagicMock(return_value=self.mock_content)) + + def test_raise_vim_fault(self): + exc = vim.fault.VimFault() + exc.msg = 'VimFault msg' + type(self.mock_content).rootFolder = PropertyMock(side_effect=exc) + with self.assertRaises(excs.VMwareApiError) as excinfo: + salt.utils.vmware.get_root_folder(self.mock_si) + self.assertEqual(excinfo.exception.strerror, 'VimFault msg') + + def test_raise_runtime_fault(self): + exc = vmodl.RuntimeFault() + exc.msg = 'RuntimeFault msg' + type(self.mock_content).rootFolder = PropertyMock(side_effect=exc) + with self.assertRaises(excs.VMwareRuntimeError) as excinfo: + salt.utils.vmware.get_root_folder(self.mock_si) + self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg') + + def test_return(self): + ret = salt.utils.vmware.get_root_folder(self.mock_si) + self.assertEqual(ret, self.mock_root_folder) + + if __name__ == '__main__': from integration import run_tests run_tests(WaitForTaskTestCase, needs_daemon=False) run_tests(GetMorsWithPropertiesTestCase, needs_daemon=False) run_tests(GetContentTestCase, needs_daemon=False) + run_tests(GetRootFolderTestCase, needs_daemon=False)