"Fossies" - the Fresh Open Source Software Archive

Member "salt-3002.2/tests/unit/utils/test_vmware.py" (18 Nov 2020, 205132 Bytes) of package /linux/misc/salt-3002.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_vmware.py": 3002.1_vs_3002.2.

    1 """
    2 :codeauthor: Alexandru Bleotu <alexandru.bleotu@morganstanley.com>
    3 
    4 Tests for cluster related functions in salt.utils.vmware
    5 """
    6 
    7 
    8 import base64
    9 import logging
   10 import ssl
   11 
   12 import salt.utils.vmware
   13 from salt.exceptions import (
   14     ArgumentValueError,
   15     CommandExecutionError,
   16     VMwareApiError,
   17     VMwareConnectionError,
   18     VMwareObjectRetrievalError,
   19     VMwareRuntimeError,
   20     VMwareSystemError,
   21 )
   22 from tests.support.mixins import LoaderModuleMockMixin
   23 from tests.support.mock import MagicMock, PropertyMock, call, patch
   24 from tests.support.runtests import RUNTIME_VARS
   25 from tests.support.unit import TestCase, skipIf
   26 
   27 try:
   28     from pyVmomi import vim, vmodl  # pylint: disable=no-name-in-module
   29 
   30     HAS_PYVMOMI = True
   31 except ImportError:
   32     HAS_PYVMOMI = False
   33 
   34 try:
   35     import gssapi
   36 
   37     HAS_GSSAPI = True
   38 except ImportError:
   39     HAS_GSSAPI = False
   40 
   41 log = logging.getLogger(__name__)
   42 
   43 
   44 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
   45 class GetClusterTestCase(TestCase):
   46     """
   47     Tests for salt.utils.vmware.get_cluster
   48     """
   49 
   50     def setUp(self):
   51         patches = (
   52             ("salt.utils.vmware.get_managed_object_name", MagicMock()),
   53             ("salt.utils.vmware.get_service_instance_from_managed_object", MagicMock()),
   54             (
   55                 "salt.utils.vmware.get_mors_with_properties",
   56                 MagicMock(
   57                     return_value=[{"name": "fake_cluster", "object": MagicMock()}]
   58                 ),
   59             ),
   60         )
   61         for mod, mock in patches:
   62             patcher = patch(mod, mock)
   63             patcher.start()
   64             self.addCleanup(patcher.stop)
   65         self.mock_si = MagicMock()
   66         self.mock_dc = MagicMock()
   67         self.mock_cluster1 = MagicMock()
   68         self.mock_cluster2 = MagicMock()
   69         self.mock_entries = [
   70             {"name": "fake_cluster1", "object": self.mock_cluster1},
   71             {"name": "fake_cluster2", "object": self.mock_cluster2},
   72         ]
   73         for attr in (
   74             "mock_si",
   75             "mock_dc",
   76             "mock_cluster1",
   77             "mock_cluster2",
   78             "mock_entries",
   79         ):
   80             self.addCleanup(delattr, self, attr)
   81 
   82     def test_get_managed_object_name_call(self):
   83         mock_get_managed_object_name = MagicMock()
   84         with patch(
   85             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
   86         ):
   87             salt.utils.vmware.get_cluster(self.mock_dc, "fake_cluster")
   88         mock_get_managed_object_name.assert_called_once_with(self.mock_dc)
   89 
   90     def test_get_service_instance_from_managed_object(self):
   91         mock_dc_name = MagicMock()
   92         mock_get_service_instance_from_managed_object = MagicMock()
   93         with patch(
   94             "salt.utils.vmware.get_managed_object_name",
   95             MagicMock(return_value=mock_dc_name),
   96         ):
   97             with patch(
   98                 "salt.utils.vmware.get_service_instance_from_managed_object",
   99                 mock_get_service_instance_from_managed_object,
  100             ):
  101 
  102                 salt.utils.vmware.get_cluster(self.mock_dc, "fake_cluster")
  103         mock_get_service_instance_from_managed_object.assert_called_once_with(
  104             self.mock_dc, name=mock_dc_name
  105         )
  106 
  107     def test_traversal_spec_init(self):
  108         mock_dc_name = MagicMock()
  109         mock_traversal_spec = MagicMock()
  110         mock_traversal_spec_ini = MagicMock(return_value=mock_traversal_spec)
  111         mock_get_service_instance_from_managed_object = MagicMock()
  112         patch_traversal_spec_str = (
  113             "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec"
  114         )
  115 
  116         with patch(patch_traversal_spec_str, mock_traversal_spec_ini):
  117             salt.utils.vmware.get_cluster(self.mock_dc, "fake_cluster")
  118         mock_traversal_spec_ini.assert_has_calls(
  119             [
  120                 call(path="childEntity", skip=False, type=vim.Folder),
  121                 call(
  122                     path="hostFolder",
  123                     skip=True,
  124                     type=vim.Datacenter,
  125                     selectSet=[mock_traversal_spec],
  126                 ),
  127             ]
  128         )
  129 
  130     def test_get_mors_with_properties_call(self):
  131         mock_get_mors_with_properties = MagicMock(
  132             return_value=[{"name": "fake_cluster", "object": MagicMock()}]
  133         )
  134         mock_traversal_spec = MagicMock()
  135         patch_traversal_spec_str = (
  136             "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec"
  137         )
  138         with patch(
  139             "salt.utils.vmware.get_service_instance_from_managed_object",
  140             MagicMock(return_value=self.mock_si),
  141         ):
  142 
  143             with patch(
  144                 "salt.utils.vmware.get_mors_with_properties",
  145                 mock_get_mors_with_properties,
  146             ):
  147                 with patch(
  148                     patch_traversal_spec_str,
  149                     MagicMock(return_value=mock_traversal_spec),
  150                 ):
  151 
  152                     salt.utils.vmware.get_cluster(self.mock_dc, "fake_cluster")
  153         mock_get_mors_with_properties.assert_called_once_with(
  154             self.mock_si,
  155             vim.ClusterComputeResource,
  156             container_ref=self.mock_dc,
  157             property_list=["name"],
  158             traversal_spec=mock_traversal_spec,
  159         )
  160 
  161     def test_get_mors_with_properties_returns_empty_array(self):
  162         with patch(
  163             "salt.utils.vmware.get_managed_object_name",
  164             MagicMock(return_value="fake_dc"),
  165         ):
  166             with patch(
  167                 "salt.utils.vmware.get_mors_with_properties", MagicMock(return_value=[])
  168             ):
  169                 with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  170                     salt.utils.vmware.get_cluster(self.mock_dc, "fake_cluster")
  171         self.assertEqual(
  172             excinfo.exception.strerror,
  173             "Cluster 'fake_cluster' was not found in " "datacenter 'fake_dc'",
  174         )
  175 
  176     def test_cluster_not_found(self):
  177         with patch(
  178             "salt.utils.vmware.get_managed_object_name",
  179             MagicMock(return_value="fake_dc"),
  180         ):
  181             with patch(
  182                 "salt.utils.vmware.get_mors_with_properties",
  183                 MagicMock(return_value=self.mock_entries),
  184             ):
  185                 with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  186                     salt.utils.vmware.get_cluster(self.mock_dc, "fake_cluster")
  187         self.assertEqual(
  188             excinfo.exception.strerror,
  189             "Cluster 'fake_cluster' was not found in " "datacenter 'fake_dc'",
  190         )
  191 
  192     def test_cluster_found(self):
  193         with patch(
  194             "salt.utils.vmware.get_managed_object_name",
  195             MagicMock(return_value="fake_dc"),
  196         ):
  197             with patch(
  198                 "salt.utils.vmware.get_mors_with_properties",
  199                 MagicMock(return_value=self.mock_entries),
  200             ):
  201                 res = salt.utils.vmware.get_cluster(self.mock_dc, "fake_cluster2")
  202         self.assertEqual(res, self.mock_cluster2)
  203 
  204 
  205 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  206 class CreateClusterTestCase(TestCase):
  207     """
  208     Tests for salt.utils.vmware.create_cluster
  209     """
  210 
  211     def setUp(self):
  212         patches = (("salt.utils.vmware.get_managed_object_name", MagicMock()),)
  213         for mod, mock in patches:
  214             patcher = patch(mod, mock)
  215             patcher.start()
  216             self.addCleanup(patcher.stop)
  217         self.mock_create_cluster_ex = MagicMock()
  218         self.mock_dc = MagicMock(
  219             hostFolder=MagicMock(CreateClusterEx=self.mock_create_cluster_ex)
  220         )
  221         self.mock_cluster_spec = MagicMock()
  222         for attr in ("mock_create_cluster_ex", "mock_dc", "mock_cluster_spec"):
  223             self.addCleanup(delattr, self, attr)
  224 
  225     def test_get_managed_object_name(self):
  226         mock_get_managed_object_name = MagicMock()
  227         with patch(
  228             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
  229         ):
  230             salt.utils.vmware.create_cluster(
  231                 self.mock_dc, "fake_cluster", self.mock_cluster_spec
  232             )
  233         mock_get_managed_object_name.assert_called_once_with(self.mock_dc)
  234 
  235     def test_create_cluster_call(self):
  236         salt.utils.vmware.create_cluster(
  237             self.mock_dc, "fake_cluster", self.mock_cluster_spec
  238         )
  239         self.mock_create_cluster_ex.assert_called_once_with(
  240             "fake_cluster", self.mock_cluster_spec
  241         )
  242 
  243     def test_create_cluster_raise_no_permission(self):
  244         exc = vim.fault.NoPermission()
  245         exc.privilegeId = "Fake privilege"
  246         self.mock_dc.hostFolder.CreateClusterEx = MagicMock(side_effect=exc)
  247         with self.assertRaises(VMwareApiError) as excinfo:
  248             salt.utils.vmware.create_cluster(
  249                 self.mock_dc, "fake_cluster", self.mock_cluster_spec
  250             )
  251         self.assertEqual(
  252             excinfo.exception.strerror,
  253             "Not enough permissions. Required privilege: " "Fake privilege",
  254         )
  255 
  256     def test_create_cluster_raise_vim_fault(self):
  257         exc = vim.fault.VimFault()
  258         exc.msg = "VimFault msg"
  259         self.mock_dc.hostFolder.CreateClusterEx = MagicMock(side_effect=exc)
  260         with self.assertRaises(VMwareApiError) as excinfo:
  261             salt.utils.vmware.create_cluster(
  262                 self.mock_dc, "fake_cluster", self.mock_cluster_spec
  263             )
  264         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  265 
  266     def test_create_cluster_raise_runtime_fault(self):
  267         exc = vmodl.RuntimeFault()
  268         exc.msg = "RuntimeFault msg"
  269         self.mock_dc.hostFolder.CreateClusterEx = MagicMock(side_effect=exc)
  270         with self.assertRaises(VMwareRuntimeError) as excinfo:
  271             salt.utils.vmware.create_cluster(
  272                 self.mock_dc, "fake_cluster", self.mock_cluster_spec
  273             )
  274         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
  275 
  276 
  277 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  278 class UpdateClusterTestCase(TestCase):
  279     """
  280     Tests for salt.utils.vmware.update_cluster
  281     """
  282 
  283     def setUp(self):
  284         patches = (
  285             ("salt.utils.vmware.get_managed_object_name", MagicMock()),
  286             ("salt.utils.vmware.wait_for_task", MagicMock()),
  287         )
  288         for mod, mock in patches:
  289             patcher = patch(mod, mock)
  290             patcher.start()
  291             self.addCleanup(patcher.stop)
  292         self.mock_task = MagicMock()
  293         self.mock_reconfigure_compute_resource_task = MagicMock(
  294             return_value=self.mock_task
  295         )
  296         self.mock_cluster = MagicMock(
  297             ReconfigureComputeResource_Task=self.mock_reconfigure_compute_resource_task
  298         )
  299         self.mock_cluster_spec = MagicMock()
  300         for attr in (
  301             "mock_task",
  302             "mock_reconfigure_compute_resource_task",
  303             "mock_cluster",
  304             "mock_cluster_spec",
  305         ):
  306             self.addCleanup(delattr, self, attr)
  307 
  308     def test_get_managed_object_name(self):
  309         mock_get_managed_object_name = MagicMock()
  310         with patch(
  311             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
  312         ):
  313             salt.utils.vmware.update_cluster(self.mock_cluster, self.mock_cluster_spec)
  314         mock_get_managed_object_name.assert_called_once_with(self.mock_cluster)
  315 
  316     def test_reconfigure_compute_resource_task_call(self):
  317         salt.utils.vmware.update_cluster(self.mock_cluster, self.mock_cluster_spec)
  318         self.mock_reconfigure_compute_resource_task.assert_called_once_with(
  319             self.mock_cluster_spec, modify=True
  320         )
  321 
  322     def test_reconfigure_compute_resource_raise_no_permission(self):
  323         exc = vim.fault.NoPermission()
  324         exc.privilegeId = "Fake privilege"
  325         self.mock_cluster.ReconfigureComputeResource_Task = MagicMock(side_effect=exc)
  326         with self.assertRaises(VMwareApiError) as excinfo:
  327             salt.utils.vmware.update_cluster(self.mock_cluster, self.mock_cluster_spec)
  328         self.assertEqual(
  329             excinfo.exception.strerror,
  330             "Not enough permissions. Required privilege: " "Fake privilege",
  331         )
  332 
  333     def test_reconfigure_compute_resource_raise_vim_fault(self):
  334         exc = vim.fault.VimFault()
  335         exc.msg = "VimFault msg"
  336         self.mock_cluster.ReconfigureComputeResource_Task = MagicMock(side_effect=exc)
  337         with self.assertRaises(VMwareApiError) as excinfo:
  338             salt.utils.vmware.update_cluster(self.mock_cluster, self.mock_cluster_spec)
  339         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  340 
  341     def test_reconfigure_compute_resource_raise_runtime_fault(self):
  342         exc = vmodl.RuntimeFault()
  343         exc.msg = "RuntimeFault msg"
  344         self.mock_cluster.ReconfigureComputeResource_Task = MagicMock(side_effect=exc)
  345         with self.assertRaises(VMwareRuntimeError) as excinfo:
  346             salt.utils.vmware.update_cluster(self.mock_cluster, self.mock_cluster_spec)
  347         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
  348 
  349     def test_wait_for_task_call(self):
  350         mock_wait_for_task = MagicMock()
  351         with patch(
  352             "salt.utils.vmware.get_managed_object_name",
  353             MagicMock(return_value="fake_cluster"),
  354         ):
  355             with patch("salt.utils.vmware.wait_for_task", mock_wait_for_task):
  356                 salt.utils.vmware.update_cluster(
  357                     self.mock_cluster, self.mock_cluster_spec
  358                 )
  359         mock_wait_for_task.assert_called_once_with(
  360             self.mock_task, "fake_cluster", "ClusterUpdateTask"
  361         )
  362 
  363 
  364 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  365 class WaitForTaskTestCase(TestCase):
  366     """
  367     Tests for salt.utils.vmware.wait_for_task
  368     """
  369 
  370     def setUp(self):
  371         patches = (
  372             ("salt.utils.vmware.time.time", MagicMock(return_value=1)),
  373             ("salt.utils.vmware.time.sleep", MagicMock(return_value=None)),
  374         )
  375         for mod, mock in patches:
  376             patcher = patch(mod, mock)
  377             patcher.start()
  378             self.addCleanup(patcher.stop)
  379 
  380     def test_first_task_info_raise_no_permission(self):
  381         exc = vim.fault.NoPermission()
  382         exc.privilegeId = "Fake privilege"
  383         mock_task = MagicMock()
  384         type(mock_task).info = PropertyMock(side_effect=exc)
  385         with self.assertRaises(VMwareApiError) as excinfo:
  386             salt.utils.vmware.wait_for_task(
  387                 mock_task, "fake_instance_name", "task_type"
  388             )
  389         self.assertEqual(
  390             excinfo.exception.strerror,
  391             "Not enough permissions. Required privilege: " "Fake privilege",
  392         )
  393 
  394     def test_first_task_info_raise_vim_fault(self):
  395         exc = vim.fault.VimFault()
  396         exc.msg = "VimFault msg"
  397         mock_task = MagicMock()
  398         type(mock_task).info = PropertyMock(side_effect=exc)
  399         with self.assertRaises(VMwareApiError) as excinfo:
  400             salt.utils.vmware.wait_for_task(
  401                 mock_task, "fake_instance_name", "task_type"
  402             )
  403         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  404 
  405     def test_first_task_info_raise_runtime_fault(self):
  406         exc = vmodl.RuntimeFault()
  407         exc.msg = "RuntimeFault msg"
  408         mock_task = MagicMock()
  409         type(mock_task).info = PropertyMock(side_effect=exc)
  410         with self.assertRaises(VMwareRuntimeError) as excinfo:
  411             salt.utils.vmware.wait_for_task(
  412                 mock_task, "fake_instance_name", "task_type"
  413             )
  414         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
  415 
  416     def test_inner_loop_task_info_raise_no_permission(self):
  417         exc = vim.fault.NoPermission()
  418         exc.privilegeId = "Fake privilege"
  419         mock_task = MagicMock()
  420         mock_info1 = MagicMock()
  421         type(mock_task).info = PropertyMock(side_effect=[mock_info1, exc])
  422         type(mock_info1).state = PropertyMock(side_effect=["running", "bad"])
  423         with self.assertRaises(VMwareApiError) as excinfo:
  424             salt.utils.vmware.wait_for_task(
  425                 mock_task, "fake_instance_name", "task_type"
  426             )
  427         self.assertEqual(
  428             excinfo.exception.strerror,
  429             "Not enough permissions. Required privilege: " "Fake privilege",
  430         )
  431 
  432     def test_inner_loop_task_info_raise_vim_fault(self):
  433         exc = vim.fault.VimFault()
  434         exc.msg = "VimFault msg"
  435         mock_task = MagicMock()
  436         mock_info1 = MagicMock()
  437         type(mock_task).info = PropertyMock(side_effect=[mock_info1, exc])
  438         type(mock_info1).state = PropertyMock(side_effect=["running", "bad"])
  439         with self.assertRaises(VMwareApiError) as excinfo:
  440             salt.utils.vmware.wait_for_task(
  441                 mock_task, "fake_instance_name", "task_type"
  442             )
  443         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  444 
  445     def test_inner_loop_task_info_raise_runtime_fault(self):
  446         exc = vmodl.RuntimeFault()
  447         exc.msg = "RuntimeFault msg"
  448         mock_task = MagicMock()
  449         mock_info1 = MagicMock()
  450         type(mock_task).info = PropertyMock(side_effect=[mock_info1, exc])
  451         type(mock_info1).state = PropertyMock(side_effect=["running", "bad"])
  452         with self.assertRaises(VMwareRuntimeError) as excinfo:
  453             salt.utils.vmware.wait_for_task(
  454                 mock_task, "fake_instance_name", "task_type"
  455             )
  456         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
  457 
  458     def test_info_state_running(self):
  459         # The 'bad' values are invalid in the while loop
  460         mock_task = MagicMock()
  461         prop_mock_state = PropertyMock(side_effect=["running", "bad", "bad", "success"])
  462         prop_mock_result = PropertyMock()
  463         type(mock_task.info).state = prop_mock_state
  464         type(mock_task.info).result = prop_mock_result
  465         salt.utils.vmware.wait_for_task(mock_task, "fake_instance_name", "task_type")
  466         self.assertEqual(prop_mock_state.call_count, 4)
  467         self.assertEqual(prop_mock_result.call_count, 1)
  468 
  469     def test_info_state_running_continues_loop(self):
  470         mock_task = MagicMock()
  471         # The 'fake' values are required to match all the lookups and end the
  472         # loop
  473         prop_mock_state = PropertyMock(
  474             side_effect=["running", "fake", "fake", "success"]
  475         )
  476         prop_mock_result = PropertyMock()
  477         type(mock_task.info).state = prop_mock_state
  478         type(mock_task.info).result = prop_mock_result
  479         salt.utils.vmware.wait_for_task(mock_task, "fake_instance_name", "task_type")
  480         self.assertEqual(prop_mock_state.call_count, 4)
  481         self.assertEqual(prop_mock_result.call_count, 1)
  482 
  483     def test_info_state_queued_continues_loop(self):
  484         mock_task = MagicMock()
  485         # The 'fake' values are required to match all the lookups and end the
  486         # loop
  487         prop_mock_state = PropertyMock(
  488             side_effect=["fake", "queued", "fake", "fake", "success"]
  489         )
  490         prop_mock_result = PropertyMock()
  491         type(mock_task.info).state = prop_mock_state
  492         type(mock_task.info).result = prop_mock_result
  493         salt.utils.vmware.wait_for_task(mock_task, "fake_instance_name", "task_type")
  494         self.assertEqual(prop_mock_state.call_count, 5)
  495         self.assertEqual(prop_mock_result.call_count, 1)
  496 
  497     def test_info_state_success(self):
  498         mock_task = MagicMock()
  499         prop_mock_state = PropertyMock(return_value="success")
  500         prop_mock_result = PropertyMock()
  501         type(mock_task.info).state = prop_mock_state
  502         type(mock_task.info).result = prop_mock_result
  503         salt.utils.vmware.wait_for_task(mock_task, "fake_instance_name", "task_type")
  504         self.assertEqual(prop_mock_state.call_count, 3)
  505         self.assertEqual(prop_mock_result.call_count, 1)
  506 
  507     def test_info_error_exception(self):
  508         mock_task = MagicMock()
  509         prop_mock_state = PropertyMock(return_value="error")
  510         prop_mock_error = PropertyMock(side_effect=Exception("error exc"))
  511         type(mock_task.info).state = prop_mock_state
  512         type(mock_task.info).error = prop_mock_error
  513         with self.assertRaises(Exception) as excinfo:
  514             salt.utils.vmware.wait_for_task(
  515                 mock_task, "fake_instance_name", "task_type"
  516             )
  517         self.assertEqual(str(excinfo.exception), "error exc")
  518 
  519     def test_info_error_no_permission(self):
  520         exc = vim.fault.NoPermission()
  521         exc.privilegeId = "Fake privilege"
  522         mock_task = MagicMock()
  523         prop_mock_state = PropertyMock(return_value="error")
  524         prop_mock_error = PropertyMock(side_effect=exc)
  525         type(mock_task.info).state = prop_mock_state
  526         type(mock_task.info).error = prop_mock_error
  527         with self.assertRaises(VMwareApiError) as excinfo:
  528             salt.utils.vmware.wait_for_task(
  529                 mock_task, "fake_instance_name", "task_type"
  530             )
  531         self.assertEqual(
  532             excinfo.exception.strerror,
  533             "Not enough permissions. Required privilege: " "Fake privilege",
  534         )
  535 
  536     def test_info_error_vim_fault(self):
  537         exc = vim.fault.VimFault()
  538         exc.msg = "VimFault msg"
  539         mock_task = MagicMock()
  540         prop_mock_state = PropertyMock(return_value="error")
  541         prop_mock_error = PropertyMock(side_effect=exc)
  542         type(mock_task.info).state = prop_mock_state
  543         type(mock_task.info).error = prop_mock_error
  544         with self.assertRaises(VMwareApiError) as excinfo:
  545             salt.utils.vmware.wait_for_task(
  546                 mock_task, "fake_instance_name", "task_type"
  547             )
  548         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  549 
  550     def test_info_error_system_fault(self):
  551         exc = vmodl.fault.SystemError()
  552         exc.msg = "SystemError msg"
  553         mock_task = MagicMock()
  554         prop_mock_state = PropertyMock(return_value="error")
  555         prop_mock_error = PropertyMock(side_effect=exc)
  556         type(mock_task.info).state = prop_mock_state
  557         type(mock_task.info).error = prop_mock_error
  558         with self.assertRaises(VMwareSystemError) as excinfo:
  559             salt.utils.vmware.wait_for_task(
  560                 mock_task, "fake_instance_name", "task_type"
  561             )
  562         self.assertEqual(excinfo.exception.strerror, "SystemError msg")
  563 
  564     def test_info_error_invalid_argument_no_fault_message(self):
  565         exc = vmodl.fault.InvalidArgument()
  566         exc.faultMessage = None
  567         exc.msg = "InvalidArgumentFault msg"
  568         mock_task = MagicMock()
  569         prop_mock_state = PropertyMock(return_value="error")
  570         prop_mock_error = PropertyMock(side_effect=exc)
  571         type(mock_task.info).state = prop_mock_state
  572         type(mock_task.info).error = prop_mock_error
  573         with self.assertRaises(VMwareApiError) as excinfo:
  574             salt.utils.vmware.wait_for_task(
  575                 mock_task, "fake_instance_name", "task_type"
  576             )
  577         self.assertEqual(excinfo.exception.strerror, "InvalidArgumentFault msg")
  578 
  579     def test_info_error_invalid_argument_with_fault_message(self):
  580         exc = vmodl.fault.InvalidArgument()
  581         fault_message = vim.LocalizableMessage()
  582         fault_message.message = "LocalFault msg"
  583         exc.faultMessage = [fault_message]
  584         exc.msg = "InvalidArgumentFault msg"
  585         mock_task = MagicMock()
  586         prop_mock_state = PropertyMock(return_value="error")
  587         prop_mock_error = PropertyMock(side_effect=exc)
  588         type(mock_task.info).state = prop_mock_state
  589         type(mock_task.info).error = prop_mock_error
  590         with self.assertRaises(VMwareApiError) as excinfo:
  591             salt.utils.vmware.wait_for_task(
  592                 mock_task, "fake_instance_name", "task_type"
  593             )
  594         self.assertEqual(
  595             excinfo.exception.strerror, "InvalidArgumentFault msg (LocalFault msg)"
  596         )
  597 
  598 
  599 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  600 class GetMorsWithPropertiesTestCase(TestCase):
  601     """
  602     Tests for salt.utils.get_mors_with_properties
  603     """
  604 
  605     si = None
  606     obj_type = None
  607     prop_list = None
  608     container_ref = None
  609     traversal_spec = None
  610 
  611     def setUp(self):
  612         self.si = MagicMock()
  613         self.obj_type = MagicMock()
  614         self.prop_list = MagicMock()
  615         self.container_ref = MagicMock()
  616         self.traversal_spec = MagicMock()
  617 
  618     def test_empty_content(self):
  619         get_content = MagicMock(return_value=[])
  620         with patch("salt.utils.vmware.get_content", get_content):
  621             ret = salt.utils.vmware.get_mors_with_properties(
  622                 self.si,
  623                 self.obj_type,
  624                 self.prop_list,
  625                 self.container_ref,
  626                 self.traversal_spec,
  627             )
  628         get_content.assert_called_once_with(
  629             self.si,
  630             self.obj_type,
  631             property_list=self.prop_list,
  632             container_ref=self.container_ref,
  633             traversal_spec=self.traversal_spec,
  634             local_properties=False,
  635         )
  636         self.assertEqual(ret, [])
  637 
  638     def test_local_properties_set(self):
  639         obj_mock = MagicMock()
  640         # obj.propSet
  641         propSet_prop = PropertyMock(return_value=[])
  642         type(obj_mock).propSet = propSet_prop
  643         # obj.obj
  644         inner_obj_mock = MagicMock()
  645         obj_prop = PropertyMock(return_value=inner_obj_mock)
  646         type(obj_mock).obj = obj_prop
  647 
  648         get_content = MagicMock(return_value=[obj_mock])
  649         with patch("salt.utils.vmware.get_content", get_content):
  650             ret = salt.utils.vmware.get_mors_with_properties(
  651                 self.si,
  652                 self.obj_type,
  653                 self.prop_list,
  654                 self.container_ref,
  655                 self.traversal_spec,
  656                 local_properties=True,
  657             )
  658         get_content.assert_called_once_with(
  659             self.si,
  660             self.obj_type,
  661             property_list=self.prop_list,
  662             container_ref=self.container_ref,
  663             traversal_spec=self.traversal_spec,
  664             local_properties=True,
  665         )
  666 
  667     def test_one_element_content(self):
  668         obj_mock = MagicMock()
  669         # obj.propSet
  670         propSet_prop = PropertyMock(return_value=[])
  671         type(obj_mock).propSet = propSet_prop
  672         # obj.obj
  673         inner_obj_mock = MagicMock()
  674         obj_prop = PropertyMock(return_value=inner_obj_mock)
  675         type(obj_mock).obj = obj_prop
  676         get_content = MagicMock(return_value=[obj_mock])
  677         with patch("salt.utils.vmware.get_content", get_content):
  678             ret = salt.utils.vmware.get_mors_with_properties(
  679                 self.si,
  680                 self.obj_type,
  681                 self.prop_list,
  682                 self.container_ref,
  683                 self.traversal_spec,
  684             )
  685             get_content.assert_called_once_with(
  686                 self.si,
  687                 self.obj_type,
  688                 property_list=self.prop_list,
  689                 container_ref=self.container_ref,
  690                 traversal_spec=self.traversal_spec,
  691                 local_properties=False,
  692             )
  693         self.assertEqual(propSet_prop.call_count, 1)
  694         self.assertEqual(obj_prop.call_count, 1)
  695         self.assertEqual(len(ret), 1)
  696         self.assertDictEqual(ret[0], {"object": inner_obj_mock})
  697 
  698     def test_multiple_element_content(self):
  699         # obj1
  700         obj1_mock = MagicMock()
  701         # obj1.propSet
  702         obj1_propSet_prop = PropertyMock(return_value=[])
  703         type(obj1_mock).propSet = obj1_propSet_prop
  704         # obj1.obj
  705         obj1_inner_obj_mock = MagicMock()
  706         obj1_obj_prop = PropertyMock(return_value=obj1_inner_obj_mock)
  707         type(obj1_mock).obj = obj1_obj_prop
  708         # obj2
  709         obj2_mock = MagicMock()
  710         # obj2.propSet
  711         obj2_propSet_prop = PropertyMock(return_value=[])
  712         type(obj2_mock).propSet = obj2_propSet_prop
  713         # obj2.obj
  714         obj2_inner_obj_mock = MagicMock()
  715         obj2_obj_prop = PropertyMock(return_value=obj2_inner_obj_mock)
  716         type(obj2_mock).obj = obj2_obj_prop
  717 
  718         get_content = MagicMock(return_value=[obj1_mock, obj2_mock])
  719         with patch("salt.utils.vmware.get_content", get_content):
  720             ret = salt.utils.vmware.get_mors_with_properties(
  721                 self.si,
  722                 self.obj_type,
  723                 self.prop_list,
  724                 self.container_ref,
  725                 self.traversal_spec,
  726             )
  727         get_content.assert_called_once_with(
  728             self.si,
  729             self.obj_type,
  730             property_list=self.prop_list,
  731             container_ref=self.container_ref,
  732             traversal_spec=self.traversal_spec,
  733             local_properties=False,
  734         )
  735         self.assertEqual(obj1_propSet_prop.call_count, 1)
  736         self.assertEqual(obj2_propSet_prop.call_count, 1)
  737         self.assertEqual(obj1_obj_prop.call_count, 1)
  738         self.assertEqual(obj2_obj_prop.call_count, 1)
  739         self.assertEqual(len(ret), 2)
  740         self.assertDictEqual(ret[0], {"object": obj1_inner_obj_mock})
  741         self.assertDictEqual(ret[1], {"object": obj2_inner_obj_mock})
  742 
  743     def test_one_elem_one_property(self):
  744         obj_mock = MagicMock()
  745 
  746         # property mock
  747         prop_set_obj_mock = MagicMock()
  748         prop_set_obj_name_prop = PropertyMock(return_value="prop_name")
  749         prop_set_obj_val_prop = PropertyMock(return_value="prop_value")
  750         type(prop_set_obj_mock).name = prop_set_obj_name_prop
  751         type(prop_set_obj_mock).val = prop_set_obj_val_prop
  752 
  753         # obj.propSet
  754         propSet_prop = PropertyMock(return_value=[prop_set_obj_mock])
  755         type(obj_mock).propSet = propSet_prop
  756 
  757         # obj.obj
  758         inner_obj_mock = MagicMock()
  759         obj_prop = PropertyMock(return_value=inner_obj_mock)
  760         type(obj_mock).obj = obj_prop
  761 
  762         get_content = MagicMock(return_value=[obj_mock])
  763         with patch("salt.utils.vmware.get_content", get_content):
  764             ret = salt.utils.vmware.get_mors_with_properties(
  765                 self.si,
  766                 self.obj_type,
  767                 self.prop_list,
  768                 self.container_ref,
  769                 self.traversal_spec,
  770                 local_properties=False,
  771             )
  772         get_content.assert_called_once_with(
  773             self.si,
  774             self.obj_type,
  775             property_list=self.prop_list,
  776             container_ref=self.container_ref,
  777             traversal_spec=self.traversal_spec,
  778             local_properties=False,
  779         )
  780         self.assertEqual(propSet_prop.call_count, 1)
  781         self.assertEqual(prop_set_obj_name_prop.call_count, 1)
  782         self.assertEqual(prop_set_obj_val_prop.call_count, 1)
  783         self.assertEqual(obj_prop.call_count, 1)
  784         self.assertEqual(len(ret), 1)
  785         self.assertDictEqual(
  786             ret[0], {"prop_name": "prop_value", "object": inner_obj_mock}
  787         )
  788 
  789     def test_one_elem_multiple_properties(self):
  790         obj_mock = MagicMock()
  791 
  792         # property1  mock
  793         prop_set_obj1_mock = MagicMock()
  794         prop_set_obj1_name_prop = PropertyMock(return_value="prop_name1")
  795         prop_set_obj1_val_prop = PropertyMock(return_value="prop_value1")
  796         type(prop_set_obj1_mock).name = prop_set_obj1_name_prop
  797         type(prop_set_obj1_mock).val = prop_set_obj1_val_prop
  798 
  799         # property2  mock
  800         prop_set_obj2_mock = MagicMock()
  801         prop_set_obj2_name_prop = PropertyMock(return_value="prop_name2")
  802         prop_set_obj2_val_prop = PropertyMock(return_value="prop_value2")
  803         type(prop_set_obj2_mock).name = prop_set_obj2_name_prop
  804         type(prop_set_obj2_mock).val = prop_set_obj2_val_prop
  805 
  806         # obj.propSet
  807         propSet_prop = PropertyMock(
  808             return_value=[prop_set_obj1_mock, prop_set_obj2_mock]
  809         )
  810         type(obj_mock).propSet = propSet_prop
  811 
  812         # obj.obj
  813         inner_obj_mock = MagicMock()
  814         obj_prop = PropertyMock(return_value=inner_obj_mock)
  815         type(obj_mock).obj = obj_prop
  816 
  817         get_content = MagicMock(return_value=[obj_mock])
  818         with patch("salt.utils.vmware.get_content", get_content):
  819             ret = salt.utils.vmware.get_mors_with_properties(
  820                 self.si,
  821                 self.obj_type,
  822                 self.prop_list,
  823                 self.container_ref,
  824                 self.traversal_spec,
  825             )
  826         get_content.assert_called_once_with(
  827             self.si,
  828             self.obj_type,
  829             property_list=self.prop_list,
  830             container_ref=self.container_ref,
  831             traversal_spec=self.traversal_spec,
  832             local_properties=False,
  833         )
  834         self.assertEqual(propSet_prop.call_count, 1)
  835         self.assertEqual(prop_set_obj1_name_prop.call_count, 1)
  836         self.assertEqual(prop_set_obj1_val_prop.call_count, 1)
  837         self.assertEqual(prop_set_obj2_name_prop.call_count, 1)
  838         self.assertEqual(prop_set_obj2_val_prop.call_count, 1)
  839         self.assertEqual(obj_prop.call_count, 1)
  840         self.assertEqual(len(ret), 1)
  841         self.assertDictEqual(
  842             ret[0],
  843             {
  844                 "prop_name1": "prop_value1",
  845                 "prop_name2": "prop_value2",
  846                 "object": inner_obj_mock,
  847             },
  848         )
  849 
  850 
  851 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  852 class GetPropertiesOfManagedObjectTestCase(TestCase):
  853     """
  854     Tests for salt.utils.get_properties_of_managed_object
  855     """
  856 
  857     def setUp(self):
  858         patches = (
  859             ("salt.utils.vmware.get_service_instance_from_managed_object", MagicMock()),
  860             (
  861                 "salt.utils.vmware.get_mors_with_properties",
  862                 MagicMock(return_value=[MagicMock()]),
  863             ),
  864         )
  865         for mod, mock in patches:
  866             patcher = patch(mod, mock)
  867             patcher.start()
  868             self.addCleanup(patcher.stop)
  869         self.mock_si = MagicMock()
  870         self.fake_mo_ref = vim.ManagedEntity("Fake")
  871         self.mock_props = MagicMock()
  872         self.mock_item_name = {"name": "fake_name"}
  873         self.mock_item = MagicMock()
  874 
  875     def test_get_service_instance_from_managed_object_call(self):
  876         mock_get_instance_from_managed_object = MagicMock()
  877         with patch(
  878             "salt.utils.vmware.get_service_instance_from_managed_object",
  879             mock_get_instance_from_managed_object,
  880         ):
  881 
  882             salt.utils.vmware.get_properties_of_managed_object(
  883                 self.fake_mo_ref, self.mock_props
  884             )
  885         mock_get_instance_from_managed_object.assert_called_once_with(self.fake_mo_ref)
  886 
  887     def test_get_mors_with_properties_calls(self):
  888         mock_get_mors_with_properties = MagicMock(return_value=[MagicMock()])
  889         with patch(
  890             "salt.utils.vmware.get_service_instance_from_managed_object",
  891             MagicMock(return_value=self.mock_si),
  892         ):
  893 
  894             with patch(
  895                 "salt.utils.vmware.get_mors_with_properties",
  896                 mock_get_mors_with_properties,
  897             ):
  898                 salt.utils.vmware.get_properties_of_managed_object(
  899                     self.fake_mo_ref, self.mock_props
  900                 )
  901         mock_get_mors_with_properties.assert_has_calls(
  902             [
  903                 call(
  904                     self.mock_si,
  905                     vim.ManagedEntity,
  906                     container_ref=self.fake_mo_ref,
  907                     property_list=["name"],
  908                     local_properties=True,
  909                 ),
  910                 call(
  911                     self.mock_si,
  912                     vim.ManagedEntity,
  913                     container_ref=self.fake_mo_ref,
  914                     property_list=self.mock_props,
  915                     local_properties=True,
  916                 ),
  917             ]
  918         )
  919 
  920     def test_managed_object_no_name_property(self):
  921         with patch(
  922             "salt.utils.vmware.get_mors_with_properties",
  923             MagicMock(side_effect=[vmodl.query.InvalidProperty(), []]),
  924         ):
  925             with self.assertRaises(VMwareApiError) as excinfo:
  926                 salt.utils.vmware.get_properties_of_managed_object(
  927                     self.fake_mo_ref, self.mock_props
  928                 )
  929         self.assertEqual(
  930             "Properties of managed object '<unnamed>' weren't " "retrieved",
  931             excinfo.exception.strerror,
  932         )
  933 
  934     def test_no_items_named_object(self):
  935         with patch(
  936             "salt.utils.vmware.get_mors_with_properties",
  937             MagicMock(side_effect=[[self.mock_item_name], []]),
  938         ):
  939             with self.assertRaises(VMwareApiError) as excinfo:
  940                 salt.utils.vmware.get_properties_of_managed_object(
  941                     self.fake_mo_ref, self.mock_props
  942                 )
  943         self.assertEqual(
  944             "Properties of managed object 'fake_name' weren't " "retrieved",
  945             excinfo.exception.strerror,
  946         )
  947 
  948 
  949 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  950 class GetManagedObjectName(TestCase):
  951     """
  952     Tests for salt.utils.get_managed_object_name
  953     """
  954 
  955     def setUp(self):
  956         patches = (
  957             (
  958                 "salt.utils.vmware.get_properties_of_managed_object",
  959                 MagicMock(return_value={"key": "value"}),
  960             ),
  961         )
  962         for mod, mock in patches:
  963             patcher = patch(mod, mock)
  964             patcher.start()
  965             self.addCleanup(patcher.stop)
  966         self.mock_mo_ref = MagicMock()
  967 
  968     def test_get_properties_of_managed_object_call(self):
  969         mock_get_properties_of_managed_object = MagicMock()
  970         with patch(
  971             "salt.utils.vmware.get_properties_of_managed_object",
  972             mock_get_properties_of_managed_object,
  973         ):
  974             salt.utils.vmware.get_managed_object_name(self.mock_mo_ref)
  975         mock_get_properties_of_managed_object.assert_called_once_with(
  976             self.mock_mo_ref, ["name"]
  977         )
  978 
  979     def test_no_name_in_property_dict(self):
  980         ret = salt.utils.vmware.get_managed_object_name(self.mock_mo_ref)
  981         self.assertIsNone(ret)
  982 
  983     def test_return_managed_object_name(self):
  984         mock_get_properties_of_managed_object = MagicMock()
  985         with patch(
  986             "salt.utils.vmware.get_properties_of_managed_object",
  987             MagicMock(return_value={"name": "fake_name"}),
  988         ):
  989             ret = salt.utils.vmware.get_managed_object_name(self.mock_mo_ref)
  990         self.assertEqual(ret, "fake_name")
  991 
  992 
  993 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  994 class GetContentTestCase(TestCase):
  995     """
  996     Tests for salt.utils.get_content
  997     """
  998 
  999     # Method names to be patched
 1000     traversal_spec_method_name = (
 1001         "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec"
 1002     )
 1003     property_spec_method_name = (
 1004         "salt.utils.vmware.vmodl.query.PropertyCollector.PropertySpec"
 1005     )
 1006     obj_spec_method_name = "salt.utils.vmware.vmodl.query.PropertyCollector.ObjectSpec"
 1007     filter_spec_method_name = (
 1008         "salt.utils.vmware.vmodl.query.PropertyCollector.FilterSpec"
 1009     )
 1010 
 1011     # Class variables
 1012     si_mock = None
 1013     root_folder_mock = None
 1014     root_folder_prop = None
 1015     container_view_mock = None
 1016     create_container_view_mock = None
 1017     result_mock = None
 1018     retrieve_contents_mock = None
 1019     destroy_mock = None
 1020     obj_type_mock = None
 1021     traversal_spec_ret_mock = None
 1022     traversal_spec_mock = None
 1023     property_spec_ret_mock = None
 1024     property_spec_mock = None
 1025     obj_spec_ret_mock = None
 1026     obj_spec_mock = None
 1027     filter_spec_ret_mock = None
 1028     filter_spec_mock = None
 1029 
 1030     def setUp(self):
 1031         patches = (
 1032             ("salt.utils.vmware.get_root_folder", MagicMock()),
 1033             (
 1034                 "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 1035                 MagicMock(return_value=MagicMock()),
 1036             ),
 1037             (
 1038                 "salt.utils.vmware.vmodl.query.PropertyCollector.PropertySpec",
 1039                 MagicMock(return_value=MagicMock()),
 1040             ),
 1041             (
 1042                 "salt.utils.vmware.vmodl.query.PropertyCollector.ObjectSpec",
 1043                 MagicMock(return_value=MagicMock()),
 1044             ),
 1045             (
 1046                 "salt.utils.vmware.vmodl.query.PropertyCollector.FilterSpec",
 1047                 MagicMock(return_value=MagicMock()),
 1048             ),
 1049         )
 1050         for mod, mock in patches:
 1051             patcher = patch(mod, mock)
 1052             patcher.start()
 1053             self.addCleanup(patcher.stop)
 1054         # setup the service instance
 1055         self.si_mock = MagicMock()
 1056         # RootFolder
 1057         self.root_folder_mock = MagicMock()
 1058         self.get_root_folder_mock = MagicMock(return_value=self.root_folder_mock)
 1059         # CreateContainerView()
 1060         self.container_view_mock = MagicMock()
 1061         self.create_container_view_mock = MagicMock(
 1062             return_value=self.container_view_mock
 1063         )
 1064         self.si_mock.content.viewManager.CreateContainerView = (
 1065             self.create_container_view_mock
 1066         )
 1067         # RetrieveContents()
 1068         self.result_mock = MagicMock()
 1069         self.retrieve_contents_mock = MagicMock(return_value=self.result_mock)
 1070         self.si_mock.content.propertyCollector.RetrieveContents = (
 1071             self.retrieve_contents_mock
 1072         )
 1073         # Destroy()
 1074         self.destroy_mock = MagicMock()
 1075         self.container_view_mock.Destroy = self.destroy_mock
 1076 
 1077         # override mocks
 1078         self.obj_type_mock = MagicMock()
 1079         self.traversal_spec_ret_mock = MagicMock()
 1080         self.traversal_spec_mock = MagicMock(return_value=self.traversal_spec_ret_mock)
 1081         self.property_spec_ret_mock = MagicMock()
 1082         self.property_spec_mock = MagicMock(return_value=self.property_spec_ret_mock)
 1083         self.obj_spec_ret_mock = MagicMock()
 1084         self.obj_spec_mock = MagicMock(return_value=self.obj_spec_ret_mock)
 1085         self.filter_spec_ret_mock = MagicMock()
 1086         self.filter_spec_mock = MagicMock(return_value=self.filter_spec_ret_mock)
 1087 
 1088     def test_empty_container_ref(self):
 1089         with patch("salt.utils.vmware.get_root_folder", self.get_root_folder_mock):
 1090             salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
 1091         self.get_root_folder_mock.assert_called_once_with(self.si_mock)
 1092         self.create_container_view_mock.assert_called_once_with(
 1093             self.root_folder_mock, [self.obj_type_mock], True
 1094         )
 1095 
 1096     def test_defined_container_ref(self):
 1097         container_ref_mock = MagicMock()
 1098         with patch("salt.utils.vmware.get_root_folder", self.get_root_folder_mock):
 1099             with patch(self.obj_spec_method_name, self.obj_type_mock):
 1100                 salt.utils.vmware.get_content(
 1101                     self.si_mock, self.obj_type_mock, container_ref=container_ref_mock
 1102                 )
 1103         self.assertEqual(self.get_root_folder_mock.call_count, 0)
 1104         self.create_container_view_mock.assert_called_once_with(
 1105             container_ref_mock, [self.obj_type_mock], True
 1106         )
 1107 
 1108     # Also checks destroy is called
 1109     def test_local_traversal_spec(self):
 1110         with patch("salt.utils.vmware.get_root_folder", self.get_root_folder_mock):
 1111             with patch(self.traversal_spec_method_name, self.traversal_spec_mock):
 1112                 with patch(self.obj_spec_method_name, self.obj_spec_mock):
 1113                     ret = salt.utils.vmware.get_content(
 1114                         self.si_mock, self.obj_type_mock
 1115                     )
 1116         self.create_container_view_mock.assert_called_once_with(
 1117             self.root_folder_mock, [self.obj_type_mock], True
 1118         )
 1119         self.traversal_spec_mock.assert_called_once_with(
 1120             name="traverseEntities",
 1121             path="view",
 1122             skip=False,
 1123             type=vim.view.ContainerView,
 1124         )
 1125         self.obj_spec_mock.assert_called_once_with(
 1126             obj=self.container_view_mock,
 1127             skip=True,
 1128             selectSet=[self.traversal_spec_ret_mock],
 1129         )
 1130         # check destroy is called
 1131         self.assertEqual(self.destroy_mock.call_count, 1)
 1132 
 1133     def test_create_container_view_raise_no_permission(self):
 1134         exc = vim.fault.NoPermission()
 1135         exc.privilegeId = "Fake privilege"
 1136         self.si_mock.content.viewManager.CreateContainerView = MagicMock(
 1137             side_effect=exc
 1138         )
 1139         with patch("salt.utils.vmware.get_root_folder", self.get_root_folder_mock):
 1140             with self.assertRaises(VMwareApiError) as excinfo:
 1141                 salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
 1142         self.assertEqual(
 1143             excinfo.exception.strerror,
 1144             "Not enough permissions. Required privilege: " "Fake privilege",
 1145         )
 1146 
 1147     def test_create_container_view_raise_vim_fault(self):
 1148         exc = vim.fault.VimFault()
 1149         exc.msg = "VimFault msg"
 1150         self.si_mock.content.viewManager.CreateContainerView = MagicMock(
 1151             side_effect=exc
 1152         )
 1153         with patch("salt.utils.vmware.get_root_folder", self.get_root_folder_mock):
 1154             with self.assertRaises(VMwareApiError) as excinfo:
 1155                 salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
 1156         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 1157 
 1158     def test_create_container_view_raise_runtime_fault(self):
 1159         exc = vmodl.RuntimeFault()
 1160         exc.msg = "RuntimeFault msg"
 1161         self.si_mock.content.viewManager.CreateContainerView = MagicMock(
 1162             side_effect=exc
 1163         )
 1164         with patch("salt.utils.vmware.get_root_folder", self.get_root_folder_mock):
 1165             with self.assertRaises(VMwareRuntimeError) as excinfo:
 1166                 salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
 1167         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 1168 
 1169     def test_destroy_raise_no_permission(self):
 1170         exc = vim.fault.NoPermission()
 1171         exc.privilegeId = "Fake privilege"
 1172         self.si_mock.content.viewManager.CreateContainerView = MagicMock(
 1173             return_value=MagicMock(Destroy=MagicMock(side_effect=exc))
 1174         )
 1175         with patch("salt.utils.vmware.get_root_folder", self.get_root_folder_mock):
 1176             with self.assertRaises(VMwareApiError) as excinfo:
 1177                 salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
 1178         self.assertEqual(
 1179             excinfo.exception.strerror,
 1180             "Not enough permissions. Required privilege: " "Fake privilege",
 1181         )
 1182 
 1183     def test_destroy_raise_vim_fault(self):
 1184         exc = vim.fault.VimFault()
 1185         exc.msg = "VimFault msg"
 1186         self.si_mock.content.viewManager.CreateContainerView = MagicMock(
 1187             return_value=MagicMock(Destroy=MagicMock(side_effect=exc))
 1188         )
 1189         with patch("salt.utils.vmware.get_root_folder", self.get_root_folder_mock):
 1190             with self.assertRaises(VMwareApiError) as excinfo:
 1191                 salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
 1192         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 1193 
 1194     def test_destroy_raise_runtime_fault(self):
 1195         exc = vmodl.RuntimeFault()
 1196         exc.msg = "RuntimeFault msg"
 1197         self.si_mock.content.viewManager.CreateContainerView = MagicMock(
 1198             return_value=MagicMock(Destroy=MagicMock(side_effect=exc))
 1199         )
 1200         with patch("salt.utils.vmware.get_root_folder", self.get_root_folder_mock):
 1201             with self.assertRaises(VMwareRuntimeError) as excinfo:
 1202                 salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
 1203         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 1204 
 1205     # Also checks destroy is not called
 1206     def test_external_traversal_spec(self):
 1207         traversal_spec_obj_mock = MagicMock()
 1208         with patch("salt.utils.vmware.get_root_folder", self.get_root_folder_mock):
 1209             with patch(self.traversal_spec_method_name, self.traversal_spec_mock):
 1210                 with patch(self.obj_spec_method_name, self.obj_spec_mock):
 1211                     salt.utils.vmware.get_content(
 1212                         self.si_mock,
 1213                         self.obj_type_mock,
 1214                         traversal_spec=traversal_spec_obj_mock,
 1215                     )
 1216         self.obj_spec_mock.assert_called_once_with(
 1217             obj=self.root_folder_mock, skip=True, selectSet=[traversal_spec_obj_mock]
 1218         )
 1219         # Check local traversal methods are not called
 1220         self.assertEqual(self.create_container_view_mock.call_count, 0)
 1221         self.assertEqual(self.traversal_spec_mock.call_count, 0)
 1222         # check destroy is not called
 1223         self.assertEqual(self.destroy_mock.call_count, 0)
 1224 
 1225     def test_property_obj_filter_specs_and_contents(self):
 1226         with patch(self.traversal_spec_method_name, self.traversal_spec_mock):
 1227             with patch(self.property_spec_method_name, self.property_spec_mock):
 1228                 with patch(self.obj_spec_method_name, self.obj_spec_mock):
 1229                     with patch(self.filter_spec_method_name, self.filter_spec_mock):
 1230                         ret = salt.utils.vmware.get_content(
 1231                             self.si_mock, self.obj_type_mock
 1232                         )
 1233         self.traversal_spec_mock.assert_called_once_with(
 1234             name="traverseEntities",
 1235             path="view",
 1236             skip=False,
 1237             type=vim.view.ContainerView,
 1238         )
 1239         self.property_spec_mock.assert_called_once_with(
 1240             type=self.obj_type_mock, all=True, pathSet=None
 1241         )
 1242         self.obj_spec_mock.assert_called_once_with(
 1243             obj=self.container_view_mock,
 1244             skip=True,
 1245             selectSet=[self.traversal_spec_ret_mock],
 1246         )
 1247         self.retrieve_contents_mock.assert_called_once_with([self.filter_spec_ret_mock])
 1248         self.assertEqual(ret, self.result_mock)
 1249 
 1250     def test_retrieve_contents_raise_no_permission(self):
 1251         exc = vim.fault.NoPermission()
 1252         exc.privilegeId = "Fake privilege"
 1253         self.si_mock.content.propertyCollector.RetrieveContents = MagicMock(
 1254             side_effect=exc
 1255         )
 1256         with self.assertRaises(VMwareApiError) as excinfo:
 1257             salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
 1258         self.assertEqual(
 1259             excinfo.exception.strerror,
 1260             "Not enough permissions. Required privilege: " "Fake privilege",
 1261         )
 1262 
 1263     def test_retrieve_contents_raise_vim_fault(self):
 1264         exc = vim.fault.VimFault()
 1265         exc.msg = "VimFault msg"
 1266         self.si_mock.content.propertyCollector.RetrieveContents = MagicMock(
 1267             side_effect=exc
 1268         )
 1269         with self.assertRaises(VMwareApiError) as excinfo:
 1270             salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
 1271         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 1272 
 1273     def test_retrieve_contents_raise_runtime_fault(self):
 1274         exc = vmodl.RuntimeFault()
 1275         exc.msg = "RuntimeFault msg"
 1276         self.si_mock.content.propertyCollector.RetrieveContents = MagicMock(
 1277             side_effect=exc
 1278         )
 1279         with self.assertRaises(VMwareRuntimeError) as excinfo:
 1280             salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
 1281         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 1282 
 1283     def test_local_properties_set(self):
 1284         container_ref_mock = MagicMock()
 1285         with patch(self.traversal_spec_method_name, self.traversal_spec_mock):
 1286             with patch(self.property_spec_method_name, self.property_spec_mock):
 1287                 with patch(self.obj_spec_method_name, self.obj_spec_mock):
 1288                     salt.utils.vmware.get_content(
 1289                         self.si_mock,
 1290                         self.obj_type_mock,
 1291                         container_ref=container_ref_mock,
 1292                         local_properties=True,
 1293                     )
 1294         self.assertEqual(self.traversal_spec_mock.call_count, 0)
 1295         self.obj_spec_mock.assert_called_once_with(
 1296             obj=container_ref_mock, skip=False, selectSet=None
 1297         )
 1298 
 1299 
 1300 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 1301 class GetRootFolderTestCase(TestCase):
 1302     """
 1303     Tests for salt.utils.get_root_folder
 1304     """
 1305 
 1306     def setUp(self):
 1307         self.mock_root_folder = MagicMock()
 1308         self.mock_content = MagicMock(rootFolder=self.mock_root_folder)
 1309         self.mock_si = MagicMock(
 1310             RetrieveContent=MagicMock(return_value=self.mock_content)
 1311         )
 1312 
 1313     def test_raise_no_permission(self):
 1314         exc = vim.fault.NoPermission()
 1315         exc.privilegeId = "Fake privilege"
 1316         type(self.mock_content).rootFolder = PropertyMock(side_effect=exc)
 1317         with self.assertRaises(VMwareApiError) as excinfo:
 1318             salt.utils.vmware.get_root_folder(self.mock_si)
 1319         self.assertEqual(
 1320             excinfo.exception.strerror,
 1321             "Not enough permissions. Required privilege: " "Fake privilege",
 1322         )
 1323 
 1324     def test_raise_vim_fault(self):
 1325         exc = vim.fault.VimFault()
 1326         exc.msg = "VimFault msg"
 1327         type(self.mock_content).rootFolder = PropertyMock(side_effect=exc)
 1328         with self.assertRaises(VMwareApiError) as excinfo:
 1329             salt.utils.vmware.get_root_folder(self.mock_si)
 1330         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 1331 
 1332     def test_raise_runtime_fault(self):
 1333         exc = vmodl.RuntimeFault()
 1334         exc.msg = "RuntimeFault msg"
 1335         type(self.mock_content).rootFolder = PropertyMock(side_effect=exc)
 1336         with self.assertRaises(VMwareRuntimeError) as excinfo:
 1337             salt.utils.vmware.get_root_folder(self.mock_si)
 1338         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 1339 
 1340     def test_return(self):
 1341         ret = salt.utils.vmware.get_root_folder(self.mock_si)
 1342         self.assertEqual(ret, self.mock_root_folder)
 1343 
 1344 
 1345 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 1346 class GetServiceInfoTestCase(TestCase):
 1347     """
 1348     Tests for salt.utils.vmware.get_service_info
 1349     """
 1350 
 1351     def setUp(self):
 1352         self.mock_about = MagicMock()
 1353         self.mock_si = MagicMock(content=MagicMock())
 1354         type(self.mock_si.content).about = PropertyMock(return_value=self.mock_about)
 1355 
 1356     def tearDown(self):
 1357         for attr in ("mock_si", "mock_about"):
 1358             delattr(self, attr)
 1359 
 1360     def test_about_ret(self):
 1361         ret = salt.utils.vmware.get_service_info(self.mock_si)
 1362         self.assertEqual(ret, self.mock_about)
 1363 
 1364     def test_about_raises_no_permission(self):
 1365         exc = vim.fault.NoPermission()
 1366         exc.privilegeId = "Fake privilege"
 1367         type(self.mock_si.content).about = PropertyMock(side_effect=exc)
 1368         with self.assertRaises(VMwareApiError) as excinfo:
 1369             salt.utils.vmware.get_service_info(self.mock_si)
 1370         self.assertEqual(
 1371             excinfo.exception.strerror,
 1372             "Not enough permissions. Required privilege: " "Fake privilege",
 1373         )
 1374 
 1375     def test_about_raises_vim_fault(self):
 1376         exc = vim.fault.VimFault()
 1377         exc.msg = "VimFault msg"
 1378         type(self.mock_si.content).about = PropertyMock(side_effect=exc)
 1379         with self.assertRaises(VMwareApiError) as excinfo:
 1380             salt.utils.vmware.get_service_info(self.mock_si)
 1381         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 1382 
 1383     def test_about_raises_runtime_fault(self):
 1384         exc = vmodl.RuntimeFault()
 1385         exc.msg = "RuntimeFault msg"
 1386         type(self.mock_si.content).about = PropertyMock(side_effect=exc)
 1387         with self.assertRaises(VMwareRuntimeError) as excinfo:
 1388             salt.utils.vmware.get_service_info(self.mock_si)
 1389         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 1390 
 1391 
 1392 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 1393 @skipIf(not HAS_GSSAPI, "The 'gssapi' library is missing")
 1394 class GssapiTokenTest(TestCase):
 1395     """
 1396     Test cases for salt.utils.vmware.get_gssapi_token
 1397     """
 1398 
 1399     def setUp(self):
 1400         patches = (
 1401             ("gssapi.Name", MagicMock(return_value="service")),
 1402             ("gssapi.InitContext", MagicMock()),
 1403         )
 1404         for mod, mock in patches:
 1405             patcher = patch(mod, mock)
 1406             patcher.start()
 1407             self.addCleanup(patcher.stop)
 1408 
 1409     def test_no_gssapi(self):
 1410         with patch("salt.utils.vmware.HAS_GSSAPI", False):
 1411             with self.assertRaises(ImportError) as excinfo:
 1412                 salt.utils.vmware.get_gssapi_token("principal", "host", "domain")
 1413                 self.assertIn(
 1414                     "The gssapi library is not imported.", excinfo.exception.message
 1415                 )
 1416 
 1417     @skipIf(not HAS_GSSAPI, "The 'gssapi' library is missing")
 1418     def test_service_name(self):
 1419         mock_name = MagicMock()
 1420         with patch.object(salt.utils.vmware.gssapi, "Name", mock_name):
 1421 
 1422             with self.assertRaises(CommandExecutionError):
 1423                 salt.utils.vmware.get_gssapi_token("principal", "host", "domain")
 1424             mock_name.assert_called_once_with(
 1425                 "principal/host@domain", gssapi.C_NT_USER_NAME
 1426             )
 1427 
 1428     @skipIf(not HAS_GSSAPI, "The 'gssapi' library is missing")
 1429     def test_out_token_defined(self):
 1430         mock_context = MagicMock(return_value=MagicMock())
 1431         mock_context.return_value.established = False
 1432         mock_context.return_value.step = MagicMock(return_value="out_token")
 1433         with patch.object(salt.utils.vmware.gssapi, "InitContext", mock_context):
 1434             ret = salt.utils.vmware.get_gssapi_token("principal", "host", "domain")
 1435             self.assertEqual(mock_context.return_value.step.called, 1)
 1436             self.assertEqual(ret, base64.b64encode(b"out_token"))
 1437 
 1438     @skipIf(not HAS_GSSAPI, "The 'gssapi' library is missing")
 1439     def test_out_token_undefined(self):
 1440         mock_context = MagicMock(return_value=MagicMock())
 1441         mock_context.return_value.established = False
 1442         mock_context.return_value.step = MagicMock(return_value=None)
 1443         with patch.object(salt.utils.vmware.gssapi, "InitContext", mock_context):
 1444             with self.assertRaises(CommandExecutionError) as excinfo:
 1445                 salt.utils.vmware.get_gssapi_token("principal", "host", "domain")
 1446             self.assertEqual(mock_context.return_value.step.called, 1)
 1447             self.assertIn("Can't receive token", excinfo.exception.strerror)
 1448 
 1449     @skipIf(not HAS_GSSAPI, "The 'gssapi' library is missing")
 1450     def test_context_extablished(self):
 1451         mock_context = MagicMock(return_value=MagicMock())
 1452         mock_context.return_value.established = True
 1453         mock_context.return_value.step = MagicMock(return_value="out_token")
 1454         with patch.object(salt.utils.vmware.gssapi, "InitContext", mock_context):
 1455             mock_context.established = True
 1456             mock_context.step = MagicMock(return_value=None)
 1457             with self.assertRaises(CommandExecutionError) as excinfo:
 1458                 salt.utils.vmware.get_gssapi_token("principal", "host", "domain")
 1459             self.assertEqual(mock_context.step.called, 0)
 1460             self.assertIn(
 1461                 "Context established, but didn't receive token",
 1462                 excinfo.exception.strerror,
 1463             )
 1464 
 1465 
 1466 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 1467 class PrivateGetServiceInstanceTestCase(TestCase):
 1468     """
 1469     Tests for salt.utils.vmware._get_service_instance
 1470     """
 1471 
 1472     def setUp(self):
 1473         patches = (
 1474             ("salt.utils.vmware.SmartConnect", MagicMock()),
 1475             ("salt.utils.vmware.Disconnect", MagicMock()),
 1476             (
 1477                 "salt.utils.vmware.get_gssapi_token",
 1478                 MagicMock(return_value="fake_token"),
 1479             ),
 1480         )
 1481         for mod, mock in patches:
 1482             patcher = patch(mod, mock)
 1483             patcher.start()
 1484             self.addCleanup(patcher.stop)
 1485 
 1486     def test_invalid_mechianism(self):
 1487         with self.assertRaises(CommandExecutionError) as excinfo:
 1488             salt.utils.vmware._get_service_instance(
 1489                 host="fake_host.fqdn",
 1490                 username="fake_username",
 1491                 password="fake_password",
 1492                 protocol="fake_protocol",
 1493                 port=1,
 1494                 mechanism="invalid_mechanism",
 1495                 principal="fake principal",
 1496                 domain="fake_domain",
 1497             )
 1498         self.assertIn("Unsupported mechanism", excinfo.exception.strerror)
 1499 
 1500     def test_userpass_mechanism_empty_username(self):
 1501         with self.assertRaises(CommandExecutionError) as excinfo:
 1502             salt.utils.vmware._get_service_instance(
 1503                 host="fake_host.fqdn",
 1504                 username=None,
 1505                 password="fake_password",
 1506                 protocol="fake_protocol",
 1507                 port=1,
 1508                 mechanism="userpass",
 1509                 principal="fake principal",
 1510                 domain="fake_domain",
 1511             )
 1512         self.assertIn("mandatory parameter 'username'", excinfo.exception.strerror)
 1513 
 1514     def test_userpass_mechanism_empty_password(self):
 1515         with self.assertRaises(CommandExecutionError) as excinfo:
 1516             salt.utils.vmware._get_service_instance(
 1517                 host="fake_host.fqdn",
 1518                 username="fake_username",
 1519                 password=None,
 1520                 protocol="fake_protocol",
 1521                 port=1,
 1522                 mechanism="userpass",
 1523                 principal="fake principal",
 1524                 domain="fake_domain",
 1525             )
 1526         self.assertIn("mandatory parameter 'password'", excinfo.exception.strerror)
 1527 
 1528     def test_userpass_mechanism_no_domain(self):
 1529         mock_sc = MagicMock()
 1530         with patch("salt.utils.vmware.SmartConnect", mock_sc):
 1531             salt.utils.vmware._get_service_instance(
 1532                 host="fake_host.fqdn",
 1533                 username="fake_username",
 1534                 password="fake_password",
 1535                 protocol="fake_protocol",
 1536                 port=1,
 1537                 mechanism="userpass",
 1538                 principal="fake principal",
 1539                 domain=None,
 1540             )
 1541             mock_sc.assert_called_once_with(
 1542                 host="fake_host.fqdn",
 1543                 user="fake_username",
 1544                 pwd="fake_password",
 1545                 protocol="fake_protocol",
 1546                 port=1,
 1547                 b64token=None,
 1548                 mechanism="userpass",
 1549             )
 1550 
 1551     def test_userpass_mech_domain_unused(self):
 1552         mock_sc = MagicMock()
 1553         with patch("salt.utils.vmware.SmartConnect", mock_sc):
 1554             salt.utils.vmware._get_service_instance(
 1555                 host="fake_host.fqdn",
 1556                 username="fake_username@domain",
 1557                 password="fake_password",
 1558                 protocol="fake_protocol",
 1559                 port=1,
 1560                 mechanism="userpass",
 1561                 principal="fake principal",
 1562                 domain="fake_domain",
 1563             )
 1564             mock_sc.assert_called_once_with(
 1565                 host="fake_host.fqdn",
 1566                 user="fake_username@domain",
 1567                 pwd="fake_password",
 1568                 protocol="fake_protocol",
 1569                 port=1,
 1570                 b64token=None,
 1571                 mechanism="userpass",
 1572             )
 1573             mock_sc.reset_mock()
 1574             salt.utils.vmware._get_service_instance(
 1575                 host="fake_host.fqdn",
 1576                 username="domain\\fake_username",
 1577                 password="fake_password",
 1578                 protocol="fake_protocol",
 1579                 port=1,
 1580                 mechanism="userpass",
 1581                 principal="fake principal",
 1582                 domain="fake_domain",
 1583             )
 1584             mock_sc.assert_called_once_with(
 1585                 host="fake_host.fqdn",
 1586                 user="domain\\fake_username",
 1587                 pwd="fake_password",
 1588                 protocol="fake_protocol",
 1589                 port=1,
 1590                 b64token=None,
 1591                 mechanism="userpass",
 1592             )
 1593 
 1594     def test_sspi_empty_principal(self):
 1595         with self.assertRaises(CommandExecutionError) as excinfo:
 1596             salt.utils.vmware._get_service_instance(
 1597                 host="fake_host.fqdn",
 1598                 username="fake_username",
 1599                 password="fake_password",
 1600                 protocol="fake_protocol",
 1601                 port=1,
 1602                 mechanism="sspi",
 1603                 principal=None,
 1604                 domain="fake_domain",
 1605             )
 1606         self.assertIn("mandatory parameters are missing", excinfo.exception.strerror)
 1607 
 1608     def test_sspi_empty_domain(self):
 1609         with self.assertRaises(CommandExecutionError) as excinfo:
 1610             salt.utils.vmware._get_service_instance(
 1611                 host="fake_host.fqdn",
 1612                 username="fake_username",
 1613                 password="fake_password",
 1614                 protocol="fake_protocol",
 1615                 port=1,
 1616                 mechanism="sspi",
 1617                 principal="fake_principal",
 1618                 domain=None,
 1619             )
 1620         self.assertIn("mandatory parameters are missing", excinfo.exception.strerror)
 1621 
 1622     def test_sspi_get_token_error(self):
 1623         mock_token = MagicMock(side_effect=Exception("Exception"))
 1624 
 1625         with patch("salt.utils.vmware.get_gssapi_token", mock_token):
 1626             with self.assertRaises(VMwareConnectionError) as excinfo:
 1627                 salt.utils.vmware._get_service_instance(
 1628                     host="fake_host.fqdn",
 1629                     username="fake_username",
 1630                     password="fake_password",
 1631                     protocol="fake_protocol",
 1632                     port=1,
 1633                     mechanism="sspi",
 1634                     principal="fake_principal",
 1635                     domain="fake_domain",
 1636                 )
 1637             mock_token.assert_called_once_with(
 1638                 "fake_principal", "fake_host.fqdn", "fake_domain"
 1639             )
 1640             self.assertEqual("Exception", excinfo.exception.strerror)
 1641 
 1642     def test_sspi_get_token_success_(self):
 1643         mock_token = MagicMock(return_value="fake_token")
 1644         mock_sc = MagicMock()
 1645 
 1646         with patch("salt.utils.vmware.get_gssapi_token", mock_token):
 1647             with patch("salt.utils.vmware.SmartConnect", mock_sc):
 1648                 salt.utils.vmware._get_service_instance(
 1649                     host="fake_host.fqdn",
 1650                     username="fake_username",
 1651                     password="fake_password",
 1652                     protocol="fake_protocol",
 1653                     port=1,
 1654                     mechanism="sspi",
 1655                     principal="fake_principal",
 1656                     domain="fake_domain",
 1657                 )
 1658             mock_token.assert_called_once_with(
 1659                 "fake_principal", "fake_host.fqdn", "fake_domain"
 1660             )
 1661             mock_sc.assert_called_once_with(
 1662                 host="fake_host.fqdn",
 1663                 user="fake_username",
 1664                 pwd="fake_password",
 1665                 protocol="fake_protocol",
 1666                 port=1,
 1667                 b64token="fake_token",
 1668                 mechanism="sspi",
 1669             )
 1670 
 1671     def test_first_attempt_successful_connection(self):
 1672         mock_sc = MagicMock()
 1673         with patch("salt.utils.vmware.SmartConnect", mock_sc):
 1674             salt.utils.vmware._get_service_instance(
 1675                 host="fake_host.fqdn",
 1676                 username="fake_username",
 1677                 password="fake_password",
 1678                 protocol="fake_protocol",
 1679                 port=1,
 1680                 mechanism="sspi",
 1681                 principal="fake_principal",
 1682                 domain="fake_domain",
 1683             )
 1684             mock_sc.assert_called_once_with(
 1685                 host="fake_host.fqdn",
 1686                 user="fake_username",
 1687                 pwd="fake_password",
 1688                 protocol="fake_protocol",
 1689                 port=1,
 1690                 b64token="fake_token",
 1691                 mechanism="sspi",
 1692             )
 1693 
 1694     def test_second_attempt_successful_connection(self):
 1695         with patch("ssl.SSLContext", MagicMock()), patch(
 1696             "ssl._create_unverified_context", MagicMock()
 1697         ):
 1698             exc = vim.fault.HostConnectFault()
 1699             exc.msg = "[SSL: CERTIFICATE_VERIFY_FAILED]"
 1700             mock_sc = MagicMock(side_effect=[exc, None])
 1701             mock_ssl = MagicMock()
 1702 
 1703             with patch("salt.utils.vmware.SmartConnect", mock_sc):
 1704                 with patch("ssl._create_unverified_context", mock_ssl):
 1705 
 1706                     salt.utils.vmware._get_service_instance(
 1707                         host="fake_host.fqdn",
 1708                         username="fake_username",
 1709                         password="fake_password",
 1710                         protocol="fake_protocol",
 1711                         port=1,
 1712                         mechanism="sspi",
 1713                         principal="fake_principal",
 1714                         domain="fake_domain",
 1715                     )
 1716 
 1717                     mock_ssl.assert_called_once_with()
 1718                     calls = [
 1719                         call(
 1720                             host="fake_host.fqdn",
 1721                             user="fake_username",
 1722                             pwd="fake_password",
 1723                             protocol="fake_protocol",
 1724                             port=1,
 1725                             b64token="fake_token",
 1726                             mechanism="sspi",
 1727                         ),
 1728                         call(
 1729                             host="fake_host.fqdn",
 1730                             user="fake_username",
 1731                             pwd="fake_password",
 1732                             protocol="fake_protocol",
 1733                             port=1,
 1734                             sslContext=mock_ssl.return_value,
 1735                             b64token="fake_token",
 1736                             mechanism="sspi",
 1737                         ),
 1738                     ]
 1739                     mock_sc.assert_has_calls(calls)
 1740 
 1741     def test_third_attempt_successful_connection(self):
 1742         with patch("ssl.SSLContext", MagicMock()), patch(
 1743             "ssl._create_unverified_context", MagicMock()
 1744         ):
 1745             exc = vim.fault.HostConnectFault()
 1746             exc.msg = "[SSL: CERTIFICATE_VERIFY_FAILED]"
 1747             exc2 = Exception("certificate verify failed")
 1748             mock_sc = MagicMock(side_effect=[exc, exc2, None])
 1749             mock_ssl_unverif = MagicMock()
 1750             mock_ssl_context = MagicMock()
 1751 
 1752             with patch("salt.utils.vmware.SmartConnect", mock_sc):
 1753                 with patch("ssl._create_unverified_context", mock_ssl_unverif):
 1754                     with patch("ssl.SSLContext", mock_ssl_context):
 1755 
 1756                         salt.utils.vmware._get_service_instance(
 1757                             host="fake_host.fqdn",
 1758                             username="fake_username",
 1759                             password="fake_password",
 1760                             protocol="fake_protocol",
 1761                             port=1,
 1762                             mechanism="sspi",
 1763                             principal="fake_principal",
 1764                             domain="fake_domain",
 1765                         )
 1766 
 1767                         mock_ssl_context.assert_called_once_with(ssl.PROTOCOL_TLSv1)
 1768                         mock_ssl_unverif.assert_called_once_with()
 1769                         calls = [
 1770                             call(
 1771                                 host="fake_host.fqdn",
 1772                                 user="fake_username",
 1773                                 pwd="fake_password",
 1774                                 protocol="fake_protocol",
 1775                                 port=1,
 1776                                 b64token="fake_token",
 1777                                 mechanism="sspi",
 1778                             ),
 1779                             call(
 1780                                 host="fake_host.fqdn",
 1781                                 user="fake_username",
 1782                                 pwd="fake_password",
 1783                                 protocol="fake_protocol",
 1784                                 port=1,
 1785                                 sslContext=mock_ssl_unverif.return_value,
 1786                                 b64token="fake_token",
 1787                                 mechanism="sspi",
 1788                             ),
 1789                             call(
 1790                                 host="fake_host.fqdn",
 1791                                 user="fake_username",
 1792                                 pwd="fake_password",
 1793                                 protocol="fake_protocol",
 1794                                 port=1,
 1795                                 sslContext=mock_ssl_context.return_value,
 1796                                 b64token="fake_token",
 1797                                 mechanism="sspi",
 1798                             ),
 1799                         ]
 1800                         mock_sc.assert_has_calls(calls)
 1801 
 1802     def test_first_attempt_unsuccessful_connection_default_error(self):
 1803         exc = Exception("Exception")
 1804         mock_sc = MagicMock(side_effect=exc)
 1805 
 1806         with patch("salt.utils.vmware.SmartConnect", mock_sc):
 1807             with self.assertRaises(VMwareConnectionError) as excinfo:
 1808                 salt.utils.vmware._get_service_instance(
 1809                     host="fake_host.fqdn",
 1810                     username="fake_username",
 1811                     password="fake_password",
 1812                     protocol="fake_protocol",
 1813                     port=1,
 1814                     mechanism="sspi",
 1815                     principal="fake_principal",
 1816                     domain="fake_domain",
 1817                 )
 1818 
 1819                 self.assertEqual(mock_sc.call_count, 1)
 1820                 self.assertIn(
 1821                     "Could not connect to host 'fake_host.fqdn'",
 1822                     excinfo.Exception.message,
 1823                 )
 1824 
 1825     def test_first_attempt_unsuccessful_connection_vim_fault(self):
 1826         exc = vim.fault.VimFault()
 1827         exc.msg = "VimFault"
 1828         mock_sc = MagicMock(side_effect=exc)
 1829 
 1830         with patch("salt.utils.vmware.SmartConnect", mock_sc):
 1831             with self.assertRaises(VMwareConnectionError) as excinfo:
 1832                 salt.utils.vmware._get_service_instance(
 1833                     host="fake_host.fqdn",
 1834                     username="fake_username",
 1835                     password="fake_password",
 1836                     protocol="fake_protocol",
 1837                     port=1,
 1838                     mechanism="sspi",
 1839                     principal="fake_principal",
 1840                     domain="fake_domain",
 1841                 )
 1842 
 1843                 self.assertEqual(mock_sc.call_count, 1)
 1844                 self.assertEqual("VimFault", excinfo.Exception.message)
 1845 
 1846     def test_second_attempt_unsuccsessful_connection_default_error(self):
 1847         with patch("ssl.SSLContext", MagicMock()), patch(
 1848             "ssl._create_unverified_context", MagicMock()
 1849         ):
 1850             exc = vim.fault.HostConnectFault()
 1851             exc.msg = "[SSL: CERTIFICATE_VERIFY_FAILED]"
 1852             exc2 = Exception("Exception")
 1853             mock_sc = MagicMock(side_effect=[exc, exc2])
 1854 
 1855             with patch("salt.utils.vmware.SmartConnect", mock_sc):
 1856                 with self.assertRaises(VMwareConnectionError) as excinfo:
 1857                     salt.utils.vmware._get_service_instance(
 1858                         host="fake_host.fqdn",
 1859                         username="fake_username",
 1860                         password="fake_password",
 1861                         protocol="fake_protocol",
 1862                         port=1,
 1863                         mechanism="sspi",
 1864                         principal="fake_principal",
 1865                         domain="fake_domain",
 1866                     )
 1867 
 1868                     self.assertEqual(mock_sc.call_count, 2)
 1869                     self.assertIn(
 1870                         "Could not connect to host 'fake_host.fqdn'",
 1871                         excinfo.Exception.message,
 1872                     )
 1873 
 1874     def test_second_attempt_unsuccsessful_connection_vim_fault(self):
 1875         with patch("ssl.SSLContext", MagicMock()), patch(
 1876             "ssl._create_unverified_context", MagicMock()
 1877         ):
 1878             exc = vim.fault.HostConnectFault()
 1879             exc.msg = "[SSL: CERTIFICATE_VERIFY_FAILED]"
 1880             exc2 = vim.fault.VimFault()
 1881             exc2.msg = "VimFault"
 1882             mock_sc = MagicMock(side_effect=[exc, exc2])
 1883 
 1884             with patch("salt.utils.vmware.SmartConnect", mock_sc):
 1885                 with self.assertRaises(VMwareConnectionError) as excinfo:
 1886                     salt.utils.vmware._get_service_instance(
 1887                         host="fake_host.fqdn",
 1888                         username="fake_username",
 1889                         password="fake_password",
 1890                         protocol="fake_protocol",
 1891                         port=1,
 1892                         mechanism="sspi",
 1893                         principal="fake_principal",
 1894                         domain="fake_domain",
 1895                     )
 1896 
 1897                     self.assertEqual(mock_sc.call_count, 2)
 1898                     self.assertIn("VimFault", excinfo.Exception.message)
 1899 
 1900     def test_third_attempt_unsuccessful_connection_detault_error(self):
 1901         with patch("ssl.SSLContext", MagicMock()), patch(
 1902             "ssl._create_unverified_context", MagicMock()
 1903         ):
 1904             exc = vim.fault.HostConnectFault()
 1905             exc.msg = "[SSL: CERTIFICATE_VERIFY_FAILED]"
 1906             exc2 = Exception("certificate verify failed")
 1907             exc3 = Exception("Exception")
 1908             mock_sc = MagicMock(side_effect=[exc, exc2, exc3])
 1909 
 1910             with patch("salt.utils.vmware.SmartConnect", mock_sc):
 1911                 with self.assertRaises(VMwareConnectionError) as excinfo:
 1912                     salt.utils.vmware._get_service_instance(
 1913                         host="fake_host.fqdn",
 1914                         username="fake_username",
 1915                         password="fake_password",
 1916                         protocol="fake_protocol",
 1917                         port=1,
 1918                         mechanism="sspi",
 1919                         principal="fake_principal",
 1920                         domain="fake_domain",
 1921                     )
 1922 
 1923                     self.assertEqual(mock_sc.call_count, 3)
 1924                     self.assertIn("Exception", excinfo.Exception.message)
 1925 
 1926     def test_third_attempt_unsuccessful_connection_vim_fault(self):
 1927         with patch("ssl.SSLContext", MagicMock()), patch(
 1928             "ssl._create_unverified_context", MagicMock()
 1929         ):
 1930             exc = vim.fault.HostConnectFault()
 1931             exc.msg = "[SSL: CERTIFICATE_VERIFY_FAILED]"
 1932             exc2 = Exception("certificate verify failed")
 1933             exc3 = vim.fault.VimFault()
 1934             exc3.msg = "VimFault"
 1935             mock_sc = MagicMock(side_effect=[exc, exc2, exc3])
 1936 
 1937             with patch("salt.utils.vmware.SmartConnect", mock_sc):
 1938                 with self.assertRaises(VMwareConnectionError) as excinfo:
 1939                     salt.utils.vmware._get_service_instance(
 1940                         host="fake_host.fqdn",
 1941                         username="fake_username",
 1942                         password="fake_password",
 1943                         protocol="fake_protocol",
 1944                         port=1,
 1945                         mechanism="sspi",
 1946                         principal="fake_principal",
 1947                         domain="fake_domain",
 1948                     )
 1949 
 1950                     self.assertEqual(mock_sc.call_count, 3)
 1951                     self.assertIn("VimFault", excinfo.Exception.message)
 1952 
 1953 
 1954 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 1955 class GetServiceInstanceTestCase(TestCase):
 1956     """
 1957     Tests for salt.utils.vmware.get_service_instance
 1958     """
 1959 
 1960     def setUp(self):
 1961         patches = (
 1962             ("salt.utils.vmware.GetSi", MagicMock(return_value=None)),
 1963             (
 1964                 "salt.utils.vmware._get_service_instance",
 1965                 MagicMock(return_value=MagicMock()),
 1966             ),
 1967         )
 1968         for mod, mock in patches:
 1969             patcher = patch(mod, mock)
 1970             patcher.start()
 1971             self.addCleanup(patcher.stop)
 1972 
 1973     def test_default_params(self):
 1974         mock_get_si = MagicMock()
 1975         with patch("salt.utils.vmware._get_service_instance", mock_get_si):
 1976             salt.utils.vmware.get_service_instance(host="fake_host")
 1977             mock_get_si.assert_called_once_with(
 1978                 "fake_host", None, None, "https", 443, "userpass", None, None
 1979             )
 1980 
 1981     def test_no_cached_service_instance_same_host_on_proxy(self):
 1982         with patch("salt.utils.platform.is_proxy", MagicMock(return_value=True)):
 1983             # Service instance is uncached when using class default mock objs
 1984             mock_get_si = MagicMock()
 1985             with patch("salt.utils.vmware._get_service_instance", mock_get_si):
 1986                 salt.utils.vmware.get_service_instance(
 1987                     host="fake_host",
 1988                     username="fake_username",
 1989                     password="fake_password",
 1990                     protocol="fake_protocol",
 1991                     port=1,
 1992                     mechanism="fake_mechanism",
 1993                     principal="fake_principal",
 1994                     domain="fake_domain",
 1995                 )
 1996                 mock_get_si.assert_called_once_with(
 1997                     "fake_host",
 1998                     "fake_username",
 1999                     "fake_password",
 2000                     "fake_protocol",
 2001                     1,
 2002                     "fake_mechanism",
 2003                     "fake_principal",
 2004                     "fake_domain",
 2005                 )
 2006 
 2007     def test_cached_service_instance_different_host(self):
 2008         mock_si = MagicMock()
 2009         mock_disconnect = MagicMock()
 2010         mock_get_si = MagicMock(return_value=mock_si)
 2011         mock_getstub = MagicMock()
 2012         with patch("salt.utils.vmware.GetSi", mock_get_si):
 2013             with patch("salt.utils.vmware.GetStub", mock_getstub):
 2014                 with patch("salt.utils.vmware.Disconnect", mock_disconnect):
 2015                     salt.utils.vmware.get_service_instance(
 2016                         host="fake_host",
 2017                         username="fake_username",
 2018                         password="fake_password",
 2019                         protocol="fake_protocol",
 2020                         port=1,
 2021                         mechanism="fake_mechanism",
 2022                         principal="fake_principal",
 2023                         domain="fake_domain",
 2024                     )
 2025             self.assertEqual(mock_get_si.call_count, 1)
 2026             self.assertEqual(mock_getstub.call_count, 1)
 2027             self.assertEqual(mock_disconnect.call_count, 1)
 2028 
 2029     def test_uncached_service_instance(self):
 2030         # Service instance is uncached when using class default mock objs
 2031         mock_get_si = MagicMock()
 2032         with patch("salt.utils.vmware._get_service_instance", mock_get_si):
 2033             salt.utils.vmware.get_service_instance(
 2034                 host="fake_host",
 2035                 username="fake_username",
 2036                 password="fake_password",
 2037                 protocol="fake_protocol",
 2038                 port=1,
 2039                 mechanism="fake_mechanism",
 2040                 principal="fake_principal",
 2041                 domain="fake_domain",
 2042             )
 2043             mock_get_si.assert_called_once_with(
 2044                 "fake_host",
 2045                 "fake_username",
 2046                 "fake_password",
 2047                 "fake_protocol",
 2048                 1,
 2049                 "fake_mechanism",
 2050                 "fake_principal",
 2051                 "fake_domain",
 2052             )
 2053 
 2054     def test_unauthenticated_service_instance(self):
 2055         mock_si_current_time = MagicMock(side_effect=vim.fault.NotAuthenticated)
 2056         mock_si = MagicMock()
 2057         mock_get_si = MagicMock(return_value=mock_si)
 2058         mock_si.CurrentTime = mock_si_current_time
 2059         mock_disconnect = MagicMock()
 2060         with patch("salt.utils.vmware._get_service_instance", mock_get_si):
 2061             with patch("salt.utils.vmware.Disconnect", mock_disconnect):
 2062                 salt.utils.vmware.get_service_instance(
 2063                     host="fake_host",
 2064                     username="fake_username",
 2065                     password="fake_password",
 2066                     protocol="fake_protocol",
 2067                     port=1,
 2068                     mechanism="fake_mechanism",
 2069                     principal="fake_principal",
 2070                     domain="fake_domain",
 2071                 )
 2072                 self.assertEqual(mock_si_current_time.call_count, 1)
 2073                 self.assertEqual(mock_disconnect.call_count, 1)
 2074                 self.assertEqual(mock_get_si.call_count, 2)
 2075 
 2076     def test_cached_unauthenticated_service_instance(self):
 2077         mock_si_current_time = MagicMock(side_effect=vim.fault.NotAuthenticated)
 2078         mock_si = MagicMock()
 2079         mock_get_si = MagicMock(return_value=mock_si)
 2080         mock_getsi = MagicMock(return_value=mock_si)
 2081         mock_si.CurrentTime = mock_si_current_time
 2082         mock_disconnect = MagicMock()
 2083         with patch("salt.utils.vmware.GetSi", mock_getsi):
 2084             with patch("salt.utils.vmware._get_service_instance", mock_get_si):
 2085                 with patch("salt.utils.vmware.Disconnect", mock_disconnect):
 2086                     salt.utils.vmware.get_service_instance(
 2087                         host="fake_host",
 2088                         username="fake_username",
 2089                         password="fake_password",
 2090                         protocol="fake_protocol",
 2091                         port=1,
 2092                         mechanism="fake_mechanism",
 2093                         principal="fake_principal",
 2094                         domain="fake_domain",
 2095                     )
 2096                     self.assertEqual(mock_si_current_time.call_count, 1)
 2097                     self.assertEqual(mock_disconnect.call_count, 1)
 2098                     self.assertEqual(mock_get_si.call_count, 1)
 2099 
 2100     def test_current_time_raise_no_permission(self):
 2101         exc = vim.fault.NoPermission()
 2102         exc.privilegeId = "Fake privilege"
 2103         with patch(
 2104             "salt.utils.vmware._get_service_instance",
 2105             MagicMock(return_value=MagicMock(CurrentTime=MagicMock(side_effect=exc))),
 2106         ):
 2107             with self.assertRaises(VMwareApiError) as excinfo:
 2108                 salt.utils.vmware.get_service_instance(
 2109                     host="fake_host",
 2110                     username="fake_username",
 2111                     password="fake_password",
 2112                     protocol="fake_protocol",
 2113                     port=1,
 2114                     mechanism="fake_mechanism",
 2115                     principal="fake_principal",
 2116                     domain="fake_domain",
 2117                 )
 2118         self.assertEqual(
 2119             excinfo.exception.strerror,
 2120             "Not enough permissions. Required privilege: " "Fake privilege",
 2121         )
 2122 
 2123     def test_current_time_raise_vim_fault(self):
 2124         exc = vim.fault.VimFault()
 2125         exc.msg = "VimFault msg"
 2126         with patch(
 2127             "salt.utils.vmware._get_service_instance",
 2128             MagicMock(return_value=MagicMock(CurrentTime=MagicMock(side_effect=exc))),
 2129         ):
 2130             with self.assertRaises(VMwareApiError) as excinfo:
 2131                 salt.utils.vmware.get_service_instance(
 2132                     host="fake_host",
 2133                     username="fake_username",
 2134                     password="fake_password",
 2135                     protocol="fake_protocol",
 2136                     port=1,
 2137                     mechanism="fake_mechanism",
 2138                     principal="fake_principal",
 2139                     domain="fake_domain",
 2140                 )
 2141         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 2142 
 2143     def test_current_time_raise_runtime_fault(self):
 2144         exc = vmodl.RuntimeFault()
 2145         exc.msg = "RuntimeFault msg"
 2146         with patch(
 2147             "salt.utils.vmware._get_service_instance",
 2148             MagicMock(return_value=MagicMock(CurrentTime=MagicMock(side_effect=exc))),
 2149         ):
 2150             with self.assertRaises(VMwareRuntimeError) as excinfo:
 2151                 salt.utils.vmware.get_service_instance(
 2152                     host="fake_host",
 2153                     username="fake_username",
 2154                     password="fake_password",
 2155                     protocol="fake_protocol",
 2156                     port=1,
 2157                     mechanism="fake_mechanism",
 2158                     principal="fake_principal",
 2159                     domain="fake_domain",
 2160                 )
 2161         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 2162 
 2163 
 2164 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 2165 class DisconnectTestCase(TestCase):
 2166     """
 2167     Tests for salt.utils.vmware.disconnect
 2168     """
 2169 
 2170     def setUp(self):
 2171         self.mock_si = MagicMock()
 2172         self.addCleanup(delattr, self, "mock_si")
 2173 
 2174     def test_disconnect(self):
 2175         mock_disconnect = MagicMock()
 2176         with patch("salt.utils.vmware.Disconnect", mock_disconnect):
 2177             salt.utils.vmware.disconnect(service_instance=self.mock_si)
 2178             mock_disconnect.assert_called_once_with(self.mock_si)
 2179 
 2180     def test_disconnect_raise_no_permission(self):
 2181         exc = vim.fault.NoPermission()
 2182         exc.privilegeId = "Fake privilege"
 2183         with patch("salt.utils.vmware.Disconnect", MagicMock(side_effect=exc)):
 2184             with self.assertRaises(VMwareApiError) as excinfo:
 2185                 salt.utils.vmware.disconnect(service_instance=self.mock_si)
 2186         self.assertEqual(
 2187             excinfo.exception.strerror,
 2188             "Not enough permissions. Required privilege: " "Fake privilege",
 2189         )
 2190 
 2191     def test_disconnect_raise_vim_fault(self):
 2192         exc = vim.fault.VimFault()
 2193         exc.msg = "VimFault msg"
 2194         with patch("salt.utils.vmware.Disconnect", MagicMock(side_effect=exc)):
 2195             with self.assertRaises(VMwareApiError) as excinfo:
 2196                 salt.utils.vmware.disconnect(service_instance=self.mock_si)
 2197         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 2198 
 2199     def test_disconnect_raise_runtime_fault(self):
 2200         exc = vmodl.RuntimeFault()
 2201         exc.msg = "RuntimeFault msg"
 2202         with patch("salt.utils.vmware.Disconnect", MagicMock(side_effect=exc)):
 2203             with self.assertRaises(VMwareRuntimeError) as excinfo:
 2204                 salt.utils.vmware.disconnect(service_instance=self.mock_si)
 2205         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 2206 
 2207 
 2208 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 2209 class IsConnectionToAVCenterTestCase(TestCase):
 2210     """
 2211     Tests for salt.utils.vmware.is_connection_to_a_vcenter
 2212     """
 2213 
 2214     def test_api_type_raise_no_permission(self):
 2215         exc = vim.fault.NoPermission()
 2216         exc.privilegeId = "Fake privilege"
 2217         mock_si = MagicMock()
 2218         type(mock_si.content.about).apiType = PropertyMock(side_effect=exc)
 2219         with self.assertRaises(VMwareApiError) as excinfo:
 2220             salt.utils.vmware.is_connection_to_a_vcenter(mock_si)
 2221         self.assertEqual(
 2222             excinfo.exception.strerror,
 2223             "Not enough permissions. Required privilege: " "Fake privilege",
 2224         )
 2225 
 2226     def test_api_type_raise_vim_fault(self):
 2227         exc = vim.fault.VimFault()
 2228         exc.msg = "VimFault msg"
 2229         mock_si = MagicMock()
 2230         type(mock_si.content.about).apiType = PropertyMock(side_effect=exc)
 2231         with self.assertRaises(VMwareApiError) as excinfo:
 2232             salt.utils.vmware.is_connection_to_a_vcenter(mock_si)
 2233         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 2234 
 2235     def test_api_type_raise_runtime_fault(self):
 2236         exc = vmodl.RuntimeFault()
 2237         exc.msg = "RuntimeFault msg"
 2238         mock_si = MagicMock()
 2239         type(mock_si.content.about).apiType = PropertyMock(side_effect=exc)
 2240         with self.assertRaises(VMwareRuntimeError) as excinfo:
 2241             salt.utils.vmware.is_connection_to_a_vcenter(mock_si)
 2242         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 2243 
 2244     def test_connected_to_a_vcenter(self):
 2245         mock_si = MagicMock()
 2246         mock_si.content.about.apiType = "VirtualCenter"
 2247 
 2248         ret = salt.utils.vmware.is_connection_to_a_vcenter(mock_si)
 2249         self.assertTrue(ret)
 2250 
 2251     def test_connected_to_a_host(self):
 2252         mock_si = MagicMock()
 2253         mock_si.content.about.apiType = "HostAgent"
 2254 
 2255         ret = salt.utils.vmware.is_connection_to_a_vcenter(mock_si)
 2256         self.assertFalse(ret)
 2257 
 2258     def test_connected_to_invalid_entity(self):
 2259         mock_si = MagicMock()
 2260         mock_si.content.about.apiType = "UnsupportedType"
 2261 
 2262         with self.assertRaises(VMwareApiError) as excinfo:
 2263             salt.utils.vmware.is_connection_to_a_vcenter(mock_si)
 2264         self.assertIn(
 2265             "Unexpected api type 'UnsupportedType'", excinfo.exception.strerror
 2266         )
 2267 
 2268 
 2269 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 2270 class GetNewServiceInstanceStub(TestCase, LoaderModuleMockMixin):
 2271     """
 2272     Tests for salt.utils.vmware.get_new_service_instance_stub
 2273     """
 2274 
 2275     def setup_loader_modules(self):
 2276         return {salt.utils.vmware: {"sys": MagicMock(), "ssl": MagicMock()}}
 2277 
 2278     def setUp(self):
 2279         self.mock_stub = MagicMock(host="fake_host:1000", cookie='ignore"fake_cookie')
 2280         self.mock_si = MagicMock(_stub=self.mock_stub)
 2281         self.mock_ret = MagicMock()
 2282         self.mock_new_stub = MagicMock()
 2283         self.context_dict = {}
 2284         patches = (
 2285             (
 2286                 "salt.utils.vmware.VmomiSupport.GetRequestContext",
 2287                 MagicMock(return_value=self.context_dict),
 2288             ),
 2289             (
 2290                 "salt.utils.vmware.SoapStubAdapter",
 2291                 MagicMock(return_value=self.mock_new_stub),
 2292             ),
 2293         )
 2294         for mod, mock in patches:
 2295             patcher = patch(mod, mock)
 2296             patcher.start()
 2297             self.addCleanup(patcher.stop)
 2298 
 2299         self.mock_context = MagicMock()
 2300         self.mock_create_default_context = MagicMock(return_value=self.mock_context)
 2301         salt.utils.vmware.ssl.create_default_context = self.mock_create_default_context
 2302 
 2303     def tearDown(self):
 2304         for attr in (
 2305             "mock_stub",
 2306             "mock_si",
 2307             "mock_ret",
 2308             "mock_new_stub",
 2309             "context_dict",
 2310             "mock_context",
 2311             "mock_create_default_context",
 2312         ):
 2313             delattr(self, attr)
 2314 
 2315     def test_ssl_default_context_loaded(self):
 2316         salt.utils.vmware.get_new_service_instance_stub(self.mock_si, "fake_path")
 2317         self.mock_create_default_context.assert_called_once_with()
 2318         self.assertFalse(self.mock_context.check_hostname)
 2319         self.assertEqual(self.mock_context.verify_mode, salt.utils.vmware.ssl.CERT_NONE)
 2320 
 2321     def test_session_cookie_in_context(self):
 2322         salt.utils.vmware.get_new_service_instance_stub(self.mock_si, "fake_path")
 2323         self.assertEqual(self.context_dict["vcSessionCookie"], "fake_cookie")
 2324 
 2325     def test_get_new_stub(self):
 2326         mock_get_new_stub = MagicMock()
 2327         with patch("salt.utils.vmware.SoapStubAdapter", mock_get_new_stub):
 2328             salt.utils.vmware.get_new_service_instance_stub(
 2329                 self.mock_si, "fake_path", "fake_ns", "fake_version"
 2330             )
 2331         mock_get_new_stub.assert_called_once_with(
 2332             host="fake_host",
 2333             ns="fake_ns",
 2334             path="fake_path",
 2335             version="fake_version",
 2336             poolSize=0,
 2337             sslContext=self.mock_context,
 2338         )
 2339 
 2340     def test_new_stub_returned(self):
 2341         ret = salt.utils.vmware.get_new_service_instance_stub(self.mock_si, "fake_path")
 2342         self.assertEqual(self.mock_new_stub.cookie, 'ignore"fake_cookie')
 2343         self.assertEqual(ret, self.mock_new_stub)
 2344 
 2345 
 2346 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 2347 class GetServiceInstanceFromManagedObjectTestCase(TestCase):
 2348     """
 2349     Tests for salt.utils.vmware.get_managed_instance_from_managed_object
 2350     """
 2351 
 2352     def setUp(self):
 2353         patches = (("salt.utils.vmware.vim.ServiceInstance", MagicMock()),)
 2354         for mod, mock in patches:
 2355             patcher = patch(mod, mock)
 2356             patcher.start()
 2357             self.addCleanup(patcher.stop)
 2358         self.mock_si = MagicMock()
 2359         self.mock_stub = PropertyMock()
 2360         self.mock_mo_ref = MagicMock(_stub=self.mock_stub)
 2361         for attr in ("mock_si", "mock_stub", "mock_mo_ref"):
 2362             self.addCleanup(delattr, self, attr)
 2363 
 2364     def test_default_name_parameter(self):
 2365         mock_trace = MagicMock()
 2366         type(salt.utils.vmware.log).trace = mock_trace
 2367         salt.utils.vmware.get_service_instance_from_managed_object(self.mock_mo_ref)
 2368         mock_trace.assert_called_once_with(
 2369             "[%s] Retrieving service instance from managed object", "<unnamed>"
 2370         )
 2371 
 2372     def test_name_parameter_passed_in(self):
 2373         mock_trace = MagicMock()
 2374         type(salt.utils.vmware.log).trace = mock_trace
 2375         salt.utils.vmware.get_service_instance_from_managed_object(
 2376             self.mock_mo_ref, "fake_mo_name"
 2377         )
 2378         mock_trace.assert_called_once_with(
 2379             "[%s] Retrieving service instance from managed object", "fake_mo_name"
 2380         )
 2381 
 2382     def test_service_instance_instantiation(self):
 2383         mock_service_instance_ini = MagicMock()
 2384         with patch("salt.utils.vmware.vim.ServiceInstance", mock_service_instance_ini):
 2385             salt.utils.vmware.get_service_instance_from_managed_object(self.mock_mo_ref)
 2386         mock_service_instance_ini.assert_called_once_with("ServiceInstance")
 2387 
 2388     def test_si_return_and_stub_assignment(self):
 2389         with patch(
 2390             "salt.utils.vmware.vim.ServiceInstance",
 2391             MagicMock(return_value=self.mock_si),
 2392         ):
 2393             ret = salt.utils.vmware.get_service_instance_from_managed_object(
 2394                 self.mock_mo_ref
 2395             )
 2396         self.assertEqual(ret, self.mock_si)
 2397         self.assertEqual(ret._stub, self.mock_stub)
 2398 
 2399 
 2400 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 2401 class GetDatacentersTestCase(TestCase):
 2402     """
 2403     Tests for salt.utils.vmware.get_datacenters
 2404     """
 2405 
 2406     def setUp(self):
 2407         patches = (
 2408             (
 2409                 "salt.utils.vmware.get_mors_with_properties",
 2410                 MagicMock(return_value=[{"name": "fake_dc", "object": MagicMock()}]),
 2411             ),
 2412         )
 2413         for mod, mock in patches:
 2414             patcher = patch(mod, mock)
 2415             patcher.start()
 2416             self.addCleanup(patcher.stop)
 2417         self.mock_si = MagicMock()
 2418         self.mock_dc1 = MagicMock()
 2419         self.mock_dc2 = MagicMock()
 2420         self.mock_entries = [
 2421             {"name": "fake_dc1", "object": self.mock_dc1},
 2422             {"name": "fake_dc2", "object": self.mock_dc2},
 2423         ]
 2424 
 2425     def test_get_mors_with_properties_call(self):
 2426         mock_get_mors_with_properties = MagicMock(
 2427             return_value=[{"name": "fake_dc", "object": MagicMock()}]
 2428         )
 2429         with patch(
 2430             "salt.utils.vmware.get_mors_with_properties", mock_get_mors_with_properties
 2431         ):
 2432             salt.utils.vmware.get_datacenters(
 2433                 self.mock_si, datacenter_names=["fake_dc1"]
 2434             )
 2435         mock_get_mors_with_properties.assert_called_once_with(
 2436             self.mock_si, vim.Datacenter, property_list=["name"]
 2437         )
 2438 
 2439     def test_get_mors_with_properties_returns_empty_array(self):
 2440         with patch(
 2441             "salt.utils.vmware.get_mors_with_properties", MagicMock(return_value=[])
 2442         ):
 2443             res = salt.utils.vmware.get_datacenters(
 2444                 self.mock_si, datacenter_names=["fake_dc1"]
 2445             )
 2446         self.assertEqual(res, [])
 2447 
 2448     def test_no_parameters(self):
 2449         with patch(
 2450             "salt.utils.vmware.get_mors_with_properties",
 2451             MagicMock(return_value=self.mock_entries),
 2452         ):
 2453             res = salt.utils.vmware.get_datacenters(self.mock_si)
 2454         self.assertEqual(res, [])
 2455 
 2456     def test_datastore_not_found(self):
 2457         with patch(
 2458             "salt.utils.vmware.get_mors_with_properties",
 2459             MagicMock(return_value=self.mock_entries),
 2460         ):
 2461             res = salt.utils.vmware.get_datacenters(
 2462                 self.mock_si, datacenter_names=["fake_dc"]
 2463             )
 2464         self.assertEqual(res, [])
 2465 
 2466     def test_datastore_found(self):
 2467         with patch(
 2468             "salt.utils.vmware.get_mors_with_properties",
 2469             MagicMock(return_value=self.mock_entries),
 2470         ):
 2471             res = salt.utils.vmware.get_datacenters(
 2472                 self.mock_si, datacenter_names=["fake_dc2"]
 2473             )
 2474         self.assertEqual(res, [self.mock_dc2])
 2475 
 2476     def test_get_all_datastores(self):
 2477         with patch(
 2478             "salt.utils.vmware.get_mors_with_properties",
 2479             MagicMock(return_value=self.mock_entries),
 2480         ):
 2481             res = salt.utils.vmware.get_datacenters(
 2482                 self.mock_si, get_all_datacenters=True
 2483             )
 2484         self.assertEqual(res, [self.mock_dc1, self.mock_dc2])
 2485 
 2486 
 2487 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 2488 class GetDatacenterTestCase(TestCase):
 2489     """
 2490     Tests for salt.utils.vmware.get_datacenter
 2491     """
 2492 
 2493     def setUp(self):
 2494         patches = (
 2495             (
 2496                 "salt.utils.vmware.get_datacenters",
 2497                 MagicMock(return_value=[MagicMock()]),
 2498             ),
 2499         )
 2500         for mod, mock in patches:
 2501             patcher = patch(mod, mock)
 2502             patcher.start()
 2503             self.addCleanup(patcher.stop)
 2504         self.mock_si = MagicMock()
 2505         self.mock_dc = MagicMock()
 2506 
 2507     def test_get_datacenters_call(self):
 2508         mock_get_datacenters = MagicMock(return_value=[MagicMock()])
 2509         with patch("salt.utils.vmware.get_datacenters", mock_get_datacenters):
 2510             salt.utils.vmware.get_datacenter(self.mock_si, "fake_dc1")
 2511         mock_get_datacenters.assert_called_once_with(
 2512             self.mock_si, datacenter_names=["fake_dc1"]
 2513         )
 2514 
 2515     def test_no_datacenters_returned(self):
 2516         with patch("salt.utils.vmware.get_datacenters", MagicMock(return_value=[])):
 2517             with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
 2518                 salt.utils.vmware.get_datacenter(self.mock_si, "fake_dc1")
 2519         self.assertEqual(
 2520             "Datacenter 'fake_dc1' was not found", excinfo.exception.strerror
 2521         )
 2522 
 2523     def test_get_datacenter_return(self):
 2524         with patch(
 2525             "salt.utils.vmware.get_datacenters", MagicMock(return_value=[self.mock_dc])
 2526         ):
 2527             res = salt.utils.vmware.get_datacenter(self.mock_si, "fake_dc1")
 2528         self.assertEqual(res, self.mock_dc)
 2529 
 2530 
 2531 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 2532 class CreateDatacenterTestCase(TestCase):
 2533     """
 2534     Tests for salt.utils.vmware.create_datacenter
 2535     """
 2536 
 2537     def setUp(self):
 2538         patches = (("salt.utils.vmware.get_root_folder", MagicMock()),)
 2539         for mod, mock in patches:
 2540             patcher = patch(mod, mock)
 2541             patcher.start()
 2542             self.addCleanup(patcher.stop)
 2543         self.mock_si = MagicMock()
 2544         self.mock_dc = MagicMock()
 2545         self.mock_create_datacenter = MagicMock(return_value=self.mock_dc)
 2546         self.mock_root_folder = MagicMock(CreateDatacenter=self.mock_create_datacenter)
 2547 
 2548     def test_get_root_folder(self):
 2549         mock_get_root_folder = MagicMock()
 2550         with patch("salt.utils.vmware.get_root_folder", mock_get_root_folder):
 2551             salt.utils.vmware.create_datacenter(self.mock_si, "fake_dc")
 2552         mock_get_root_folder.assert_called_once_with(self.mock_si)
 2553 
 2554     def test_create_datacenter_call(self):
 2555         with patch(
 2556             "salt.utils.vmware.get_root_folder",
 2557             MagicMock(return_value=self.mock_root_folder),
 2558         ):
 2559             salt.utils.vmware.create_datacenter(self.mock_si, "fake_dc")
 2560         self.mock_create_datacenter.assert_called_once_with("fake_dc")
 2561 
 2562     def test_create_datacenter_raise_no_permission(self):
 2563         exc = vim.fault.NoPermission()
 2564         exc.privilegeId = "Fake privilege"
 2565         self.mock_root_folder = MagicMock(CreateDatacenter=MagicMock(side_effect=exc))
 2566         with patch(
 2567             "salt.utils.vmware.get_root_folder",
 2568             MagicMock(return_value=self.mock_root_folder),
 2569         ):
 2570             with self.assertRaises(VMwareApiError) as excinfo:
 2571                 salt.utils.vmware.create_datacenter(self.mock_si, "fake_dc")
 2572         self.assertEqual(
 2573             excinfo.exception.strerror,
 2574             "Not enough permissions. Required privilege: " "Fake privilege",
 2575         )
 2576 
 2577     def test_create_datacenter_raise_vim_fault(self):
 2578         exc = vim.VimFault()
 2579         exc.msg = "VimFault msg"
 2580         self.mock_root_folder = MagicMock(CreateDatacenter=MagicMock(side_effect=exc))
 2581         with patch(
 2582             "salt.utils.vmware.get_root_folder",
 2583             MagicMock(return_value=self.mock_root_folder),
 2584         ):
 2585             with self.assertRaises(VMwareApiError) as excinfo:
 2586                 salt.utils.vmware.create_datacenter(self.mock_si, "fake_dc")
 2587         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 2588 
 2589     def test_create_datacenter_runtime_fault(self):
 2590         exc = vmodl.RuntimeFault()
 2591         exc.msg = "RuntimeFault msg"
 2592         self.mock_root_folder = MagicMock(CreateDatacenter=MagicMock(side_effect=exc))
 2593         with patch(
 2594             "salt.utils.vmware.get_root_folder",
 2595             MagicMock(return_value=self.mock_root_folder),
 2596         ):
 2597             with self.assertRaises(VMwareRuntimeError) as excinfo:
 2598                 salt.utils.vmware.create_datacenter(self.mock_si, "fake_dc")
 2599         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 2600 
 2601     def test_datastore_successfully_created(self):
 2602         with patch(
 2603             "salt.utils.vmware.get_root_folder",
 2604             MagicMock(return_value=self.mock_root_folder),
 2605         ):
 2606             res = salt.utils.vmware.create_datacenter(self.mock_si, "fake_dc")
 2607         self.assertEqual(res, self.mock_dc)
 2608 
 2609 
 2610 class FakeTaskClass:
 2611     pass
 2612 
 2613 
 2614 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 2615 class GetDvssTestCase(TestCase):
 2616     def setUp(self):
 2617         self.mock_si = MagicMock()
 2618         self.mock_dc_ref = MagicMock()
 2619         self.mock_traversal_spec = MagicMock()
 2620         self.mock_items = [
 2621             {"object": MagicMock(), "name": "fake_dvs1"},
 2622             {"object": MagicMock(), "name": "fake_dvs2"},
 2623             {"object": MagicMock(), "name": "fake_dvs3"},
 2624         ]
 2625         self.mock_get_mors = MagicMock(return_value=self.mock_items)
 2626 
 2627         patches = (
 2628             ("salt.utils.vmware.get_managed_object_name", MagicMock()),
 2629             ("salt.utils.vmware.get_mors_with_properties", self.mock_get_mors),
 2630             (
 2631                 "salt.utils.vmware.get_service_instance_from_managed_object",
 2632                 MagicMock(return_value=self.mock_si),
 2633             ),
 2634             (
 2635                 "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 2636                 MagicMock(return_value=self.mock_traversal_spec),
 2637             ),
 2638         )
 2639         for mod, mock in patches:
 2640             patcher = patch(mod, mock)
 2641             patcher.start()
 2642             self.addCleanup(patcher.stop)
 2643 
 2644     def tearDown(self):
 2645         for attr in (
 2646             "mock_si",
 2647             "mock_dc_ref",
 2648             "mock_traversal_spec",
 2649             "mock_items",
 2650             "mock_get_mors",
 2651         ):
 2652             delattr(self, attr)
 2653 
 2654     def test_get_managed_object_name_call(self):
 2655         mock_get_managed_object_name = MagicMock()
 2656         with patch(
 2657             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
 2658         ):
 2659             salt.utils.vmware.get_dvss(self.mock_dc_ref)
 2660         mock_get_managed_object_name.assert_called_once_with(self.mock_dc_ref)
 2661 
 2662     def test_traversal_spec(self):
 2663         mock_traversal_spec = MagicMock(return_value="traversal_spec")
 2664         with patch(
 2665             "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 2666             mock_traversal_spec,
 2667         ):
 2668 
 2669             salt.utils.vmware.get_dvss(self.mock_dc_ref)
 2670         mock_traversal_spec.assert_has_calls(
 2671             [
 2672                 call(path="childEntity", skip=False, type=vim.Folder),
 2673                 call(
 2674                     path="networkFolder",
 2675                     skip=True,
 2676                     type=vim.Datacenter,
 2677                     selectSet=["traversal_spec"],
 2678                 ),
 2679             ]
 2680         )
 2681 
 2682     def test_get_mors_with_properties(self):
 2683         salt.utils.vmware.get_dvss(self.mock_dc_ref)
 2684         self.mock_get_mors.assert_called_once_with(
 2685             self.mock_si,
 2686             vim.DistributedVirtualSwitch,
 2687             container_ref=self.mock_dc_ref,
 2688             property_list=["name"],
 2689             traversal_spec=self.mock_traversal_spec,
 2690         )
 2691 
 2692     def test_get_no_dvss(self):
 2693         ret = salt.utils.vmware.get_dvss(self.mock_dc_ref)
 2694         self.assertEqual(ret, [])
 2695 
 2696     def test_get_all_dvss(self):
 2697         ret = salt.utils.vmware.get_dvss(self.mock_dc_ref, get_all_dvss=True)
 2698         self.assertEqual(ret, [i["object"] for i in self.mock_items])
 2699 
 2700     def test_filtered_all_dvss(self):
 2701         ret = salt.utils.vmware.get_dvss(
 2702             self.mock_dc_ref, dvs_names=["fake_dvs1", "fake_dvs3", "no_dvs"]
 2703         )
 2704         self.assertEqual(
 2705             ret, [self.mock_items[0]["object"], self.mock_items[2]["object"]]
 2706         )
 2707 
 2708 
 2709 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 2710 class GetNetworkFolderTestCase(TestCase):
 2711     def setUp(self):
 2712         self.mock_si = MagicMock()
 2713         self.mock_dc_ref = MagicMock()
 2714         self.mock_traversal_spec = MagicMock()
 2715         self.mock_entries = [{"object": MagicMock(), "name": "fake_netw_folder"}]
 2716         self.mock_get_mors = MagicMock(return_value=self.mock_entries)
 2717 
 2718         patches = (
 2719             (
 2720                 "salt.utils.vmware.get_managed_object_name",
 2721                 MagicMock(return_value="fake_dc"),
 2722             ),
 2723             (
 2724                 "salt.utils.vmware.get_service_instance_from_managed_object",
 2725                 MagicMock(return_value=self.mock_si),
 2726             ),
 2727             (
 2728                 "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 2729                 MagicMock(return_value=self.mock_traversal_spec),
 2730             ),
 2731             ("salt.utils.vmware.get_mors_with_properties", self.mock_get_mors),
 2732         )
 2733         for mod, mock in patches:
 2734             patcher = patch(mod, mock)
 2735             patcher.start()
 2736             self.addCleanup(patcher.stop)
 2737 
 2738     def tearDown(self):
 2739         for attr in (
 2740             "mock_si",
 2741             "mock_dc_ref",
 2742             "mock_traversal_spec",
 2743             "mock_entries",
 2744             "mock_get_mors",
 2745         ):
 2746             delattr(self, attr)
 2747 
 2748     def test_get_managed_object_name_call(self):
 2749         mock_get_managed_object_name = MagicMock()
 2750         with patch(
 2751             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
 2752         ):
 2753             salt.utils.vmware.get_network_folder(self.mock_dc_ref)
 2754         mock_get_managed_object_name.assert_called_once_with(self.mock_dc_ref)
 2755 
 2756     def test_traversal_spec(self):
 2757         mock_traversal_spec = MagicMock(return_value="traversal_spec")
 2758         with patch(
 2759             "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 2760             mock_traversal_spec,
 2761         ):
 2762 
 2763             salt.utils.vmware.get_network_folder(self.mock_dc_ref)
 2764         mock_traversal_spec.assert_called_once_with(
 2765             path="networkFolder", skip=False, type=vim.Datacenter
 2766         )
 2767 
 2768     def test_get_mors_with_properties(self):
 2769         salt.utils.vmware.get_network_folder(self.mock_dc_ref)
 2770         self.mock_get_mors.assert_called_once_with(
 2771             self.mock_si,
 2772             vim.Folder,
 2773             container_ref=self.mock_dc_ref,
 2774             property_list=["name"],
 2775             traversal_spec=self.mock_traversal_spec,
 2776         )
 2777 
 2778     def test_get_no_network_folder(self):
 2779         with patch(
 2780             "salt.utils.vmware.get_mors_with_properties", MagicMock(return_value=[])
 2781         ):
 2782             with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
 2783                 salt.utils.vmware.get_network_folder(self.mock_dc_ref)
 2784         self.assertEqual(
 2785             excinfo.exception.strerror,
 2786             "Network folder in datacenter 'fake_dc' wasn't " "retrieved",
 2787         )
 2788 
 2789     def test_get_network_folder(self):
 2790         ret = salt.utils.vmware.get_network_folder(self.mock_dc_ref)
 2791         self.assertEqual(ret, self.mock_entries[0]["object"])
 2792 
 2793 
 2794 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 2795 class CreateDvsTestCase(TestCase):
 2796     def setUp(self):
 2797         self.mock_dc_ref = MagicMock()
 2798         self.mock_dvs_create_spec = MagicMock()
 2799         self.mock_task = MagicMock(spec=FakeTaskClass)
 2800         self.mock_netw_folder = MagicMock(
 2801             CreateDVS_Task=MagicMock(return_value=self.mock_task)
 2802         )
 2803         self.mock_wait_for_task = MagicMock()
 2804 
 2805         patches = (
 2806             (
 2807                 "salt.utils.vmware.get_managed_object_name",
 2808                 MagicMock(return_value="fake_dc"),
 2809             ),
 2810             (
 2811                 "salt.utils.vmware.get_network_folder",
 2812                 MagicMock(return_value=self.mock_netw_folder),
 2813             ),
 2814             ("salt.utils.vmware.wait_for_task", self.mock_wait_for_task),
 2815         )
 2816         for mod, mock in patches:
 2817             patcher = patch(mod, mock)
 2818             patcher.start()
 2819             self.addCleanup(patcher.stop)
 2820 
 2821     def tearDown(self):
 2822         for attr in (
 2823             "mock_dc_ref",
 2824             "mock_dvs_create_spec",
 2825             "mock_task",
 2826             "mock_netw_folder",
 2827             "mock_wait_for_task",
 2828         ):
 2829             delattr(self, attr)
 2830 
 2831     def test_get_managed_object_name_call(self):
 2832         mock_get_managed_object_name = MagicMock()
 2833         with patch(
 2834             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
 2835         ):
 2836             salt.utils.vmware.create_dvs(self.mock_dc_ref, "fake_dvs")
 2837         mock_get_managed_object_name.assert_called_once_with(self.mock_dc_ref)
 2838 
 2839     def test_no_dvs_create_spec(self):
 2840         mock_spec = MagicMock(configSpec=None)
 2841         mock_config_spec = MagicMock()
 2842         mock_dvs_create_spec = MagicMock(return_value=mock_spec)
 2843         mock_vmware_dvs_config_spec = MagicMock(return_value=mock_config_spec)
 2844         with patch("salt.utils.vmware.vim.DVSCreateSpec", mock_dvs_create_spec):
 2845             with patch(
 2846                 "salt.utils.vmware.vim.VMwareDVSConfigSpec", mock_vmware_dvs_config_spec
 2847             ):
 2848                 salt.utils.vmware.create_dvs(self.mock_dc_ref, "fake_dvs")
 2849         mock_dvs_create_spec.assert_called_once_with()
 2850         mock_vmware_dvs_config_spec.assert_called_once_with()
 2851         self.assertEqual(mock_spec.configSpec, mock_config_spec)
 2852         self.assertEqual(mock_config_spec.name, "fake_dvs")
 2853         self.mock_netw_folder.CreateDVS_Task.assert_called_once_with(mock_spec)
 2854 
 2855     def test_get_network_folder(self):
 2856         mock_get_network_folder = MagicMock()
 2857         with patch("salt.utils.vmware.get_network_folder", mock_get_network_folder):
 2858             salt.utils.vmware.create_dvs(self.mock_dc_ref, "fake_dvs")
 2859         mock_get_network_folder.assert_called_once_with(self.mock_dc_ref)
 2860 
 2861     def test_create_dvs_task_passed_in_spec(self):
 2862         salt.utils.vmware.create_dvs(
 2863             self.mock_dc_ref, "fake_dvs", dvs_create_spec=self.mock_dvs_create_spec
 2864         )
 2865         self.mock_netw_folder.CreateDVS_Task.assert_called_once_with(
 2866             self.mock_dvs_create_spec
 2867         )
 2868 
 2869     def test_create_dvs_task_raises_no_permission(self):
 2870         exc = vim.fault.NoPermission()
 2871         exc.privilegeId = "Fake privilege"
 2872         self.mock_netw_folder.CreateDVS_Task = MagicMock(side_effect=exc)
 2873         with self.assertRaises(VMwareApiError) as excinfo:
 2874             salt.utils.vmware.create_dvs(
 2875                 self.mock_dc_ref, "fake_dvs", dvs_create_spec=self.mock_dvs_create_spec
 2876             )
 2877         self.assertEqual(
 2878             excinfo.exception.strerror,
 2879             "Not enough permissions. Required privilege: " "Fake privilege",
 2880         )
 2881 
 2882     def test_create_dvs_task_raises_vim_fault(self):
 2883         exc = vim.fault.VimFault()
 2884         exc.msg = "VimFault msg"
 2885         self.mock_netw_folder.CreateDVS_Task = MagicMock(side_effect=exc)
 2886         with self.assertRaises(VMwareApiError) as excinfo:
 2887             salt.utils.vmware.create_dvs(
 2888                 self.mock_dc_ref, "fake_dvs", dvs_create_spec=self.mock_dvs_create_spec
 2889             )
 2890         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 2891 
 2892     def test_create_dvs_task_raises_runtime_fault(self):
 2893         exc = vmodl.RuntimeFault()
 2894         exc.msg = "RuntimeFault msg"
 2895         self.mock_netw_folder.CreateDVS_Task = MagicMock(side_effect=exc)
 2896         with self.assertRaises(VMwareRuntimeError) as excinfo:
 2897             salt.utils.vmware.create_dvs(
 2898                 self.mock_dc_ref, "fake_dvs", dvs_create_spec=self.mock_dvs_create_spec
 2899             )
 2900         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 2901 
 2902     def test_wait_for_tasks(self):
 2903         salt.utils.vmware.create_dvs(
 2904             self.mock_dc_ref, "fake_dvs", dvs_create_spec=self.mock_dvs_create_spec
 2905         )
 2906         self.mock_wait_for_task.assert_called_once_with(
 2907             self.mock_task,
 2908             "fake_dvs",
 2909             "<class '{}unit.utils.test_vmware.FakeTaskClass'>".format(
 2910                 "tests." if RUNTIME_VARS.PYTEST_SESSION else ""
 2911             ),
 2912         )
 2913 
 2914 
 2915 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 2916 class UpdateDvsTestCase(TestCase):
 2917     def setUp(self):
 2918         self.mock_task = MagicMock(spec=FakeTaskClass)
 2919         self.mock_dvs_ref = MagicMock(
 2920             ReconfigureDvs_Task=MagicMock(return_value=self.mock_task)
 2921         )
 2922         self.mock_dvs_spec = MagicMock()
 2923         self.mock_wait_for_task = MagicMock()
 2924 
 2925         patches = (
 2926             (
 2927                 "salt.utils.vmware.get_managed_object_name",
 2928                 MagicMock(return_value="fake_dvs"),
 2929             ),
 2930             ("salt.utils.vmware.wait_for_task", self.mock_wait_for_task),
 2931         )
 2932         for mod, mock in patches:
 2933             patcher = patch(mod, mock)
 2934             patcher.start()
 2935             self.addCleanup(patcher.stop)
 2936 
 2937     def tearDown(self):
 2938         for attr in (
 2939             "mock_dvs_ref",
 2940             "mock_task",
 2941             "mock_dvs_spec",
 2942             "mock_wait_for_task",
 2943         ):
 2944             delattr(self, attr)
 2945 
 2946     def test_get_managed_object_name_call(self):
 2947         mock_get_managed_object_name = MagicMock()
 2948         with patch(
 2949             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
 2950         ):
 2951             salt.utils.vmware.update_dvs(self.mock_dvs_ref, self.mock_dvs_spec)
 2952         mock_get_managed_object_name.assert_called_once_with(self.mock_dvs_ref)
 2953 
 2954     def test_reconfigure_dvs_task(self):
 2955         salt.utils.vmware.update_dvs(self.mock_dvs_ref, self.mock_dvs_spec)
 2956         self.mock_dvs_ref.ReconfigureDvs_Task.assert_called_once_with(
 2957             self.mock_dvs_spec
 2958         )
 2959 
 2960     def test_reconfigure_dvs_task_raises_no_permission(self):
 2961         exc = vim.fault.NoPermission()
 2962         exc.privilegeId = "Fake privilege"
 2963         self.mock_dvs_ref.ReconfigureDvs_Task = MagicMock(side_effect=exc)
 2964         with self.assertRaises(VMwareApiError) as excinfo:
 2965             salt.utils.vmware.update_dvs(self.mock_dvs_ref, self.mock_dvs_spec)
 2966         self.assertEqual(
 2967             excinfo.exception.strerror,
 2968             "Not enough permissions. Required privilege: " "Fake privilege",
 2969         )
 2970 
 2971     def test_reconfigure_dvs_task_raises_vim_fault(self):
 2972         exc = vim.fault.VimFault()
 2973         exc.msg = "VimFault msg"
 2974         self.mock_dvs_ref.ReconfigureDvs_Task = MagicMock(side_effect=exc)
 2975         with self.assertRaises(VMwareApiError) as excinfo:
 2976             salt.utils.vmware.update_dvs(self.mock_dvs_ref, self.mock_dvs_spec)
 2977         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 2978 
 2979     def test_reconfigure_dvs_task_raises_runtime_fault(self):
 2980         exc = vmodl.RuntimeFault()
 2981         exc.msg = "RuntimeFault msg"
 2982         self.mock_dvs_ref.ReconfigureDvs_Task = MagicMock(side_effect=exc)
 2983         with self.assertRaises(VMwareRuntimeError) as excinfo:
 2984             salt.utils.vmware.update_dvs(self.mock_dvs_ref, self.mock_dvs_spec)
 2985         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 2986 
 2987     def test_wait_for_tasks(self):
 2988         salt.utils.vmware.update_dvs(self.mock_dvs_ref, self.mock_dvs_spec)
 2989         self.mock_wait_for_task.assert_called_once_with(
 2990             self.mock_task,
 2991             "fake_dvs",
 2992             "<class '{}unit.utils.test_vmware.FakeTaskClass'>".format(
 2993                 "tests." if RUNTIME_VARS.PYTEST_SESSION else ""
 2994             ),
 2995         )
 2996 
 2997 
 2998 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 2999 class SetDvsNetworkResourceManagementEnabledTestCase(TestCase):
 3000     def setUp(self):
 3001         self.mock_enabled = MagicMock()
 3002         self.mock_dvs_ref = MagicMock(EnableNetworkResourceManagement=MagicMock())
 3003 
 3004         patches = (
 3005             (
 3006                 "salt.utils.vmware.get_managed_object_name",
 3007                 MagicMock(return_value="fake_dvs"),
 3008             ),
 3009         )
 3010         for mod, mock in patches:
 3011             patcher = patch(mod, mock)
 3012             patcher.start()
 3013             self.addCleanup(patcher.stop)
 3014 
 3015     def tearDown(self):
 3016         for attr in ("mock_dvs_ref", "mock_enabled"):
 3017             delattr(self, attr)
 3018 
 3019     def test_get_managed_object_name_call(self):
 3020         mock_get_managed_object_name = MagicMock()
 3021         with patch(
 3022             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
 3023         ):
 3024             salt.utils.vmware.set_dvs_network_resource_management_enabled(
 3025                 self.mock_dvs_ref, self.mock_enabled
 3026             )
 3027         mock_get_managed_object_name.assert_called_once_with(self.mock_dvs_ref)
 3028 
 3029     def test_enable_network_resource_management(self):
 3030         salt.utils.vmware.set_dvs_network_resource_management_enabled(
 3031             self.mock_dvs_ref, self.mock_enabled
 3032         )
 3033         self.mock_dvs_ref.EnableNetworkResourceManagement.assert_called_once_with(
 3034             enable=self.mock_enabled
 3035         )
 3036 
 3037     def test_enable_network_resource_management_raises_no_permission(self):
 3038         exc = vim.fault.NoPermission()
 3039         exc.privilegeId = "Fake privilege"
 3040         self.mock_dvs_ref.EnableNetworkResourceManagement = MagicMock(side_effect=exc)
 3041         with self.assertRaises(VMwareApiError) as excinfo:
 3042             salt.utils.vmware.set_dvs_network_resource_management_enabled(
 3043                 self.mock_dvs_ref, self.mock_enabled
 3044             )
 3045         self.assertEqual(
 3046             excinfo.exception.strerror,
 3047             "Not enough permissions. Required privilege: " "Fake privilege",
 3048         )
 3049 
 3050     def test_enable_network_resource_management_raises_vim_fault(self):
 3051         exc = vim.fault.VimFault()
 3052         exc.msg = "VimFault msg"
 3053         self.mock_dvs_ref.EnableNetworkResourceManagement = MagicMock(side_effect=exc)
 3054         with self.assertRaises(VMwareApiError) as excinfo:
 3055             salt.utils.vmware.set_dvs_network_resource_management_enabled(
 3056                 self.mock_dvs_ref, self.mock_enabled
 3057             )
 3058 
 3059     def test_enable_network_resource_management_raises_runtime_fault(self):
 3060         exc = vmodl.RuntimeFault()
 3061         exc.msg = "RuntimeFault msg"
 3062         self.mock_dvs_ref.EnableNetworkResourceManagement = MagicMock(side_effect=exc)
 3063         with self.assertRaises(VMwareRuntimeError) as excinfo:
 3064             salt.utils.vmware.set_dvs_network_resource_management_enabled(
 3065                 self.mock_dvs_ref, self.mock_enabled
 3066             )
 3067         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 3068 
 3069 
 3070 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 3071 class GetDvportgroupsTestCase(TestCase):
 3072     def setUp(self):
 3073         self.mock_si = MagicMock()
 3074         self.mock_dc_ref = MagicMock(spec=vim.Datacenter)
 3075         self.mock_dvs_ref = MagicMock(spec=vim.DistributedVirtualSwitch)
 3076         self.mock_traversal_spec = MagicMock()
 3077         self.mock_items = [
 3078             {"object": MagicMock(), "name": "fake_pg1"},
 3079             {"object": MagicMock(), "name": "fake_pg2"},
 3080             {"object": MagicMock(), "name": "fake_pg3"},
 3081         ]
 3082         self.mock_get_mors = MagicMock(return_value=self.mock_items)
 3083 
 3084         patches = (
 3085             ("salt.utils.vmware.get_managed_object_name", MagicMock()),
 3086             ("salt.utils.vmware.get_mors_with_properties", self.mock_get_mors),
 3087             (
 3088                 "salt.utils.vmware.get_service_instance_from_managed_object",
 3089                 MagicMock(return_value=self.mock_si),
 3090             ),
 3091             (
 3092                 "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 3093                 MagicMock(return_value=self.mock_traversal_spec),
 3094             ),
 3095         )
 3096         for mod, mock in patches:
 3097             patcher = patch(mod, mock)
 3098             patcher.start()
 3099             self.addCleanup(patcher.stop)
 3100 
 3101     def tearDown(self):
 3102         for attr in (
 3103             "mock_si",
 3104             "mock_dc_ref",
 3105             "mock_dvs_ref",
 3106             "mock_traversal_spec",
 3107             "mock_items",
 3108             "mock_get_mors",
 3109         ):
 3110             delattr(self, attr)
 3111 
 3112     def test_unsupported_parrent(self):
 3113         with self.assertRaises(ArgumentValueError) as excinfo:
 3114             salt.utils.vmware.get_dvportgroups(MagicMock())
 3115         self.assertEqual(
 3116             excinfo.exception.strerror,
 3117             "Parent has to be either a datacenter, or a " "distributed virtual switch",
 3118         )
 3119 
 3120     def test_get_managed_object_name_call(self):
 3121         mock_get_managed_object_name = MagicMock()
 3122         with patch(
 3123             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
 3124         ):
 3125             salt.utils.vmware.get_dvportgroups(self.mock_dc_ref)
 3126         mock_get_managed_object_name.assert_called_once_with(self.mock_dc_ref)
 3127 
 3128     def test_traversal_spec_datacenter_parent(self):
 3129         mock_traversal_spec = MagicMock(return_value="traversal_spec")
 3130         with patch(
 3131             "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 3132             mock_traversal_spec,
 3133         ):
 3134 
 3135             salt.utils.vmware.get_dvportgroups(self.mock_dc_ref)
 3136         mock_traversal_spec.assert_has_calls(
 3137             [
 3138                 call(path="childEntity", skip=False, type=vim.Folder),
 3139                 call(
 3140                     path="networkFolder",
 3141                     skip=True,
 3142                     type=vim.Datacenter,
 3143                     selectSet=["traversal_spec"],
 3144                 ),
 3145             ]
 3146         )
 3147 
 3148     def test_traversal_spec_dvs_parent(self):
 3149         mock_traversal_spec = MagicMock(return_value="traversal_spec")
 3150         with patch(
 3151             "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 3152             mock_traversal_spec,
 3153         ):
 3154 
 3155             salt.utils.vmware.get_dvportgroups(self.mock_dvs_ref)
 3156         mock_traversal_spec.assert_called_once_with(
 3157             path="portgroup", skip=False, type=vim.DistributedVirtualSwitch
 3158         )
 3159 
 3160     def test_get_mors_with_properties(self):
 3161         salt.utils.vmware.get_dvportgroups(self.mock_dvs_ref)
 3162         self.mock_get_mors.assert_called_once_with(
 3163             self.mock_si,
 3164             vim.DistributedVirtualPortgroup,
 3165             container_ref=self.mock_dvs_ref,
 3166             property_list=["name"],
 3167             traversal_spec=self.mock_traversal_spec,
 3168         )
 3169 
 3170     def test_get_no_pgs(self):
 3171         ret = salt.utils.vmware.get_dvportgroups(self.mock_dvs_ref)
 3172         self.assertEqual(ret, [])
 3173 
 3174     def test_get_all_pgs(self):
 3175         ret = salt.utils.vmware.get_dvportgroups(
 3176             self.mock_dvs_ref, get_all_portgroups=True
 3177         )
 3178         self.assertEqual(ret, [i["object"] for i in self.mock_items])
 3179 
 3180     def test_filtered_pgs(self):
 3181         ret = salt.utils.vmware.get_dvss(
 3182             self.mock_dc_ref, dvs_names=["fake_pg1", "fake_pg3", "no_pg"]
 3183         )
 3184         self.assertEqual(
 3185             ret, [self.mock_items[0]["object"], self.mock_items[2]["object"]]
 3186         )
 3187 
 3188 
 3189 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 3190 class GetUplinkDvportgroupTestCase(TestCase):
 3191     def setUp(self):
 3192         self.mock_si = MagicMock()
 3193         self.mock_dvs_ref = MagicMock(spec=vim.DistributedVirtualSwitch)
 3194         self.mock_traversal_spec = MagicMock()
 3195         self.mock_items = [
 3196             {"object": MagicMock(), "tag": [MagicMock(key="fake_tag")]},
 3197             {"object": MagicMock(), "tag": [MagicMock(key="SYSTEM/DVS.UPLINKPG")]},
 3198         ]
 3199         self.mock_get_mors = MagicMock(return_value=self.mock_items)
 3200 
 3201         patches = (
 3202             (
 3203                 "salt.utils.vmware.get_managed_object_name",
 3204                 MagicMock(return_value="fake_dvs"),
 3205             ),
 3206             ("salt.utils.vmware.get_mors_with_properties", self.mock_get_mors),
 3207             (
 3208                 "salt.utils.vmware.get_service_instance_from_managed_object",
 3209                 MagicMock(return_value=self.mock_si),
 3210             ),
 3211             (
 3212                 "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 3213                 MagicMock(return_value=self.mock_traversal_spec),
 3214             ),
 3215         )
 3216         for mod, mock in patches:
 3217             patcher = patch(mod, mock)
 3218             patcher.start()
 3219             self.addCleanup(patcher.stop)
 3220 
 3221     def tearDown(self):
 3222         for attr in (
 3223             "mock_si",
 3224             "mock_dvs_ref",
 3225             "mock_traversal_spec",
 3226             "mock_items",
 3227             "mock_get_mors",
 3228         ):
 3229             delattr(self, attr)
 3230 
 3231     def test_get_managed_object_name_call(self):
 3232         mock_get_managed_object_name = MagicMock()
 3233         with patch(
 3234             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
 3235         ):
 3236             salt.utils.vmware.get_uplink_dvportgroup(self.mock_dvs_ref)
 3237         mock_get_managed_object_name.assert_called_once_with(self.mock_dvs_ref)
 3238 
 3239     def test_traversal_spec(self):
 3240         mock_traversal_spec = MagicMock(return_value="traversal_spec")
 3241         with patch(
 3242             "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 3243             mock_traversal_spec,
 3244         ):
 3245 
 3246             salt.utils.vmware.get_uplink_dvportgroup(self.mock_dvs_ref)
 3247         mock_traversal_spec.assert_called_once_with(
 3248             path="portgroup", skip=False, type=vim.DistributedVirtualSwitch
 3249         )
 3250 
 3251     def test_get_mors_with_properties(self):
 3252         salt.utils.vmware.get_uplink_dvportgroup(self.mock_dvs_ref)
 3253         self.mock_get_mors.assert_called_once_with(
 3254             self.mock_si,
 3255             vim.DistributedVirtualPortgroup,
 3256             container_ref=self.mock_dvs_ref,
 3257             property_list=["tag"],
 3258             traversal_spec=self.mock_traversal_spec,
 3259         )
 3260 
 3261     def test_get_no_uplink_pg(self):
 3262         with patch(
 3263             "salt.utils.vmware.get_mors_with_properties", MagicMock(return_value=[])
 3264         ):
 3265             with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
 3266                 salt.utils.vmware.get_uplink_dvportgroup(self.mock_dvs_ref)
 3267         self.assertEqual(
 3268             excinfo.exception.strerror,
 3269             "Uplink portgroup of DVS 'fake_dvs' wasn't found",
 3270         )
 3271 
 3272     def test_get_uplink_pg(self):
 3273         ret = salt.utils.vmware.get_uplink_dvportgroup(self.mock_dvs_ref)
 3274         self.assertEqual(ret, self.mock_items[1]["object"])
 3275 
 3276 
 3277 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 3278 class CreateDvportgroupTestCase(TestCase):
 3279     def setUp(self):
 3280         self.mock_pg_spec = MagicMock()
 3281         self.mock_task = MagicMock(spec=FakeTaskClass)
 3282         self.mock_dvs_ref = MagicMock(
 3283             CreateDVPortgroup_Task=MagicMock(return_value=self.mock_task)
 3284         )
 3285         self.mock_wait_for_task = MagicMock()
 3286 
 3287         patches = (
 3288             (
 3289                 "salt.utils.vmware.get_managed_object_name",
 3290                 MagicMock(return_value="fake_dvs"),
 3291             ),
 3292             ("salt.utils.vmware.wait_for_task", self.mock_wait_for_task),
 3293         )
 3294         for mod, mock in patches:
 3295             patcher = patch(mod, mock)
 3296             patcher.start()
 3297             self.addCleanup(patcher.stop)
 3298 
 3299     def tearDown(self):
 3300         for attr in ("mock_pg_spec", "mock_dvs_ref", "mock_task", "mock_wait_for_task"):
 3301             delattr(self, attr)
 3302 
 3303     def test_get_managed_object_name_call(self):
 3304         mock_get_managed_object_name = MagicMock()
 3305         with patch(
 3306             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
 3307         ):
 3308             salt.utils.vmware.create_dvportgroup(self.mock_dvs_ref, self.mock_pg_spec)
 3309         mock_get_managed_object_name.assert_called_once_with(self.mock_dvs_ref)
 3310 
 3311     def test_create_dvporgroup_task(self):
 3312         salt.utils.vmware.create_dvportgroup(self.mock_dvs_ref, self.mock_pg_spec)
 3313         self.mock_dvs_ref.CreateDVPortgroup_Task.assert_called_once_with(
 3314             self.mock_pg_spec
 3315         )
 3316 
 3317     def test_create_dvporgroup_task_raises_no_permission(self):
 3318         exc = vim.fault.NoPermission()
 3319         exc.privilegeId = "Fake privilege"
 3320         self.mock_dvs_ref.CreateDVPortgroup_Task = MagicMock(side_effect=exc)
 3321         with self.assertRaises(VMwareApiError) as excinfo:
 3322             salt.utils.vmware.create_dvportgroup(self.mock_dvs_ref, self.mock_pg_spec)
 3323         self.assertEqual(
 3324             excinfo.exception.strerror,
 3325             "Not enough permissions. Required privilege: " "Fake privilege",
 3326         )
 3327 
 3328     def test_create_dvporgroup_task_raises_vim_fault(self):
 3329         exc = vim.fault.VimFault()
 3330         exc.msg = "VimFault msg"
 3331         self.mock_dvs_ref.CreateDVPortgroup_Task = MagicMock(side_effect=exc)
 3332         with self.assertRaises(VMwareApiError) as excinfo:
 3333             salt.utils.vmware.create_dvportgroup(self.mock_dvs_ref, self.mock_pg_spec)
 3334         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 3335 
 3336     def test_create_dvporgroup_task_raises_runtime_fault(self):
 3337         exc = vmodl.RuntimeFault()
 3338         exc.msg = "RuntimeFault msg"
 3339         self.mock_dvs_ref.CreateDVPortgroup_Task = MagicMock(side_effect=exc)
 3340         with self.assertRaises(VMwareRuntimeError) as excinfo:
 3341             salt.utils.vmware.create_dvportgroup(self.mock_dvs_ref, self.mock_pg_spec)
 3342         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 3343 
 3344     def test_wait_for_tasks(self):
 3345         salt.utils.vmware.create_dvportgroup(self.mock_dvs_ref, self.mock_pg_spec)
 3346         self.mock_wait_for_task.assert_called_once_with(
 3347             self.mock_task,
 3348             "fake_dvs",
 3349             "<class '{}unit.utils.test_vmware.FakeTaskClass'>".format(
 3350                 "tests." if RUNTIME_VARS.PYTEST_SESSION else ""
 3351             ),
 3352         )
 3353 
 3354 
 3355 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 3356 class UpdateDvportgroupTestCase(TestCase):
 3357     def setUp(self):
 3358         self.mock_pg_spec = MagicMock()
 3359         self.mock_task = MagicMock(spec=FakeTaskClass)
 3360         self.mock_pg_ref = MagicMock(
 3361             ReconfigureDVPortgroup_Task=MagicMock(return_value=self.mock_task)
 3362         )
 3363         self.mock_wait_for_task = MagicMock()
 3364 
 3365         patches = (
 3366             (
 3367                 "salt.utils.vmware.get_managed_object_name",
 3368                 MagicMock(return_value="fake_pg"),
 3369             ),
 3370             ("salt.utils.vmware.wait_for_task", self.mock_wait_for_task),
 3371         )
 3372         for mod, mock in patches:
 3373             patcher = patch(mod, mock)
 3374             patcher.start()
 3375             self.addCleanup(patcher.stop)
 3376 
 3377     def tearDown(self):
 3378         for attr in ("mock_pg_spec", "mock_pg_ref", "mock_task", "mock_wait_for_task"):
 3379             delattr(self, attr)
 3380 
 3381     def test_get_managed_object_name_call(self):
 3382         mock_get_managed_object_name = MagicMock()
 3383         with patch(
 3384             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
 3385         ):
 3386             salt.utils.vmware.update_dvportgroup(self.mock_pg_ref, self.mock_pg_spec)
 3387         mock_get_managed_object_name.assert_called_once_with(self.mock_pg_ref)
 3388 
 3389     def test_reconfigure_dvporgroup_task(self):
 3390         salt.utils.vmware.update_dvportgroup(self.mock_pg_ref, self.mock_pg_spec)
 3391         self.mock_pg_ref.ReconfigureDVPortgroup_Task.assert_called_once_with(
 3392             self.mock_pg_spec
 3393         )
 3394 
 3395     def test_reconfigure_dvporgroup_task_raises_no_permission(self):
 3396         exc = vim.fault.NoPermission()
 3397         exc.privilegeId = "Fake privilege"
 3398         self.mock_pg_ref.ReconfigureDVPortgroup_Task = MagicMock(side_effect=exc)
 3399         with self.assertRaises(VMwareApiError) as excinfo:
 3400             salt.utils.vmware.update_dvportgroup(self.mock_pg_ref, self.mock_pg_spec)
 3401         self.assertEqual(
 3402             excinfo.exception.strerror,
 3403             "Not enough permissions. Required privilege: " "Fake privilege",
 3404         )
 3405 
 3406     def test_reconfigure_dvporgroup_task_raises_vim_fault(self):
 3407         exc = vim.fault.VimFault()
 3408         exc.msg = "VimFault msg"
 3409         self.mock_pg_ref.ReconfigureDVPortgroup_Task = MagicMock(side_effect=exc)
 3410         with self.assertRaises(VMwareApiError) as excinfo:
 3411             salt.utils.vmware.update_dvportgroup(self.mock_pg_ref, self.mock_pg_spec)
 3412         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 3413 
 3414     def test_reconfigure_dvporgroup_task_raises_runtime_fault(self):
 3415         exc = vmodl.RuntimeFault()
 3416         exc.msg = "RuntimeFault msg"
 3417         self.mock_pg_ref.ReconfigureDVPortgroup_Task = MagicMock(side_effect=exc)
 3418         with self.assertRaises(VMwareRuntimeError) as excinfo:
 3419             salt.utils.vmware.update_dvportgroup(self.mock_pg_ref, self.mock_pg_spec)
 3420         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 3421 
 3422     def test_wait_for_tasks(self):
 3423         salt.utils.vmware.update_dvportgroup(self.mock_pg_ref, self.mock_pg_spec)
 3424         self.mock_wait_for_task.assert_called_once_with(
 3425             self.mock_task,
 3426             "fake_pg",
 3427             "<class '{}unit.utils.test_vmware.FakeTaskClass'>".format(
 3428                 "tests." if RUNTIME_VARS.PYTEST_SESSION else ""
 3429             ),
 3430         )
 3431 
 3432 
 3433 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 3434 class RemoveDvportgroupTestCase(TestCase):
 3435     def setUp(self):
 3436         self.mock_task = MagicMock(spec=FakeTaskClass)
 3437         self.mock_pg_ref = MagicMock(
 3438             Destroy_Task=MagicMock(return_value=self.mock_task)
 3439         )
 3440         self.mock_wait_for_task = MagicMock()
 3441 
 3442         patches = (
 3443             (
 3444                 "salt.utils.vmware.get_managed_object_name",
 3445                 MagicMock(return_value="fake_pg"),
 3446             ),
 3447             ("salt.utils.vmware.wait_for_task", self.mock_wait_for_task),
 3448         )
 3449         for mod, mock in patches:
 3450             patcher = patch(mod, mock)
 3451             patcher.start()
 3452             self.addCleanup(patcher.stop)
 3453 
 3454     def tearDown(self):
 3455         for attr in ("mock_pg_ref", "mock_task", "mock_wait_for_task"):
 3456             delattr(self, attr)
 3457 
 3458     def test_get_managed_object_name_call(self):
 3459         mock_get_managed_object_name = MagicMock()
 3460         with patch(
 3461             "salt.utils.vmware.get_managed_object_name", mock_get_managed_object_name
 3462         ):
 3463             salt.utils.vmware.remove_dvportgroup(self.mock_pg_ref)
 3464         mock_get_managed_object_name.assert_called_once_with(self.mock_pg_ref)
 3465 
 3466     def test_destroy_task(self):
 3467         salt.utils.vmware.remove_dvportgroup(self.mock_pg_ref)
 3468         self.mock_pg_ref.Destroy_Task.assert_called_once_with()
 3469 
 3470     def test_destroy_task_raises_no_permission(self):
 3471         exc = vim.fault.NoPermission()
 3472         exc.privilegeId = "Fake privilege"
 3473         self.mock_pg_ref.Destroy_Task = MagicMock(side_effect=exc)
 3474         with self.assertRaises(VMwareApiError) as excinfo:
 3475             salt.utils.vmware.remove_dvportgroup(self.mock_pg_ref)
 3476         self.assertEqual(
 3477             excinfo.exception.strerror,
 3478             "Not enough permissions. Required privilege: " "Fake privilege",
 3479         )
 3480 
 3481     def test_destroy_treconfigure_dvporgroup_task_raises_vim_fault(self):
 3482         exc = vim.fault.VimFault()
 3483         exc.msg = "VimFault msg"
 3484         self.mock_pg_ref.Destroy_Task = MagicMock(side_effect=exc)
 3485         with self.assertRaises(VMwareApiError) as excinfo:
 3486             salt.utils.vmware.remove_dvportgroup(self.mock_pg_ref)
 3487         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 3488 
 3489     def test_destroy_treconfigure_dvporgroup_task_raises_runtime_fault(self):
 3490         exc = vmodl.RuntimeFault()
 3491         exc.msg = "RuntimeFault msg"
 3492         self.mock_pg_ref.Destroy_Task = MagicMock(side_effect=exc)
 3493         with self.assertRaises(VMwareRuntimeError) as excinfo:
 3494             salt.utils.vmware.remove_dvportgroup(self.mock_pg_ref)
 3495         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 3496 
 3497     def test_wait_for_tasks(self):
 3498         salt.utils.vmware.remove_dvportgroup(self.mock_pg_ref)
 3499         self.mock_wait_for_task.assert_called_once_with(
 3500             self.mock_task,
 3501             "fake_pg",
 3502             "<class '{}unit.utils.test_vmware.FakeTaskClass'>".format(
 3503                 "tests." if RUNTIME_VARS.PYTEST_SESSION else ""
 3504             ),
 3505         )
 3506 
 3507 
 3508 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 3509 class GetHostsTestCase(TestCase):
 3510     """
 3511     Tests for salt.utils.vmware.get_hosts
 3512     """
 3513 
 3514     def setUp(self):
 3515         patches = (
 3516             ("salt.utils.vmware.get_mors_with_properties", MagicMock(return_value=[])),
 3517             ("salt.utils.vmware.get_datacenter", MagicMock(return_value=None)),
 3518             ("salt.utils.vmware.get_cluster", MagicMock(return_value=None)),
 3519         )
 3520         for mod, mock in patches:
 3521             patcher = patch(mod, mock)
 3522             patcher.start()
 3523             self.addCleanup(patcher.stop)
 3524         self.mock_root_folder = MagicMock()
 3525         self.mock_si = MagicMock()
 3526         self.mock_host1, self.mock_host2, self.mock_host3 = (
 3527             MagicMock(),
 3528             MagicMock(),
 3529             MagicMock(),
 3530         )
 3531         self.mock_prop_host1 = {"name": "fake_hostname1", "object": self.mock_host1}
 3532         self.mock_prop_host2 = {"name": "fake_hostname2", "object": self.mock_host2}
 3533         self.mock_prop_host3 = {"name": "fake_hostname3", "object": self.mock_host3}
 3534         self.mock_prop_hosts = [
 3535             self.mock_prop_host1,
 3536             self.mock_prop_host2,
 3537             self.mock_prop_host3,
 3538         ]
 3539 
 3540     def test_cluster_no_datacenter(self):
 3541         with self.assertRaises(ArgumentValueError) as excinfo:
 3542             salt.utils.vmware.get_hosts(self.mock_si, cluster_name="fake_cluster")
 3543         self.assertEqual(
 3544             excinfo.exception.strerror,
 3545             "Must specify the datacenter when specifying the " "cluster",
 3546         )
 3547 
 3548     def test_get_si_no_datacenter_no_cluster(self):
 3549         mock_get_mors = MagicMock()
 3550         mock_get_root_folder = MagicMock(return_value=self.mock_root_folder)
 3551         with patch("salt.utils.vmware.get_root_folder", mock_get_root_folder):
 3552             with patch("salt.utils.vmware.get_mors_with_properties", mock_get_mors):
 3553                 salt.utils.vmware.get_hosts(self.mock_si)
 3554         mock_get_root_folder.assert_called_once_with(self.mock_si)
 3555         mock_get_mors.assert_called_once_with(
 3556             self.mock_si,
 3557             vim.HostSystem,
 3558             container_ref=self.mock_root_folder,
 3559             property_list=["name"],
 3560         )
 3561 
 3562     def test_get_si_datacenter_name_no_cluster_name(self):
 3563         mock_dc = MagicMock()
 3564         mock_get_dc = MagicMock(return_value=mock_dc)
 3565         mock_get_mors = MagicMock()
 3566         with patch("salt.utils.vmware.get_datacenter", mock_get_dc):
 3567             with patch("salt.utils.vmware.get_mors_with_properties", mock_get_mors):
 3568                 salt.utils.vmware.get_hosts(
 3569                     self.mock_si, datacenter_name="fake_datacenter"
 3570                 )
 3571         mock_get_dc.assert_called_once_with(self.mock_si, "fake_datacenter")
 3572         mock_get_mors.assert_called_once_with(
 3573             self.mock_si, vim.HostSystem, container_ref=mock_dc, property_list=["name"]
 3574         )
 3575 
 3576     def test_get_si_datacenter_name_and_cluster_name(self):
 3577         mock_dc = MagicMock()
 3578         mock_get_dc = MagicMock(return_value=mock_dc)
 3579         mock_get_cl = MagicMock()
 3580         mock_get_mors = MagicMock()
 3581         with patch("salt.utils.vmware.get_datacenter", mock_get_dc):
 3582             with patch("salt.utils.vmware.get_cluster", mock_get_cl):
 3583                 with patch("salt.utils.vmware.get_mors_with_properties", mock_get_mors):
 3584                     salt.utils.vmware.get_hosts(
 3585                         self.mock_si,
 3586                         datacenter_name="fake_datacenter",
 3587                         cluster_name="fake_cluster",
 3588                     )
 3589         mock_get_dc.assert_called_once_with(self.mock_si, "fake_datacenter")
 3590         mock_get_mors.assert_called_once_with(
 3591             self.mock_si,
 3592             vim.HostSystem,
 3593             container_ref=mock_dc,
 3594             property_list=["name", "parent"],
 3595         )
 3596 
 3597     def test_host_get_all_hosts(self):
 3598         with patch(
 3599             "salt.utils.vmware.get_root_folder",
 3600             MagicMock(return_value=self.mock_root_folder),
 3601         ):
 3602             with patch(
 3603                 "salt.utils.vmware.get_mors_with_properties",
 3604                 MagicMock(return_value=self.mock_prop_hosts),
 3605             ):
 3606                 res = salt.utils.vmware.get_hosts(self.mock_si, get_all_hosts=True)
 3607         self.assertEqual(res, [self.mock_host1, self.mock_host2, self.mock_host3])
 3608 
 3609     def test_filter_hostname(self):
 3610         with patch(
 3611             "salt.utils.vmware.get_mors_with_properties",
 3612             MagicMock(return_value=self.mock_prop_hosts),
 3613         ):
 3614             res = salt.utils.vmware.get_hosts(
 3615                 self.mock_si, host_names=["fake_hostname1", "fake_hostname2"]
 3616             )
 3617         self.assertEqual(res, [self.mock_host1, self.mock_host2])
 3618 
 3619     def test_get_all_host_flag_not_set_and_no_host_names(self):
 3620         with patch(
 3621             "salt.utils.vmware.get_mors_with_properties",
 3622             MagicMock(return_value=self.mock_prop_hosts),
 3623         ):
 3624             res = salt.utils.vmware.get_hosts(self.mock_si)
 3625         self.assertEqual(res, [])
 3626 
 3627     def test_filter_cluster(self):
 3628         self.mock_prop_host1["parent"] = vim.ClusterComputeResource("cluster")
 3629         self.mock_prop_host2["parent"] = vim.ClusterComputeResource("cluster")
 3630         self.mock_prop_host3["parent"] = vim.Datacenter("dc")
 3631         mock_get_cl_name = MagicMock(
 3632             side_effect=["fake_bad_cluster", "fake_good_cluster"]
 3633         )
 3634         with patch(
 3635             "salt.utils.vmware.get_mors_with_properties",
 3636             MagicMock(return_value=self.mock_prop_hosts),
 3637         ):
 3638             with patch("salt.utils.vmware.get_managed_object_name", mock_get_cl_name):
 3639                 res = salt.utils.vmware.get_hosts(
 3640                     self.mock_si,
 3641                     datacenter_name="fake_datacenter",
 3642                     cluster_name="fake_good_cluster",
 3643                     get_all_hosts=True,
 3644                 )
 3645         self.assertEqual(mock_get_cl_name.call_count, 2)
 3646         self.assertEqual(res, [self.mock_host2])
 3647 
 3648     def test_no_hosts(self):
 3649         with patch(
 3650             "salt.utils.vmware.get_mors_with_properties", MagicMock(return_value=[])
 3651         ):
 3652             res = salt.utils.vmware.get_hosts(self.mock_si, get_all_hosts=True)
 3653         self.assertEqual(res, [])
 3654 
 3655     def test_one_host_returned(self):
 3656         with patch(
 3657             "salt.utils.vmware.get_mors_with_properties",
 3658             MagicMock(return_value=[self.mock_prop_host1]),
 3659         ):
 3660             res = salt.utils.vmware.get_hosts(self.mock_si, get_all_hosts=True)
 3661         self.assertEqual(res, [self.mock_host1])
 3662 
 3663 
 3664 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 3665 class GetLicenseManagerTestCase(TestCase):
 3666     """
 3667     Tests for salt.utils.vmware.get_license_manager
 3668     """
 3669 
 3670     def setUp(self):
 3671         self.mock_si = MagicMock()
 3672         self.mock_lic_mgr = MagicMock()
 3673         type(self.mock_si.content).licenseManager = PropertyMock(
 3674             return_value=self.mock_lic_mgr
 3675         )
 3676 
 3677     def tearDown(self):
 3678         for attr in ("mock_si", "mock_lic_mgr"):
 3679             delattr(self, attr)
 3680 
 3681     def test_raise_no_permission(self):
 3682         exc = vim.fault.NoPermission()
 3683         exc.privilegeId = "Fake privilege"
 3684         type(self.mock_si.content).licenseManager = PropertyMock(side_effect=exc)
 3685         with self.assertRaises(VMwareApiError) as excinfo:
 3686             salt.utils.vmware.get_license_manager(self.mock_si)
 3687         self.assertEqual(
 3688             excinfo.exception.strerror,
 3689             "Not enough permissions. Required privilege: " "Fake privilege",
 3690         )
 3691 
 3692     def test_raise_vim_fault(self):
 3693         exc = vim.fault.VimFault()
 3694         exc.msg = "VimFault msg"
 3695         type(self.mock_si.content).licenseManager = PropertyMock(side_effect=exc)
 3696         with self.assertRaises(VMwareApiError) as excinfo:
 3697             salt.utils.vmware.get_license_manager(self.mock_si)
 3698         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 3699 
 3700     def test_raise_runtime_fault(self):
 3701         exc = vmodl.RuntimeFault()
 3702         exc.msg = "RuntimeFault msg"
 3703         type(self.mock_si.content).licenseManager = PropertyMock(side_effect=exc)
 3704         with self.assertRaises(VMwareRuntimeError) as excinfo:
 3705             salt.utils.vmware.get_license_manager(self.mock_si)
 3706         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 3707 
 3708     def test_valid_assignment_manager(self):
 3709         ret = salt.utils.vmware.get_license_manager(self.mock_si)
 3710         self.assertEqual(ret, self.mock_lic_mgr)
 3711 
 3712 
 3713 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 3714 class GetLicenseAssignmentManagerTestCase(TestCase):
 3715     """
 3716     Tests for salt.utils.vmware.get_license_assignment_manager
 3717     """
 3718 
 3719     def setUp(self):
 3720         self.mock_si = MagicMock()
 3721         self.mock_lic_assign_mgr = MagicMock()
 3722         type(
 3723             self.mock_si.content.licenseManager
 3724         ).licenseAssignmentManager = PropertyMock(return_value=self.mock_lic_assign_mgr)
 3725 
 3726     def tearDown(self):
 3727         for attr in ("mock_si", "mock_lic_assign_mgr"):
 3728             delattr(self, attr)
 3729 
 3730     def test_raise_no_permission(self):
 3731         exc = vim.fault.NoPermission()
 3732         exc.privilegeId = "Fake privilege"
 3733         type(
 3734             self.mock_si.content.licenseManager
 3735         ).licenseAssignmentManager = PropertyMock(side_effect=exc)
 3736         with self.assertRaises(VMwareApiError) as excinfo:
 3737             salt.utils.vmware.get_license_assignment_manager(self.mock_si)
 3738         self.assertEqual(
 3739             excinfo.exception.strerror,
 3740             "Not enough permissions. Required privilege: " "Fake privilege",
 3741         )
 3742 
 3743     def test_raise_vim_fault(self):
 3744         exc = vim.fault.VimFault()
 3745         exc.msg = "VimFault msg"
 3746         type(
 3747             self.mock_si.content.licenseManager
 3748         ).licenseAssignmentManager = PropertyMock(side_effect=exc)
 3749         with self.assertRaises(VMwareApiError) as excinfo:
 3750             salt.utils.vmware.get_license_assignment_manager(self.mock_si)
 3751         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 3752 
 3753     def test_raise_runtime_fault(self):
 3754         exc = vmodl.RuntimeFault()
 3755         exc.msg = "RuntimeFault msg"
 3756         type(
 3757             self.mock_si.content.licenseManager
 3758         ).licenseAssignmentManager = PropertyMock(side_effect=exc)
 3759         with self.assertRaises(VMwareRuntimeError) as excinfo:
 3760             salt.utils.vmware.get_license_assignment_manager(self.mock_si)
 3761         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 3762 
 3763     def test_empty_license_assignment_manager(self):
 3764         type(
 3765             self.mock_si.content.licenseManager
 3766         ).licenseAssignmentManager = PropertyMock(return_value=None)
 3767         with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
 3768             salt.utils.vmware.get_license_assignment_manager(self.mock_si)
 3769         self.assertEqual(
 3770             excinfo.exception.strerror, "License assignment manager was not retrieved"
 3771         )
 3772 
 3773     def test_valid_assignment_manager(self):
 3774         ret = salt.utils.vmware.get_license_assignment_manager(self.mock_si)
 3775         self.assertEqual(ret, self.mock_lic_assign_mgr)
 3776 
 3777 
 3778 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 3779 class GetLicensesTestCase(TestCase):
 3780     """
 3781     Tests for salt.utils.vmware.get_licenses
 3782     """
 3783 
 3784     def setUp(self):
 3785         self.mock_si = MagicMock()
 3786         self.mock_licenses = [MagicMock(), MagicMock()]
 3787         self.mock_lic_mgr = MagicMock()
 3788         type(self.mock_lic_mgr).licenses = PropertyMock(return_value=self.mock_licenses)
 3789         patches = (
 3790             (
 3791                 "salt.utils.vmware.get_license_manager",
 3792                 MagicMock(return_value=self.mock_lic_mgr),
 3793             ),
 3794         )
 3795         for mod, mock in patches:
 3796             patcher = patch(mod, mock)
 3797             patcher.start()
 3798             self.addCleanup(patcher.stop)
 3799 
 3800     def tearDown(self):
 3801         for attr in ("mock_si", "mock_lic_mgr", "mock_licenses"):
 3802             delattr(self, attr)
 3803 
 3804     def test_no_license_manager_passed_in(self):
 3805         mock_get_license_manager = MagicMock()
 3806         with patch("salt.utils.vmware.get_license_manager", mock_get_license_manager):
 3807             salt.utils.vmware.get_licenses(self.mock_si)
 3808         mock_get_license_manager.assert_called_once_with(self.mock_si)
 3809 
 3810     def test_license_manager_passed_in(self):
 3811         mock_licenses = PropertyMock()
 3812         mock_lic_mgr = MagicMock()
 3813         type(mock_lic_mgr).licenses = mock_licenses
 3814         mock_get_license_manager = MagicMock()
 3815         with patch("salt.utils.vmware.get_license_manager", mock_get_license_manager):
 3816             salt.utils.vmware.get_licenses(self.mock_si, license_manager=mock_lic_mgr)
 3817         self.assertEqual(mock_get_license_manager.call_count, 0)
 3818         self.assertEqual(mock_licenses.call_count, 1)
 3819 
 3820     def test_raise_no_permission(self):
 3821         exc = vim.fault.NoPermission()
 3822         exc.privilegeId = "Fake privilege"
 3823         type(self.mock_lic_mgr).licenses = PropertyMock(side_effect=exc)
 3824         with self.assertRaises(VMwareApiError) as excinfo:
 3825             salt.utils.vmware.get_licenses(self.mock_si)
 3826         self.assertEqual(
 3827             excinfo.exception.strerror,
 3828             "Not enough permissions. Required privilege: " "Fake privilege",
 3829         )
 3830 
 3831     def test_raise_vim_fault(self):
 3832         exc = vim.fault.VimFault()
 3833         exc.msg = "VimFault msg"
 3834         type(self.mock_lic_mgr).licenses = PropertyMock(side_effect=exc)
 3835         with self.assertRaises(VMwareApiError) as excinfo:
 3836             salt.utils.vmware.get_licenses(self.mock_si)
 3837         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 3838 
 3839     def test_raise_runtime_fault(self):
 3840         exc = vmodl.RuntimeFault()
 3841         exc.msg = "RuntimeFault msg"
 3842         type(self.mock_lic_mgr).licenses = PropertyMock(side_effect=exc)
 3843         with self.assertRaises(VMwareRuntimeError) as excinfo:
 3844             salt.utils.vmware.get_licenses(self.mock_si)
 3845         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 3846 
 3847     def test_valid_licenses(self):
 3848         ret = salt.utils.vmware.get_licenses(self.mock_si)
 3849         self.assertEqual(ret, self.mock_licenses)
 3850 
 3851 
 3852 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 3853 class AddLicenseTestCase(TestCase):
 3854     """
 3855     Tests for salt.utils.vmware.add_license
 3856     """
 3857 
 3858     def setUp(self):
 3859         self.mock_si = MagicMock()
 3860         self.mock_license = MagicMock()
 3861         self.mock_add_license = MagicMock(return_value=self.mock_license)
 3862         self.mock_lic_mgr = MagicMock(AddLicense=self.mock_add_license)
 3863         self.mock_label = MagicMock()
 3864         patches = (
 3865             (
 3866                 "salt.utils.vmware.get_license_manager",
 3867                 MagicMock(return_value=self.mock_lic_mgr),
 3868             ),
 3869             ("salt.utils.vmware.vim.KeyValue", MagicMock(return_value=self.mock_label)),
 3870         )
 3871         for mod, mock in patches:
 3872             patcher = patch(mod, mock)
 3873             patcher.start()
 3874             self.addCleanup(patcher.stop)
 3875 
 3876     def tearDown(self):
 3877         for attr in (
 3878             "mock_si",
 3879             "mock_lic_mgr",
 3880             "mock_license",
 3881             "mock_add_license",
 3882             "mock_label",
 3883         ):
 3884             delattr(self, attr)
 3885 
 3886     def test_no_license_manager_passed_in(self):
 3887         mock_get_license_manager = MagicMock()
 3888         with patch("salt.utils.vmware.get_license_manager", mock_get_license_manager):
 3889             salt.utils.vmware.add_license(
 3890                 self.mock_si, "fake_license_key", "fake_license_description"
 3891             )
 3892         mock_get_license_manager.assert_called_once_with(self.mock_si)
 3893 
 3894     def test_license_manager_passed_in(self):
 3895         mock_get_license_manager = MagicMock()
 3896         with patch("salt.utils.vmware.get_license_manager", mock_get_license_manager):
 3897             salt.utils.vmware.add_license(
 3898                 self.mock_si,
 3899                 "fake_license_key",
 3900                 "fake_license_description",
 3901                 license_manager=self.mock_lic_mgr,
 3902             )
 3903         self.assertEqual(mock_get_license_manager.call_count, 0)
 3904         self.assertEqual(self.mock_add_license.call_count, 1)
 3905 
 3906     def test_label_settings(self):
 3907         salt.utils.vmware.add_license(
 3908             self.mock_si, "fake_license_key", "fake_license_description"
 3909         )
 3910         self.assertEqual(self.mock_label.key, "VpxClientLicenseLabel")
 3911         self.assertEqual(self.mock_label.value, "fake_license_description")
 3912 
 3913     def test_add_license_arguments(self):
 3914         salt.utils.vmware.add_license(
 3915             self.mock_si, "fake_license_key", "fake_license_description"
 3916         )
 3917         self.mock_add_license.assert_called_once_with(
 3918             "fake_license_key", [self.mock_label]
 3919         )
 3920 
 3921     def test_add_license_raises_no_permission(self):
 3922         exc = vim.fault.NoPermission()
 3923         exc.privilegeId = "Fake privilege"
 3924         self.mock_lic_mgr.AddLicense = MagicMock(side_effect=exc)
 3925         with self.assertRaises(VMwareApiError) as excinfo:
 3926             salt.utils.vmware.add_license(
 3927                 self.mock_si, "fake_license_key", "fake_license_description"
 3928             )
 3929         self.assertEqual(
 3930             excinfo.exception.strerror,
 3931             "Not enough permissions. Required privilege: " "Fake privilege",
 3932         )
 3933 
 3934     def test_add_license_raises_vim_fault(self):
 3935         exc = vim.fault.VimFault()
 3936         exc.msg = "VimFault msg"
 3937         self.mock_lic_mgr.AddLicense = MagicMock(side_effect=exc)
 3938         with self.assertRaises(VMwareApiError) as excinfo:
 3939             salt.utils.vmware.add_license(
 3940                 self.mock_si, "fake_license_key", "fake_license_description"
 3941             )
 3942         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 3943 
 3944     def test_add_license_raises_runtime_fault(self):
 3945         exc = vmodl.RuntimeFault()
 3946         exc.msg = "RuntimeFault msg"
 3947         self.mock_lic_mgr.AddLicense = MagicMock(side_effect=exc)
 3948         with self.assertRaises(VMwareRuntimeError) as excinfo:
 3949             salt.utils.vmware.add_license(
 3950                 self.mock_si, "fake_license_key", "fake_license_description"
 3951             )
 3952         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 3953 
 3954     def test_valid_license_added(self):
 3955         ret = salt.utils.vmware.add_license(
 3956             self.mock_si, "fake_license_key", "fake_license_description"
 3957         )
 3958         self.assertEqual(ret, self.mock_license)
 3959 
 3960 
 3961 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 3962 class GetAssignedLicensesTestCase(TestCase):
 3963     """
 3964     Tests for salt.utils.vmware.get_assigned_licenses
 3965     """
 3966 
 3967     def setUp(self):
 3968         self.mock_ent_id = MagicMock()
 3969         self.mock_si = MagicMock()
 3970         type(self.mock_si.content.about).instanceUuid = PropertyMock(
 3971             return_value=self.mock_ent_id
 3972         )
 3973         self.mock_moid = MagicMock()
 3974         self.prop_mock_moid = PropertyMock(return_value=self.mock_moid)
 3975         self.mock_entity_ref = MagicMock()
 3976         type(self.mock_entity_ref)._moId = self.prop_mock_moid
 3977         self.mock_assignments = [
 3978             MagicMock(entityDisplayName="fake_ent1"),
 3979             MagicMock(entityDisplayName="fake_ent2"),
 3980         ]
 3981         self.mock_query_assigned_licenses = MagicMock(
 3982             return_value=[
 3983                 MagicMock(assignedLicense=self.mock_assignments[0]),
 3984                 MagicMock(assignedLicense=self.mock_assignments[1]),
 3985             ]
 3986         )
 3987         self.mock_lic_assign_mgr = MagicMock(
 3988             QueryAssignedLicenses=self.mock_query_assigned_licenses
 3989         )
 3990         patches = (
 3991             (
 3992                 "salt.utils.vmware.get_license_assignment_manager",
 3993                 MagicMock(return_value=self.mock_lic_assign_mgr),
 3994             ),
 3995         )
 3996         for mod, mock in patches:
 3997             patcher = patch(mod, mock)
 3998             patcher.start()
 3999             self.addCleanup(patcher.stop)
 4000 
 4001     def tearDown(self):
 4002         for attr in (
 4003             "mock_ent_id",
 4004             "mock_si",
 4005             "mock_moid",
 4006             "prop_mock_moid",
 4007             "mock_entity_ref",
 4008             "mock_assignments",
 4009             "mock_query_assigned_licenses",
 4010             "mock_lic_assign_mgr",
 4011         ):
 4012             delattr(self, attr)
 4013 
 4014     def test_no_license_assignment_manager_passed_in(self):
 4015         mock_get_license_assign_manager = MagicMock()
 4016         with patch(
 4017             "salt.utils.vmware.get_license_assignment_manager",
 4018             mock_get_license_assign_manager,
 4019         ):
 4020             salt.utils.vmware.get_assigned_licenses(
 4021                 self.mock_si, self.mock_entity_ref, "fake_entity_name"
 4022             )
 4023         mock_get_license_assign_manager.assert_called_once_with(self.mock_si)
 4024 
 4025     def test_license_assignment_manager_passed_in(self):
 4026         mock_get_license_assign_manager = MagicMock()
 4027         with patch(
 4028             "salt.utils.vmware.get_license_assignment_manager",
 4029             mock_get_license_assign_manager,
 4030         ):
 4031             salt.utils.vmware.get_assigned_licenses(
 4032                 self.mock_si,
 4033                 self.mock_entity_ref,
 4034                 "fake_entity_name",
 4035                 license_assignment_manager=self.mock_lic_assign_mgr,
 4036             )
 4037         self.assertEqual(mock_get_license_assign_manager.call_count, 0)
 4038 
 4039     def test_entity_name(self):
 4040         mock_trace = MagicMock()
 4041         with patch("salt._logging.impl.SaltLoggingClass.trace", mock_trace):
 4042             salt.utils.vmware.get_assigned_licenses(
 4043                 self.mock_si, self.mock_entity_ref, "fake_entity_name"
 4044             )
 4045         mock_trace.assert_called_once_with(
 4046             "Retrieving licenses assigned to '%s'", "fake_entity_name"
 4047         )
 4048 
 4049     def test_instance_uuid(self):
 4050         mock_instance_uuid_prop = PropertyMock()
 4051         type(self.mock_si.content.about).instanceUuid = mock_instance_uuid_prop
 4052         self.mock_lic_assign_mgr.QueryAssignedLicenses = MagicMock(
 4053             return_value=[MagicMock(entityDisplayName="fake_vcenter")]
 4054         )
 4055         salt.utils.vmware.get_assigned_licenses(
 4056             self.mock_si, entity_name="fake_vcenter"
 4057         )
 4058         self.assertEqual(mock_instance_uuid_prop.call_count, 1)
 4059 
 4060     def test_instance_uuid_raises_no_permission(self):
 4061         exc = vim.fault.NoPermission()
 4062         exc.privilegeId = "Fake privilege"
 4063         type(self.mock_si.content.about).instanceUuid = PropertyMock(side_effect=exc)
 4064         with self.assertRaises(VMwareApiError) as excinfo:
 4065             salt.utils.vmware.get_assigned_licenses(
 4066                 self.mock_si, entity_name="fake_vcenter"
 4067             )
 4068         self.assertEqual(
 4069             excinfo.exception.strerror,
 4070             "Not enough permissions. Required privilege: " "Fake privilege",
 4071         )
 4072 
 4073     def test_instance_uuid_raises_vim_fault(self):
 4074         exc = vim.fault.VimFault()
 4075         exc.msg = "VimFault msg"
 4076         type(self.mock_si.content.about).instanceUuid = PropertyMock(side_effect=exc)
 4077         with self.assertRaises(VMwareApiError) as excinfo:
 4078             salt.utils.vmware.get_assigned_licenses(
 4079                 self.mock_si, entity_name="fake_vcenter"
 4080             )
 4081         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 4082 
 4083     def test_instance_uuid_raises_runtime_fault(self):
 4084         exc = vmodl.RuntimeFault()
 4085         exc.msg = "RuntimeFault msg"
 4086         type(self.mock_si.content.about).instanceUuid = PropertyMock(side_effect=exc)
 4087         with self.assertRaises(VMwareRuntimeError) as excinfo:
 4088             salt.utils.vmware.get_assigned_licenses(
 4089                 self.mock_si, entity_name="fake_vcenter"
 4090             )
 4091         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 4092 
 4093     def test_vcenter_entity_too_many_assignements(self):
 4094         self.mock_lic_assign_mgr.QueryAssignedLicenses = MagicMock(
 4095             return_value=[MagicMock(), MagicMock()]
 4096         )
 4097         with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
 4098             salt.utils.vmware.get_assigned_licenses(
 4099                 self.mock_si, entity_name="fake_vcenter"
 4100             )
 4101         self.assertEqual(
 4102             excinfo.exception.strerror,
 4103             "Unexpected return. Expect only a single assignment",
 4104         )
 4105 
 4106     def test_wrong_vcenter_name(self):
 4107         self.mock_lic_assign_mgr.QueryAssignedLicenses = MagicMock(
 4108             return_value=[MagicMock(entityDisplayName="bad_vcenter")]
 4109         )
 4110         with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
 4111             salt.utils.vmware.get_assigned_licenses(
 4112                 self.mock_si, entity_name="fake_vcenter"
 4113             )
 4114         self.assertEqual(
 4115             excinfo.exception.strerror,
 4116             "Got license assignment info for a different vcenter",
 4117         )
 4118 
 4119     def test_query_assigned_licenses_vcenter(self):
 4120         with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
 4121             salt.utils.vmware.get_assigned_licenses(
 4122                 self.mock_si, entity_name="fake_vcenter"
 4123             )
 4124         self.mock_query_assigned_licenses.assert_called_once_with(self.mock_ent_id)
 4125 
 4126     def test_query_assigned_licenses_with_entity(self):
 4127         salt.utils.vmware.get_assigned_licenses(
 4128             self.mock_si, self.mock_entity_ref, "fake_entity_name"
 4129         )
 4130         self.mock_query_assigned_licenses.assert_called_once_with(self.mock_moid)
 4131 
 4132     def test_query_assigned_licenses_raises_no_permission(self):
 4133         exc = vim.fault.NoPermission()
 4134         exc.privilegeId = "Fake privilege"
 4135         self.mock_lic_assign_mgr.QueryAssignedLicenses = MagicMock(side_effect=exc)
 4136         with self.assertRaises(VMwareApiError) as excinfo:
 4137             salt.utils.vmware.get_assigned_licenses(
 4138                 self.mock_si, self.mock_entity_ref, "fake_entity_name"
 4139             )
 4140         self.assertEqual(
 4141             excinfo.exception.strerror,
 4142             "Not enough permissions. Required privilege: " "Fake privilege",
 4143         )
 4144 
 4145     def test_query_assigned_licenses_raises_vim_fault(self):
 4146         exc = vim.fault.VimFault()
 4147         exc.msg = "VimFault msg"
 4148         self.mock_lic_assign_mgr.QueryAssignedLicenses = MagicMock(side_effect=exc)
 4149         with self.assertRaises(VMwareApiError) as excinfo:
 4150             salt.utils.vmware.get_assigned_licenses(
 4151                 self.mock_si, self.mock_entity_ref, "fake_entity_name"
 4152             )
 4153         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 4154 
 4155     def test_query_assigned_licenses_raises_runtime_fault(self):
 4156         exc = vmodl.RuntimeFault()
 4157         exc.msg = "RuntimeFault msg"
 4158         self.mock_lic_assign_mgr.QueryAssignedLicenses = MagicMock(side_effect=exc)
 4159         with self.assertRaises(VMwareRuntimeError) as excinfo:
 4160             salt.utils.vmware.get_assigned_licenses(
 4161                 self.mock_si, self.mock_entity_ref, "fake_entity_name"
 4162             )
 4163         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 4164 
 4165     def test_valid_assignments(self):
 4166         ret = salt.utils.vmware.get_assigned_licenses(
 4167             self.mock_si, self.mock_entity_ref, "fake_entity_name"
 4168         )
 4169         self.assertEqual(ret, self.mock_assignments)
 4170 
 4171 
 4172 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 4173 class AssignLicenseTestCase(TestCase):
 4174     """
 4175     Tests for salt.utils.vmware.assign_license
 4176     """
 4177 
 4178     def setUp(self):
 4179         self.mock_ent_id = MagicMock()
 4180         self.mock_si = MagicMock()
 4181         type(self.mock_si.content.about).instanceUuid = PropertyMock(
 4182             return_value=self.mock_ent_id
 4183         )
 4184         self.mock_lic_key = MagicMock()
 4185         self.mock_moid = MagicMock()
 4186         self.prop_mock_moid = PropertyMock(return_value=self.mock_moid)
 4187         self.mock_entity_ref = MagicMock()
 4188         type(self.mock_entity_ref)._moId = self.prop_mock_moid
 4189         self.mock_license = MagicMock()
 4190         self.mock_update_assigned_license = MagicMock(return_value=self.mock_license)
 4191         self.mock_lic_assign_mgr = MagicMock(
 4192             UpdateAssignedLicense=self.mock_update_assigned_license
 4193         )
 4194         patches = (
 4195             (
 4196                 "salt.utils.vmware.get_license_assignment_manager",
 4197                 MagicMock(return_value=self.mock_lic_assign_mgr),
 4198             ),
 4199         )
 4200         for mod, mock in patches:
 4201             patcher = patch(mod, mock)
 4202             patcher.start()
 4203             self.addCleanup(patcher.stop)
 4204 
 4205     def test_no_license_assignment_manager_passed_in(self):
 4206         mock_get_license_assign_manager = MagicMock()
 4207         with patch(
 4208             "salt.utils.vmware.get_license_assignment_manager",
 4209             mock_get_license_assign_manager,
 4210         ):
 4211             salt.utils.vmware.assign_license(
 4212                 self.mock_si,
 4213                 self.mock_lic_key,
 4214                 "fake_license_name",
 4215                 self.mock_entity_ref,
 4216                 "fake_entity_name",
 4217             )
 4218         mock_get_license_assign_manager.assert_called_once_with(self.mock_si)
 4219 
 4220     def test_license_assignment_manager_passed_in(self):
 4221         mock_get_license_assign_manager = MagicMock()
 4222         with patch(
 4223             "salt.utils.vmware.get_license_assignment_manager",
 4224             mock_get_license_assign_manager,
 4225         ):
 4226             salt.utils.vmware.assign_license(
 4227                 self.mock_si,
 4228                 self.mock_lic_key,
 4229                 "fake_license_name",
 4230                 self.mock_entity_ref,
 4231                 "fake_entity_name",
 4232                 license_assignment_manager=self.mock_lic_assign_mgr,
 4233             )
 4234         self.assertEqual(mock_get_license_assign_manager.call_count, 0)
 4235         self.assertEqual(self.mock_update_assigned_license.call_count, 1)
 4236 
 4237     def test_entity_name(self):
 4238         mock_trace = MagicMock()
 4239         with patch("salt._logging.impl.SaltLoggingClass.trace", mock_trace):
 4240             salt.utils.vmware.assign_license(
 4241                 self.mock_si,
 4242                 self.mock_lic_key,
 4243                 "fake_license_name",
 4244                 self.mock_entity_ref,
 4245                 "fake_entity_name",
 4246             )
 4247         mock_trace.assert_called_once_with(
 4248             "Assigning license to '%s'", "fake_entity_name"
 4249         )
 4250 
 4251     def test_instance_uuid(self):
 4252         mock_instance_uuid_prop = PropertyMock()
 4253         type(self.mock_si.content.about).instanceUuid = mock_instance_uuid_prop
 4254         self.mock_lic_assign_mgr.UpdateAssignedLicense = MagicMock(
 4255             return_value=[MagicMock(entityDisplayName="fake_vcenter")]
 4256         )
 4257         salt.utils.vmware.assign_license(
 4258             self.mock_si,
 4259             self.mock_lic_key,
 4260             "fake_license_name",
 4261             entity_name="fake_entity_name",
 4262         )
 4263         self.assertEqual(mock_instance_uuid_prop.call_count, 1)
 4264 
 4265     def test_instance_uuid_raises_no_permission(self):
 4266         exc = vim.fault.NoPermission()
 4267         exc.privilegeId = "Fake privilege"
 4268         type(self.mock_si.content.about).instanceUuid = PropertyMock(side_effect=exc)
 4269         with self.assertRaises(VMwareApiError) as excinfo:
 4270             salt.utils.vmware.assign_license(
 4271                 self.mock_si,
 4272                 self.mock_lic_key,
 4273                 "fake_license_name",
 4274                 entity_name="fake_entity_name",
 4275             )
 4276         self.assertEqual(
 4277             excinfo.exception.strerror,
 4278             "Not enough permissions. Required privilege: " "Fake privilege",
 4279         )
 4280 
 4281     def test_instance_uuid_raises_vim_fault(self):
 4282         exc = vim.fault.VimFault()
 4283         exc.msg = "VimFault msg"
 4284         type(self.mock_si.content.about).instanceUuid = PropertyMock(side_effect=exc)
 4285         with self.assertRaises(VMwareApiError) as excinfo:
 4286             salt.utils.vmware.assign_license(
 4287                 self.mock_si,
 4288                 self.mock_lic_key,
 4289                 "fake_license_name",
 4290                 entity_name="fake_entity_name",
 4291             )
 4292         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 4293 
 4294     def test_instance_uuid_raises_runtime_fault(self):
 4295         exc = vmodl.RuntimeFault()
 4296         exc.msg = "RuntimeFault msg"
 4297         type(self.mock_si.content.about).instanceUuid = PropertyMock(side_effect=exc)
 4298         with self.assertRaises(VMwareRuntimeError) as excinfo:
 4299             salt.utils.vmware.assign_license(
 4300                 self.mock_si,
 4301                 self.mock_lic_key,
 4302                 "fake_license_name",
 4303                 entity_name="fake_entity_name",
 4304             )
 4305         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 4306 
 4307     def test_update_assigned_licenses_vcenter(self):
 4308         salt.utils.vmware.assign_license(
 4309             self.mock_si,
 4310             self.mock_lic_key,
 4311             "fake_license_name",
 4312             entity_name="fake_entity_name",
 4313         )
 4314         self.mock_update_assigned_license.assert_called_once_with(
 4315             self.mock_ent_id, self.mock_lic_key, "fake_license_name"
 4316         )
 4317 
 4318     def test_update_assigned_licenses_call_with_entity(self):
 4319         salt.utils.vmware.assign_license(
 4320             self.mock_si,
 4321             self.mock_lic_key,
 4322             "fake_license_name",
 4323             self.mock_entity_ref,
 4324             "fake_entity_name",
 4325         )
 4326         self.mock_update_assigned_license.assert_called_once_with(
 4327             self.mock_moid, self.mock_lic_key, "fake_license_name"
 4328         )
 4329 
 4330     def test_update_assigned_licenses_raises_no_permission(self):
 4331         exc = vim.fault.NoPermission()
 4332         exc.privilegeId = "Fake privilege"
 4333         self.mock_lic_assign_mgr.UpdateAssignedLicense = MagicMock(side_effect=exc)
 4334         with self.assertRaises(VMwareApiError) as excinfo:
 4335             salt.utils.vmware.assign_license(
 4336                 self.mock_si,
 4337                 self.mock_lic_key,
 4338                 "fake_license_name",
 4339                 self.mock_entity_ref,
 4340                 "fake_entity_name",
 4341             )
 4342         self.assertEqual(
 4343             excinfo.exception.strerror,
 4344             "Not enough permissions. Required privilege: " "Fake privilege",
 4345         )
 4346 
 4347     def test_update_assigned_licenses_raises_vim_fault(self):
 4348         exc = vim.fault.VimFault()
 4349         exc.msg = "VimFault msg"
 4350         self.mock_lic_assign_mgr.UpdateAssignedLicense = MagicMock(side_effect=exc)
 4351         with self.assertRaises(VMwareApiError) as excinfo:
 4352             salt.utils.vmware.assign_license(
 4353                 self.mock_si,
 4354                 self.mock_lic_key,
 4355                 "fake_license_name",
 4356                 self.mock_entity_ref,
 4357                 "fake_entity_name",
 4358             )
 4359         self.assertEqual(excinfo.exception.strerror, "VimFault msg")
 4360 
 4361     def test_update_assigned_licenses_raises_runtime_fault(self):
 4362         exc = vmodl.RuntimeFault()
 4363         exc.msg = "RuntimeFault msg"
 4364         self.mock_lic_assign_mgr.UpdateAssignedLicense = MagicMock(side_effect=exc)
 4365         with self.assertRaises(VMwareRuntimeError) as excinfo:
 4366             salt.utils.vmware.assign_license(
 4367                 self.mock_si,
 4368                 self.mock_lic_key,
 4369                 "fake_license_name",
 4370                 self.mock_entity_ref,
 4371                 "fake_entity_name",
 4372             )
 4373         self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
 4374 
 4375     def test_valid_assignments(self):
 4376         ret = salt.utils.vmware.assign_license(
 4377             self.mock_si,
 4378             self.mock_lic_key,
 4379             "fake_license_name",
 4380             self.mock_entity_ref,
 4381             "fake_entity_name",
 4382         )
 4383         self.assertEqual(ret, self.mock_license)
 4384 
 4385 
 4386 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 4387 class GetStorageSystemTestCase(TestCase):
 4388     """
 4389     Tests for salt.utils.vmware.get_storage_system
 4390     """
 4391 
 4392     def setUp(self):
 4393         self.mock_si = MagicMock(content=MagicMock())
 4394         self.mock_host_ref = MagicMock()
 4395         self.mock_get_managed_object_name = MagicMock(return_value="fake_host")
 4396         self.mock_traversal_spec = MagicMock()
 4397         self.mock_obj = MagicMock()
 4398         self.mock_get_mors = MagicMock(return_value=[{"object": self.mock_obj}])
 4399 
 4400         patches = (
 4401             (
 4402                 "salt.utils.vmware.get_managed_object_name",
 4403                 self.mock_get_managed_object_name,
 4404             ),
 4405             ("salt.utils.vmware.get_mors_with_properties", self.mock_get_mors),
 4406             (
 4407                 "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 4408                 MagicMock(return_value=self.mock_traversal_spec),
 4409             ),
 4410         )
 4411         for mod, mock in patches:
 4412             patcher = patch(mod, mock)
 4413             patcher.start()
 4414             self.addCleanup(patcher.stop)
 4415 
 4416     def tearDown(self):
 4417         for attr in (
 4418             "mock_si",
 4419             "mock_host_ref",
 4420             "mock_get_managed_object_name",
 4421             "mock_traversal_spec",
 4422             "mock_obj",
 4423         ):
 4424             delattr(self, attr)
 4425 
 4426     def test_no_hostname_argument(self):
 4427         salt.utils.vmware.get_storage_system(self.mock_si, self.mock_host_ref)
 4428         self.mock_get_managed_object_name.assert_called_once_with(self.mock_host_ref)
 4429 
 4430     def test_hostname_argument(self):
 4431         salt.utils.vmware.get_storage_system(
 4432             self.mock_si, self.mock_host_ref, hostname="fake_host"
 4433         )
 4434         self.assertEqual(self.mock_get_managed_object_name.call_count, 0)
 4435 
 4436     def test_traversal_spec(self):
 4437         mock_traversal_spec = MagicMock(return_value=[{"object": self.mock_obj}])
 4438         with patch(
 4439             "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 4440             mock_traversal_spec,
 4441         ):
 4442 
 4443             salt.utils.vmware.get_storage_system(self.mock_si, self.mock_host_ref)
 4444         mock_traversal_spec.assert_called_once_with(
 4445             path="configManager.storageSystem", type=vim.HostSystem, skip=False
 4446         )
 4447 
 4448     def test_get_mors_with_properties(self):
 4449         salt.utils.vmware.get_storage_system(self.mock_si, self.mock_host_ref)
 4450         self.mock_get_mors.assert_called_once_with(
 4451             self.mock_si,
 4452             vim.HostStorageSystem,
 4453             property_list=["systemFile"],
 4454             container_ref=self.mock_host_ref,
 4455             traversal_spec=self.mock_traversal_spec,
 4456         )
 4457 
 4458     def test_empty_mors_result(self):
 4459         with patch(
 4460             "salt.utils.vmware.get_mors_with_properties", MagicMock(return_value=[])
 4461         ):
 4462             with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
 4463                 salt.utils.vmware.get_storage_system(self.mock_si, self.mock_host_ref)
 4464         self.assertEqual(
 4465             excinfo.exception.strerror,
 4466             "Host's 'fake_host' storage system was " "not retrieved",
 4467         )
 4468 
 4469     def test_valid_mors_result(self):
 4470         res = salt.utils.vmware.get_storage_system(self.mock_si, self.mock_host_ref)
 4471         self.assertEqual(res, self.mock_obj)
 4472 
 4473 
 4474 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 4475 class GetDatastoresTestCase(TestCase):
 4476     """
 4477     Tests for salt.utils.vmware.get_datastores
 4478     """
 4479 
 4480     def setUp(self):
 4481         self.mock_si = MagicMock()
 4482         self.mock_reference = MagicMock(spec=vim.HostSystem)
 4483         self.mock_mount_infos = [
 4484             MagicMock(
 4485                 volume=MagicMock(
 4486                     spec=vim.HostVmfsVolume, extent=[MagicMock(diskName="fake_disk2")]
 4487                 )
 4488             ),
 4489             MagicMock(
 4490                 volume=MagicMock(
 4491                     spec=vim.HostVmfsVolume, extent=[MagicMock(diskName="fake_disk3")]
 4492                 )
 4493             ),
 4494         ]
 4495         self.mock_mount_infos[0].volume.name = "fake_ds2"
 4496         self.mock_mount_infos[1].volume.name = "fake_ds3"
 4497         self.mock_entries = [
 4498             {"name": "fake_ds1", "object": MagicMock()},
 4499             {"name": "fake_ds2", "object": MagicMock()},
 4500             {"name": "fake_ds3", "object": MagicMock()},
 4501         ]
 4502         self.mock_storage_system = MagicMock()
 4503         self.mock_get_storage_system = MagicMock(return_value=self.mock_storage_system)
 4504         self.mock_get_managed_object_name = MagicMock(return_value="fake_host")
 4505         self.mock_traversal_spec = MagicMock()
 4506 
 4507         patches = (
 4508             (
 4509                 "salt.utils.vmware.get_managed_object_name",
 4510                 self.mock_get_managed_object_name,
 4511             ),
 4512             ("salt.utils.vmware.get_storage_system", self.mock_get_storage_system),
 4513             (
 4514                 "salt.utils.vmware.get_properties_of_managed_object",
 4515                 MagicMock(
 4516                     return_value={
 4517                         "fileSystemVolumeInfo.mountInfo": self.mock_mount_infos
 4518                     }
 4519                 ),
 4520             ),
 4521             (
 4522                 "salt.utils.vmware.get_mors_with_properties",
 4523                 MagicMock(return_value=self.mock_entries),
 4524             ),
 4525             (
 4526                 "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 4527                 MagicMock(return_value=self.mock_traversal_spec),
 4528             ),
 4529         )
 4530         for mod, mock in patches:
 4531             patcher = patch(mod, mock)
 4532             patcher.start()
 4533             self.addCleanup(patcher.stop)
 4534 
 4535     def tearDown(self):
 4536         for attr in (
 4537             "mock_si",
 4538             "mock_reference",
 4539             "mock_storage_system",
 4540             "mock_get_storage_system",
 4541             "mock_mount_infos",
 4542             "mock_entries",
 4543             "mock_get_managed_object_name",
 4544             "mock_traversal_spec",
 4545         ):
 4546             delattr(self, attr)
 4547 
 4548     def test_get_reference_name_call(self):
 4549         salt.utils.vmware.get_datastores(self.mock_si, self.mock_reference)
 4550         self.mock_get_managed_object_name.assert_called_once_with(self.mock_reference)
 4551 
 4552     def test_get_no_datastores(self):
 4553         res = salt.utils.vmware.get_datastores(self.mock_si, self.mock_reference)
 4554         self.assertEqual(res, [])
 4555 
 4556     def test_get_storage_system_call(self):
 4557         salt.utils.vmware.get_datastores(
 4558             self.mock_si, self.mock_reference, backing_disk_ids=["fake_disk1"]
 4559         )
 4560         self.mock_get_storage_system.assert_called_once_with(
 4561             self.mock_si, self.mock_reference, "fake_host"
 4562         )
 4563 
 4564     def test_get_mount_info_call(self):
 4565         mock_get_properties_of_managed_object = MagicMock()
 4566         with patch(
 4567             "salt.utils.vmware.get_properties_of_managed_object",
 4568             mock_get_properties_of_managed_object,
 4569         ):
 4570             salt.utils.vmware.get_datastores(
 4571                 self.mock_si, self.mock_reference, backing_disk_ids=["fake_disk1"]
 4572             )
 4573         mock_get_properties_of_managed_object.assert_called_once_with(
 4574             self.mock_storage_system, ["fileSystemVolumeInfo.mountInfo"]
 4575         )
 4576 
 4577     def test_backing_disks_no_mount_info(self):
 4578         with patch(
 4579             "salt.utils.vmware.get_properties_of_managed_object",
 4580             MagicMock(return_value={}),
 4581         ):
 4582             res = salt.utils.vmware.get_datastores(
 4583                 self.mock_si, self.mock_reference, backing_disk_ids=["fake_disk_id"]
 4584             )
 4585         self.assertEqual(res, [])
 4586 
 4587     def test_host_traversal_spec(self):
 4588         # Reference is of type vim.HostSystem
 4589         mock_traversal_spec_init = MagicMock()
 4590         with patch(
 4591             "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 4592             mock_traversal_spec_init,
 4593         ):
 4594 
 4595             salt.utils.vmware.get_datastores(
 4596                 self.mock_si, self.mock_reference, get_all_datastores=True
 4597             )
 4598         mock_traversal_spec_init.assert_called_once_with(
 4599             name="host_datastore_traversal",
 4600             path="datastore",
 4601             skip=False,
 4602             type=vim.HostSystem,
 4603         )
 4604 
 4605     def test_cluster_traversal_spec(self):
 4606         mock_traversal_spec_init = MagicMock()
 4607         # Reference is of type vim.ClusterComputeResource
 4608         mock_reference = MagicMock(spec=vim.ClusterComputeResource)
 4609         with patch(
 4610             "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 4611             mock_traversal_spec_init,
 4612         ):
 4613 
 4614             salt.utils.vmware.get_datastores(
 4615                 self.mock_si, mock_reference, get_all_datastores=True
 4616             )
 4617         mock_traversal_spec_init.assert_called_once_with(
 4618             name="cluster_datastore_traversal",
 4619             path="datastore",
 4620             skip=False,
 4621             type=vim.ClusterComputeResource,
 4622         )
 4623 
 4624     def test_datacenter_traversal_spec(self):
 4625         mock_traversal_spec_init = MagicMock()
 4626         # Reference is of type vim.ClusterComputeResource
 4627         mock_reference = MagicMock(spec=vim.Datacenter)
 4628         with patch(
 4629             "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 4630             mock_traversal_spec_init,
 4631         ):
 4632 
 4633             salt.utils.vmware.get_datastores(
 4634                 self.mock_si, mock_reference, get_all_datastores=True
 4635             )
 4636         mock_traversal_spec_init.assert_called_once_with(
 4637             name="datacenter_datastore_traversal",
 4638             path="datastore",
 4639             skip=False,
 4640             type=vim.Datacenter,
 4641         )
 4642 
 4643     def test_root_folder_traversal_spec(self):
 4644         mock_traversal_spec_init = MagicMock(return_value="traversal")
 4645         mock_reference = MagicMock(spec=vim.Folder)
 4646         with patch(
 4647             "salt.utils.vmware.get_managed_object_name",
 4648             MagicMock(side_effect=["fake_host", "Datacenters"]),
 4649         ):
 4650             with patch(
 4651                 "salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec",
 4652                 mock_traversal_spec_init,
 4653             ):
 4654 
 4655                 salt.utils.vmware.get_datastores(
 4656                     self.mock_si, mock_reference, get_all_datastores=True
 4657                 )
 4658 
 4659         mock_traversal_spec_init.assert_has_calls(
 4660             [
 4661                 call(path="datastore", skip=False, type=vim.Datacenter),
 4662                 call(
 4663                     path="childEntity",
 4664                     selectSet=["traversal"],
 4665                     skip=False,
 4666                     type=vim.Folder,
 4667                 ),
 4668             ]
 4669         )
 4670 
 4671     def test_unsupported_reference_type(self):
 4672         class FakeClass:
 4673             pass
 4674 
 4675         mock_reference = MagicMock(spec=FakeClass)
 4676         with self.assertRaises(ArgumentValueError) as excinfo:
 4677             salt.utils.vmware.get_datastores(
 4678                 self.mock_si, mock_reference, get_all_datastores=True
 4679             )
 4680         self.assertEqual(
 4681             excinfo.exception.strerror, "Unsupported reference type 'FakeClass'"
 4682         )
 4683 
 4684     def test_get_mors_with_properties(self):
 4685         mock_get_mors_with_properties = MagicMock()
 4686         with patch(
 4687             "salt.utils.vmware.get_mors_with_properties", mock_get_mors_with_properties
 4688         ):
 4689             salt.utils.vmware.get_datastores(
 4690                 self.mock_si, self.mock_reference, get_all_datastores=True
 4691             )
 4692         mock_get_mors_with_properties.assert_called_once_with(
 4693             self.mock_si,
 4694             object_type=vim.Datastore,
 4695             property_list=["name"],
 4696             container_ref=self.mock_reference,
 4697             traversal_spec=self.mock_traversal_spec,
 4698         )
 4699 
 4700     def test_get_all_datastores(self):
 4701         res = salt.utils.vmware.get_datastores(
 4702             self.mock_si, self.mock_reference, get_all_datastores=True
 4703         )
 4704         self.assertEqual(
 4705             res,
 4706             [
 4707                 self.mock_entries[0]["object"],
 4708                 self.mock_entries[1]["object"],
 4709                 self.mock_entries[2]["object"],
 4710             ],
 4711         )
 4712 
 4713     def test_get_datastores_filtered_by_name(self):
 4714         res = salt.utils.vmware.get_datastores(
 4715             self.mock_si, self.mock_reference, datastore_names=["fake_ds1", "fake_ds2"]
 4716         )
 4717         self.assertEqual(
 4718             res, [self.mock_entries[0]["object"], self.mock_entries[1]["object"]]
 4719         )
 4720 
 4721     def test_get_datastores_filtered_by_backing_disk(self):
 4722         res = salt.utils.vmware.get_datastores(
 4723             self.mock_si,
 4724             self.mock_reference,
 4725             backing_disk_ids=["fake_disk2", "fake_disk3"],
 4726         )
 4727         self.assertEqual(
 4728             res, [self.mock_entries[1]["object"], self.mock_entries[2]["object"]]
 4729         )
 4730 
 4731     def test_get_datastores_filtered_by_both_name_and_backing_disk(self):
 4732         # Simulate VMware data model for volumes fake_ds2, fake_ds3
 4733         res = salt.utils.vmware.get_datastores(
 4734             self.mock_si,
 4735             self.mock_reference,
 4736             datastore_names=["fake_ds1"],
 4737             backing_disk_ids=["fake_disk3"],
 4738         )
 4739         self.assertEqual(
 4740             res, [self.mock_entries[0]["object"], self.mock_entries[2]["object"]]
 4741         )
 4742 
 4743 
 4744 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 4745 class RenameDatastoreTestCase(TestCase):
 4746     """
 4747     Tests for salt.utils.vmware.rename_datastore
 4748     """
 4749 
 4750     def setUp(self):
 4751         self.mock_ds_ref = MagicMock()
 4752         self.mock_get_managed_object_name = MagicMock(return_value="fake_ds")
 4753 
 4754         patches = (
 4755             (
 4756                 "salt.utils.vmware.get_managed_object_name",
 4757                 self.mock_get_managed_object_name,
 4758             ),
 4759         )
 4760         for mod, mock in patches:
 4761             patcher = patch(mod, mock)
 4762             patcher.start()
 4763             self.addCleanup(patcher.stop)
 4764 
 4765     def tearDown(self):
 4766         for attr in ("mock_ds_ref", "mock_get_managed_object_name"):
 4767             delattr(self, attr)
 4768 
 4769     def test_datastore_name_call(self):
 4770         salt.utils.vmware.rename_datastore(self.mock_ds_ref, "fake_new_name")
 4771         self.mock_get_managed_object_name.assert_called_once_with(self.mock_ds_ref)
 4772 
 4773     def test_rename_datastore_raise_no_permission(self):
 4774         exc = vim.fault.NoPermission()
 4775         exc.privilegeId = "Fake privilege"
 4776         type(self.mock_ds_ref).RenameDatastore = MagicMock(side_effect=exc)
 4777         with self.assertRaises(VMwareApiError) as excinfo:
 4778             salt.utils.vmware.rename_datastore(self.mock_ds_ref, "fake_new_name")
 4779         self.assertEqual(
 4780             excinfo.exception.strerror,
 4781             "Not enough permissions. Required privilege: " "Fake privilege",
 4782         )
 4783 
 4784     def test_rename_datastore_raise_vim_fault(self):
 4785         exc = vim.VimFault()
 4786         exc.msg = "vim_fault"
 4787         type(self.mock_ds_ref).RenameDatastore = MagicMock(side_effect=exc)
 4788         with self.assertRaises(VMwareApiError) as excinfo:
 4789             salt.utils.vmware.rename_datastore(self.mock_ds_ref, "fake_new_name")
 4790         self.assertEqual(excinfo.exception.strerror, "vim_fault")
 4791 
 4792     def test_rename_datastore_raise_runtime_fault(self):
 4793         exc = vmodl.RuntimeFault()
 4794         exc.msg = "runtime_fault"
 4795         type(self.mock_ds_ref).RenameDatastore = MagicMock(side_effect=exc)
 4796         with self.assertRaises(VMwareRuntimeError) as excinfo:
 4797             salt.utils.vmware.rename_datastore(self.mock_ds_ref, "fake_new_name")
 4798         self.assertEqual(excinfo.exception.strerror, "runtime_fault")
 4799 
 4800     def test_rename_datastore(self):
 4801         salt.utils.vmware.rename_datastore(self.mock_ds_ref, "fake_new_name")
 4802         self.mock_ds_ref.RenameDatastore.assert_called_once_with("fake_new_name")
 4803 
 4804 
 4805 class ConvertToKbTestCase(TestCase):
 4806     """
 4807     Tests for converting units
 4808     """
 4809 
 4810     def setUp(self):
 4811         pass
 4812 
 4813     def test_gb_conversion_call(self):
 4814         self.assertEqual(
 4815             salt.utils.vmware.convert_to_kb("Gb", 10),
 4816             {"size": int(10485760), "unit": "KB"},
 4817         )
 4818 
 4819     def test_mb_conversion_call(self):
 4820         self.assertEqual(
 4821             salt.utils.vmware.convert_to_kb("Mb", 10),
 4822             {"size": int(10240), "unit": "KB"},
 4823         )
 4824 
 4825     def test_kb_conversion_call(self):
 4826         self.assertEqual(
 4827             salt.utils.vmware.convert_to_kb("Kb", 10), {"size": int(10), "unit": "KB"}
 4828         )
 4829 
 4830     def test_conversion_bad_input_argument_fault(self):
 4831         self.assertRaises(
 4832             ArgumentValueError, salt.utils.vmware.convert_to_kb, "test", 10
 4833         )
 4834 
 4835 
 4836 @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
 4837 @patch("salt.utils.vmware.get_managed_object_name", MagicMock())
 4838 @patch("salt.utils.vmware.wait_for_task", MagicMock())
 4839 class CreateVirtualMachineTestCase(TestCase):
 4840     """
 4841     Tests for salt.utils.vmware.create_vm
 4842     """
 4843 
 4844     def setUp(self):
 4845         self.vm_name = "fake_vm"
 4846         self.mock_task = MagicMock()
 4847         self.mock_config_spec = MagicMock()
 4848         self.mock_resourcepool_object = MagicMock()
 4849         self.mock_host_object = MagicMock()
 4850         self.mock_vm_create_task = MagicMock(return_value=self.mock_task)
 4851         self.mock_folder_object = MagicMock(CreateVM_Task=self.mock_vm_create_task)
 4852 
 4853     def test_create_vm_pool_task_call(self):
 4854         salt.utils.vmware.create_vm(
 4855             self.vm_name,
 4856             self.mock_config_spec,
 4857             self.mock_folder_object,
 4858             self.mock_resourcepool_object,
 4859         )
 4860         self.assert_called_once(self.mock_vm_create_task)
 4861 
 4862     def test_create_vm_host_task_call(self):
 4863         salt.utils.vmware.create_vm(
 4864             self.vm_name,
 4865             self.mock_config_spec,
 4866             self.mock_folder_object,
 4867             self.mock_resourcepool_object,
 4868             host_object=self.mock_host_object,
 4869         )
 4870         self.assert_called_once(self.mock_vm_create_task)
 4871 
 4872     def test_create_vm_raise_no_permission(self):
 4873         exception = vim.fault.NoPermission()
 4874         exception.msg = "vim.fault.NoPermission msg"
 4875         self.mock_folder_object.CreateVM_Task = MagicMock(side_effect=exception)
 4876         with self.assertRaises(VMwareApiError) as exc:
 4877             salt.utils.vmware.create_vm(
 4878                 self.vm_name,
 4879                 self.mock_config_spec,
 4880                 self.mock_folder_object,
 4881                 self.mock_resourcepool_object,
 4882             )
 4883         self.assertEqual(
 4884             exc.exception.strerror, "Not enough permissions. Required privilege: "
 4885         )
 4886 
 4887     def test_create_vm_raise_vim_fault(self):
 4888         exception = vim.fault.VimFault()
 4889         exception.msg = "vim.fault.VimFault msg"
 4890         self.mock_folder_object.CreateVM_Task = MagicMock(side_effect=exception)
 4891         with self.assertRaises(VMwareApiError) as exc:
 4892             salt.utils.vmware.create_vm(
 4893                 self.vm_name,
 4894                 self.mock_config_spec,
 4895                 self.mock_folder_object,
 4896                 self.mock_resourcepool_object,
 4897             )
 4898         self.assertEqual(exc.exception.strerror, "vim.fault.VimFault msg")
 4899 
 4900     def test_create_vm_raise_runtime_fault(self):
 4901         exception = vmodl.RuntimeFault()
 4902         exception.msg = "vmodl.RuntimeFault msg"
 4903         self.mock_folder_object.CreateVM_Task = MagicMock(side_effect=exception)
 4904         with self.assertRaises(VMwareRuntimeError) as exc:
 4905             salt.utils.vmware.create_vm(
 4906                 self.vm_name,
 4907                 self.mock_config_spec,
 4908                 self.mock_folder_object,
 4909                 self.mock_resourcepool_object,
 4910             )
 4911         self.assertEqual(exc.exception.strerror, "vmodl.RuntimeFault msg")
 4912