1
0

test_vsphere.py 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. :codeauthor: Alexandru Bleotu <alexandru.bleotu@morganstanley.com>
  5. Tests for functions in salt.modules.vsphere
  6. '''
  7. # Import Python Libs
  8. from __future__ import absolute_import, print_function, unicode_literals
  9. # Import Salt Libs
  10. from salt.ext.six import text_type
  11. import salt.modules.vsphere as vsphere
  12. from salt.exceptions import (
  13. CommandExecutionError,
  14. VMwareSaltError,
  15. ArgumentValueError,
  16. VMwareObjectRetrievalError
  17. )
  18. # Import Salt Testing Libs
  19. from tests.support.mixins import LoaderModuleMockMixin
  20. from tests.support.unit import TestCase, skipIf
  21. from tests.support.mock import (
  22. MagicMock,
  23. patch,
  24. call
  25. )
  26. # Import Third Party Libs
  27. try:
  28. from pyVmomi import vim, vmodl # pylint: disable=unused-import
  29. HAS_PYVMOMI = True
  30. except ImportError:
  31. HAS_PYVMOMI = False
  32. # Globals
  33. HOST = '1.2.3.4'
  34. USER = 'root'
  35. PASSWORD = 'SuperSecret!'
  36. ERROR = 'Some Testing Error Message'
  37. class VsphereTestCase(TestCase, LoaderModuleMockMixin):
  38. '''
  39. Unit TestCase for the salt.modules.vsphere module.
  40. '''
  41. def setup_loader_modules(self):
  42. return {vsphere: {'__virtual__': MagicMock(return_value='vsphere')}}
  43. # Tests for get_coredump_network_config function
  44. def test_get_coredump_network_config_esxi_hosts_not_list(self):
  45. '''
  46. Tests CommandExecutionError is raised when esxi_hosts is provided,
  47. but is not a list.
  48. '''
  49. self.assertRaises(CommandExecutionError,
  50. vsphere.get_coredump_network_config,
  51. HOST, USER, PASSWORD, esxi_hosts='foo')
  52. def test_get_coredump_network_config_host_list_bad_retcode(self):
  53. '''
  54. Tests error message returned with list of esxi_hosts.
  55. '''
  56. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})):
  57. host_1 = 'host_1.foo.com'
  58. self.assertEqual({host_1: {'Error': ERROR}},
  59. vsphere.get_coredump_network_config(HOST, USER, PASSWORD, esxi_hosts=[host_1]))
  60. def test_get_coredump_network_config_host_list_success(self):
  61. '''
  62. Tests successful function return when an esxi_host is provided.
  63. '''
  64. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})):
  65. with patch('salt.modules.vsphere._format_coredump_stdout', MagicMock(return_value={})):
  66. host_1 = 'host_1.foo.com'
  67. self.assertEqual({host_1: {'Coredump Config': {}}},
  68. vsphere.get_coredump_network_config(HOST, USER, PASSWORD, esxi_hosts=[host_1]))
  69. def test_get_coredump_network_config_bad_retcode(self):
  70. '''
  71. Tests error message given for a single ESXi host.
  72. '''
  73. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})):
  74. self.assertEqual({HOST: {'Error': ERROR}},
  75. vsphere.get_coredump_network_config(HOST, USER, PASSWORD))
  76. def test_get_coredump_network_config_success(self):
  77. '''
  78. Tests successful function return for a single ESXi host.
  79. '''
  80. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})):
  81. with patch('salt.modules.vsphere._format_coredump_stdout', MagicMock(return_value={})):
  82. self.assertEqual({HOST: {'Coredump Config': {}}},
  83. vsphere.get_coredump_network_config(HOST, USER, PASSWORD))
  84. # Tests for coredump_network_enable function
  85. def test_coredump_network_enable_esxi_hosts_not_list(self):
  86. '''
  87. Tests CommandExecutionError is raised when esxi_hosts is provided,
  88. but is not a list.
  89. '''
  90. self.assertRaises(CommandExecutionError,
  91. vsphere.coredump_network_enable,
  92. HOST, USER, PASSWORD, True, esxi_hosts='foo')
  93. def test_coredump_network_enable_host_list_bad_retcode(self):
  94. '''
  95. Tests error message returned with list of esxi_hosts.
  96. '''
  97. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})):
  98. host_1 = 'host_1.foo.com'
  99. self.assertEqual({host_1: {'Error': ERROR}},
  100. vsphere.coredump_network_enable(HOST, USER, PASSWORD, True, esxi_hosts=[host_1]))
  101. def test_coredump_network_enable_host_list_success(self):
  102. '''
  103. Tests successful function return when an esxi_host is provided.
  104. '''
  105. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})):
  106. with patch('salt.modules.vsphere._format_coredump_stdout', MagicMock(return_value={})):
  107. enabled = True
  108. host_1 = 'host_1.foo.com'
  109. self.assertEqual({host_1: {'Coredump Enabled': enabled}},
  110. vsphere.coredump_network_enable(HOST, USER, PASSWORD, enabled, esxi_hosts=[host_1]))
  111. def test_coredump_network_enable_bad_retcode(self):
  112. '''
  113. Tests error message given for a single ESXi host.
  114. '''
  115. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})):
  116. self.assertEqual({HOST: {'Error': ERROR}},
  117. vsphere.coredump_network_enable(HOST, USER, PASSWORD, True))
  118. def test_coredump_network_enable_success(self):
  119. '''
  120. Tests successful function return for a single ESXi host.
  121. '''
  122. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})):
  123. with patch('salt.modules.vsphere._format_coredump_stdout', MagicMock(return_value={})):
  124. enabled = True
  125. self.assertEqual({HOST: {'Coredump Enabled': enabled}},
  126. vsphere.coredump_network_enable(HOST, USER, PASSWORD, enabled))
  127. # Tests for set_coredump_network_config function
  128. def test_set_coredump_network_config_esxi_hosts_not_list(self):
  129. '''
  130. Tests CommandExecutionError is raised when esxi_hosts is provided,
  131. but is not a list.
  132. '''
  133. self.assertRaises(CommandExecutionError,
  134. vsphere.set_coredump_network_config,
  135. HOST, USER, PASSWORD, 'loghost', 'foo', esxi_hosts='bar')
  136. def test_set_coredump_network_config_host_list_bad_retcode(self):
  137. '''
  138. Tests error message returned with list of esxi_hosts.
  139. '''
  140. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1})):
  141. host_1 = 'host_1.foo.com'
  142. self.assertEqual({host_1: {'retcode': 1, 'success': False}},
  143. vsphere.set_coredump_network_config(HOST,
  144. USER,
  145. PASSWORD,
  146. 'dump-ip.test.com',
  147. esxi_hosts=[host_1]))
  148. def test_set_coredump_network_config_host_list_success(self):
  149. '''
  150. Tests successful function return when an esxi_host is provided.
  151. '''
  152. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0})):
  153. host_1 = 'host_1.foo.com'
  154. self.assertEqual({host_1: {'retcode': 0, 'success': True}},
  155. vsphere.set_coredump_network_config(HOST,
  156. USER,
  157. PASSWORD,
  158. 'dump-ip.test.com',
  159. esxi_hosts=[host_1]))
  160. def test_set_coredump_network_config_bad_retcode(self):
  161. '''
  162. Tests error message given for a single ESXi host.
  163. '''
  164. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1})):
  165. self.assertEqual({HOST: {'retcode': 1, 'success': False}},
  166. vsphere.set_coredump_network_config(HOST,
  167. USER,
  168. PASSWORD,
  169. 'dump-ip.test.com'))
  170. def test_set_coredump_network_config_success(self):
  171. '''
  172. Tests successful function return for a single ESXi host.
  173. '''
  174. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0})):
  175. self.assertEqual({HOST: {'retcode': 0, 'success': True}},
  176. vsphere.set_coredump_network_config(HOST,
  177. USER,
  178. PASSWORD,
  179. 'dump-ip.test.com'))
  180. # Tests for get_firewall_status function
  181. def test_get_firewall_status_esxi_hosts_not_list(self):
  182. '''
  183. Tests CommandExecutionError is raised when esxi_hosts is provided,
  184. but is not a list.
  185. '''
  186. self.assertRaises(CommandExecutionError,
  187. vsphere.get_firewall_status,
  188. HOST, USER, PASSWORD, esxi_hosts='foo')
  189. def test_get_firewall_status_host_list_bad_retcode(self):
  190. '''
  191. Tests error message returned with list of esxi_hosts.
  192. '''
  193. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})):
  194. host_1 = 'host_1.foo.com'
  195. self.assertEqual({host_1: {'success': False, 'Error': ERROR, 'rulesets': None}},
  196. vsphere.get_firewall_status(HOST, USER, PASSWORD, esxi_hosts=[host_1]))
  197. def test_get_firewall_status_host_list_success(self):
  198. '''
  199. Tests successful function return when an esxi_host is provided.
  200. '''
  201. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})):
  202. host_1 = 'host_1.foo.com'
  203. self.assertEqual({host_1: {'rulesets': {}, 'success': True}},
  204. vsphere.get_firewall_status(HOST, USER, PASSWORD, esxi_hosts=[host_1]))
  205. def test_get_firewall_status_bad_retcode(self):
  206. '''
  207. Tests error message given for a single ESXi host.
  208. '''
  209. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})):
  210. self.assertEqual({HOST: {'success': False, 'Error': ERROR, 'rulesets': None}},
  211. vsphere.get_firewall_status(HOST, USER, PASSWORD))
  212. def test_get_firewall_status_success(self):
  213. '''
  214. Tests successful function return for a single ESXi host.
  215. '''
  216. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})):
  217. self.assertEqual({HOST: {'rulesets': {}, 'success': True}},
  218. vsphere.get_firewall_status(HOST, USER, PASSWORD))
  219. # Tests for enable_firewall_ruleset function
  220. def test_enable_firewall_ruleset_esxi_hosts_not_list(self):
  221. '''
  222. Tests CommandExecutionError is raised when esxi_hosts is provided,
  223. but is not a list.
  224. '''
  225. self.assertRaises(CommandExecutionError,
  226. vsphere.enable_firewall_ruleset,
  227. HOST, USER, PASSWORD, 'foo', 'bar', esxi_hosts='baz')
  228. # Tests for syslog_service_reload function
  229. def test_syslog_service_reload_esxi_hosts_not_list(self):
  230. '''
  231. Tests CommandExecutionError is raised when esxi_hosts is provided,
  232. but is not a list.
  233. '''
  234. self.assertRaises(CommandExecutionError,
  235. vsphere.syslog_service_reload,
  236. HOST, USER, PASSWORD, esxi_hosts='foo')
  237. # Tests for set_syslog_config function.
  238. # These tests only test the firewall=True and syslog_config == 'loghost' if block.
  239. # The rest of the function is tested in the _set_syslog_config_helper tests below.
  240. def test_set_syslog_config_esxi_hosts_not_list(self):
  241. '''
  242. Tests CommandExecutionError is raised when esxi_hosts is provided,
  243. but is not a list, but we don't enter the 'loghost'/firewall loop.
  244. '''
  245. self.assertRaises(CommandExecutionError,
  246. vsphere.set_syslog_config,
  247. HOST, USER, PASSWORD, 'foo', 'bar', esxi_hosts='baz')
  248. def test_set_syslog_config_esxi_hosts_not_list_firewall(self):
  249. '''
  250. Tests CommandExecutionError is raised when esxi_hosts is provided,
  251. but is not a list, and we enter the 'loghost'/firewall loop.
  252. '''
  253. self.assertRaises(CommandExecutionError,
  254. vsphere.set_syslog_config,
  255. HOST, USER, PASSWORD, 'loghost', 'foo', firewall=True, esxi_hosts='bar')
  256. def test_set_syslog_config_host_list_firewall_bad_retcode(self):
  257. '''
  258. Tests error message returned with list of esxi_hosts with 'loghost' as syslog_config.
  259. '''
  260. with patch('salt.modules.vsphere.enable_firewall_ruleset',
  261. MagicMock(return_value={'host_1.foo.com': {'retcode': 1, 'stdout': ERROR}})):
  262. with patch('salt.modules.vsphere._set_syslog_config_helper',
  263. MagicMock(return_value={})):
  264. host_1 = 'host_1.foo.com'
  265. self.assertEqual({host_1: {'enable_firewall': {'message': ERROR, 'success': False}}},
  266. vsphere.set_syslog_config(HOST,
  267. USER,
  268. PASSWORD,
  269. 'loghost',
  270. 'foo',
  271. firewall=True,
  272. esxi_hosts=[host_1]))
  273. def test_set_syslog_config_host_list_firewall_success(self):
  274. '''
  275. Tests successful function return with list of esxi_hosts with 'loghost' as syslog_config.
  276. '''
  277. with patch('salt.modules.vsphere.enable_firewall_ruleset',
  278. MagicMock(return_value={'host_1.foo.com': {'retcode': 0}})):
  279. with patch('salt.modules.vsphere._set_syslog_config_helper',
  280. MagicMock(return_value={})):
  281. host_1 = 'host_1.foo.com'
  282. self.assertEqual({host_1: {'enable_firewall': {'success': True}}},
  283. vsphere.set_syslog_config(HOST,
  284. USER,
  285. PASSWORD,
  286. 'loghost',
  287. 'foo',
  288. firewall=True,
  289. esxi_hosts=[host_1]))
  290. def test_set_syslog_config_firewall_bad_retcode(self):
  291. '''
  292. Tests error message given for a single ESXi host with 'loghost' as syslog_config.
  293. '''
  294. with patch('salt.modules.vsphere.enable_firewall_ruleset',
  295. MagicMock(return_value={HOST: {'retcode': 1, 'stdout': ERROR}})):
  296. with patch('salt.modules.vsphere._set_syslog_config_helper',
  297. MagicMock(return_value={})):
  298. self.assertEqual({HOST: {'enable_firewall': {'message': ERROR, 'success': False}}},
  299. vsphere.set_syslog_config(HOST,
  300. USER,
  301. PASSWORD,
  302. 'loghost',
  303. 'foo',
  304. firewall=True))
  305. def test_set_syslog_config_firewall_success(self):
  306. '''
  307. Tests successful function return for a single ESXi host with 'loghost' as syslog_config.
  308. '''
  309. with patch('salt.modules.vsphere.enable_firewall_ruleset',
  310. MagicMock(return_value={HOST: {'retcode': 0}})):
  311. with patch('salt.modules.vsphere._set_syslog_config_helper',
  312. MagicMock(return_value={})):
  313. self.assertEqual({HOST: {'enable_firewall': {'success': True}}},
  314. vsphere.set_syslog_config(HOST,
  315. USER,
  316. PASSWORD,
  317. 'loghost',
  318. 'foo',
  319. firewall=True))
  320. # Tests for get_syslog_config function
  321. def test_get_syslog_config_esxi_hosts_not_list(self):
  322. '''
  323. Tests CommandExecutionError is raised when esxi_hosts is provided,
  324. but is not a list.
  325. '''
  326. self.assertRaises(CommandExecutionError,
  327. vsphere.get_syslog_config,
  328. HOST, USER, PASSWORD, esxi_hosts='foo')
  329. def test_get_syslog_config_host_list_bad_retcode(self):
  330. '''
  331. Tests error message returned with list of esxi_hosts.
  332. '''
  333. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})):
  334. host_1 = 'host_1.foo.com'
  335. self.assertEqual({host_1: {'message': ERROR, 'success': False}},
  336. vsphere.get_syslog_config(HOST, USER, PASSWORD, esxi_hosts=[host_1]))
  337. def test_get_syslog_config_host_list_success(self):
  338. '''
  339. Tests successful function return when an esxi_host is provided.
  340. '''
  341. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})):
  342. host_1 = 'host_1.foo.com'
  343. self.assertEqual({host_1: {'success': True}},
  344. vsphere.get_syslog_config(HOST, USER, PASSWORD, esxi_hosts=[host_1]))
  345. def test_get_syslog_config_bad_retcode(self):
  346. '''
  347. Tests error message given for a single ESXi host.
  348. '''
  349. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})):
  350. self.assertEqual({HOST: {'message': ERROR, 'success': False}},
  351. vsphere.get_syslog_config(HOST, USER, PASSWORD))
  352. def test_get_syslog_config_success(self):
  353. '''
  354. Tests successful function return for a single ESXi host.
  355. '''
  356. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})):
  357. self.assertEqual({HOST: {'success': True}},
  358. vsphere.get_syslog_config(HOST, USER, PASSWORD))
  359. # Tests for reset_syslog_config function
  360. def test_reset_syslog_config_no_syslog_config(self):
  361. '''
  362. Tests CommandExecutionError is raised when a syslog_config parameter is missing.
  363. '''
  364. self.assertRaises(CommandExecutionError,
  365. vsphere.reset_syslog_config,
  366. HOST, USER, PASSWORD)
  367. def test_reset_syslog_config_esxi_hosts_not_list(self):
  368. '''
  369. Tests CommandExecutionError is raised when esxi_hosts is provided,
  370. but is not a list.
  371. '''
  372. self.assertRaises(CommandExecutionError,
  373. vsphere.reset_syslog_config,
  374. HOST, USER, PASSWORD, syslog_config='test', esxi_hosts='foo')
  375. def test_reset_syslog_config_invalid_config_param(self):
  376. '''
  377. Tests error message returned when an invalid syslog_config parameter is provided.
  378. '''
  379. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={})):
  380. error = 'Invalid syslog configuration parameter'
  381. self.assertEqual({HOST: {'success': False, 'test': {'message': error, 'success': False}}},
  382. vsphere.reset_syslog_config(HOST, USER, PASSWORD,
  383. syslog_config='test'))
  384. def test_reset_syslog_config_host_list_bad_retcode(self):
  385. '''
  386. Tests error message returned with list of esxi_hosts.
  387. '''
  388. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})):
  389. host_1 = 'host_1.foo.com'
  390. self.assertEqual({host_1: {'success': False, 'logdir': {'message': ERROR, 'success': False}}},
  391. vsphere.reset_syslog_config(HOST, USER, PASSWORD,
  392. syslog_config='logdir',
  393. esxi_hosts=[host_1]))
  394. def test_reset_syslog_config_host_list_success(self):
  395. '''
  396. Tests successful function return when an esxi_host is provided.
  397. '''
  398. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})):
  399. host_1 = 'host_1.foo.com'
  400. self.assertEqual({host_1: {'success': True, 'loghost': {'success': True}}},
  401. vsphere.reset_syslog_config(HOST, USER, PASSWORD,
  402. syslog_config='loghost',
  403. esxi_hosts=[host_1]))
  404. def test_reset_syslog_config_bad_retcode(self):
  405. '''
  406. Tests error message given for a single ESXi host.
  407. '''
  408. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})):
  409. self.assertEqual({HOST: {'success': False, 'logdir-unique': {'message': ERROR, 'success': False}}},
  410. vsphere.reset_syslog_config(HOST, USER, PASSWORD,
  411. syslog_config='logdir-unique'))
  412. def test_reset_syslog_config_success(self):
  413. '''
  414. Tests successful function return for a single ESXi host.
  415. '''
  416. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})):
  417. self.assertEqual({HOST: {'success': True, 'default-rotate': {'success': True}}},
  418. vsphere.reset_syslog_config(HOST, USER, PASSWORD,
  419. syslog_config='default-rotate'))
  420. def test_reset_syslog_config_success_multiple_configs(self):
  421. '''
  422. Tests successful function return for a single ESXi host when passing in multiple syslog_config values.
  423. '''
  424. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})):
  425. self.assertEqual({HOST: {'success': True,
  426. 'default-size': {'success': True},
  427. 'default-timeout': {'success': True}}},
  428. vsphere.reset_syslog_config(HOST, USER, PASSWORD,
  429. syslog_config='default-size,default-timeout'))
  430. def test_reset_syslog_config_success_all_configs(self):
  431. '''
  432. Tests successful function return for a single ESXi host when passing in multiple syslog_config values.
  433. '''
  434. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0, 'stdout': ''})):
  435. self.assertEqual({HOST: {'success': True,
  436. 'logdir': {'success': True},
  437. 'loghost': {'success': True},
  438. 'default-rotate': {'success': True},
  439. 'default-size': {'success': True},
  440. 'default-timeout': {'success': True},
  441. 'logdir-unique': {'success': True}}},
  442. vsphere.reset_syslog_config(HOST, USER, PASSWORD,
  443. syslog_config='all'))
  444. # Tests for _reset_syslog_config_params function
  445. def test_reset_syslog_config_params_no_valid_reset(self):
  446. '''
  447. Tests function returns False when an invalid syslog config is passed.
  448. '''
  449. valid_resets = ['hello', 'world']
  450. config = 'foo'
  451. ret = {'success': False, config: {'success': False, 'message': 'Invalid syslog configuration parameter'}}
  452. self.assertEqual(ret, vsphere._reset_syslog_config_params(HOST, USER, PASSWORD,
  453. 'cmd', config, valid_resets))
  454. def test_reset_syslog_config_params_error(self):
  455. '''
  456. Tests function returns False when the esxxli function returns an unsuccessful retcode.
  457. '''
  458. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})):
  459. valid_resets = ['hello', 'world']
  460. error_dict = {'success': False, 'message': ERROR}
  461. ret = {'success': False, 'hello': error_dict, 'world': error_dict}
  462. self.assertDictEqual(ret, vsphere._reset_syslog_config_params(HOST, USER, PASSWORD,
  463. 'cmd', valid_resets, valid_resets))
  464. def test_reset_syslog_config_params_success(self):
  465. '''
  466. Tests function returns True when the esxxli function returns a successful retcode.
  467. '''
  468. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0})):
  469. valid_resets = ['hello', 'world']
  470. ret = {'success': True, 'hello': {'success': True}, 'world': {'success': True}}
  471. self.assertDictEqual(ret, vsphere._reset_syslog_config_params(HOST, USER, PASSWORD,
  472. 'cmd', valid_resets, valid_resets))
  473. # Tests for _set_syslog_config_helper function
  474. def test_set_syslog_config_helper_no_valid_reset(self):
  475. '''
  476. Tests function returns False when an invalid syslog config is passed.
  477. '''
  478. config = 'foo'
  479. ret = {'success': False, 'message': '\'{0}\' is not a valid config variable.'.format(config)}
  480. self.assertEqual(ret, vsphere._set_syslog_config_helper(HOST, USER, PASSWORD, config, 'bar'))
  481. def test_set_syslog_config_helper_bad_retcode(self):
  482. '''
  483. Tests function returns False when the esxcli function returns an unsuccessful retcode.
  484. '''
  485. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 1, 'stdout': ERROR})):
  486. config = 'default-rotate'
  487. self.assertEqual({config: {'success': False, 'message': ERROR}},
  488. vsphere._set_syslog_config_helper(HOST, USER, PASSWORD, config, 'foo'))
  489. def test_set_syslog_config_helper_success(self):
  490. '''
  491. Tests successful function return.
  492. '''
  493. with patch('salt.utils.vmware.esxcli', MagicMock(return_value={'retcode': 0})):
  494. config = 'logdir'
  495. self.assertEqual({config: {'success': True}},
  496. vsphere._set_syslog_config_helper(HOST, USER, PASSWORD, config, 'foo'))
  497. class GetProxyTypeTestCase(TestCase, LoaderModuleMockMixin):
  498. '''
  499. Tests for salt.modules.vsphere.get_proxy_type
  500. '''
  501. def setup_loader_modules(self):
  502. return {vsphere: {'__virtual__': MagicMock(return_value='vsphere')}}
  503. def test_output(self):
  504. with patch.dict(vsphere.__pillar__,
  505. {'proxy': {'proxytype': 'fake_proxy_type'}}):
  506. ret = vsphere.get_proxy_type()
  507. self.assertEqual('fake_proxy_type', ret)
  508. class SupportsProxiesTestCase(TestCase, LoaderModuleMockMixin):
  509. '''
  510. Tests for salt.modules.vsphere.supports_proxies decorator
  511. '''
  512. def setup_loader_modules(self):
  513. return {vsphere: {'__virtual__': MagicMock(return_value='vsphere')}}
  514. def test_supported_proxy(self):
  515. @vsphere.supports_proxies('supported')
  516. def mock_function():
  517. return 'fake_function'
  518. with patch('salt.modules.vsphere.get_proxy_type',
  519. MagicMock(return_value='supported')):
  520. ret = mock_function()
  521. self.assertEqual('fake_function', ret)
  522. def test_unsupported_proxy(self):
  523. @vsphere.supports_proxies('supported')
  524. def mock_function():
  525. return 'fake_function'
  526. with patch('salt.modules.vsphere.get_proxy_type',
  527. MagicMock(return_value='unsupported')):
  528. with self.assertRaises(CommandExecutionError) as excinfo:
  529. mock_function()
  530. self.assertEqual('\'unsupported\' proxy is not supported by '
  531. 'function mock_function',
  532. excinfo.exception.strerror)
  533. class _GetProxyConnectionDetailsTestCase(TestCase, LoaderModuleMockMixin):
  534. '''
  535. Tests for salt.modules.vsphere._get_proxy_connection_details
  536. '''
  537. def setup_loader_modules(self):
  538. return {vsphere: {'__virtual__': MagicMock(return_value='vsphere')}}
  539. def setUp(self):
  540. self.esxi_host_details = {'host': 'fake_host',
  541. 'username': 'fake_username',
  542. 'password': 'fake_password',
  543. 'protocol': 'fake_protocol',
  544. 'port': 'fake_port',
  545. 'mechanism': 'fake_mechanism',
  546. 'principal': 'fake_principal',
  547. 'domain': 'fake_domain'}
  548. self.esxi_vcenter_details = {'vcenter': 'fake_vcenter',
  549. 'username': 'fake_username',
  550. 'password': 'fake_password',
  551. 'protocol': 'fake_protocol',
  552. 'port': 'fake_port',
  553. 'mechanism': 'fake_mechanism',
  554. 'principal': 'fake_principal',
  555. 'domain': 'fake_domain'}
  556. self.esxdatacenter_details = {'vcenter': 'fake_vcenter',
  557. 'datacenter': 'fake_dc',
  558. 'username': 'fake_username',
  559. 'password': 'fake_password',
  560. 'protocol': 'fake_protocol',
  561. 'port': 'fake_port',
  562. 'mechanism': 'fake_mechanism',
  563. 'principal': 'fake_principal',
  564. 'domain': 'fake_domain'}
  565. self.esxcluster_details = {'vcenter': 'fake_vcenter',
  566. 'datacenter': 'fake_dc',
  567. 'cluster': 'fake_cluster',
  568. 'username': 'fake_username',
  569. 'password': 'fake_password',
  570. 'protocol': 'fake_protocol',
  571. 'port': 'fake_port',
  572. 'mechanism': 'fake_mechanism',
  573. 'principal': 'fake_principal',
  574. 'domain': 'fake_domain'}
  575. self.vcenter_details = {'vcenter': 'fake_vcenter',
  576. 'username': 'fake_username',
  577. 'password': 'fake_password',
  578. 'protocol': 'fake_protocol',
  579. 'port': 'fake_port',
  580. 'mechanism': 'fake_mechanism',
  581. 'principal': 'fake_principal',
  582. 'domain': 'fake_domain'}
  583. def tearDown(self):
  584. for attrname in ('esxi_host_details', 'esxi_vcenter_details',
  585. 'esxdatacenter_details', 'esxcluster_details'):
  586. try:
  587. delattr(self, attrname)
  588. except AttributeError:
  589. continue
  590. def test_esxi_proxy_host_details(self):
  591. with patch('salt.modules.vsphere.get_proxy_type',
  592. MagicMock(return_value='esxi')):
  593. with patch.dict(vsphere.__salt__,
  594. {'esxi.get_details':
  595. MagicMock(return_value=self.esxi_host_details)}):
  596. ret = vsphere._get_proxy_connection_details()
  597. self.assertEqual(('fake_host', 'fake_username', 'fake_password',
  598. 'fake_protocol', 'fake_port', 'fake_mechanism',
  599. 'fake_principal', 'fake_domain'), ret)
  600. def test_esxdatacenter_proxy_details(self):
  601. with patch('salt.modules.vsphere.get_proxy_type',
  602. MagicMock(return_value='esxdatacenter')):
  603. with patch.dict(vsphere.__salt__,
  604. {'esxdatacenter.get_details': MagicMock(
  605. return_value=self.esxdatacenter_details)}):
  606. ret = vsphere._get_proxy_connection_details()
  607. self.assertEqual(('fake_vcenter', 'fake_username', 'fake_password',
  608. 'fake_protocol', 'fake_port', 'fake_mechanism',
  609. 'fake_principal', 'fake_domain'), ret)
  610. def test_esxcluster_proxy_details(self):
  611. with patch('salt.modules.vsphere.get_proxy_type',
  612. MagicMock(return_value='esxcluster')):
  613. with patch.dict(vsphere.__salt__,
  614. {'esxcluster.get_details': MagicMock(
  615. return_value=self.esxcluster_details)}):
  616. ret = vsphere._get_proxy_connection_details()
  617. self.assertEqual(('fake_vcenter', 'fake_username', 'fake_password',
  618. 'fake_protocol', 'fake_port', 'fake_mechanism',
  619. 'fake_principal', 'fake_domain'), ret)
  620. def test_esxi_proxy_vcenter_details(self):
  621. with patch('salt.modules.vsphere.get_proxy_type',
  622. MagicMock(return_value='esxi')):
  623. with patch.dict(vsphere.__salt__,
  624. {'esxi.get_details':
  625. MagicMock(
  626. return_value=self.esxi_vcenter_details)}):
  627. ret = vsphere._get_proxy_connection_details()
  628. self.assertEqual(('fake_vcenter', 'fake_username', 'fake_password',
  629. 'fake_protocol', 'fake_port', 'fake_mechanism',
  630. 'fake_principal', 'fake_domain'), ret)
  631. def test_vcenter_proxy_details(self):
  632. with patch('salt.modules.vsphere.get_proxy_type',
  633. MagicMock(return_value='vcenter')):
  634. with patch.dict(vsphere.__salt__,
  635. {'vcenter.get_details': MagicMock(
  636. return_value=self.vcenter_details)}):
  637. ret = vsphere._get_proxy_connection_details()
  638. self.assertEqual(('fake_vcenter', 'fake_username', 'fake_password',
  639. 'fake_protocol', 'fake_port', 'fake_mechanism',
  640. 'fake_principal', 'fake_domain'), ret)
  641. def test_unsupported_proxy_details(self):
  642. with patch('salt.modules.vsphere.get_proxy_type',
  643. MagicMock(return_value='unsupported')):
  644. with self.assertRaises(CommandExecutionError) as excinfo:
  645. ret = vsphere._get_proxy_connection_details()
  646. self.assertEqual('\'unsupported\' proxy is not supported',
  647. excinfo.exception.strerror)
  648. class GetsServiceInstanceViaProxyTestCase(TestCase, LoaderModuleMockMixin):
  649. '''
  650. Tests for salt.modules.vsphere.gets_service_instance_via_proxy
  651. decorator
  652. '''
  653. def setup_loader_modules(self):
  654. patcher = patch('salt.utils.vmware.get_service_instance', MagicMock())
  655. patcher.start()
  656. self.addCleanup(patcher.stop)
  657. patcher = patch('salt.utils.vmware.disconnect', MagicMock())
  658. patcher.start()
  659. self.addCleanup(patcher.stop)
  660. return {
  661. vsphere: {
  662. '__virtual__': MagicMock(return_value='vsphere'),
  663. '_get_proxy_connection_details': MagicMock(),
  664. }
  665. }
  666. def setUp(self):
  667. self.mock_si = MagicMock()
  668. self.mock_details1 = MagicMock()
  669. self.mock_details2 = MagicMock()
  670. def tearDown(self):
  671. for attrname in ('mock_si', 'mock_details1', 'mock_details2'):
  672. try:
  673. delattr(self, attrname)
  674. except AttributeError:
  675. continue
  676. def test_no_service_instance_or_kwargs_parameters(self):
  677. @vsphere.gets_service_instance_via_proxy
  678. def mock_function():
  679. return 'fake_function'
  680. with self.assertRaises(CommandExecutionError) as excinfo:
  681. mock_function()
  682. self.assertEqual('Function mock_function must have either a '
  683. '\'service_instance\', or a \'**kwargs\' type '
  684. 'parameter', excinfo.exception.strerror)
  685. def test___get_proxy_connection_details_call(self):
  686. mock__get_proxy_connection_details = MagicMock()
  687. @vsphere.gets_service_instance_via_proxy
  688. def mock_function(service_instance=None):
  689. return service_instance
  690. with patch('salt.modules.vsphere._get_proxy_connection_details',
  691. mock__get_proxy_connection_details):
  692. mock_function()
  693. mock__get_proxy_connection_details.assert_called_once_with()
  694. def test_service_instance_named_parameter_no_value(self):
  695. mock_get_service_instance = MagicMock(return_value=self.mock_si)
  696. mock_disconnect = MagicMock()
  697. @vsphere.gets_service_instance_via_proxy
  698. def mock_function(service_instance=None):
  699. return service_instance
  700. with patch('salt.modules.vsphere._get_proxy_connection_details',
  701. MagicMock(return_value=(self.mock_details1,
  702. self.mock_details2))):
  703. with patch('salt.utils.vmware.get_service_instance',
  704. mock_get_service_instance):
  705. with patch('salt.utils.vmware.disconnect', mock_disconnect):
  706. ret = mock_function()
  707. mock_get_service_instance.assert_called_once_with(self.mock_details1,
  708. self.mock_details2)
  709. mock_disconnect.assert_called_once_with(self.mock_si)
  710. self.assertEqual(ret, self.mock_si)
  711. def test_service_instance_kwargs_parameter_no_value(self):
  712. mock_get_service_instance = MagicMock(return_value=self.mock_si)
  713. mock_disconnect = MagicMock()
  714. @vsphere.gets_service_instance_via_proxy
  715. def mock_function(**kwargs):
  716. return kwargs['service_instance']
  717. with patch('salt.modules.vsphere._get_proxy_connection_details',
  718. MagicMock(return_value=(self.mock_details1,
  719. self.mock_details2))):
  720. with patch('salt.utils.vmware.get_service_instance',
  721. mock_get_service_instance):
  722. with patch('salt.utils.vmware.disconnect', mock_disconnect):
  723. ret = mock_function()
  724. mock_get_service_instance.assert_called_once_with(self.mock_details1,
  725. self.mock_details2)
  726. mock_disconnect.assert_called_once_with(self.mock_si)
  727. self.assertEqual(ret, self.mock_si)
  728. def test_service_instance_positional_parameter_no_default_value(self):
  729. mock_get_service_instance = MagicMock()
  730. mock_disconnect = MagicMock()
  731. @vsphere.gets_service_instance_via_proxy
  732. def mock_function(service_instance):
  733. return service_instance
  734. with patch('salt.modules.vsphere._get_proxy_connection_details',
  735. MagicMock(return_value=(self.mock_details1,
  736. self.mock_details2))):
  737. with patch('salt.utils.vmware.get_service_instance',
  738. mock_get_service_instance):
  739. with patch('salt.utils.vmware.disconnect', mock_disconnect):
  740. ret = mock_function(self.mock_si)
  741. self.assertEqual(mock_get_service_instance.call_count, 0)
  742. self.assertEqual(mock_disconnect.call_count, 0)
  743. self.assertEqual(ret, self.mock_si)
  744. def test_service_instance_positional_parameter_with_default_value(self):
  745. mock_get_service_instance = MagicMock()
  746. mock_disconnect = MagicMock()
  747. @vsphere.gets_service_instance_via_proxy
  748. def mock_function(service_instance=None):
  749. return service_instance
  750. with patch('salt.modules.vsphere._get_proxy_connection_details',
  751. MagicMock(return_value=(self.mock_details1,
  752. self.mock_details2))):
  753. with patch('salt.utils.vmware.get_service_instance',
  754. mock_get_service_instance):
  755. with patch('salt.utils.vmware.disconnect', mock_disconnect):
  756. ret = mock_function(self.mock_si)
  757. self.assertEqual(mock_get_service_instance.call_count, 0)
  758. self.assertEqual(mock_disconnect.call_count, 0)
  759. self.assertEqual(ret, self.mock_si)
  760. def test_service_instance_named_parameter_with_default_value(self):
  761. mock_get_service_instance = MagicMock()
  762. mock_disconnect = MagicMock()
  763. @vsphere.gets_service_instance_via_proxy
  764. def mock_function(service_instance=None):
  765. return service_instance
  766. with patch('salt.modules.vsphere._get_proxy_connection_details',
  767. MagicMock(return_value=(self.mock_details1,
  768. self.mock_details2))):
  769. with patch('salt.utils.vmware.get_service_instance',
  770. mock_get_service_instance):
  771. with patch('salt.utils.vmware.disconnect', mock_disconnect):
  772. ret = mock_function(service_instance=self.mock_si)
  773. self.assertEqual(mock_get_service_instance.call_count, 0)
  774. self.assertEqual(mock_disconnect.call_count, 0)
  775. self.assertEqual(ret, self.mock_si)
  776. def test_service_instance_kwargs_parameter_passthrough(self):
  777. mock_get_service_instance = MagicMock()
  778. mock_disconnect = MagicMock()
  779. @vsphere.gets_service_instance_via_proxy
  780. def mock_function(**kwargs):
  781. return kwargs['service_instance']
  782. with patch('salt.modules.vsphere._get_proxy_connection_details',
  783. MagicMock(return_value=(self.mock_details1,
  784. self.mock_details2))):
  785. with patch('salt.utils.vmware.get_service_instance',
  786. mock_get_service_instance):
  787. with patch('salt.utils.vmware.disconnect', mock_disconnect):
  788. ret = mock_function(service_instance=self.mock_si)
  789. self.assertEqual(mock_get_service_instance.call_count, 0)
  790. self.assertEqual(mock_disconnect.call_count, 0)
  791. self.assertEqual(ret, self.mock_si)
  792. class GetServiceInstanceViaProxyTestCase(TestCase, LoaderModuleMockMixin):
  793. '''
  794. Tests for salt.modules.vsphere.get_service_instance_via_proxy
  795. '''
  796. def setup_loader_modules(self):
  797. patcher = patch('salt.utils.vmware.get_service_instance', MagicMock())
  798. patcher.start()
  799. self.addCleanup(patcher.stop)
  800. return {
  801. vsphere: {
  802. '__virtual__': MagicMock(return_value='vsphere'),
  803. 'get_proxy_type': MagicMock(return_value='esxi'),
  804. '_get_proxy_connection_details': MagicMock()
  805. }
  806. }
  807. def test_supported_proxies(self):
  808. supported_proxies = ['esxi', 'esxcluster', 'esxdatacenter', 'vcenter', 'esxvm']
  809. for proxy_type in supported_proxies:
  810. with patch('salt.modules.vsphere.get_proxy_type',
  811. MagicMock(return_value=proxy_type)):
  812. vsphere.get_service_instance_via_proxy()
  813. def test_get_service_instance_call(self):
  814. mock_connection_details = [MagicMock(), MagicMock(), MagicMock()]
  815. mock_get_service_instance = MagicMock()
  816. with patch('salt.modules.vsphere._get_proxy_connection_details',
  817. MagicMock(return_value=mock_connection_details)):
  818. with patch('salt.utils.vmware.get_service_instance',
  819. mock_get_service_instance):
  820. vsphere.get_service_instance_via_proxy()
  821. mock_get_service_instance.assert_called_once_with(
  822. *mock_connection_details)
  823. def test_output(self):
  824. mock_si = MagicMock()
  825. with patch('salt.utils.vmware.get_service_instance',
  826. MagicMock(return_value=mock_si)):
  827. res = vsphere.get_service_instance_via_proxy()
  828. self.assertEqual(res, mock_si)
  829. class DisconnectTestCase(TestCase, LoaderModuleMockMixin):
  830. '''
  831. Tests for salt.modules.vsphere.disconnect
  832. '''
  833. def setup_loader_modules(self):
  834. self.mock_si = MagicMock()
  835. self.addCleanup(delattr, self, 'mock_si')
  836. patcher = patch('salt.utils.vmware.disconnect', MagicMock())
  837. patcher.start()
  838. self.addCleanup(patcher.stop)
  839. return {
  840. vsphere: {
  841. '__virtual__': MagicMock(return_value='vsphere'),
  842. '_get_proxy_connection_details': MagicMock(),
  843. 'get_proxy_type': MagicMock(return_value='esxi')
  844. }
  845. }
  846. def test_supported_proxies(self):
  847. supported_proxies = ['esxi', 'esxcluster', 'esxdatacenter', 'vcenter', 'esxvm']
  848. for proxy_type in supported_proxies:
  849. with patch('salt.modules.vsphere.get_proxy_type',
  850. MagicMock(return_value=proxy_type)):
  851. vsphere.disconnect(self.mock_si)
  852. def test_disconnect_call(self):
  853. mock_disconnect = MagicMock()
  854. with patch('salt.utils.vmware.disconnect', mock_disconnect):
  855. vsphere.disconnect(self.mock_si)
  856. mock_disconnect.assert_called_once_with(self.mock_si)
  857. def test_output(self):
  858. res = vsphere.disconnect(self.mock_si)
  859. self.assertEqual(res, True)
  860. class TestVcenterConnectionTestCase(TestCase, LoaderModuleMockMixin):
  861. '''
  862. Tests for salt.modules.vsphere.test_vcenter_connection
  863. '''
  864. def setup_loader_modules(self):
  865. self.mock_si = MagicMock()
  866. self.addCleanup(delattr, self, 'mock_si')
  867. patcher = patch('salt.utils.vmware.get_service_instance', MagicMock(return_value=self.mock_si))
  868. patcher.start()
  869. self.addCleanup(patcher.stop)
  870. patcher = patch('salt.utils.vmware.disconnect', MagicMock())
  871. patcher.start()
  872. self.addCleanup(patcher.stop)
  873. patcher = patch('salt.utils.vmware.is_connection_to_a_vcenter', MagicMock())
  874. patcher.start()
  875. self.addCleanup(patcher.stop)
  876. return {
  877. vsphere: {
  878. '__virtual__': MagicMock(return_value='vsphere'),
  879. '_get_proxy_connection_details': MagicMock(),
  880. 'get_proxy_type': MagicMock(return_value='esxi')
  881. }
  882. }
  883. def test_supported_proxies(self):
  884. supported_proxies = ['esxi', 'esxcluster', 'esxdatacenter', 'vcenter', 'esxvm']
  885. for proxy_type in supported_proxies:
  886. with patch('salt.modules.vsphere.get_proxy_type',
  887. MagicMock(return_value=proxy_type)):
  888. vsphere.test_vcenter_connection()
  889. def test_is_connection_to_a_vcenter_call_default_service_instance(self):
  890. mock_is_connection_to_a_vcenter = MagicMock()
  891. with patch('salt.utils.vmware.is_connection_to_a_vcenter',
  892. mock_is_connection_to_a_vcenter):
  893. vsphere.test_vcenter_connection()
  894. mock_is_connection_to_a_vcenter.assert_called_once_with(self.mock_si)
  895. def test_is_connection_to_a_vcenter_call_explicit_service_instance(self):
  896. expl_mock_si = MagicMock()
  897. mock_is_connection_to_a_vcenter = MagicMock()
  898. with patch('salt.utils.vmware.is_connection_to_a_vcenter',
  899. mock_is_connection_to_a_vcenter):
  900. vsphere.test_vcenter_connection(expl_mock_si)
  901. mock_is_connection_to_a_vcenter.assert_called_once_with(expl_mock_si)
  902. def test_is_connection_to_a_vcenter_raises_vmware_salt_error(self):
  903. exc = VMwareSaltError('VMwareSaltError')
  904. with patch('salt.utils.vmware.is_connection_to_a_vcenter',
  905. MagicMock(side_effect=exc)):
  906. res = vsphere.test_vcenter_connection()
  907. self.assertEqual(res, False)
  908. def test_is_connection_to_a_vcenter_raises_non_vmware_salt_error(self):
  909. exc = Exception('NonVMwareSaltError')
  910. with patch('salt.utils.vmware.is_connection_to_a_vcenter',
  911. MagicMock(side_effect=exc)):
  912. with self.assertRaises(Exception) as excinfo:
  913. res = vsphere.test_vcenter_connection()
  914. self.assertEqual('NonVMwareSaltError', text_type(excinfo.exception))
  915. def test_output_true(self):
  916. with patch('salt.utils.vmware.is_connection_to_a_vcenter',
  917. MagicMock(return_value=True)):
  918. res = vsphere.test_vcenter_connection()
  919. self.assertEqual(res, True)
  920. def test_output_false(self):
  921. with patch('salt.utils.vmware.is_connection_to_a_vcenter',
  922. MagicMock(return_value=False)):
  923. res = vsphere.test_vcenter_connection()
  924. self.assertEqual(res, False)
  925. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  926. class ListDatacentersViaProxyTestCase(TestCase, LoaderModuleMockMixin):
  927. '''
  928. Tests for salt.modules.vsphere.list_datacenters_via_proxy
  929. '''
  930. def setup_loader_modules(self):
  931. self.mock_si = MagicMock()
  932. self.addCleanup(delattr, self, 'mock_si')
  933. patcher = patch('salt.utils.vmware.get_service_instance',
  934. MagicMock(return_value=self.mock_si))
  935. patcher.start()
  936. self.addCleanup(patcher.stop)
  937. patcher = patch('salt.utils.vmware.get_datacenters', MagicMock())
  938. patcher.start()
  939. self.addCleanup(patcher.stop)
  940. patcher = patch('salt.utils.vmware.get_managed_object_name',
  941. MagicMock())
  942. patcher.start()
  943. self.addCleanup(patcher.stop)
  944. return {
  945. vsphere: {
  946. '__virtual__': MagicMock(return_value='vsphere'),
  947. '_get_proxy_connection_details': MagicMock(),
  948. 'get_proxy_type': MagicMock(return_value='esxdatacenter')
  949. }
  950. }
  951. def test_supported_proxies(self):
  952. supported_proxies = ['esxcluster', 'esxdatacenter', 'vcenter', 'esxvm']
  953. for proxy_type in supported_proxies:
  954. with patch('salt.modules.vsphere.get_proxy_type',
  955. MagicMock(return_value=proxy_type)):
  956. vsphere.list_datacenters_via_proxy()
  957. def test_default_params(self):
  958. mock_get_datacenters = MagicMock()
  959. with patch('salt.utils.vmware.get_datacenters',
  960. mock_get_datacenters):
  961. vsphere.list_datacenters_via_proxy()
  962. mock_get_datacenters.assert_called_once_with(self.mock_si,
  963. get_all_datacenters=True)
  964. def test_defined_service_instance(self):
  965. mock_si = MagicMock()
  966. mock_get_datacenters = MagicMock()
  967. with patch('salt.utils.vmware.get_datacenters',
  968. mock_get_datacenters):
  969. vsphere.list_datacenters_via_proxy(service_instance=mock_si)
  970. mock_get_datacenters.assert_called_once_with(mock_si,
  971. get_all_datacenters=True)
  972. def test_defined_datacenter_names(self):
  973. mock_datacenters = MagicMock()
  974. mock_get_datacenters = MagicMock()
  975. with patch('salt.utils.vmware.get_datacenters',
  976. mock_get_datacenters):
  977. vsphere.list_datacenters_via_proxy(mock_datacenters)
  978. mock_get_datacenters.assert_called_once_with(self.mock_si,
  979. mock_datacenters)
  980. def test_get_managed_object_name_calls(self):
  981. mock_get_managed_object_name = MagicMock()
  982. mock_dcs = [MagicMock(), MagicMock()]
  983. with patch('salt.utils.vmware.get_datacenters',
  984. MagicMock(return_value=mock_dcs)):
  985. with patch('salt.utils.vmware.get_managed_object_name',
  986. mock_get_managed_object_name):
  987. vsphere.list_datacenters_via_proxy()
  988. mock_get_managed_object_name.assert_has_calls([call(mock_dcs[0]),
  989. call(mock_dcs[1])])
  990. def test_returned_array(self):
  991. with patch('salt.utils.vmware.get_datacenters',
  992. MagicMock(return_value=[MagicMock(), MagicMock()])):
  993. # 2 datacenters
  994. with patch('salt.utils.vmware.get_managed_object_name',
  995. MagicMock(side_effect=['fake_dc1', 'fake_dc2',
  996. 'fake_dc3'])):
  997. # 3 possible names
  998. res = vsphere.list_datacenters_via_proxy()
  999. # Just the first two names are in the result
  1000. self.assertEqual(res, [{'name': 'fake_dc1'}, {'name': 'fake_dc2'}])
  1001. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1002. class CreateDatacenterTestCase(TestCase, LoaderModuleMockMixin):
  1003. '''
  1004. Tests for salt.modules.vsphere.create_datacenter
  1005. '''
  1006. def setup_loader_modules(self):
  1007. self.mock_si = MagicMock()
  1008. self.addCleanup(delattr, self, 'mock_si')
  1009. patcher = patch('salt.utils.vmware.get_service_instance', MagicMock(return_value=self.mock_si))
  1010. patcher.start()
  1011. self.addCleanup(patcher.stop)
  1012. patcher = patch('salt.utils.vmware.create_datacenter', MagicMock())
  1013. patcher.start()
  1014. self.addCleanup(patcher.stop)
  1015. return {
  1016. vsphere: {
  1017. '__virtual__': MagicMock(return_value='vsphere'),
  1018. '_get_proxy_connection_details': MagicMock(),
  1019. 'get_proxy_type': MagicMock(return_value='esxdatacenter')
  1020. }
  1021. }
  1022. def test_supported_proxies(self):
  1023. supported_proxies = ['esxdatacenter', 'vcenter']
  1024. for proxy_type in supported_proxies:
  1025. with patch('salt.modules.vsphere.get_proxy_type',
  1026. MagicMock(return_value=proxy_type)):
  1027. vsphere.create_datacenter('fake_dc1')
  1028. def test_default_service_instance(self):
  1029. mock_create_datacenter = MagicMock()
  1030. with patch('salt.utils.vmware.create_datacenter',
  1031. mock_create_datacenter):
  1032. vsphere.create_datacenter('fake_dc1')
  1033. mock_create_datacenter.assert_called_once_with(self.mock_si,
  1034. 'fake_dc1')
  1035. def test_defined_service_instance(self):
  1036. mock_si = MagicMock()
  1037. mock_create_datacenter = MagicMock()
  1038. with patch('salt.utils.vmware.create_datacenter',
  1039. mock_create_datacenter):
  1040. vsphere.create_datacenter('fake_dc1', service_instance=mock_si)
  1041. mock_create_datacenter.assert_called_once_with(mock_si, 'fake_dc1')
  1042. def test_returned_value(self):
  1043. res = vsphere.create_datacenter('fake_dc1')
  1044. self.assertEqual(res, {'create_datacenter': True})
  1045. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1046. class EraseDiskPartitionsTestCase(TestCase, LoaderModuleMockMixin):
  1047. '''
  1048. Tests for salt.modules.vsphere.erase_disk_partitions
  1049. '''
  1050. def setup_loader_modules(self):
  1051. return {
  1052. vsphere: {
  1053. '__virtual__': MagicMock(return_value='vsphere'),
  1054. '_get_proxy_connection_details': MagicMock(),
  1055. '__proxy__': {'esxi.get_details': MagicMock(
  1056. return_value={'esxi_host': 'fake_host'})}
  1057. }
  1058. }
  1059. def setUp(self):
  1060. attrs = (('mock_si', MagicMock()),
  1061. ('mock_host', MagicMock()))
  1062. for attr, mock_obj in attrs:
  1063. setattr(self, attr, mock_obj)
  1064. self.addCleanup(delattr, self, attr)
  1065. attrs = (('mock_proxy_target', MagicMock(return_value=self.mock_host)),
  1066. ('mock_erase_disk_partitions', MagicMock()))
  1067. for attr, mock_obj in attrs:
  1068. setattr(self, attr, mock_obj)
  1069. self.addCleanup(delattr, self, attr)
  1070. patches = (
  1071. ('salt.utils.vmware.get_service_instance',
  1072. MagicMock(return_value=self.mock_si)),
  1073. ('salt.modules.vsphere.get_proxy_type',
  1074. MagicMock(return_value='esxi')),
  1075. ('salt.modules.vsphere._get_proxy_target',
  1076. MagicMock(return_value=self.mock_host)),
  1077. ('salt.utils.vmware.erase_disk_partitions',
  1078. self.mock_erase_disk_partitions))
  1079. for module, mock_obj in patches:
  1080. patcher = patch(module, mock_obj)
  1081. patcher.start()
  1082. self.addCleanup(patcher.stop)
  1083. def test_supported_proxies(self):
  1084. supported_proxies = ['esxi']
  1085. for proxy_type in supported_proxies:
  1086. with patch('salt.modules.vsphere.get_proxy_type',
  1087. MagicMock(return_value=proxy_type)):
  1088. vsphere.erase_disk_partitions(disk_id='fake_disk')
  1089. def test_no_disk_id_or_scsi_address(self):
  1090. with self.assertRaises(ArgumentValueError) as excinfo:
  1091. vsphere.erase_disk_partitions()
  1092. self.assertEqual('Either \'disk_id\' or \'scsi_address\' needs to '
  1093. 'be specified', excinfo.exception.strerror)
  1094. def test_get_proxy_target(self):
  1095. mock_test_proxy_target = MagicMock()
  1096. with patch('salt.modules.vsphere._get_proxy_target',
  1097. mock_test_proxy_target):
  1098. vsphere.erase_disk_partitions(disk_id='fake_disk')
  1099. mock_test_proxy_target.assert_called_once_with(self.mock_si)
  1100. def test_scsi_address_not_found(self):
  1101. mock = MagicMock(return_value={'bad_scsi_address': 'bad_disk_id'})
  1102. with patch('salt.utils.vmware.get_scsi_address_to_lun_map', mock):
  1103. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  1104. vsphere.erase_disk_partitions(scsi_address='fake_scsi_address')
  1105. self.assertEqual('Scsi lun with address \'fake_scsi_address\' was '
  1106. 'not found on host \'fake_host\'',
  1107. excinfo.exception.strerror)
  1108. def test_scsi_address_to_disk_id_map(self):
  1109. mock_disk_id = MagicMock(canonicalName='fake_scsi_disk_id')
  1110. mock_get_scsi_addr_to_lun = \
  1111. MagicMock(return_value={'fake_scsi_address': mock_disk_id})
  1112. with patch('salt.utils.vmware.get_scsi_address_to_lun_map',
  1113. mock_get_scsi_addr_to_lun):
  1114. vsphere.erase_disk_partitions(scsi_address='fake_scsi_address')
  1115. mock_get_scsi_addr_to_lun.assert_called_once_with(self.mock_host)
  1116. self.mock_erase_disk_partitions.assert_called_once_with(
  1117. self.mock_si, self.mock_host, 'fake_scsi_disk_id',
  1118. hostname='fake_host')
  1119. def test_erase_disk_partitions(self):
  1120. vsphere.erase_disk_partitions(disk_id='fake_disk_id')
  1121. self.mock_erase_disk_partitions.assert_called_once_with(
  1122. self.mock_si, self.mock_host, 'fake_disk_id', hostname='fake_host')
  1123. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1124. class RemoveDatastoreTestCase(TestCase, LoaderModuleMockMixin):
  1125. '''
  1126. Tests for salt.modules.vsphere.remove_datastore
  1127. '''
  1128. def setup_loader_modules(self):
  1129. return {
  1130. vsphere: {
  1131. '__virtual__': MagicMock(return_value='vsphere'),
  1132. '_get_proxy_connection_details': MagicMock(),
  1133. 'get_proxy_type': MagicMock(return_value='esxdatacenter'),
  1134. }
  1135. }
  1136. def setUp(self):
  1137. attrs = (('mock_si', MagicMock()),
  1138. ('mock_target', MagicMock()),
  1139. ('mock_ds', MagicMock()))
  1140. for attr, mock_obj in attrs:
  1141. setattr(self, attr, mock_obj)
  1142. self.addCleanup(delattr, self, attr)
  1143. patches = (
  1144. ('salt.utils.vmware.get_service_instance',
  1145. MagicMock(return_value=self.mock_si)),
  1146. ('salt.modules.vsphere.get_proxy_type',
  1147. MagicMock(return_value='esxdatacenter')),
  1148. ('salt.modules.vsphere._get_proxy_target',
  1149. MagicMock(return_value=self.mock_target)),
  1150. ('salt.utils.vmware.get_datastores',
  1151. MagicMock(return_value=[self.mock_ds])),
  1152. ('salt.utils.vmware.remove_datastore', MagicMock()))
  1153. for module, mock_obj in patches:
  1154. patcher = patch(module, mock_obj)
  1155. patcher.start()
  1156. self.addCleanup(patcher.stop)
  1157. def test_supported_proxes(self):
  1158. supported_proxies = ['esxi', 'esxcluster', 'esxdatacenter']
  1159. for proxy_type in supported_proxies:
  1160. with patch('salt.modules.vsphere.get_proxy_type',
  1161. MagicMock(return_value=proxy_type)):
  1162. vsphere.remove_datastore(datastore='fake_ds_name')
  1163. def test__get_proxy_target_call(self):
  1164. mock__get_proxy_target = MagicMock(return_value=self.mock_target)
  1165. with patch('salt.modules.vsphere._get_proxy_target',
  1166. mock__get_proxy_target):
  1167. vsphere.remove_datastore(datastore='fake_ds_name')
  1168. mock__get_proxy_target.assert_called_once_with(self.mock_si)
  1169. def test_get_datastores_call(self):
  1170. mock_get_datastores = MagicMock()
  1171. with patch('salt.utils.vmware.get_datastores',
  1172. mock_get_datastores):
  1173. vsphere.remove_datastore(datastore='fake_ds')
  1174. mock_get_datastores.assert_called_once_with(
  1175. self.mock_si, reference=self.mock_target,
  1176. datastore_names=['fake_ds'])
  1177. def test_datastore_not_found(self):
  1178. with patch('salt.utils.vmware.get_datastores',
  1179. MagicMock(return_value=[])):
  1180. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  1181. vsphere.remove_datastore(datastore='fake_ds')
  1182. self.assertEqual('Datastore \'fake_ds\' was not found',
  1183. excinfo.exception.strerror)
  1184. def test_multiple_datastores_found(self):
  1185. with patch('salt.utils.vmware.get_datastores',
  1186. MagicMock(return_value=[MagicMock(), MagicMock()])):
  1187. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  1188. vsphere.remove_datastore(datastore='fake_ds')
  1189. self.assertEqual('Multiple datastores \'fake_ds\' were found',
  1190. excinfo.exception.strerror)
  1191. def test_remove_datastore_call(self):
  1192. mock_remove_datastore = MagicMock()
  1193. with patch('salt.utils.vmware.remove_datastore',
  1194. mock_remove_datastore):
  1195. vsphere.remove_datastore(datastore='fake_ds')
  1196. mock_remove_datastore.assert_called_once_with(
  1197. self.mock_si, self.mock_ds)
  1198. def test_success_output(self):
  1199. res = vsphere.remove_datastore(datastore='fake_ds')
  1200. self.assertTrue(res)
  1201. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1202. class RemoveDiskgroupTestCase(TestCase, LoaderModuleMockMixin):
  1203. '''
  1204. Tests for salt.modules.vsphere.remove_diskgroup
  1205. '''
  1206. def setup_loader_modules(self):
  1207. return {
  1208. vsphere: {
  1209. '__virtual__': MagicMock(return_value='vsphere'),
  1210. '_get_proxy_connection_details': MagicMock(),
  1211. '__proxy__': {'esxi.get_details': MagicMock(
  1212. return_value={'esxi_host': 'fake_host'})}
  1213. }
  1214. }
  1215. def setUp(self):
  1216. attrs = (('mock_si', MagicMock()),
  1217. ('mock_host', MagicMock()),
  1218. ('mock_diskgroup', MagicMock()))
  1219. for attr, mock_obj in attrs:
  1220. setattr(self, attr, mock_obj)
  1221. self.addCleanup(delattr, self, attr)
  1222. patches = (
  1223. ('salt.utils.vmware.get_service_instance',
  1224. MagicMock(return_value=self.mock_si)),
  1225. ('salt.modules.vsphere.get_proxy_type',
  1226. MagicMock(return_value='esxi')),
  1227. ('salt.modules.vsphere._get_proxy_target',
  1228. MagicMock(return_value=self.mock_host)),
  1229. ('salt.utils.vmware.get_diskgroups',
  1230. MagicMock(return_value=[self.mock_diskgroup])),
  1231. ('salt.utils.vsan.remove_diskgroup', MagicMock()))
  1232. for module, mock_obj in patches:
  1233. patcher = patch(module, mock_obj)
  1234. patcher.start()
  1235. self.addCleanup(patcher.stop)
  1236. def test_supported_proxes(self):
  1237. supported_proxies = ['esxi']
  1238. for proxy_type in supported_proxies:
  1239. with patch('salt.modules.vsphere.get_proxy_type',
  1240. MagicMock(return_value=proxy_type)):
  1241. vsphere.remove_diskgroup(cache_disk_id='fake_disk_id')
  1242. def test__get_proxy_target_call(self):
  1243. mock__get_proxy_target = MagicMock(return_value=self.mock_host)
  1244. with patch('salt.modules.vsphere._get_proxy_target',
  1245. mock__get_proxy_target):
  1246. vsphere.remove_diskgroup(cache_disk_id='fake_disk_id')
  1247. mock__get_proxy_target.assert_called_once_with(self.mock_si)
  1248. def test_get_disk_groups(self):
  1249. mock_get_diskgroups = MagicMock(return_value=[self.mock_diskgroup])
  1250. with patch('salt.utils.vmware.get_diskgroups',
  1251. mock_get_diskgroups):
  1252. vsphere.remove_diskgroup(cache_disk_id='fake_disk_id')
  1253. mock_get_diskgroups.assert_called_once_with(
  1254. self.mock_host, cache_disk_ids=['fake_disk_id'])
  1255. def test_disk_group_not_found_safety_checks_set(self):
  1256. with patch('salt.utils.vmware.get_diskgroups',
  1257. MagicMock(return_value=[])):
  1258. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  1259. vsphere.remove_diskgroup(cache_disk_id='fake_disk_id')
  1260. self.assertEqual('No diskgroup with cache disk id '
  1261. '\'fake_disk_id\' was found in ESXi host '
  1262. '\'fake_host\'',
  1263. excinfo.exception.strerror)
  1264. def test_remove_disk_group(self):
  1265. mock_remove_diskgroup = MagicMock(return_value=None)
  1266. with patch('salt.utils.vsan.remove_diskgroup',
  1267. mock_remove_diskgroup):
  1268. vsphere.remove_diskgroup(cache_disk_id='fake_disk_id')
  1269. mock_remove_diskgroup.assert_called_once_with(
  1270. self.mock_si, self.mock_host, self.mock_diskgroup,
  1271. data_accessibility=True)
  1272. def test_remove_disk_group_data_accessibility_false(self):
  1273. mock_remove_diskgroup = MagicMock(return_value=None)
  1274. with patch('salt.utils.vsan.remove_diskgroup',
  1275. mock_remove_diskgroup):
  1276. vsphere.remove_diskgroup(cache_disk_id='fake_disk_id',
  1277. data_accessibility=False)
  1278. mock_remove_diskgroup.assert_called_once_with(
  1279. self.mock_si, self.mock_host, self.mock_diskgroup,
  1280. data_accessibility=False)
  1281. def test_success_output(self):
  1282. res = vsphere.remove_diskgroup(cache_disk_id='fake_disk_id')
  1283. self.assertTrue(res)
  1284. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1285. @skipIf(not vsphere.HAS_JSONSCHEMA, 'The \'jsonschema\' library is missing')
  1286. class RemoveCapacityFromDiskgroupTestCase(TestCase, LoaderModuleMockMixin):
  1287. '''
  1288. Tests for salt.modules.vsphere.remove_capacity_from_diskgroup
  1289. '''
  1290. def setup_loader_modules(self):
  1291. return {
  1292. vsphere: {
  1293. '__virtual__': MagicMock(return_value='vsphere'),
  1294. '_get_proxy_connection_details': MagicMock(),
  1295. '__proxy__': {'esxi.get_details': MagicMock(
  1296. return_value={'esxi_host': 'fake_host'})}
  1297. }
  1298. }
  1299. def setUp(self):
  1300. attrs = (('mock_si', MagicMock()),
  1301. ('mock_schema', MagicMock()),
  1302. ('mock_host', MagicMock()),
  1303. ('mock_disk1', MagicMock(canonicalName='fake_disk1')),
  1304. ('mock_disk2', MagicMock(canonicalName='fake_disk2')),
  1305. ('mock_disk3', MagicMock(canonicalName='fake_disk3')),
  1306. ('mock_diskgroup', MagicMock()))
  1307. for attr, mock_obj in attrs:
  1308. setattr(self, attr, mock_obj)
  1309. self.addCleanup(delattr, self, attr)
  1310. patches = (
  1311. ('salt.utils.vmware.get_service_instance',
  1312. MagicMock(return_value=self.mock_si)),
  1313. ('salt.modules.vsphere.DiskGroupsDiskIdSchema.serialize',
  1314. MagicMock(return_value=self.mock_schema)),
  1315. ('salt.modules.vsphere.jsonschema.validate', MagicMock()),
  1316. ('salt.modules.vsphere.get_proxy_type',
  1317. MagicMock(return_value='esxi')),
  1318. ('salt.modules.vsphere._get_proxy_target',
  1319. MagicMock(return_value=self.mock_host)),
  1320. ('salt.utils.vmware.get_disks',
  1321. MagicMock(return_value=[self.mock_disk1, self.mock_disk2,
  1322. self.mock_disk3])),
  1323. ('salt.utils.vmware.get_diskgroups',
  1324. MagicMock(return_value=[self.mock_diskgroup])),
  1325. ('salt.utils.vsan.remove_capacity_from_diskgroup', MagicMock()))
  1326. for module, mock_obj in patches:
  1327. patcher = patch(module, mock_obj)
  1328. patcher.start()
  1329. self.addCleanup(patcher.stop)
  1330. def test_validate(self):
  1331. mock_schema_validate = MagicMock()
  1332. with patch('salt.modules.vsphere.jsonschema.validate',
  1333. mock_schema_validate):
  1334. vsphere.remove_capacity_from_diskgroup(
  1335. cache_disk_id='fake_cache_disk_id',
  1336. capacity_disk_ids=['fake_disk1', 'fake_disk2'])
  1337. mock_schema_validate.assert_called_once_with(
  1338. {'diskgroups': [{'cache_id': 'fake_cache_disk_id',
  1339. 'capacity_ids': ['fake_disk1',
  1340. 'fake_disk2']}]},
  1341. self.mock_schema)
  1342. def test_invalid_schema_validation(self):
  1343. mock_schema_validate = MagicMock(
  1344. side_effect=vsphere.jsonschema.exceptions.ValidationError('err'))
  1345. with patch('salt.modules.vsphere.jsonschema.validate',
  1346. mock_schema_validate):
  1347. with self.assertRaises(ArgumentValueError) as excinfo:
  1348. vsphere.remove_capacity_from_diskgroup(
  1349. cache_disk_id='fake_cache_disk_id',
  1350. capacity_disk_ids=['fake_disk1', 'fake_disk2'])
  1351. self.assertEqual('err', excinfo.exception.strerror)
  1352. def test_supported_proxes(self):
  1353. supported_proxies = ['esxi']
  1354. for proxy_type in supported_proxies:
  1355. with patch('salt.modules.vsphere.get_proxy_type',
  1356. MagicMock(return_value=proxy_type)):
  1357. vsphere.remove_capacity_from_diskgroup(
  1358. cache_disk_id='fake_cache_disk_id',
  1359. capacity_disk_ids=['fake_disk1', 'fake_disk2'])
  1360. def test__get_proxy_target_call(self):
  1361. mock__get_proxy_target = MagicMock(return_value=self.mock_host)
  1362. with patch('salt.modules.vsphere._get_proxy_target',
  1363. mock__get_proxy_target):
  1364. vsphere.remove_capacity_from_diskgroup(
  1365. cache_disk_id='fake_cache_disk_id',
  1366. capacity_disk_ids=['fake_disk1', 'fake_disk2'])
  1367. mock__get_proxy_target.assert_called_once_with(self.mock_si)
  1368. def test_get_disks(self):
  1369. mock_get_disks = MagicMock(
  1370. return_value=[self.mock_disk1, self.mock_disk2, self.mock_disk3])
  1371. with patch('salt.utils.vmware.get_disks', mock_get_disks):
  1372. vsphere.remove_capacity_from_diskgroup(
  1373. cache_disk_id='fake_cache_disk_id',
  1374. capacity_disk_ids=['fake_disk1', 'fake_disk2'])
  1375. mock_get_disks.assert_called_once_with(
  1376. self.mock_host, disk_ids=['fake_disk1', 'fake_disk2'])
  1377. def test_disk_not_found_safety_checks_set(self):
  1378. mock_get_disks = MagicMock(
  1379. return_value=[self.mock_disk1, self.mock_disk2, self.mock_disk3])
  1380. with patch('salt.utils.vmware.get_disks', mock_get_disks):
  1381. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  1382. vsphere.remove_capacity_from_diskgroup(
  1383. cache_disk_id='fake_cache_disk_id',
  1384. capacity_disk_ids=['fake_disk1', 'fake_disk4'],
  1385. safety_checks=True)
  1386. self.assertEqual('No disk with id \'fake_disk4\' was found '
  1387. 'in ESXi host \'fake_host\'',
  1388. excinfo.exception.strerror)
  1389. def test_get_diskgroups(self):
  1390. mock_get_diskgroups = MagicMock(return_value=[self.mock_diskgroup])
  1391. with patch('salt.utils.vmware.get_diskgroups',
  1392. mock_get_diskgroups):
  1393. vsphere.remove_capacity_from_diskgroup(
  1394. cache_disk_id='fake_cache_disk_id',
  1395. capacity_disk_ids=['fake_disk1', 'fake_disk2'])
  1396. mock_get_diskgroups.assert_called_once_with(
  1397. self.mock_host, cache_disk_ids=['fake_cache_disk_id'])
  1398. def test_diskgroup_not_found(self):
  1399. with patch('salt.utils.vmware.get_diskgroups',
  1400. MagicMock(return_value=[])):
  1401. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  1402. vsphere.remove_capacity_from_diskgroup(
  1403. cache_disk_id='fake_cache_disk_id',
  1404. capacity_disk_ids=['fake_disk1', 'fake_disk2'])
  1405. self.assertEqual('No diskgroup with cache disk id '
  1406. '\'fake_cache_disk_id\' was found in ESXi host '
  1407. '\'fake_host\'',
  1408. excinfo.exception.strerror)
  1409. def test_remove_capacity_from_diskgroup(self):
  1410. mock_remove_capacity_from_diskgroup = MagicMock()
  1411. with patch('salt.utils.vsan.remove_capacity_from_diskgroup',
  1412. mock_remove_capacity_from_diskgroup):
  1413. vsphere.remove_capacity_from_diskgroup(
  1414. cache_disk_id='fake_cache_disk_id',
  1415. capacity_disk_ids=['fake_disk1', 'fake_disk2'])
  1416. mock_remove_capacity_from_diskgroup.assert_called_once_with(
  1417. self.mock_si, self.mock_host, self.mock_diskgroup,
  1418. capacity_disks=[self.mock_disk1, self.mock_disk2],
  1419. data_evacuation=True)
  1420. def test_remove_capacity_from_diskgroup_data_evacuation_false(self):
  1421. mock_remove_capacity_from_diskgroup = MagicMock()
  1422. with patch('salt.utils.vsan.remove_capacity_from_diskgroup',
  1423. mock_remove_capacity_from_diskgroup):
  1424. vsphere.remove_capacity_from_diskgroup(
  1425. cache_disk_id='fake_cache_disk_id',
  1426. capacity_disk_ids=['fake_disk1', 'fake_disk2'],
  1427. data_evacuation=False)
  1428. mock_remove_capacity_from_diskgroup.assert_called_once_with(
  1429. self.mock_si, self.mock_host, self.mock_diskgroup,
  1430. capacity_disks=[self.mock_disk1, self.mock_disk2],
  1431. data_evacuation=False)
  1432. def test_success_output(self):
  1433. res = vsphere.remove_capacity_from_diskgroup(
  1434. cache_disk_id='fake_cache_disk_id',
  1435. capacity_disk_ids=['fake_disk1', 'fake_disk2'])
  1436. self.assertTrue(res)
  1437. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1438. class ListClusterTestCase(TestCase, LoaderModuleMockMixin):
  1439. '''
  1440. Tests for salt.modules.vsphere.list_cluster
  1441. '''
  1442. def setup_loader_modules(self):
  1443. return {
  1444. vsphere: {
  1445. '__virtual__': MagicMock(return_value='vsphere'),
  1446. '_get_proxy_connection_details': MagicMock(),
  1447. '__salt__': {}
  1448. }
  1449. }
  1450. def setUp(self):
  1451. attrs = (('mock_si', MagicMock()),
  1452. ('mock_dc', MagicMock()),
  1453. ('mock_cl', MagicMock()),
  1454. ('mock__get_cluster_dict', MagicMock()))
  1455. for attr, mock_obj in attrs:
  1456. setattr(self, attr, mock_obj)
  1457. self.addCleanup(delattr, self, attr)
  1458. attrs = (('mock_get_cluster', MagicMock(return_value=self.mock_cl)),)
  1459. for attr, mock_obj in attrs:
  1460. setattr(self, attr, mock_obj)
  1461. self.addCleanup(delattr, self, attr)
  1462. patches = (
  1463. ('salt.utils.vmware.get_service_instance',
  1464. MagicMock(return_value=self.mock_si)),
  1465. ('salt.modules.vsphere.get_proxy_type',
  1466. MagicMock(return_value='esxcluster')),
  1467. ('salt.modules.vsphere._get_proxy_target',
  1468. MagicMock(return_value=self.mock_cl)),
  1469. ('salt.utils.vmware.get_cluster', self.mock_get_cluster),
  1470. ('salt.modules.vsphere._get_cluster_dict',
  1471. self.mock__get_cluster_dict))
  1472. for module, mock_obj in patches:
  1473. patcher = patch(module, mock_obj)
  1474. patcher.start()
  1475. self.addCleanup(patcher.stop)
  1476. # Patch __salt__ dunder
  1477. patcher = patch.dict(vsphere.__salt__,
  1478. {'esxcluster.get_details':
  1479. MagicMock(return_value={'cluster': 'cl'})})
  1480. patcher.start()
  1481. self.addCleanup(patcher.stop)
  1482. def test_supported_proxies(self):
  1483. supported_proxies = ['esxcluster', 'esxdatacenter']
  1484. for proxy_type in supported_proxies:
  1485. with patch('salt.modules.vsphere.get_proxy_type',
  1486. MagicMock(return_value=proxy_type)):
  1487. vsphere.list_cluster(cluster='cl')
  1488. def test_default_service_instance(self):
  1489. mock__get_proxy_target = MagicMock()
  1490. with patch('salt.modules.vsphere._get_proxy_target',
  1491. mock__get_proxy_target):
  1492. vsphere.list_cluster()
  1493. mock__get_proxy_target.assert_called_once_with(self.mock_si)
  1494. def test_defined_service_instance(self):
  1495. mock_si = MagicMock()
  1496. mock__get_proxy_target = MagicMock()
  1497. with patch('salt.modules.vsphere._get_proxy_target',
  1498. mock__get_proxy_target):
  1499. vsphere.list_cluster(service_instance=mock_si)
  1500. mock__get_proxy_target.assert_called_once_with(mock_si)
  1501. def test_no_cluster_raises_argument_value_error(self):
  1502. with patch('salt.modules.vsphere.get_proxy_type',
  1503. MagicMock(return_value='esxdatacenter')):
  1504. with patch('salt.modules.vsphere._get_proxy_target', MagicMock()):
  1505. with self.assertRaises(ArgumentValueError) as excinfo:
  1506. vsphere.list_cluster()
  1507. self.assertEqual(excinfo.exception.strerror,
  1508. '\'cluster\' needs to be specified')
  1509. def test_get_cluster_call(self):
  1510. with patch('salt.modules.vsphere.get_proxy_type',
  1511. MagicMock(return_value='esxdatacenter')):
  1512. with patch('salt.modules.vsphere._get_proxy_target',
  1513. MagicMock(return_value=self.mock_dc)):
  1514. vsphere.list_cluster(cluster='cl')
  1515. self.mock_get_cluster.assert_called_once_with(self.mock_dc, 'cl')
  1516. def test__get_cluster_dict_call(self):
  1517. vsphere.list_cluster()
  1518. self.mock__get_cluster_dict.assert_called_once_with('cl', self.mock_cl)
  1519. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1520. class RenameDatastoreTestCase(TestCase, LoaderModuleMockMixin):
  1521. '''
  1522. Tests for salt.modules.vsphere.rename_datastore
  1523. '''
  1524. def setup_loader_modules(self):
  1525. return {
  1526. vsphere: {
  1527. '__virtual__': MagicMock(return_value='vsphere'),
  1528. '_get_proxy_connection_details': MagicMock(),
  1529. 'get_proxy_type': MagicMock(return_value='esxdatacenter')
  1530. }
  1531. }
  1532. def setUp(self):
  1533. self.mock_si = MagicMock()
  1534. self.mock_target = MagicMock()
  1535. self.mock_ds_ref = MagicMock()
  1536. self.mock_get_datastores = MagicMock(return_value=[self.mock_ds_ref])
  1537. self.mock_rename_datastore = MagicMock()
  1538. patches = (
  1539. ('salt.utils.vmware.get_service_instance',
  1540. MagicMock(return_value=self.mock_si)),
  1541. ('salt.modules.vsphere._get_proxy_target',
  1542. MagicMock(return_value=self.mock_target)),
  1543. ('salt.utils.vmware.get_datastores',
  1544. self.mock_get_datastores),
  1545. ('salt.utils.vmware.rename_datastore',
  1546. self.mock_rename_datastore))
  1547. for mod, mock in patches:
  1548. patcher = patch(mod, mock)
  1549. patcher.start()
  1550. self.addCleanup(patcher.stop)
  1551. def tearDown(self):
  1552. for attr in ('mock_si', 'mock_target', 'mock_ds_ref',
  1553. 'mock_get_datastores', 'mock_rename_datastore'):
  1554. delattr(self, attr)
  1555. def test_supported_proxes(self):
  1556. supported_proxies = ['esxi', 'esxcluster', 'esxdatacenter']
  1557. for proxy_type in supported_proxies:
  1558. with patch('salt.modules.vsphere.get_proxy_type',
  1559. MagicMock(return_value=proxy_type)):
  1560. vsphere.rename_datastore('current_ds_name', 'new_ds_name')
  1561. def test_default_service_instance(self):
  1562. mock__get_proxy_target = MagicMock()
  1563. with patch('salt.modules.vsphere._get_proxy_target',
  1564. mock__get_proxy_target):
  1565. vsphere.rename_datastore('current_ds_name', 'new_ds_name')
  1566. mock__get_proxy_target.assert_called_once_with(self.mock_si)
  1567. def test_defined_service_instance(self):
  1568. mock_si = MagicMock()
  1569. mock__get_proxy_target = MagicMock()
  1570. with patch('salt.modules.vsphere._get_proxy_target',
  1571. mock__get_proxy_target):
  1572. vsphere.rename_datastore('current_ds_name', 'new_ds_name',
  1573. service_instance=mock_si)
  1574. mock__get_proxy_target.assert_called_once_with(mock_si)
  1575. def test_get_datastore_call(self):
  1576. vsphere.rename_datastore('current_ds_name', 'new_ds_name')
  1577. self.mock_get_datastores.assert_called_once_with(
  1578. self.mock_si, self.mock_target,
  1579. datastore_names=['current_ds_name'])
  1580. def test_get_no_datastores(self):
  1581. with patch('salt.utils.vmware.get_datastores',
  1582. MagicMock(return_value=[])):
  1583. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  1584. vsphere.rename_datastore('current_ds_name', 'new_ds_name')
  1585. self.assertEqual(excinfo.exception.strerror,
  1586. 'Datastore \'current_ds_name\' was not found')
  1587. def test_rename_datastore_call(self):
  1588. vsphere.rename_datastore('current_ds_name', 'new_ds_name')
  1589. self.mock_rename_datastore.assert_called_once_with(
  1590. self.mock_ds_ref, 'new_ds_name')
  1591. class _GetProxyTargetTestCase(TestCase, LoaderModuleMockMixin):
  1592. '''
  1593. Tests for salt.modules.vsphere._get_proxy_target
  1594. '''
  1595. def setup_loader_modules(self):
  1596. return {
  1597. vsphere: {
  1598. '__virtual__': MagicMock(return_value='vsphere'),
  1599. '_get_proxy_connection_details': MagicMock(),
  1600. 'get_proxy_type': MagicMock(return_value='esxdatacenter')
  1601. }
  1602. }
  1603. def setUp(self):
  1604. attrs = (('mock_si', MagicMock()),
  1605. ('mock_dc', MagicMock()),
  1606. ('mock_cl', MagicMock()),
  1607. ('mock_root', MagicMock()))
  1608. for attr, mock_obj in attrs:
  1609. setattr(self, attr, mock_obj)
  1610. self.addCleanup(delattr, self, attr)
  1611. attrs = (('mock_get_datacenter', MagicMock(return_value=self.mock_dc)),
  1612. ('mock_get_cluster', MagicMock(return_value=self.mock_cl)),
  1613. ('mock_get_root_folder',
  1614. MagicMock(return_value=self.mock_root)))
  1615. for attr, mock_obj in attrs:
  1616. setattr(self, attr, mock_obj)
  1617. self.addCleanup(delattr, self, attr)
  1618. patches = (
  1619. ('salt.modules.vsphere.get_proxy_type',
  1620. MagicMock(return_value='esxcluster')),
  1621. ('salt.utils.vmware.is_connection_to_a_vcenter',
  1622. MagicMock(return_value=True)),
  1623. ('salt.modules.vsphere._get_esxcluster_proxy_details',
  1624. MagicMock(return_value=(None, None, None, None, None, None, None,
  1625. None, 'datacenter', 'cluster'))),
  1626. ('salt.modules.vsphere._get_esxdatacenter_proxy_details',
  1627. MagicMock(return_value=(None, None, None, None, None, None, None,
  1628. None, 'datacenter'))),
  1629. ('salt.utils.vmware.get_datacenter', self.mock_get_datacenter),
  1630. ('salt.utils.vmware.get_cluster', self.mock_get_cluster),
  1631. ('salt.utils.vmware.get_root_folder', self.mock_get_root_folder))
  1632. for module, mock_obj in patches:
  1633. patcher = patch(module, mock_obj)
  1634. patcher.start()
  1635. self.addCleanup(patcher.stop)
  1636. def test_supported_proxies(self):
  1637. supported_proxies = ['esxcluster', 'esxdatacenter']
  1638. for proxy_type in supported_proxies:
  1639. with patch('salt.modules.vsphere.get_proxy_type',
  1640. MagicMock(return_value=proxy_type)):
  1641. vsphere._get_proxy_target(self.mock_si)
  1642. def test_connected_to_esxi(self):
  1643. with patch('salt.utils.vmware.is_connection_to_a_vcenter',
  1644. MagicMock(return_value=False)):
  1645. with self.assertRaises(CommandExecutionError) as excinfo:
  1646. vsphere._get_proxy_target(self.mock_si)
  1647. self.assertEqual(excinfo.exception.strerror,
  1648. '\'_get_proxy_target\' not supported when '
  1649. 'connected via the ESXi host')
  1650. def test_get_cluster_call(self):
  1651. vsphere._get_proxy_target(self.mock_si)
  1652. self.mock_get_datacenter.assert_called_once_with(self.mock_si,
  1653. 'datacenter')
  1654. self.mock_get_cluster.assert_called_once_with(self.mock_dc, 'cluster')
  1655. def test_esxcluster_proxy_return(self):
  1656. with patch('salt.modules.vsphere.get_proxy_type',
  1657. MagicMock(return_value='esxcluster')):
  1658. ret = vsphere._get_proxy_target(self.mock_si)
  1659. self.assertEqual(ret, self.mock_cl)
  1660. def test_get_datacenter_call(self):
  1661. with patch('salt.modules.vsphere.get_proxy_type',
  1662. MagicMock(return_value='esxdatacenter')):
  1663. vsphere._get_proxy_target(self.mock_si)
  1664. self.mock_get_datacenter.assert_called_once_with(self.mock_si,
  1665. 'datacenter')
  1666. self.assertEqual(self.mock_get_cluster.call_count, 0)
  1667. def test_esxdatacenter_proxy_return(self):
  1668. with patch('salt.modules.vsphere.get_proxy_type',
  1669. MagicMock(return_value='esxdatacenter')):
  1670. ret = vsphere._get_proxy_target(self.mock_si)
  1671. self.assertEqual(ret, self.mock_dc)
  1672. def test_vcenter_proxy_return(self):
  1673. with patch('salt.modules.vsphere.get_proxy_type',
  1674. MagicMock(return_value='vcenter')):
  1675. ret = vsphere._get_proxy_target(self.mock_si)
  1676. self.mock_get_root_folder.assert_called_once_with(self.mock_si)
  1677. self.assertEqual(ret, self.mock_root)