1
0

test_pbm.py 29 KB

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