1
0

test_vsan.py 46 KB

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