1
0

test_pbm.py 29 KB

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