1
0

test_vsan.py 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import TestCase, skipIf
  12. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock, \
  13. PropertyMock
  14. # Import Salt libraries
  15. from salt.exceptions import VMwareApiError, VMwareRuntimeError, \
  16. VMwareObjectRetrievalError
  17. from salt.utils import vsan
  18. try:
  19. from pyVmomi import vim, vmodl
  20. HAS_PYVMOMI = True
  21. except ImportError:
  22. HAS_PYVMOMI = False
  23. HAS_PYVSAN = vsan.HAS_PYVSAN
  24. # Get Logging Started
  25. log = logging.getLogger(__name__)
  26. @skipIf(NO_MOCK, NO_MOCK_REASON)
  27. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  28. @skipIf(not HAS_PYVSAN, 'The \'vsan\' ext library is missing')
  29. class VsanSupportedTestCase(TestCase):
  30. '''Tests for salt.utils.vsan.vsan_supported'''
  31. def test_supported_api_version(self):
  32. mock_si = MagicMock(content=MagicMock(about=MagicMock()))
  33. type(mock_si.content.about).apiVersion = \
  34. PropertyMock(return_value='6.0')
  35. self.assertTrue(vsan.vsan_supported(mock_si))
  36. def test_unsupported_api_version(self):
  37. mock_si = MagicMock(content=MagicMock(about=MagicMock()))
  38. type(mock_si.content.about).apiVersion = \
  39. PropertyMock(return_value='5.0')
  40. self.assertFalse(vsan.vsan_supported(mock_si))
  41. def test_api_version_raises_no_permission(self):
  42. exc = vim.fault.NoPermission()
  43. exc.privilegeId = 'Fake privilege'
  44. mock_si = MagicMock(content=MagicMock(about=MagicMock()))
  45. type(mock_si.content.about).apiVersion = PropertyMock(side_effect=exc)
  46. with self.assertRaises(VMwareApiError) as excinfo:
  47. vsan.vsan_supported(mock_si)
  48. self.assertEqual(excinfo.exception.strerror,
  49. 'Not enough permissions. Required privilege: '
  50. 'Fake privilege')
  51. def test_api_version_raises_vim_fault(self):
  52. exc = vim.fault.VimFault()
  53. exc.msg = 'VimFault msg'
  54. mock_si = MagicMock(content=MagicMock(about=MagicMock()))
  55. type(mock_si.content.about).apiVersion = PropertyMock(side_effect=exc)
  56. with self.assertRaises(VMwareApiError) as excinfo:
  57. vsan.vsan_supported(mock_si)
  58. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  59. def test_api_version_raises_runtime_fault(self):
  60. exc = vmodl.RuntimeFault()
  61. exc.msg = 'RuntimeFault msg'
  62. mock_si = MagicMock(content=MagicMock(about=MagicMock()))
  63. type(mock_si.content.about).apiVersion = PropertyMock(side_effect=exc)
  64. with self.assertRaises(VMwareRuntimeError) as excinfo:
  65. vsan.vsan_supported(mock_si)
  66. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  67. @skipIf(NO_MOCK, NO_MOCK_REASON)
  68. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  69. @skipIf(not HAS_PYVSAN, 'The \'vsan\' ext library is missing')
  70. class GetVsanClusterConfigSystemTestCase(TestCase, LoaderModuleMockMixin):
  71. '''Tests for salt.utils.vsan.get_vsan_cluster_config_system'''
  72. def setup_loader_modules(self):
  73. return {vsan: {
  74. '__virtual__': MagicMock(return_value='vsan'),
  75. 'sys': MagicMock(),
  76. 'ssl': MagicMock()}}
  77. def setUp(self):
  78. self.mock_si = MagicMock()
  79. self.mock_ret = MagicMock()
  80. patches = (('salt.utils.vsan.vsanapiutils.GetVsanVcMos',
  81. MagicMock(
  82. return_value={'vsan-cluster-config-system':
  83. self.mock_ret})),)
  84. for mod, mock in patches:
  85. patcher = patch(mod, mock)
  86. patcher.start()
  87. self.addCleanup(patcher.stop)
  88. type(vsan.sys).version_info = PropertyMock(return_value=(2, 7, 9))
  89. self.mock_context = MagicMock()
  90. self.mock_create_default_context = \
  91. MagicMock(return_value=self.mock_context)
  92. vsan.ssl.create_default_context = self.mock_create_default_context
  93. def tearDown(self):
  94. for attr in ('mock_si', 'mock_ret', 'mock_context',
  95. 'mock_create_default_context'):
  96. delattr(self, attr)
  97. def test_ssl_default_context_loaded(self):
  98. vsan.get_vsan_cluster_config_system(self.mock_si)
  99. self.mock_create_default_context.assert_called_once_with()
  100. self.assertFalse(self.mock_context.check_hostname)
  101. self.assertEqual(self.mock_context.verify_mode, vsan.ssl.CERT_NONE)
  102. def test_ssl_default_context_not_loaded(self):
  103. type(vsan.sys).version_info = PropertyMock(return_value=(2, 7, 8))
  104. vsan.get_vsan_cluster_config_system(self.mock_si)
  105. self.assertEqual(self.mock_create_default_context.call_count, 0)
  106. def test_GetVsanVcMos_call(self):
  107. mock_get_vsan_vc_mos = MagicMock()
  108. with patch('salt.utils.vsan.vsanapiutils.GetVsanVcMos',
  109. mock_get_vsan_vc_mos):
  110. vsan.get_vsan_cluster_config_system(self.mock_si)
  111. mock_get_vsan_vc_mos.assert_called_once_with(self.mock_si._stub,
  112. context=self.mock_context)
  113. def test_return(self):
  114. ret = vsan.get_vsan_cluster_config_system(self.mock_si)
  115. self.assertEqual(ret, self.mock_ret)
  116. @skipIf(NO_MOCK, NO_MOCK_REASON)
  117. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  118. @skipIf(not HAS_PYVSAN, 'The \'pyvsan\' bindings are missing')
  119. class GetVsanDiskManagementSystemTestCase(TestCase, LoaderModuleMockMixin):
  120. '''Tests for salt.utils.vsan.get_vsan_disk_management_system'''
  121. def setup_loader_modules(self):
  122. return {vsan: {
  123. '__virtual__': MagicMock(return_value='vsan'),
  124. 'sys': MagicMock(),
  125. 'ssl': MagicMock()}}
  126. def setUp(self):
  127. self.mock_si = MagicMock()
  128. self.mock_ret = MagicMock()
  129. patches = (('salt.utils.vsan.vsanapiutils.GetVsanVcMos',
  130. MagicMock(
  131. return_value={'vsan-disk-management-system':
  132. self.mock_ret})),)
  133. for mod, mock in patches:
  134. patcher = patch(mod, mock)
  135. patcher.start()
  136. self.addCleanup(patcher.stop)
  137. type(vsan.sys).version_info = PropertyMock(return_value=(2, 7, 9))
  138. self.mock_context = MagicMock()
  139. self.mock_create_default_context = \
  140. MagicMock(return_value=self.mock_context)
  141. vsan.ssl.create_default_context = self.mock_create_default_context
  142. def tearDown(self):
  143. for attr in ('mock_si', 'mock_ret', 'mock_context',
  144. 'mock_create_default_context'):
  145. delattr(self, attr)
  146. def test_ssl_default_context_loaded(self):
  147. vsan.get_vsan_disk_management_system(self.mock_si)
  148. self.mock_create_default_context.assert_called_once_with()
  149. self.assertFalse(self.mock_context.check_hostname)
  150. self.assertEqual(self.mock_context.verify_mode, vsan.ssl.CERT_NONE)
  151. def test_ssl_default_context_not_loaded(self):
  152. type(vsan.sys).version_info = PropertyMock(return_value=(2, 7, 8))
  153. vsan.get_vsan_disk_management_system(self.mock_si)
  154. self.assertEqual(self.mock_create_default_context.call_count, 0)
  155. def test_GetVsanVcMos_call(self):
  156. mock_get_vsan_vc_mos = MagicMock()
  157. with patch('salt.utils.vsan.vsanapiutils.GetVsanVcMos',
  158. mock_get_vsan_vc_mos):
  159. vsan.get_vsan_disk_management_system(self.mock_si)
  160. mock_get_vsan_vc_mos.assert_called_once_with(self.mock_si._stub,
  161. context=self.mock_context)
  162. def test_return(self):
  163. ret = vsan.get_vsan_disk_management_system(self.mock_si)
  164. self.assertEqual(ret, self.mock_ret)
  165. @skipIf(NO_MOCK, NO_MOCK_REASON)
  166. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  167. @skipIf(not HAS_PYVSAN, 'The \'vsan\' ext library is missing')
  168. class GetHostVsanSystemTestCase(TestCase):
  169. '''Tests for salt.utils.vsan.get_host_vsan_system'''
  170. def setUp(self):
  171. self.mock_host_ref = MagicMock()
  172. self.mock_si = MagicMock()
  173. self.mock_traversal_spec = MagicMock()
  174. self.mock_vsan_system = MagicMock()
  175. patches = (
  176. ('salt.utils.vmware.get_managed_object_name',
  177. MagicMock(return_value='fake_hostname')),
  178. ('salt.utils.vsan.vmodl.query.PropertyCollector.TraversalSpec',
  179. MagicMock(return_value=self.mock_traversal_spec)),
  180. ('salt.utils.vmware.get_mors_with_properties',
  181. MagicMock(return_value=self.mock_traversal_spec)),
  182. ('salt.utils.vmware.get_mors_with_properties',
  183. MagicMock(return_value=[{'object': self.mock_vsan_system}])))
  184. for mod, mock in patches:
  185. patcher = patch(mod, mock)
  186. patcher.start()
  187. self.addCleanup(patcher.stop)
  188. def test_get_hostname(self):
  189. mock_get_managed_object_name = MagicMock(return_value='fake_hostname')
  190. with patch('salt.utils.vmware.get_managed_object_name',
  191. mock_get_managed_object_name):
  192. vsan.get_host_vsan_system(self.mock_si, self.mock_host_ref)
  193. mock_get_managed_object_name.assert_called_once_with(
  194. self.mock_host_ref)
  195. def test_hostname_argument(self):
  196. mock_get_managed_object_name = MagicMock(return_value='fake_hostname')
  197. with patch('salt.utils.vmware.get_managed_object_name',
  198. MagicMock(return_value='fake_hostname')):
  199. vsan.get_host_vsan_system(self.mock_si,
  200. self.mock_host_ref,
  201. hostname='passedin_hostname')
  202. self.assertEqual(mock_get_managed_object_name.call_count, 0)
  203. def test_traversal_spec(self):
  204. mock_traversal_spec = MagicMock(return_value=self.mock_traversal_spec)
  205. with patch(
  206. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  207. mock_traversal_spec):
  208. vsan.get_host_vsan_system(self.mock_si, self.mock_host_ref)
  209. mock_traversal_spec.assert_called_once_with(
  210. path='configManager.vsanSystem',
  211. type=vim.HostSystem,
  212. skip=False)
  213. def test_get_mors_with_properties(self):
  214. mock_get_mors = \
  215. MagicMock(return_value=[{'object': self.mock_vsan_system}])
  216. with patch('salt.utils.vmware.get_mors_with_properties',
  217. mock_get_mors):
  218. vsan.get_host_vsan_system(self.mock_si, self.mock_host_ref)
  219. mock_get_mors.assert_called_once_with(
  220. self.mock_si,
  221. vim.HostVsanSystem,
  222. property_list=['config.enabled'],
  223. container_ref=self.mock_host_ref,
  224. traversal_spec=self.mock_traversal_spec)
  225. def test_empty_mors_result(self):
  226. mock_get_mors = MagicMock(return_value=None)
  227. with patch('salt.utils.vmware.get_mors_with_properties',
  228. mock_get_mors):
  229. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  230. vsan.get_host_vsan_system(self.mock_si, self.mock_host_ref)
  231. self.assertEqual(excinfo.exception.strerror,
  232. 'Host\'s \'fake_hostname\' VSAN system was '
  233. 'not retrieved')
  234. def test_valid_mors_result(self):
  235. res = vsan.get_host_vsan_system(self.mock_si, self.mock_host_ref)
  236. self.assertEqual(res, self.mock_vsan_system)
  237. @skipIf(NO_MOCK, NO_MOCK_REASON)
  238. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  239. @skipIf(not HAS_PYVSAN, 'The \'vsan\' ext library is missing')
  240. class CreateDiskgroupTestCase(TestCase):
  241. '''Tests for salt.utils.vsan.create_diskgroup'''
  242. def setUp(self):
  243. self.mock_si = MagicMock()
  244. self.mock_task = MagicMock()
  245. self.mock_initialise_disk_mapping = \
  246. MagicMock(return_value=self.mock_task)
  247. self.mock_vsan_disk_mgmt_system = MagicMock(
  248. InitializeDiskMappings=self.mock_initialise_disk_mapping)
  249. self.mock_host_ref = MagicMock()
  250. self.mock_cache_disk = MagicMock()
  251. self.mock_cap_disk1 = MagicMock()
  252. self.mock_cap_disk2 = MagicMock()
  253. self.mock_spec = MagicMock()
  254. patches = (
  255. ('salt.utils.vmware.get_managed_object_name',
  256. MagicMock(return_value='fake_hostname')),
  257. ('salt.utils.vsan.vim.VimVsanHostDiskMappingCreationSpec',
  258. MagicMock(return_value=self.mock_spec)),
  259. ('salt.utils.vsan._wait_for_tasks', MagicMock()))
  260. for mod, mock in patches:
  261. patcher = patch(mod, mock)
  262. patcher.start()
  263. self.addCleanup(patcher.stop)
  264. def test_get_hostname(self):
  265. mock_get_managed_object_name = MagicMock(return_value='fake_hostname')
  266. with patch('salt.utils.vmware.get_managed_object_name',
  267. mock_get_managed_object_name):
  268. vsan.create_diskgroup(self.mock_si, self.mock_vsan_disk_mgmt_system,
  269. self.mock_host_ref, self.mock_cache_disk,
  270. [self.mock_cap_disk1, self.mock_cap_disk2])
  271. mock_get_managed_object_name.assert_called_once_with(
  272. self.mock_host_ref)
  273. def test_vsan_spec_all_flash(self):
  274. self.mock_cap_disk1.ssd = True
  275. vsan.create_diskgroup(self.mock_si, self.mock_vsan_disk_mgmt_system,
  276. self.mock_host_ref, self.mock_cache_disk,
  277. [self.mock_cap_disk1, self.mock_cap_disk2])
  278. self.assertEqual(self.mock_spec.capacityDisks, [self.mock_cap_disk1,
  279. self.mock_cap_disk2])
  280. self.assertEqual(self.mock_spec.cacheDisks, [self.mock_cache_disk])
  281. self.assertEqual(self.mock_spec.creationType, 'allFlash')
  282. self.assertEqual(self.mock_spec.host, self.mock_host_ref)
  283. def test_vsan_spec_hybrid(self):
  284. self.mock_cap_disk1.ssd = False
  285. vsan.create_diskgroup(self.mock_si, self.mock_vsan_disk_mgmt_system,
  286. self.mock_host_ref, self.mock_cache_disk,
  287. [self.mock_cap_disk1, self.mock_cap_disk2])
  288. self.mock_cap_disk1.ssd = False
  289. self.assertEqual(self.mock_spec.creationType, 'hybrid')
  290. def test_initialize_disk_mapping(self):
  291. vsan.create_diskgroup(self.mock_si, self.mock_vsan_disk_mgmt_system,
  292. self.mock_host_ref, self.mock_cache_disk,
  293. [self.mock_cap_disk1, self.mock_cap_disk2])
  294. self.mock_initialise_disk_mapping.assert_called_once_with(
  295. self.mock_spec)
  296. def test_initialize_disk_mapping_raise_no_permission(self):
  297. err = vim.fault.NoPermission()
  298. err.privilegeId = 'Fake privilege'
  299. self.mock_vsan_disk_mgmt_system.InitializeDiskMappings = \
  300. MagicMock(side_effect=err)
  301. with self.assertRaises(VMwareApiError) as excinfo:
  302. vsan.create_diskgroup(self.mock_si, self.mock_vsan_disk_mgmt_system,
  303. self.mock_host_ref, self.mock_cache_disk,
  304. [self.mock_cap_disk1, self.mock_cap_disk2])
  305. self.assertEqual(excinfo.exception.strerror,
  306. 'Not enough permissions. Required privilege: '
  307. 'Fake privilege')
  308. def test_initialize_disk_mapping_raise_vim_fault(self):
  309. err = vim.fault.VimFault()
  310. err.msg = 'vim_fault'
  311. self.mock_vsan_disk_mgmt_system.InitializeDiskMappings = \
  312. MagicMock(side_effect=err)
  313. with self.assertRaises(VMwareApiError) as excinfo:
  314. vsan.create_diskgroup(self.mock_si, self.mock_vsan_disk_mgmt_system,
  315. self.mock_host_ref, self.mock_cache_disk,
  316. [self.mock_cap_disk1, self.mock_cap_disk2])
  317. self.assertEqual(excinfo.exception.strerror, 'vim_fault')
  318. def test_initialize_disk_mapping_raise_method_not_found(self):
  319. err = vmodl.fault.MethodNotFound()
  320. err.method = 'fake_method'
  321. self.mock_vsan_disk_mgmt_system.InitializeDiskMappings = \
  322. MagicMock(side_effect=err)
  323. with self.assertRaises(VMwareRuntimeError) as excinfo:
  324. vsan.create_diskgroup(self.mock_si, self.mock_vsan_disk_mgmt_system,
  325. self.mock_host_ref, self.mock_cache_disk,
  326. [self.mock_cap_disk1, self.mock_cap_disk2])
  327. self.assertEqual(excinfo.exception.strerror,
  328. 'Method \'fake_method\' not found')
  329. def test_initialize_disk_mapping_raise_runtime_fault(self):
  330. err = vmodl.RuntimeFault()
  331. err.msg = 'runtime_fault'
  332. self.mock_vsan_disk_mgmt_system.InitializeDiskMappings = \
  333. MagicMock(side_effect=err)
  334. with self.assertRaises(VMwareRuntimeError) as excinfo:
  335. vsan.create_diskgroup(self.mock_si, self.mock_vsan_disk_mgmt_system,
  336. self.mock_host_ref, self.mock_cache_disk,
  337. [self.mock_cap_disk1, self.mock_cap_disk2])
  338. self.assertEqual(excinfo.exception.strerror, 'runtime_fault')
  339. def test__wait_for_tasks(self):
  340. mock___wait_for_tasks = MagicMock()
  341. with patch('salt.utils.vsan._wait_for_tasks',
  342. mock___wait_for_tasks):
  343. vsan.create_diskgroup(self.mock_si, self.mock_vsan_disk_mgmt_system,
  344. self.mock_host_ref, self.mock_cache_disk,
  345. [self.mock_cap_disk1, self.mock_cap_disk2])
  346. mock___wait_for_tasks.assert_called_once_with(
  347. [self.mock_task], self.mock_si)
  348. def test_result(self):
  349. res = vsan.create_diskgroup(self.mock_si,
  350. self.mock_vsan_disk_mgmt_system,
  351. self.mock_host_ref, self.mock_cache_disk,
  352. [self.mock_cap_disk1, self.mock_cap_disk2])
  353. self.assertTrue(res)
  354. @skipIf(NO_MOCK, NO_MOCK_REASON)
  355. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  356. @skipIf(not HAS_PYVSAN, 'The \'vsan\' ext library is missing')
  357. class AddCapacityToDiskGroupTestCase(TestCase):
  358. '''Tests for salt.utils.vsan.add_capacity_to_diskgroup'''
  359. def setUp(self):
  360. self.mock_si = MagicMock()
  361. self.mock_task = MagicMock()
  362. self.mock_initialise_disk_mapping = \
  363. MagicMock(return_value=self.mock_task)
  364. self.mock_vsan_disk_mgmt_system = MagicMock(
  365. InitializeDiskMappings=self.mock_initialise_disk_mapping)
  366. self.mock_host_ref = MagicMock()
  367. self.mock_cache_disk = MagicMock()
  368. self.mock_diskgroup = MagicMock(ssd=self.mock_cache_disk)
  369. self.mock_cap_disk1 = MagicMock()
  370. self.mock_cap_disk2 = MagicMock()
  371. self.mock_spec = MagicMock()
  372. patches = (
  373. ('salt.utils.vmware.get_managed_object_name',
  374. MagicMock(return_value='fake_hostname')),
  375. ('salt.utils.vsan.vim.VimVsanHostDiskMappingCreationSpec',
  376. MagicMock(return_value=self.mock_spec)),
  377. ('salt.utils.vsan._wait_for_tasks', MagicMock()))
  378. for mod, mock in patches:
  379. patcher = patch(mod, mock)
  380. patcher.start()
  381. self.addCleanup(patcher.stop)
  382. def test_get_hostname(self):
  383. mock_get_managed_object_name = MagicMock(return_value='fake_hostname')
  384. with patch('salt.utils.vmware.get_managed_object_name',
  385. mock_get_managed_object_name):
  386. vsan.add_capacity_to_diskgroup(
  387. self.mock_si, self.mock_vsan_disk_mgmt_system,
  388. self.mock_host_ref, self.mock_diskgroup,
  389. [self.mock_cap_disk1, self.mock_cap_disk2])
  390. mock_get_managed_object_name.assert_called_once_with(
  391. self.mock_host_ref)
  392. def test_vsan_spec_all_flash(self):
  393. self.mock_cap_disk1.ssd = True
  394. vsan.add_capacity_to_diskgroup(
  395. self.mock_si, self.mock_vsan_disk_mgmt_system,
  396. self.mock_host_ref, self.mock_diskgroup,
  397. [self.mock_cap_disk1, self.mock_cap_disk2])
  398. self.assertEqual(self.mock_spec.capacityDisks, [self.mock_cap_disk1,
  399. self.mock_cap_disk2])
  400. self.assertEqual(self.mock_spec.cacheDisks, [self.mock_cache_disk])
  401. self.assertEqual(self.mock_spec.creationType, 'allFlash')
  402. self.assertEqual(self.mock_spec.host, self.mock_host_ref)
  403. def test_vsan_spec_hybrid(self):
  404. self.mock_cap_disk1.ssd = False
  405. vsan.add_capacity_to_diskgroup(
  406. self.mock_si, self.mock_vsan_disk_mgmt_system,
  407. self.mock_host_ref, self.mock_diskgroup,
  408. [self.mock_cap_disk1, self.mock_cap_disk2])
  409. self.mock_cap_disk1.ssd = False
  410. self.assertEqual(self.mock_spec.creationType, 'hybrid')
  411. def test_initialize_disk_mapping(self):
  412. vsan.add_capacity_to_diskgroup(
  413. self.mock_si, self.mock_vsan_disk_mgmt_system,
  414. self.mock_host_ref, self.mock_diskgroup,
  415. [self.mock_cap_disk1, self.mock_cap_disk2])
  416. self.mock_initialise_disk_mapping.assert_called_once_with(
  417. self.mock_spec)
  418. def test_initialize_disk_mapping_raise_no_permission(self):
  419. err = vim.fault.NoPermission()
  420. err.privilegeId = 'Fake privilege'
  421. self.mock_vsan_disk_mgmt_system.InitializeDiskMappings = \
  422. MagicMock(side_effect=err)
  423. with self.assertRaises(VMwareApiError) as excinfo:
  424. vsan.add_capacity_to_diskgroup(
  425. self.mock_si, self.mock_vsan_disk_mgmt_system,
  426. self.mock_host_ref, self.mock_diskgroup,
  427. [self.mock_cap_disk1, self.mock_cap_disk2])
  428. self.assertEqual(excinfo.exception.strerror,
  429. 'Not enough permissions. Required privilege: '
  430. 'Fake privilege')
  431. def test_initialize_disk_mapping_raise_vim_fault(self):
  432. err = vim.fault.VimFault()
  433. err.msg = 'vim_fault'
  434. self.mock_vsan_disk_mgmt_system.InitializeDiskMappings = \
  435. MagicMock(side_effect=err)
  436. with self.assertRaises(VMwareApiError) as excinfo:
  437. vsan.add_capacity_to_diskgroup(
  438. self.mock_si, self.mock_vsan_disk_mgmt_system,
  439. self.mock_host_ref, self.mock_diskgroup,
  440. [self.mock_cap_disk1, self.mock_cap_disk2])
  441. self.assertEqual(excinfo.exception.strerror, 'vim_fault')
  442. def test_initialize_disk_mapping_raise_method_not_found(self):
  443. err = vmodl.fault.MethodNotFound()
  444. err.method = 'fake_method'
  445. self.mock_vsan_disk_mgmt_system.InitializeDiskMappings = \
  446. MagicMock(side_effect=err)
  447. with self.assertRaises(VMwareRuntimeError) as excinfo:
  448. vsan.add_capacity_to_diskgroup(
  449. self.mock_si, self.mock_vsan_disk_mgmt_system,
  450. self.mock_host_ref, self.mock_diskgroup,
  451. [self.mock_cap_disk1, self.mock_cap_disk2])
  452. self.assertEqual(excinfo.exception.strerror,
  453. 'Method \'fake_method\' not found')
  454. def test_initialize_disk_mapping_raise_runtime_fault(self):
  455. err = vmodl.RuntimeFault()
  456. err.msg = 'runtime_fault'
  457. self.mock_vsan_disk_mgmt_system.InitializeDiskMappings = \
  458. MagicMock(side_effect=err)
  459. with self.assertRaises(VMwareRuntimeError) as excinfo:
  460. vsan.add_capacity_to_diskgroup(
  461. self.mock_si, self.mock_vsan_disk_mgmt_system,
  462. self.mock_host_ref, self.mock_diskgroup,
  463. [self.mock_cap_disk1, self.mock_cap_disk2])
  464. self.assertEqual(excinfo.exception.strerror, 'runtime_fault')
  465. def test__wait_for_tasks(self):
  466. mock___wait_for_tasks = MagicMock()
  467. with patch('salt.utils.vsan._wait_for_tasks',
  468. mock___wait_for_tasks):
  469. vsan.add_capacity_to_diskgroup(
  470. self.mock_si, self.mock_vsan_disk_mgmt_system,
  471. self.mock_host_ref, self.mock_diskgroup,
  472. [self.mock_cap_disk1, self.mock_cap_disk2])
  473. mock___wait_for_tasks.assert_called_once_with(
  474. [self.mock_task], self.mock_si)
  475. def test_result(self):
  476. res = vsan.add_capacity_to_diskgroup(
  477. self.mock_si, self.mock_vsan_disk_mgmt_system,
  478. self.mock_host_ref, self.mock_diskgroup,
  479. [self.mock_cap_disk1, self.mock_cap_disk2])
  480. self.assertTrue(res)
  481. @skipIf(NO_MOCK, NO_MOCK_REASON)
  482. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  483. @skipIf(not HAS_PYVSAN, 'The \'vsan\' ext library is missing')
  484. class RemoveCapacityFromDiskGroup(TestCase):
  485. '''Tests for salt.utils.vsan.remove_capacity_from_diskgroup'''
  486. def setUp(self):
  487. self.mock_si = MagicMock()
  488. self.mock_task = MagicMock()
  489. self.mock_remove_disk = \
  490. MagicMock(return_value=self.mock_task)
  491. self.mock_host_vsan_system = MagicMock(
  492. RemoveDisk_Task=self.mock_remove_disk)
  493. self.mock_host_ref = MagicMock()
  494. self.mock_cache_disk = MagicMock()
  495. self.mock_diskgroup = MagicMock(ssd=self.mock_cache_disk)
  496. self.mock_cap_disk1 = MagicMock()
  497. self.mock_cap_disk2 = MagicMock()
  498. self.mock_spec = MagicMock()
  499. patches = (
  500. ('salt.utils.vmware.get_managed_object_name',
  501. MagicMock(return_value='fake_hostname')),
  502. ('salt.utils.vsan.get_host_vsan_system',
  503. MagicMock(return_value=self.mock_host_vsan_system)),
  504. ('salt.utils.vsan.vim.HostMaintenanceSpec',
  505. MagicMock(return_value=self.mock_spec)),
  506. ('salt.utils.vsan.vim.VsanHostDecommissionMode', MagicMock()),
  507. ('salt.utils.vmware.wait_for_task', MagicMock()))
  508. for mod, mock in patches:
  509. patcher = patch(mod, mock)
  510. patcher.start()
  511. self.addCleanup(patcher.stop)
  512. def test_get_hostname(self):
  513. mock_get_managed_object_name = MagicMock(return_value='fake_hostname')
  514. with patch('salt.utils.vmware.get_managed_object_name',
  515. mock_get_managed_object_name):
  516. vsan.remove_capacity_from_diskgroup(
  517. self.mock_si, self.mock_host_ref, self.mock_diskgroup,
  518. [self.mock_cap_disk1, self.mock_cap_disk2])
  519. mock_get_managed_object_name.assert_called_once_with(
  520. self.mock_host_ref)
  521. def test_maintenance_mode_evacuate_all_data(self):
  522. vsan.remove_capacity_from_diskgroup(
  523. self.mock_si, self.mock_host_ref, self.mock_diskgroup,
  524. [self.mock_cap_disk1, self.mock_cap_disk2])
  525. self.assertEqual(self.mock_spec.vsanMode.objectAction,
  526. vim.VsanHostDecommissionModeObjectAction.evacuateAllData)
  527. def test_maintenance_mode_no_action(self):
  528. vsan.remove_capacity_from_diskgroup(
  529. self.mock_si, self.mock_host_ref, self.mock_diskgroup,
  530. [self.mock_cap_disk1, self.mock_cap_disk2],
  531. data_evacuation=False)
  532. self.assertEqual(self.mock_spec.vsanMode.objectAction,
  533. vim.VsanHostDecommissionModeObjectAction.noAction)
  534. def test_remove_disk(self):
  535. vsan.remove_capacity_from_diskgroup(
  536. self.mock_si, self.mock_host_ref, self.mock_diskgroup,
  537. [self.mock_cap_disk1, self.mock_cap_disk2])
  538. self.mock_remove_disk.assert_called_once_with(
  539. disk=[self.mock_cap_disk1, self.mock_cap_disk2],
  540. maintenanceSpec=self.mock_spec)
  541. def test_remove_disk_raise_no_permission(self):
  542. err = vim.fault.NoPermission()
  543. err.privilegeId = 'Fake privilege'
  544. self.mock_host_vsan_system.RemoveDisk_Task = \
  545. MagicMock(side_effect=err)
  546. with self.assertRaises(VMwareApiError) as excinfo:
  547. vsan.remove_capacity_from_diskgroup(
  548. self.mock_si, self.mock_host_ref, self.mock_diskgroup,
  549. [self.mock_cap_disk1, self.mock_cap_disk2])
  550. self.assertEqual(excinfo.exception.strerror,
  551. 'Not enough permissions. Required privilege: '
  552. 'Fake privilege')
  553. def test_remove_disk_raise_vim_fault(self):
  554. err = vim.fault.VimFault()
  555. err.msg = 'vim_fault'
  556. self.mock_host_vsan_system.RemoveDisk_Task = \
  557. MagicMock(side_effect=err)
  558. with self.assertRaises(VMwareApiError) as excinfo:
  559. vsan.remove_capacity_from_diskgroup(
  560. self.mock_si, self.mock_host_ref, self.mock_diskgroup,
  561. [self.mock_cap_disk1, self.mock_cap_disk2])
  562. self.assertEqual(excinfo.exception.strerror, 'vim_fault')
  563. def test_remove_disk_raise_runtime_fault(self):
  564. err = vmodl.RuntimeFault()
  565. err.msg = 'runtime_fault'
  566. self.mock_host_vsan_system.RemoveDisk_Task = \
  567. MagicMock(side_effect=err)
  568. with self.assertRaises(VMwareRuntimeError) as excinfo:
  569. vsan.remove_capacity_from_diskgroup(
  570. self.mock_si, self.mock_host_ref, self.mock_diskgroup,
  571. [self.mock_cap_disk1, self.mock_cap_disk2])
  572. self.assertEqual(excinfo.exception.strerror, 'runtime_fault')
  573. def test_wait_for_tasks(self):
  574. mock_wait_for_task = MagicMock()
  575. with patch('salt.utils.vmware.wait_for_task',
  576. mock_wait_for_task):
  577. vsan.remove_capacity_from_diskgroup(
  578. self.mock_si, self.mock_host_ref, self.mock_diskgroup,
  579. [self.mock_cap_disk1, self.mock_cap_disk2])
  580. mock_wait_for_task.assert_called_once_with(
  581. self.mock_task, 'fake_hostname', 'remove_capacity')
  582. def test_result(self):
  583. res = vsan.remove_capacity_from_diskgroup(
  584. self.mock_si, self.mock_host_ref, self.mock_diskgroup,
  585. [self.mock_cap_disk1, self.mock_cap_disk2])
  586. self.assertTrue(res)
  587. @skipIf(NO_MOCK, NO_MOCK_REASON)
  588. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  589. @skipIf(not HAS_PYVSAN, 'The \'vsan\' ext library is missing')
  590. class RemoveDiskgroup(TestCase):
  591. '''Tests for salt.utils.vsan.remove_diskgroup'''
  592. def setUp(self):
  593. self.mock_si = MagicMock()
  594. self.mock_task = MagicMock()
  595. self.mock_remove_disk_mapping = \
  596. MagicMock(return_value=self.mock_task)
  597. self.mock_host_vsan_system = MagicMock(
  598. RemoveDiskMapping_Task=self.mock_remove_disk_mapping)
  599. self.mock_host_ref = MagicMock()
  600. self.mock_cache_disk = MagicMock()
  601. self.mock_diskgroup = MagicMock(ssd=self.mock_cache_disk)
  602. self.mock_cap_disk1 = MagicMock()
  603. self.mock_cap_disk2 = MagicMock()
  604. self.mock_spec = MagicMock()
  605. patches = (
  606. ('salt.utils.vmware.get_managed_object_name',
  607. MagicMock(return_value='fake_hostname')),
  608. ('salt.utils.vsan.get_host_vsan_system',
  609. MagicMock(return_value=self.mock_host_vsan_system)),
  610. ('salt.utils.vsan.vim.HostMaintenanceSpec',
  611. MagicMock(return_value=self.mock_spec)),
  612. ('salt.utils.vsan.vim.VsanHostDecommissionMode', MagicMock()),
  613. ('salt.utils.vmware.wait_for_task', MagicMock()))
  614. for mod, mock in patches:
  615. patcher = patch(mod, mock)
  616. patcher.start()
  617. self.addCleanup(patcher.stop)
  618. def test_get_hostname(self):
  619. mock_get_managed_object_name = MagicMock(return_value='fake_hostname')
  620. with patch('salt.utils.vmware.get_managed_object_name',
  621. mock_get_managed_object_name):
  622. vsan.remove_diskgroup(
  623. self.mock_si, self.mock_host_ref, self.mock_diskgroup)
  624. mock_get_managed_object_name.assert_called_once_with(
  625. self.mock_host_ref)
  626. def test_maintenance_mode_evacuate_all_data(self):
  627. vsan.remove_diskgroup(
  628. self.mock_si, self.mock_host_ref, self.mock_diskgroup)
  629. vsan.remove_capacity_from_diskgroup(
  630. self.mock_si, self.mock_host_ref, self.mock_diskgroup,
  631. [self.mock_cap_disk1, self.mock_cap_disk2])
  632. self.assertEqual(self.mock_spec.vsanMode.objectAction,
  633. vim.VsanHostDecommissionModeObjectAction.evacuateAllData)
  634. def test_maintenance_mode_no_action(self):
  635. vsan.remove_diskgroup(
  636. self.mock_si, self.mock_host_ref, self.mock_diskgroup)
  637. vsan.remove_capacity_from_diskgroup(
  638. self.mock_si, self.mock_host_ref, self.mock_diskgroup,
  639. [self.mock_cap_disk1, self.mock_cap_disk2],
  640. data_evacuation=False)
  641. self.assertEqual(self.mock_spec.vsanMode.objectAction,
  642. vim.VsanHostDecommissionModeObjectAction.noAction)
  643. def test_remove_disk_mapping(self):
  644. vsan.remove_diskgroup(
  645. self.mock_si, self.mock_host_ref, self.mock_diskgroup)
  646. vsan.remove_capacity_from_diskgroup(
  647. self.mock_si, self.mock_host_ref, self.mock_diskgroup,
  648. [self.mock_cap_disk1, self.mock_cap_disk2])
  649. self.mock_remove_disk_mapping.assert_called_once_with(
  650. mapping=[self.mock_diskgroup],
  651. maintenanceSpec=self.mock_spec)
  652. def test_remove_disk_mapping_raise_no_permission(self):
  653. vsan.remove_diskgroup(
  654. self.mock_si, self.mock_host_ref, self.mock_diskgroup)
  655. err = vim.fault.NoPermission()
  656. err.privilegeId = 'Fake privilege'
  657. self.mock_host_vsan_system.RemoveDiskMapping_Task = \
  658. MagicMock(side_effect=err)
  659. with self.assertRaises(VMwareApiError) as excinfo:
  660. vsan.remove_diskgroup(
  661. self.mock_si, self.mock_host_ref, self.mock_diskgroup)
  662. self.assertEqual(excinfo.exception.strerror,
  663. 'Not enough permissions. Required privilege: '
  664. 'Fake privilege')
  665. def test_remove_disk_mapping_raise_vim_fault(self):
  666. err = vim.fault.VimFault()
  667. err.msg = 'vim_fault'
  668. self.mock_host_vsan_system.RemoveDiskMapping_Task = \
  669. MagicMock(side_effect=err)
  670. with self.assertRaises(VMwareApiError) as excinfo:
  671. vsan.remove_diskgroup(
  672. self.mock_si, self.mock_host_ref, self.mock_diskgroup)
  673. self.assertEqual(excinfo.exception.strerror, 'vim_fault')
  674. def test_remove_disk_mapping_raise_runtime_fault(self):
  675. err = vmodl.RuntimeFault()
  676. err.msg = 'runtime_fault'
  677. self.mock_host_vsan_system.RemoveDiskMapping_Task = \
  678. MagicMock(side_effect=err)
  679. with self.assertRaises(VMwareRuntimeError) as excinfo:
  680. vsan.remove_diskgroup(
  681. self.mock_si, self.mock_host_ref, self.mock_diskgroup)
  682. self.assertEqual(excinfo.exception.strerror, 'runtime_fault')
  683. def test_wait_for_tasks(self):
  684. mock_wait_for_task = MagicMock()
  685. with patch('salt.utils.vmware.wait_for_task',
  686. mock_wait_for_task):
  687. vsan.remove_diskgroup(
  688. self.mock_si, self.mock_host_ref, self.mock_diskgroup)
  689. mock_wait_for_task.assert_called_once_with(
  690. self.mock_task, 'fake_hostname', 'remove_diskgroup')
  691. def test_result(self):
  692. res = vsan.remove_diskgroup(
  693. self.mock_si, self.mock_host_ref, self.mock_diskgroup)
  694. self.assertTrue(res)
  695. @skipIf(NO_MOCK, NO_MOCK_REASON)
  696. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  697. @skipIf(not HAS_PYVSAN, 'The \'vsan\' ext library is missing')
  698. class GetClusterVsanInfoTestCase(TestCase, LoaderModuleMockMixin):
  699. '''Tests for salt.utils.vsan.get_cluster_vsan_info'''
  700. def setup_loader_modules(self):
  701. return {vsan: {
  702. '__virtual__': MagicMock(return_value='vsan')}}
  703. def setUp(self):
  704. self.mock_cl_ref = MagicMock()
  705. self.mock_si = MagicMock()
  706. patches = (
  707. ('salt.utils.vmware.get_managed_object_name', MagicMock()),
  708. ('salt.utils.vmware.get_service_instance_from_managed_object',
  709. MagicMock(return_value=self.mock_si)),
  710. ('salt.utils.vsan.get_vsan_cluster_config_system', MagicMock()))
  711. for mod, mock in patches:
  712. patcher = patch(mod, mock)
  713. patcher.start()
  714. self.addCleanup(patcher.stop)
  715. def tearDown(self):
  716. for attr in ('mock_si', 'mock_cl_ref'):
  717. delattr(self, attr)
  718. def test_get_managed_object_name_call(self):
  719. mock_get_managed_object_name = MagicMock()
  720. with patch('salt.utils.vmware.get_managed_object_name',
  721. mock_get_managed_object_name):
  722. vsan.get_cluster_vsan_info(self.mock_cl_ref)
  723. mock_get_managed_object_name.assert_called_once_with(self.mock_cl_ref)
  724. def test_get_vsan_cluster_config_system_call(self):
  725. mock_get_vsan_cl_syst = MagicMock()
  726. with patch('salt.utils.vsan.get_vsan_cluster_config_system',
  727. mock_get_vsan_cl_syst):
  728. vsan.get_cluster_vsan_info(self.mock_cl_ref)
  729. mock_get_vsan_cl_syst.assert_called_once_with(self.mock_si)
  730. def test_VsanClusterGetConfig_call(self):
  731. mock_vsan_sys = MagicMock()
  732. with patch('salt.utils.vsan.get_vsan_cluster_config_system',
  733. MagicMock(return_value=mock_vsan_sys)):
  734. vsan.get_cluster_vsan_info(self.mock_cl_ref)
  735. mock_vsan_sys.VsanClusterGetConfig.assert_called_once_with(
  736. self.mock_cl_ref)
  737. def test_VsanClusterGetConfig_raises_no_permission(self):
  738. exc = vim.fault.NoPermission()
  739. exc.privilegeId = 'Fake privilege'
  740. with patch('salt.utils.vsan.get_vsan_cluster_config_system',
  741. MagicMock(return_value=MagicMock(
  742. VsanClusterGetConfig=MagicMock(side_effect=exc)))):
  743. with self.assertRaises(VMwareApiError) as excinfo:
  744. vsan.get_cluster_vsan_info(self.mock_cl_ref)
  745. self.assertEqual(excinfo.exception.strerror,
  746. 'Not enough permissions. Required privilege: '
  747. 'Fake privilege')
  748. def test_VsanClusterGetConfig_raises_vim_fault(self):
  749. exc = vim.fault.VimFault()
  750. exc.msg = 'VimFault msg'
  751. with patch('salt.utils.vsan.get_vsan_cluster_config_system',
  752. MagicMock(return_value=MagicMock(
  753. VsanClusterGetConfig=MagicMock(side_effect=exc)))):
  754. with self.assertRaises(VMwareApiError) as excinfo:
  755. vsan.get_cluster_vsan_info(self.mock_cl_ref)
  756. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  757. def test_VsanClusterGetConfig_raises_runtime_fault(self):
  758. exc = vmodl.RuntimeFault()
  759. exc.msg = 'RuntimeFault msg'
  760. with patch('salt.utils.vsan.get_vsan_cluster_config_system',
  761. MagicMock(return_value=MagicMock(
  762. VsanClusterGetConfig=MagicMock(side_effect=exc)))):
  763. with self.assertRaises(VMwareRuntimeError) as excinfo:
  764. vsan.get_cluster_vsan_info(self.mock_cl_ref)
  765. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  766. @skipIf(NO_MOCK, NO_MOCK_REASON)
  767. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  768. @skipIf(not HAS_PYVSAN, 'The \'vsan\' ext library is missing')
  769. class ReconfigureClusterVsanTestCase(TestCase):
  770. '''Tests for salt.utils.vsan.reconfigure_cluster_vsan'''
  771. def setUp(self):
  772. self.mock_si = MagicMock()
  773. self.mock_task = MagicMock()
  774. self.mock_cl_reconf = MagicMock(return_value=self.mock_task)
  775. self.mock_get_vsan_conf_sys = MagicMock(
  776. return_value=MagicMock(VsanClusterReconfig=self.mock_cl_reconf))
  777. self.mock_cl_ref = MagicMock()
  778. self.mock_cl_vsan_spec = MagicMock()
  779. patches = (
  780. ('salt.utils.vmware.get_managed_object_name', MagicMock()),
  781. ('salt.utils.vmware.get_service_instance_from_managed_object',
  782. MagicMock(return_value=self.mock_si)),
  783. ('salt.utils.vsan.get_vsan_cluster_config_system',
  784. self.mock_get_vsan_conf_sys),
  785. ('salt.utils.vsan._wait_for_tasks', MagicMock()))
  786. for mod, mock in patches:
  787. patcher = patch(mod, mock)
  788. patcher.start()
  789. self.addCleanup(patcher.stop)
  790. def tearDown(self):
  791. for attr in ('mock_si', 'mock_cl_reconf', 'mock_get_vsan_conf_sys',
  792. 'mock_cl_ref', 'mock_cl_vsan_spec', 'mock_task'):
  793. delattr(self, attr)
  794. def test_get_cluster_name_call(self):
  795. get_managed_object_name_mock = MagicMock()
  796. with patch('salt.utils.vmware.get_managed_object_name',
  797. get_managed_object_name_mock):
  798. vsan.reconfigure_cluster_vsan(self.mock_cl_ref,
  799. self.mock_cl_vsan_spec)
  800. get_managed_object_name_mock.assert_called_once_with(
  801. self.mock_cl_ref)
  802. def test_get_service_instance_call(self):
  803. get_service_instance_from_managed_object_mock = MagicMock()
  804. with patch(
  805. 'salt.utils.vmware.get_service_instance_from_managed_object',
  806. get_service_instance_from_managed_object_mock):
  807. vsan.reconfigure_cluster_vsan(self.mock_cl_ref,
  808. self.mock_cl_vsan_spec)
  809. get_service_instance_from_managed_object_mock.assert_called_once_with(
  810. self.mock_cl_ref)
  811. def test_get_vsan_cluster_config_system_call(self):
  812. vsan.reconfigure_cluster_vsan(self.mock_cl_ref,
  813. self.mock_cl_vsan_spec)
  814. self.mock_get_vsan_conf_sys.assert_called_once_with(self.mock_si)
  815. def test_cluster_reconfig_call(self):
  816. vsan.reconfigure_cluster_vsan(self.mock_cl_ref,
  817. self.mock_cl_vsan_spec)
  818. self.mock_cl_reconf.assert_called_once_with(
  819. self.mock_cl_ref, self.mock_cl_vsan_spec)
  820. def test_cluster_reconfig_raises_no_permission(self):
  821. exc = vim.fault.NoPermission()
  822. exc.privilegeId = 'Fake privilege'
  823. with patch('salt.utils.vsan.get_vsan_cluster_config_system',
  824. MagicMock(return_value=MagicMock(
  825. VsanClusterReconfig=MagicMock(side_effect=exc)))):
  826. with self.assertRaises(VMwareApiError) as excinfo:
  827. vsan.reconfigure_cluster_vsan(self.mock_cl_ref,
  828. self.mock_cl_vsan_spec)
  829. self.assertEqual(excinfo.exception.strerror,
  830. 'Not enough permissions. Required privilege: '
  831. 'Fake privilege')
  832. def test_cluster_reconfig_raises_vim_fault(self):
  833. exc = vim.fault.VimFault()
  834. exc.msg = 'VimFault msg'
  835. with patch('salt.utils.vsan.get_vsan_cluster_config_system',
  836. MagicMock(return_value=MagicMock(
  837. VsanClusterReconfig=MagicMock(side_effect=exc)))):
  838. with self.assertRaises(VMwareApiError) as excinfo:
  839. vsan.reconfigure_cluster_vsan(self.mock_cl_ref,
  840. self.mock_cl_vsan_spec)
  841. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  842. def test_cluster_reconfig_raises_vmodl_runtime_error(self):
  843. exc = vmodl.RuntimeFault()
  844. exc.msg = 'VimRuntime msg'
  845. with patch('salt.utils.vsan.get_vsan_cluster_config_system',
  846. MagicMock(return_value=MagicMock(
  847. VsanClusterReconfig=MagicMock(side_effect=exc)))):
  848. with self.assertRaises(VMwareRuntimeError) as excinfo:
  849. vsan.reconfigure_cluster_vsan(self.mock_cl_ref,
  850. self.mock_cl_vsan_spec)
  851. self.assertEqual(excinfo.exception.strerror, 'VimRuntime msg')
  852. def test__wait_for_tasks_call(self):
  853. mock_wait_for_tasks = MagicMock()
  854. with patch('salt.utils.vsan._wait_for_tasks', mock_wait_for_tasks):
  855. vsan.reconfigure_cluster_vsan(self.mock_cl_ref,
  856. self.mock_cl_vsan_spec)
  857. mock_wait_for_tasks.assert_called_once_with([self.mock_task],
  858. self.mock_si)
  859. @skipIf(NO_MOCK, NO_MOCK_REASON)
  860. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  861. @skipIf(not HAS_PYVSAN, 'The \'vsan\' ext library is missing')
  862. class _WaitForTasks(TestCase, LoaderModuleMockMixin):
  863. '''Tests for salt.utils.vsan._wait_for_tasks'''
  864. def setup_loader_modules(self):
  865. return {vsan: {
  866. '__virtual__': MagicMock(return_value='vsan')}}
  867. def setUp(self):
  868. self.mock_si = MagicMock()
  869. self.mock_tasks = MagicMock()
  870. patches = (('salt.utils.vsan.vsanapiutils.WaitForTasks', MagicMock()),)
  871. for mod, mock in patches:
  872. patcher = patch(mod, mock)
  873. patcher.start()
  874. self.addCleanup(patcher.stop)
  875. def tearDown(self):
  876. for attr in ('mock_si', 'mock_tasks'):
  877. delattr(self, attr)
  878. def test_wait_for_tasks_call(self):
  879. mock_wait_for_tasks = MagicMock()
  880. with patch('salt.utils.vsan.vsanapiutils.WaitForTasks',
  881. mock_wait_for_tasks):
  882. vsan._wait_for_tasks(self.mock_tasks, self.mock_si)
  883. mock_wait_for_tasks.assert_called_once_with(self.mock_tasks,
  884. self.mock_si)
  885. def test_wait_for_tasks_raises_no_permission(self):
  886. exc = vim.fault.NoPermission()
  887. exc.privilegeId = 'Fake privilege'
  888. with patch('salt.utils.vsan.vsanapiutils.WaitForTasks',
  889. MagicMock(side_effect=exc)):
  890. with self.assertRaises(VMwareApiError) as excinfo:
  891. vsan._wait_for_tasks(self.mock_tasks, self.mock_si)
  892. self.assertEqual(excinfo.exception.strerror,
  893. 'Not enough permissions. Required privilege: '
  894. 'Fake privilege')
  895. def test_wait_for_tasks_raises_vim_fault(self):
  896. exc = vim.fault.VimFault()
  897. exc.msg = 'VimFault msg'
  898. with patch('salt.utils.vsan.vsanapiutils.WaitForTasks',
  899. MagicMock(side_effect=exc)):
  900. with self.assertRaises(VMwareApiError) as excinfo:
  901. vsan._wait_for_tasks(self.mock_tasks, self.mock_si)
  902. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  903. def test_wait_for_tasks_raises_vmodl_runtime_error(self):
  904. exc = vmodl.RuntimeFault()
  905. exc.msg = 'VimRuntime msg'
  906. with patch('salt.utils.vsan.vsanapiutils.WaitForTasks',
  907. MagicMock(side_effect=exc)):
  908. with self.assertRaises(VMwareRuntimeError) as excinfo:
  909. vsan._wait_for_tasks(self.mock_tasks, self.mock_si)
  910. self.assertEqual(excinfo.exception.strerror, 'VimRuntime msg')