test_pbm.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: :email:`Alexandru Bleotu <alexandru.bleotu@morganstanley.com>`
  4. Tests functions in salt.utils.vsan
  5. """
  6. # Import python libraries
  7. from __future__ import absolute_import, print_function, unicode_literals
  8. import logging
  9. import salt.utils.pbm
  10. # Import Salt libraries
  11. from salt.exceptions import (
  12. VMwareApiError,
  13. VMwareObjectRetrievalError,
  14. VMwareRuntimeError,
  15. )
  16. from salt.ext.six.moves import range
  17. from tests.support.mock import MagicMock, PropertyMock, patch
  18. # Import Salt testing libraries
  19. from tests.support.unit import TestCase, skipIf
  20. try:
  21. from pyVmomi import vim, vmodl, pbm # pylint: disable=no-name-in-module
  22. HAS_PYVMOMI = True
  23. except ImportError:
  24. HAS_PYVMOMI = False
  25. # Get Logging Started
  26. log = logging.getLogger(__name__)
  27. @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  28. class GetProfileManagerTestCase(TestCase):
  29. """Tests for salt.utils.pbm.get_profile_manager"""
  30. def setUp(self):
  31. self.mock_si = MagicMock()
  32. self.mock_stub = MagicMock()
  33. self.mock_prof_mgr = MagicMock()
  34. self.mock_content = MagicMock()
  35. self.mock_pbm_si = MagicMock(
  36. RetrieveContent=MagicMock(return_value=self.mock_content)
  37. )
  38. type(self.mock_content).profileManager = PropertyMock(
  39. return_value=self.mock_prof_mgr
  40. )
  41. patches = (
  42. (
  43. "salt.utils.vmware.get_new_service_instance_stub",
  44. MagicMock(return_value=self.mock_stub),
  45. ),
  46. (
  47. "salt.utils.pbm.pbm.ServiceInstance",
  48. MagicMock(return_value=self.mock_pbm_si),
  49. ),
  50. )
  51. for mod, mock in patches:
  52. patcher = patch(mod, mock)
  53. patcher.start()
  54. self.addCleanup(patcher.stop)
  55. def tearDown(self):
  56. for attr in (
  57. "mock_si",
  58. "mock_stub",
  59. "mock_content",
  60. "mock_pbm_si",
  61. "mock_prof_mgr",
  62. ):
  63. delattr(self, attr)
  64. def test_get_new_service_stub(self):
  65. mock_get_new_service_stub = MagicMock()
  66. with patch(
  67. "salt.utils.vmware.get_new_service_instance_stub", mock_get_new_service_stub
  68. ):
  69. salt.utils.pbm.get_profile_manager(self.mock_si)
  70. mock_get_new_service_stub.assert_called_once_with(
  71. self.mock_si, ns="pbm/2.0", path="/pbm/sdk"
  72. )
  73. def test_pbm_si(self):
  74. mock_get_pbm_si = MagicMock()
  75. with patch("salt.utils.pbm.pbm.ServiceInstance", mock_get_pbm_si):
  76. salt.utils.pbm.get_profile_manager(self.mock_si)
  77. mock_get_pbm_si.assert_called_once_with("ServiceInstance", self.mock_stub)
  78. def test_return_profile_manager(self):
  79. ret = salt.utils.pbm.get_profile_manager(self.mock_si)
  80. self.assertEqual(ret, self.mock_prof_mgr)
  81. def test_profile_manager_raises_no_permissions(self):
  82. exc = vim.fault.NoPermission()
  83. exc.privilegeId = "Fake privilege"
  84. type(self.mock_content).profileManager = PropertyMock(side_effect=exc)
  85. with self.assertRaises(VMwareApiError) as excinfo:
  86. salt.utils.pbm.get_profile_manager(self.mock_si)
  87. self.assertEqual(
  88. excinfo.exception.strerror,
  89. "Not enough permissions. Required privilege: " "Fake privilege",
  90. )
  91. def test_profile_manager_raises_vim_fault(self):
  92. exc = vim.fault.VimFault()
  93. exc.msg = "VimFault msg"
  94. type(self.mock_content).profileManager = PropertyMock(side_effect=exc)
  95. with self.assertRaises(VMwareApiError) as excinfo:
  96. salt.utils.pbm.get_profile_manager(self.mock_si)
  97. self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  98. def test_profile_manager_raises_runtime_fault(self):
  99. exc = vmodl.RuntimeFault()
  100. exc.msg = "RuntimeFault msg"
  101. type(self.mock_content).profileManager = PropertyMock(side_effect=exc)
  102. with self.assertRaises(VMwareRuntimeError) as excinfo:
  103. salt.utils.pbm.get_profile_manager(self.mock_si)
  104. self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
  105. @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  106. class GetPlacementSolverTestCase(TestCase):
  107. """Tests for salt.utils.pbm.get_placement_solver"""
  108. def setUp(self):
  109. self.mock_si = MagicMock()
  110. self.mock_stub = MagicMock()
  111. self.mock_prof_mgr = MagicMock()
  112. self.mock_content = MagicMock()
  113. self.mock_pbm_si = MagicMock(
  114. RetrieveContent=MagicMock(return_value=self.mock_content)
  115. )
  116. type(self.mock_content).placementSolver = PropertyMock(
  117. return_value=self.mock_prof_mgr
  118. )
  119. patches = (
  120. (
  121. "salt.utils.vmware.get_new_service_instance_stub",
  122. MagicMock(return_value=self.mock_stub),
  123. ),
  124. (
  125. "salt.utils.pbm.pbm.ServiceInstance",
  126. MagicMock(return_value=self.mock_pbm_si),
  127. ),
  128. )
  129. for mod, mock in patches:
  130. patcher = patch(mod, mock)
  131. patcher.start()
  132. self.addCleanup(patcher.stop)
  133. def tearDown(self):
  134. for attr in (
  135. "mock_si",
  136. "mock_stub",
  137. "mock_content",
  138. "mock_pbm_si",
  139. "mock_prof_mgr",
  140. ):
  141. delattr(self, attr)
  142. def test_get_new_service_stub(self):
  143. mock_get_new_service_stub = MagicMock()
  144. with patch(
  145. "salt.utils.vmware.get_new_service_instance_stub", mock_get_new_service_stub
  146. ):
  147. salt.utils.pbm.get_placement_solver(self.mock_si)
  148. mock_get_new_service_stub.assert_called_once_with(
  149. self.mock_si, ns="pbm/2.0", path="/pbm/sdk"
  150. )
  151. def test_pbm_si(self):
  152. mock_get_pbm_si = MagicMock()
  153. with patch("salt.utils.pbm.pbm.ServiceInstance", mock_get_pbm_si):
  154. salt.utils.pbm.get_placement_solver(self.mock_si)
  155. mock_get_pbm_si.assert_called_once_with("ServiceInstance", self.mock_stub)
  156. def test_return_profile_manager(self):
  157. ret = salt.utils.pbm.get_placement_solver(self.mock_si)
  158. self.assertEqual(ret, self.mock_prof_mgr)
  159. def test_placement_solver_raises_no_permissions(self):
  160. exc = vim.fault.NoPermission()
  161. exc.privilegeId = "Fake privilege"
  162. type(self.mock_content).placementSolver = PropertyMock(side_effect=exc)
  163. with self.assertRaises(VMwareApiError) as excinfo:
  164. salt.utils.pbm.get_placement_solver(self.mock_si)
  165. self.assertEqual(
  166. excinfo.exception.strerror,
  167. "Not enough permissions. Required privilege: " "Fake privilege",
  168. )
  169. def test_placement_solver_raises_vim_fault(self):
  170. exc = vim.fault.VimFault()
  171. exc.msg = "VimFault msg"
  172. type(self.mock_content).placementSolver = PropertyMock(side_effect=exc)
  173. with self.assertRaises(VMwareApiError) as excinfo:
  174. salt.utils.pbm.get_placement_solver(self.mock_si)
  175. self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  176. def test_placement_solver_raises_runtime_fault(self):
  177. exc = vmodl.RuntimeFault()
  178. exc.msg = "RuntimeFault msg"
  179. type(self.mock_content).placementSolver = PropertyMock(side_effect=exc)
  180. with self.assertRaises(VMwareRuntimeError) as excinfo:
  181. salt.utils.pbm.get_placement_solver(self.mock_si)
  182. self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
  183. @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  184. class GetCapabilityDefinitionsTestCase(TestCase):
  185. """Tests for salt.utils.pbm.get_capability_definitions"""
  186. def setUp(self):
  187. self.mock_res_type = MagicMock()
  188. self.mock_cap_cats = [
  189. MagicMock(capabilityMetadata=["fake_cap_meta1", "fake_cap_meta2"]),
  190. MagicMock(capabilityMetadata=["fake_cap_meta3"]),
  191. ]
  192. self.mock_prof_mgr = MagicMock(
  193. FetchCapabilityMetadata=MagicMock(return_value=self.mock_cap_cats)
  194. )
  195. patches = (
  196. (
  197. "salt.utils.pbm.pbm.profile.ResourceType",
  198. MagicMock(return_value=self.mock_res_type),
  199. ),
  200. )
  201. for mod, mock in patches:
  202. patcher = patch(mod, mock)
  203. patcher.start()
  204. self.addCleanup(patcher.stop)
  205. def tearDown(self):
  206. for attr in ("mock_res_type", "mock_cap_cats", "mock_prof_mgr"):
  207. delattr(self, attr)
  208. def test_get_res_type(self):
  209. mock_get_res_type = MagicMock()
  210. with patch("salt.utils.pbm.pbm.profile.ResourceType", mock_get_res_type):
  211. salt.utils.pbm.get_capability_definitions(self.mock_prof_mgr)
  212. mock_get_res_type.assert_called_once_with(
  213. resourceType=pbm.profile.ResourceTypeEnum.STORAGE
  214. )
  215. def test_fetch_capabilities(self):
  216. salt.utils.pbm.get_capability_definitions(self.mock_prof_mgr)
  217. self.mock_prof_mgr.FetchCapabilityMetadata.assert_called_once_with(
  218. self.mock_res_type
  219. )
  220. def test_fetch_capabilities_raises_no_permissions(self):
  221. exc = vim.fault.NoPermission()
  222. exc.privilegeId = "Fake privilege"
  223. self.mock_prof_mgr.FetchCapabilityMetadata = MagicMock(side_effect=exc)
  224. with self.assertRaises(VMwareApiError) as excinfo:
  225. salt.utils.pbm.get_capability_definitions(self.mock_prof_mgr)
  226. self.assertEqual(
  227. excinfo.exception.strerror,
  228. "Not enough permissions. Required privilege: " "Fake privilege",
  229. )
  230. def test_fetch_capabilities_raises_vim_fault(self):
  231. exc = vim.fault.VimFault()
  232. exc.msg = "VimFault msg"
  233. self.mock_prof_mgr.FetchCapabilityMetadata = MagicMock(side_effect=exc)
  234. with self.assertRaises(VMwareApiError) as excinfo:
  235. salt.utils.pbm.get_capability_definitions(self.mock_prof_mgr)
  236. self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  237. def test_fetch_capabilities_raises_runtime_fault(self):
  238. exc = vmodl.RuntimeFault()
  239. exc.msg = "RuntimeFault msg"
  240. self.mock_prof_mgr.FetchCapabilityMetadata = MagicMock(side_effect=exc)
  241. with self.assertRaises(VMwareRuntimeError) as excinfo:
  242. salt.utils.pbm.get_capability_definitions(self.mock_prof_mgr)
  243. self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
  244. def test_return_cap_definitions(self):
  245. ret = salt.utils.pbm.get_capability_definitions(self.mock_prof_mgr)
  246. self.assertEqual(ret, ["fake_cap_meta1", "fake_cap_meta2", "fake_cap_meta3"])
  247. @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  248. class GetPoliciesByIdTestCase(TestCase):
  249. """Tests for salt.utils.pbm.get_policies_by_id"""
  250. def setUp(self):
  251. self.policy_ids = MagicMock()
  252. self.mock_policies = MagicMock()
  253. self.mock_prof_mgr = MagicMock(
  254. RetrieveContent=MagicMock(return_value=self.mock_policies)
  255. )
  256. def tearDown(self):
  257. for attr in ("policy_ids", "mock_policies", "mock_prof_mgr"):
  258. delattr(self, attr)
  259. def test_retrieve_policies(self):
  260. salt.utils.pbm.get_policies_by_id(self.mock_prof_mgr, self.policy_ids)
  261. self.mock_prof_mgr.RetrieveContent.assert_called_once_with(self.policy_ids)
  262. def test_retrieve_policies_raises_no_permissions(self):
  263. exc = vim.fault.NoPermission()
  264. exc.privilegeId = "Fake privilege"
  265. self.mock_prof_mgr.RetrieveContent = MagicMock(side_effect=exc)
  266. with self.assertRaises(VMwareApiError) as excinfo:
  267. salt.utils.pbm.get_policies_by_id(self.mock_prof_mgr, self.policy_ids)
  268. self.assertEqual(
  269. excinfo.exception.strerror,
  270. "Not enough permissions. Required privilege: " "Fake privilege",
  271. )
  272. def test_retrieve_policies_raises_vim_fault(self):
  273. exc = vim.fault.VimFault()
  274. exc.msg = "VimFault msg"
  275. self.mock_prof_mgr.RetrieveContent = MagicMock(side_effect=exc)
  276. with self.assertRaises(VMwareApiError) as excinfo:
  277. salt.utils.pbm.get_policies_by_id(self.mock_prof_mgr, self.policy_ids)
  278. self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  279. def test_retrieve_policies_raises_runtime_fault(self):
  280. exc = vmodl.RuntimeFault()
  281. exc.msg = "RuntimeFault msg"
  282. self.mock_prof_mgr.RetrieveContent = MagicMock(side_effect=exc)
  283. with self.assertRaises(VMwareRuntimeError) as excinfo:
  284. salt.utils.pbm.get_policies_by_id(self.mock_prof_mgr, self.policy_ids)
  285. self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
  286. def test_return_policies(self):
  287. ret = salt.utils.pbm.get_policies_by_id(self.mock_prof_mgr, self.policy_ids)
  288. self.assertEqual(ret, self.mock_policies)
  289. @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  290. class GetStoragePoliciesTestCase(TestCase):
  291. """Tests for salt.utils.pbm.get_storage_policies"""
  292. def setUp(self):
  293. self.mock_res_type = MagicMock()
  294. self.mock_policy_ids = MagicMock()
  295. self.mock_prof_mgr = MagicMock(
  296. QueryProfile=MagicMock(return_value=self.mock_policy_ids)
  297. )
  298. # Policies
  299. self.mock_policies = []
  300. for i in range(4):
  301. mock_obj = MagicMock(
  302. resourceType=MagicMock(
  303. resourceType=pbm.profile.ResourceTypeEnum.STORAGE
  304. )
  305. )
  306. mock_obj.name = "fake_policy{0}".format(i)
  307. self.mock_policies.append(mock_obj)
  308. patches = (
  309. (
  310. "salt.utils.pbm.pbm.profile.ResourceType",
  311. MagicMock(return_value=self.mock_res_type),
  312. ),
  313. (
  314. "salt.utils.pbm.get_policies_by_id",
  315. MagicMock(return_value=self.mock_policies),
  316. ),
  317. )
  318. for mod, mock in patches:
  319. patcher = patch(mod, mock)
  320. patcher.start()
  321. self.addCleanup(patcher.stop)
  322. def tearDown(self):
  323. for attr in (
  324. "mock_res_type",
  325. "mock_policy_ids",
  326. "mock_policies",
  327. "mock_prof_mgr",
  328. ):
  329. delattr(self, attr)
  330. def test_get_res_type(self):
  331. mock_get_res_type = MagicMock()
  332. with patch("salt.utils.pbm.pbm.profile.ResourceType", mock_get_res_type):
  333. salt.utils.pbm.get_storage_policies(self.mock_prof_mgr)
  334. mock_get_res_type.assert_called_once_with(
  335. resourceType=pbm.profile.ResourceTypeEnum.STORAGE
  336. )
  337. def test_retrieve_policy_ids(self):
  338. mock_retrieve_policy_ids = MagicMock(return_value=self.mock_policy_ids)
  339. self.mock_prof_mgr.QueryProfile = mock_retrieve_policy_ids
  340. salt.utils.pbm.get_storage_policies(self.mock_prof_mgr)
  341. mock_retrieve_policy_ids.assert_called_once_with(self.mock_res_type)
  342. def test_retrieve_policy_ids_raises_no_permissions(self):
  343. exc = vim.fault.NoPermission()
  344. exc.privilegeId = "Fake privilege"
  345. self.mock_prof_mgr.QueryProfile = MagicMock(side_effect=exc)
  346. with self.assertRaises(VMwareApiError) as excinfo:
  347. salt.utils.pbm.get_storage_policies(self.mock_prof_mgr)
  348. self.assertEqual(
  349. excinfo.exception.strerror,
  350. "Not enough permissions. Required privilege: " "Fake privilege",
  351. )
  352. def test_retrieve_policy_ids_raises_vim_fault(self):
  353. exc = vim.fault.VimFault()
  354. exc.msg = "VimFault msg"
  355. self.mock_prof_mgr.QueryProfile = MagicMock(side_effect=exc)
  356. with self.assertRaises(VMwareApiError) as excinfo:
  357. salt.utils.pbm.get_storage_policies(self.mock_prof_mgr)
  358. self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  359. def test_retrieve_policy_ids_raises_runtime_fault(self):
  360. exc = vmodl.RuntimeFault()
  361. exc.msg = "RuntimeFault msg"
  362. self.mock_prof_mgr.QueryProfile = MagicMock(side_effect=exc)
  363. with self.assertRaises(VMwareRuntimeError) as excinfo:
  364. salt.utils.pbm.get_storage_policies(self.mock_prof_mgr)
  365. self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
  366. def test_get_policies_by_id(self):
  367. mock_get_policies_by_id = MagicMock(return_value=self.mock_policies)
  368. with patch("salt.utils.pbm.get_policies_by_id", mock_get_policies_by_id):
  369. salt.utils.pbm.get_storage_policies(self.mock_prof_mgr)
  370. mock_get_policies_by_id.assert_called_once_with(
  371. self.mock_prof_mgr, self.mock_policy_ids
  372. )
  373. def test_return_all_policies(self):
  374. ret = salt.utils.pbm.get_storage_policies(
  375. self.mock_prof_mgr, get_all_policies=True
  376. )
  377. self.assertEqual(ret, self.mock_policies)
  378. def test_return_filtered_policies(self):
  379. ret = salt.utils.pbm.get_storage_policies(
  380. self.mock_prof_mgr, policy_names=["fake_policy1", "fake_policy3"]
  381. )
  382. self.assertEqual(ret, [self.mock_policies[1], self.mock_policies[3]])
  383. @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  384. class CreateStoragePolicyTestCase(TestCase):
  385. """Tests for salt.utils.pbm.create_storage_policy"""
  386. def setUp(self):
  387. self.mock_policy_spec = MagicMock()
  388. self.mock_prof_mgr = MagicMock()
  389. def tearDown(self):
  390. for attr in ("mock_policy_spec", "mock_prof_mgr"):
  391. delattr(self, attr)
  392. def test_create_policy(self):
  393. salt.utils.pbm.create_storage_policy(self.mock_prof_mgr, self.mock_policy_spec)
  394. self.mock_prof_mgr.Create.assert_called_once_with(self.mock_policy_spec)
  395. def test_create_policy_raises_no_permissions(self):
  396. exc = vim.fault.NoPermission()
  397. exc.privilegeId = "Fake privilege"
  398. self.mock_prof_mgr.Create = MagicMock(side_effect=exc)
  399. with self.assertRaises(VMwareApiError) as excinfo:
  400. salt.utils.pbm.create_storage_policy(
  401. self.mock_prof_mgr, self.mock_policy_spec
  402. )
  403. self.assertEqual(
  404. excinfo.exception.strerror,
  405. "Not enough permissions. Required privilege: " "Fake privilege",
  406. )
  407. def test_create_policy_raises_vim_fault(self):
  408. exc = vim.fault.VimFault()
  409. exc.msg = "VimFault msg"
  410. self.mock_prof_mgr.Create = MagicMock(side_effect=exc)
  411. with self.assertRaises(VMwareApiError) as excinfo:
  412. salt.utils.pbm.create_storage_policy(
  413. self.mock_prof_mgr, self.mock_policy_spec
  414. )
  415. self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  416. def test_create_policy_raises_runtime_fault(self):
  417. exc = vmodl.RuntimeFault()
  418. exc.msg = "RuntimeFault msg"
  419. self.mock_prof_mgr.Create = MagicMock(side_effect=exc)
  420. with self.assertRaises(VMwareRuntimeError) as excinfo:
  421. salt.utils.pbm.create_storage_policy(
  422. self.mock_prof_mgr, self.mock_policy_spec
  423. )
  424. self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
  425. @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  426. class UpdateStoragePolicyTestCase(TestCase):
  427. """Tests for salt.utils.pbm.update_storage_policy"""
  428. def setUp(self):
  429. self.mock_policy_spec = MagicMock()
  430. self.mock_policy = MagicMock()
  431. self.mock_prof_mgr = MagicMock()
  432. def tearDown(self):
  433. for attr in ("mock_policy_spec", "mock_policy", "mock_prof_mgr"):
  434. delattr(self, attr)
  435. def test_create_policy(self):
  436. salt.utils.pbm.update_storage_policy(
  437. self.mock_prof_mgr, self.mock_policy, self.mock_policy_spec
  438. )
  439. self.mock_prof_mgr.Update.assert_called_once_with(
  440. self.mock_policy.profileId, self.mock_policy_spec
  441. )
  442. def test_create_policy_raises_no_permissions(self):
  443. exc = vim.fault.NoPermission()
  444. exc.privilegeId = "Fake privilege"
  445. self.mock_prof_mgr.Update = MagicMock(side_effect=exc)
  446. with self.assertRaises(VMwareApiError) as excinfo:
  447. salt.utils.pbm.update_storage_policy(
  448. self.mock_prof_mgr, self.mock_policy, self.mock_policy_spec
  449. )
  450. self.assertEqual(
  451. excinfo.exception.strerror,
  452. "Not enough permissions. Required privilege: " "Fake privilege",
  453. )
  454. def test_create_policy_raises_vim_fault(self):
  455. exc = vim.fault.VimFault()
  456. exc.msg = "VimFault msg"
  457. self.mock_prof_mgr.Update = MagicMock(side_effect=exc)
  458. with self.assertRaises(VMwareApiError) as excinfo:
  459. salt.utils.pbm.update_storage_policy(
  460. self.mock_prof_mgr, self.mock_policy, self.mock_policy_spec
  461. )
  462. self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  463. def test_create_policy_raises_runtime_fault(self):
  464. exc = vmodl.RuntimeFault()
  465. exc.msg = "RuntimeFault msg"
  466. self.mock_prof_mgr.Update = MagicMock(side_effect=exc)
  467. with self.assertRaises(VMwareRuntimeError) as excinfo:
  468. salt.utils.pbm.update_storage_policy(
  469. self.mock_prof_mgr, self.mock_policy, self.mock_policy_spec
  470. )
  471. self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
  472. @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  473. class GetDefaultStoragePolicyOfDatastoreTestCase(TestCase):
  474. """Tests for salt.utils.pbm.get_default_storage_policy_of_datastore"""
  475. def setUp(self):
  476. self.mock_ds = MagicMock(_moId="fake_ds_moid")
  477. self.mock_hub = MagicMock()
  478. self.mock_policy_id = "fake_policy_id"
  479. self.mock_prof_mgr = MagicMock(
  480. QueryDefaultRequirementProfile=MagicMock(return_value=self.mock_policy_id)
  481. )
  482. self.mock_policy_refs = [MagicMock()]
  483. patches = (
  484. (
  485. "salt.utils.pbm.pbm.placement.PlacementHub",
  486. MagicMock(return_value=self.mock_hub),
  487. ),
  488. (
  489. "salt.utils.pbm.get_policies_by_id",
  490. MagicMock(return_value=self.mock_policy_refs),
  491. ),
  492. )
  493. for mod, mock in patches:
  494. patcher = patch(mod, mock)
  495. patcher.start()
  496. self.addCleanup(patcher.stop)
  497. def tearDown(self):
  498. for attr in (
  499. "mock_ds",
  500. "mock_hub",
  501. "mock_policy_id",
  502. "mock_prof_mgr",
  503. "mock_policy_refs",
  504. ):
  505. delattr(self, attr)
  506. def test_get_placement_hub(self):
  507. mock_get_placement_hub = MagicMock()
  508. with patch("salt.utils.pbm.pbm.placement.PlacementHub", mock_get_placement_hub):
  509. salt.utils.pbm.get_default_storage_policy_of_datastore(
  510. self.mock_prof_mgr, self.mock_ds
  511. )
  512. mock_get_placement_hub.assert_called_once_with(
  513. hubId="fake_ds_moid", hubType="Datastore"
  514. )
  515. def test_query_default_requirement_profile(self):
  516. mock_query_prof = MagicMock(return_value=self.mock_policy_id)
  517. self.mock_prof_mgr.QueryDefaultRequirementProfile = mock_query_prof
  518. salt.utils.pbm.get_default_storage_policy_of_datastore(
  519. self.mock_prof_mgr, self.mock_ds
  520. )
  521. mock_query_prof.assert_called_once_with(self.mock_hub)
  522. def test_query_default_requirement_profile_raises_no_permissions(self):
  523. exc = vim.fault.NoPermission()
  524. exc.privilegeId = "Fake privilege"
  525. self.mock_prof_mgr.QueryDefaultRequirementProfile = MagicMock(side_effect=exc)
  526. with self.assertRaises(VMwareApiError) as excinfo:
  527. salt.utils.pbm.get_default_storage_policy_of_datastore(
  528. self.mock_prof_mgr, self.mock_ds
  529. )
  530. self.assertEqual(
  531. excinfo.exception.strerror,
  532. "Not enough permissions. Required privilege: " "Fake privilege",
  533. )
  534. def test_query_default_requirement_profile_raises_vim_fault(self):
  535. exc = vim.fault.VimFault()
  536. exc.msg = "VimFault msg"
  537. self.mock_prof_mgr.QueryDefaultRequirementProfile = MagicMock(side_effect=exc)
  538. with self.assertRaises(VMwareApiError) as excinfo:
  539. salt.utils.pbm.get_default_storage_policy_of_datastore(
  540. self.mock_prof_mgr, self.mock_ds
  541. )
  542. self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  543. def test_query_default_requirement_profile_raises_runtime_fault(self):
  544. exc = vmodl.RuntimeFault()
  545. exc.msg = "RuntimeFault msg"
  546. self.mock_prof_mgr.QueryDefaultRequirementProfile = MagicMock(side_effect=exc)
  547. with self.assertRaises(VMwareRuntimeError) as excinfo:
  548. salt.utils.pbm.get_default_storage_policy_of_datastore(
  549. self.mock_prof_mgr, self.mock_ds
  550. )
  551. self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")
  552. def test_get_policies_by_id(self):
  553. mock_get_policies_by_id = MagicMock()
  554. with patch("salt.utils.pbm.get_policies_by_id", mock_get_policies_by_id):
  555. salt.utils.pbm.get_default_storage_policy_of_datastore(
  556. self.mock_prof_mgr, self.mock_ds
  557. )
  558. mock_get_policies_by_id.assert_called_once_with(
  559. self.mock_prof_mgr, [self.mock_policy_id]
  560. )
  561. def test_no_policy_refs(self):
  562. mock_get_policies_by_id = MagicMock()
  563. with patch("salt.utils.pbm.get_policies_by_id", MagicMock(return_value=None)):
  564. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  565. salt.utils.pbm.get_default_storage_policy_of_datastore(
  566. self.mock_prof_mgr, self.mock_ds
  567. )
  568. self.assertEqual(
  569. excinfo.exception.strerror,
  570. "Storage policy with id 'fake_policy_id' was not " "found",
  571. )
  572. def test_return_policy_ref(self):
  573. mock_get_policies_by_id = MagicMock()
  574. ret = salt.utils.pbm.get_default_storage_policy_of_datastore(
  575. self.mock_prof_mgr, self.mock_ds
  576. )
  577. self.assertEqual(ret, self.mock_policy_refs[0])
  578. @skipIf(not HAS_PYVMOMI, "The 'pyvmomi' library is missing")
  579. class AssignDefaultStoragePolicyToDatastoreTestCase(TestCase):
  580. """Tests for salt.utils.pbm.assign_default_storage_policy_to_datastore"""
  581. def setUp(self):
  582. self.mock_ds = MagicMock(_moId="fake_ds_moid")
  583. self.mock_policy = MagicMock()
  584. self.mock_hub = MagicMock()
  585. self.mock_prof_mgr = MagicMock()
  586. patches = (
  587. (
  588. "salt.utils.pbm.pbm.placement.PlacementHub",
  589. MagicMock(return_value=self.mock_hub),
  590. ),
  591. )
  592. for mod, mock in patches:
  593. patcher = patch(mod, mock)
  594. patcher.start()
  595. self.addCleanup(patcher.stop)
  596. def tearDown(self):
  597. for attr in ("mock_ds", "mock_hub", "mock_policy", "mock_prof_mgr"):
  598. delattr(self, attr)
  599. def test_get_placement_hub(self):
  600. mock_get_placement_hub = MagicMock()
  601. with patch("salt.utils.pbm.pbm.placement.PlacementHub", mock_get_placement_hub):
  602. salt.utils.pbm.assign_default_storage_policy_to_datastore(
  603. self.mock_prof_mgr, self.mock_policy, self.mock_ds
  604. )
  605. mock_get_placement_hub.assert_called_once_with(
  606. hubId="fake_ds_moid", hubType="Datastore"
  607. )
  608. def test_assign_default_requirement_profile(self):
  609. mock_assign_prof = MagicMock()
  610. self.mock_prof_mgr.AssignDefaultRequirementProfile = mock_assign_prof
  611. salt.utils.pbm.assign_default_storage_policy_to_datastore(
  612. self.mock_prof_mgr, self.mock_policy, self.mock_ds
  613. )
  614. mock_assign_prof.assert_called_once_with(
  615. self.mock_policy.profileId, [self.mock_hub]
  616. )
  617. def test_assign_default_requirement_profile_raises_no_permissions(self):
  618. exc = vim.fault.NoPermission()
  619. exc.privilegeId = "Fake privilege"
  620. self.mock_prof_mgr.AssignDefaultRequirementProfile = MagicMock(side_effect=exc)
  621. with self.assertRaises(VMwareApiError) as excinfo:
  622. salt.utils.pbm.assign_default_storage_policy_to_datastore(
  623. self.mock_prof_mgr, self.mock_policy, self.mock_ds
  624. )
  625. self.assertEqual(
  626. excinfo.exception.strerror,
  627. "Not enough permissions. Required privilege: " "Fake privilege",
  628. )
  629. def test_assign_default_requirement_profile_raises_vim_fault(self):
  630. exc = vim.fault.VimFault()
  631. exc.msg = "VimFault msg"
  632. self.mock_prof_mgr.AssignDefaultRequirementProfile = MagicMock(side_effect=exc)
  633. with self.assertRaises(VMwareApiError) as excinfo:
  634. salt.utils.pbm.assign_default_storage_policy_to_datastore(
  635. self.mock_prof_mgr, self.mock_policy, self.mock_ds
  636. )
  637. self.assertEqual(excinfo.exception.strerror, "VimFault msg")
  638. def test_assign_default_requirement_profile_raises_runtime_fault(self):
  639. exc = vmodl.RuntimeFault()
  640. exc.msg = "RuntimeFault msg"
  641. self.mock_prof_mgr.AssignDefaultRequirementProfile = MagicMock(side_effect=exc)
  642. with self.assertRaises(VMwareRuntimeError) as excinfo:
  643. salt.utils.pbm.assign_default_storage_policy_to_datastore(
  644. self.mock_prof_mgr, self.mock_policy, self.mock_ds
  645. )
  646. self.assertEqual(excinfo.exception.strerror, "RuntimeFault msg")