test_vsan.py 47 KB

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