test_vmware.py 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. """
  2. :codeauthor: `Nitin Madhok <nmadhok@clemson.edu>`
  3. tests.unit.cloud.clouds.vmware_test
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. """
  6. import copy
  7. from salt import config
  8. from salt.cloud.clouds import vmware
  9. from salt.exceptions import SaltCloudSystemExit
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, Mock, patch
  12. from tests.support.unit import TestCase, skipIf
  13. # Attempt to import pyVim and pyVmomi libs
  14. HAS_LIBS = True
  15. # pylint: disable=import-error,no-name-in-module,unused-import
  16. try:
  17. from pyVim.connect import SmartConnect, Disconnect
  18. from pyVmomi import vim, vmodl
  19. except ImportError:
  20. HAS_LIBS = False
  21. # pylint: enable=import-error,no-name-in-module,unused-import
  22. # Global Variables
  23. PROVIDER_CONFIG = {
  24. "vcenter01": {
  25. "vmware": {
  26. "driver": "vmware",
  27. "url": "vcenter01.domain.com",
  28. "user": "DOMAIN\\user",
  29. "password": "verybadpass",
  30. }
  31. }
  32. }
  33. VM_NAME = "test-vm"
  34. PROFILE = {
  35. "base-gold": {
  36. "provider": "vcenter01:vmware",
  37. "datastore": "Datastore1",
  38. "resourcepool": "Resources",
  39. "folder": "vm",
  40. }
  41. }
  42. class ExtendedTestCase(TestCase, LoaderModuleMockMixin):
  43. """
  44. Extended TestCase class containing additional helper methods.
  45. """
  46. def setup_loader_modules(self):
  47. return {vmware: {"__active_provider_name__": ""}}
  48. def assertRaisesWithMessage(self, exc_type, exc_msg, func, *args, **kwargs):
  49. try:
  50. func(*args, **kwargs)
  51. self.assertFail()
  52. except Exception as exc: # pylint: disable=broad-except
  53. self.assertEqual(type(exc), exc_type)
  54. self.assertEqual(exc.message, exc_msg)
  55. @skipIf(not HAS_LIBS, "Install pyVmomi to be able to run this test.")
  56. class VMwareTestCase(ExtendedTestCase):
  57. """
  58. Unit TestCase for salt.cloud.clouds.vmware module.
  59. """
  60. def test_test_vcenter_connection_call(self):
  61. """
  62. Tests that a SaltCloudSystemExit is raised when trying to call test_vcenter_connection
  63. with anything other than --function or -f.
  64. """
  65. self.assertRaises(
  66. SaltCloudSystemExit, vmware.test_vcenter_connection, call="action"
  67. )
  68. def test_get_vcenter_version_call(self):
  69. """
  70. Tests that a SaltCloudSystemExit is raised when trying to call get_vcenter_version
  71. with anything other than --function or -f.
  72. """
  73. self.assertRaises(
  74. SaltCloudSystemExit, vmware.get_vcenter_version, call="action"
  75. )
  76. def test_avail_images_call(self):
  77. """
  78. Tests that a SaltCloudSystemExit is raised when trying to call avail_images
  79. with --action or -a.
  80. """
  81. self.assertRaises(SaltCloudSystemExit, vmware.avail_images, call="action")
  82. def test_avail_locations_call(self):
  83. """
  84. Tests that a SaltCloudSystemExit is raised when trying to call avail_locations
  85. with --action or -a.
  86. """
  87. self.assertRaises(SaltCloudSystemExit, vmware.avail_locations, call="action")
  88. def test_avail_sizes_call(self):
  89. """
  90. Tests that a SaltCloudSystemExit is raised when trying to call avail_sizes
  91. with --action or -a.
  92. """
  93. self.assertRaises(SaltCloudSystemExit, vmware.avail_sizes, call="action")
  94. def test_list_datacenters_call(self):
  95. """
  96. Tests that a SaltCloudSystemExit is raised when trying to call list_datacenters
  97. with anything other than --function or -f.
  98. """
  99. self.assertRaises(SaltCloudSystemExit, vmware.list_datacenters, call="action")
  100. def test_list_clusters_call(self):
  101. """
  102. Tests that a SaltCloudSystemExit is raised when trying to call list_clusters
  103. with anything other than --function or -f.
  104. """
  105. self.assertRaises(SaltCloudSystemExit, vmware.list_clusters, call="action")
  106. def test_list_datastore_clusters_call(self):
  107. """
  108. Tests that a SaltCloudSystemExit is raised when trying to call list_datastore_clusters
  109. with anything other than --function or -f.
  110. """
  111. self.assertRaises(
  112. SaltCloudSystemExit, vmware.list_datastore_clusters, call="action"
  113. )
  114. def test_list_datastores_call(self):
  115. """
  116. Tests that a SaltCloudSystemExit is raised when trying to call list_datastores
  117. with anything other than --function or -f.
  118. """
  119. self.assertRaises(SaltCloudSystemExit, vmware.list_datastores, call="action")
  120. def test_list_hosts_call(self):
  121. """
  122. Tests that a SaltCloudSystemExit is raised when trying to call list_hosts
  123. with anything other than --function or -f.
  124. """
  125. self.assertRaises(SaltCloudSystemExit, vmware.list_hosts, call="action")
  126. def test_list_resourcepools_call(self):
  127. """
  128. Tests that a SaltCloudSystemExit is raised when trying to call list_resourcepools
  129. with anything other than --function or -f.
  130. """
  131. self.assertRaises(SaltCloudSystemExit, vmware.list_resourcepools, call="action")
  132. def test_list_networks_call(self):
  133. """
  134. Tests that a SaltCloudSystemExit is raised when trying to call list_networks
  135. with anything other than --function or -f.
  136. """
  137. self.assertRaises(SaltCloudSystemExit, vmware.list_networks, call="action")
  138. def test_list_nodes_call(self):
  139. """
  140. Tests that a SaltCloudSystemExit is raised when trying to call list_nodes
  141. with --action or -a.
  142. """
  143. self.assertRaises(SaltCloudSystemExit, vmware.list_nodes, call="action")
  144. def test_list_nodes_min_call(self):
  145. """
  146. Tests that a SaltCloudSystemExit is raised when trying to call list_nodes_min
  147. with --action or -a.
  148. """
  149. self.assertRaises(SaltCloudSystemExit, vmware.list_nodes_min, call="action")
  150. def test_list_nodes_full_call(self):
  151. """
  152. Tests that a SaltCloudSystemExit is raised when trying to call list_nodes_full
  153. with --action or -a.
  154. """
  155. self.assertRaises(SaltCloudSystemExit, vmware.list_nodes_full, call="action")
  156. def test_list_nodes_select_call(self):
  157. """
  158. Tests that a SaltCloudSystemExit is raised when trying to call list_nodes_full
  159. with --action or -a.
  160. """
  161. self.assertRaises(SaltCloudSystemExit, vmware.list_nodes_select, call="action")
  162. def test_list_folders_call(self):
  163. """
  164. Tests that a SaltCloudSystemExit is raised when trying to call list_folders
  165. with anything other than --function or -f.
  166. """
  167. self.assertRaises(SaltCloudSystemExit, vmware.list_folders, call="action")
  168. def test_list_snapshots_call(self):
  169. """
  170. Tests that a SaltCloudSystemExit is raised when trying to call list_snapshots
  171. with anything other than --function or -f.
  172. """
  173. self.assertRaises(SaltCloudSystemExit, vmware.list_snapshots, call="action")
  174. def test_list_hosts_by_cluster_call(self):
  175. """
  176. Tests that a SaltCloudSystemExit is raised when trying to call list_hosts_by_cluster
  177. with anything other than --function or -f.
  178. """
  179. self.assertRaises(
  180. SaltCloudSystemExit, vmware.list_hosts_by_cluster, call="action"
  181. )
  182. def test_list_clusters_by_datacenter_call(self):
  183. """
  184. Tests that a SaltCloudSystemExit is raised when trying to call list_clusters_by_datacenter
  185. with anything other than --function or -f.
  186. """
  187. self.assertRaises(
  188. SaltCloudSystemExit, vmware.list_clusters_by_datacenter, call="action"
  189. )
  190. def test_list_hosts_by_datacenter_call(self):
  191. """
  192. Tests that a SaltCloudSystemExit is raised when trying to call list_hosts_by_datacenter
  193. with anything other than --function or -f.
  194. """
  195. self.assertRaises(
  196. SaltCloudSystemExit, vmware.list_hosts_by_datacenter, call="action"
  197. )
  198. def test_list_hbas_call(self):
  199. """
  200. Tests that a SaltCloudSystemExit is raised when trying to call list_hbas
  201. with anything other than --function or -f.
  202. """
  203. self.assertRaises(SaltCloudSystemExit, vmware.list_hbas, call="action")
  204. def test_list_dvs_call(self):
  205. """
  206. Tests that a SaltCloudSystemExit is raised when trying to call list_dvs
  207. with anything other than --function or -f.
  208. """
  209. self.assertRaises(SaltCloudSystemExit, vmware.list_dvs, call="action")
  210. def test_list_vapps_call(self):
  211. """
  212. Tests that a SaltCloudSystemExit is raised when trying to call list_vapps
  213. with anything other than --function or -f.
  214. """
  215. self.assertRaises(SaltCloudSystemExit, vmware.list_vapps, call="action")
  216. def test_list_templates_call(self):
  217. """
  218. Tests that a SaltCloudSystemExit is raised when trying to call list_templates
  219. with anything other than --function or -f.
  220. """
  221. self.assertRaises(SaltCloudSystemExit, vmware.list_templates, call="action")
  222. def test_create_datacenter_call(self):
  223. """
  224. Tests that a SaltCloudSystemExit is raised when trying to call create_datacenter
  225. with anything other than --function or -f.
  226. """
  227. self.assertRaises(SaltCloudSystemExit, vmware.create_datacenter, call="action")
  228. def test_create_cluster_call(self):
  229. """
  230. Tests that a SaltCloudSystemExit is raised when trying to call create_cluster
  231. with anything other than --function or -f.
  232. """
  233. self.assertRaises(SaltCloudSystemExit, vmware.create_cluster, call="action")
  234. def test_rescan_hba_call(self):
  235. """
  236. Tests that a SaltCloudSystemExit is raised when trying to call rescan_hba
  237. with anything other than --function or -f.
  238. """
  239. self.assertRaises(SaltCloudSystemExit, vmware.rescan_hba, call="action")
  240. def test_upgrade_tools_all_call(self):
  241. """
  242. Tests that a SaltCloudSystemExit is raised when trying to call upgrade_tools_all
  243. with anything other than --function or -f.
  244. """
  245. self.assertRaises(SaltCloudSystemExit, vmware.upgrade_tools_all, call="action")
  246. def test_enter_maintenance_mode_call(self):
  247. """
  248. Tests that a SaltCloudSystemExit is raised when trying to call enter_maintenance_mode
  249. with anything other than --function or -f.
  250. """
  251. self.assertRaises(
  252. SaltCloudSystemExit, vmware.enter_maintenance_mode, call="action"
  253. )
  254. def test_exit_maintenance_mode_call(self):
  255. """
  256. Tests that a SaltCloudSystemExit is raised when trying to call exit_maintenance_mode
  257. with anything other than --function or -f.
  258. """
  259. self.assertRaises(
  260. SaltCloudSystemExit, vmware.exit_maintenance_mode, call="action"
  261. )
  262. def test_create_folder_call(self):
  263. """
  264. Tests that a SaltCloudSystemExit is raised when trying to call create_folder
  265. with anything other than --function or -f.
  266. """
  267. self.assertRaises(SaltCloudSystemExit, vmware.create_folder, call="action")
  268. def test_add_host_call(self):
  269. """
  270. Tests that a SaltCloudSystemExit is raised when trying to call add_host
  271. with anything other than --function or -f.
  272. """
  273. self.assertRaises(SaltCloudSystemExit, vmware.add_host, call="action")
  274. def test_remove_host_call(self):
  275. """
  276. Tests that a SaltCloudSystemExit is raised when trying to call remove_host
  277. with anything other than --function or -f.
  278. """
  279. self.assertRaises(SaltCloudSystemExit, vmware.remove_host, call="action")
  280. def test_connect_host_call(self):
  281. """
  282. Tests that a SaltCloudSystemExit is raised when trying to call connect_host
  283. with anything other than --function or -f.
  284. """
  285. self.assertRaises(SaltCloudSystemExit, vmware.connect_host, call="action")
  286. def test_disconnect_host_call(self):
  287. """
  288. Tests that a SaltCloudSystemExit is raised when trying to call disconnect_host
  289. with anything other than --function or -f.
  290. """
  291. self.assertRaises(SaltCloudSystemExit, vmware.disconnect_host, call="action")
  292. def test_reboot_host_call(self):
  293. """
  294. Tests that a SaltCloudSystemExit is raised when trying to call reboot_host
  295. with anything other than --function or -f.
  296. """
  297. self.assertRaises(SaltCloudSystemExit, vmware.reboot_host, call="action")
  298. def test_create_datastore_cluster_call(self):
  299. """
  300. Tests that a SaltCloudSystemExit is raised when trying to call create_datastore_cluster
  301. with anything other than --function or -f.
  302. """
  303. self.assertRaises(
  304. SaltCloudSystemExit, vmware.create_datastore_cluster, call="action"
  305. )
  306. def test_show_instance_call(self):
  307. """
  308. Tests that a SaltCloudSystemExit is raised when trying to call show_instance
  309. with anything other than --action or -a.
  310. """
  311. self.assertRaises(
  312. SaltCloudSystemExit, vmware.show_instance, name=VM_NAME, call="function"
  313. )
  314. def test_start_call(self):
  315. """
  316. Tests that a SaltCloudSystemExit is raised when trying to call start
  317. with anything other than --action or -a.
  318. """
  319. self.assertRaises(
  320. SaltCloudSystemExit, vmware.start, name=VM_NAME, call="function"
  321. )
  322. def test_stop_call(self):
  323. """
  324. Tests that a SaltCloudSystemExit is raised when trying to call stop
  325. with anything other than --action or -a.
  326. """
  327. self.assertRaises(
  328. SaltCloudSystemExit, vmware.stop, name=VM_NAME, call="function"
  329. )
  330. def test_suspend_call(self):
  331. """
  332. Tests that a SaltCloudSystemExit is raised when trying to call suspend
  333. with anything other than --action or -a.
  334. """
  335. self.assertRaises(
  336. SaltCloudSystemExit, vmware.suspend, name=VM_NAME, call="function"
  337. )
  338. def test_reset_call(self):
  339. """
  340. Tests that a SaltCloudSystemExit is raised when trying to call reset
  341. with anything other than --action or -a.
  342. """
  343. self.assertRaises(
  344. SaltCloudSystemExit, vmware.reset, name=VM_NAME, call="function"
  345. )
  346. def test_terminate_call(self):
  347. """
  348. Tests that a SaltCloudSystemExit is raised when trying to call terminate
  349. with anything other than --action or -a.
  350. """
  351. self.assertRaises(
  352. SaltCloudSystemExit, vmware.terminate, name=VM_NAME, call="function"
  353. )
  354. def test_destroy_call(self):
  355. """
  356. Tests that a SaltCloudSystemExit is raised when trying to call destroy
  357. with --function or -f.
  358. """
  359. self.assertRaises(
  360. SaltCloudSystemExit, vmware.destroy, name=VM_NAME, call="function"
  361. )
  362. def test_shutdown_host_call(self):
  363. """
  364. Tests that a SaltCloudSystemExit is raised when trying to call convert_to_template
  365. with anything other than --action or -a.
  366. """
  367. with patch.object(vmware, "_get_si", Mock()), patch(
  368. "salt.utils.vmware.get_mor_by_property", Mock()
  369. ):
  370. self.assertRaises(
  371. SaltCloudSystemExit,
  372. vmware.shutdown_host,
  373. kwargs={"host": VM_NAME},
  374. call="action",
  375. )
  376. def test_upgrade_tools_call(self):
  377. """
  378. Tests that a SaltCloudSystemExit is raised when trying to call upgrade_tools
  379. with anything other than --action or -a.
  380. """
  381. self.assertRaises(
  382. SaltCloudSystemExit, vmware.upgrade_tools, name=VM_NAME, call="function"
  383. )
  384. def test_create_snapshot_call(self):
  385. """
  386. Tests that a SaltCloudSystemExit is raised when trying to call create_snapshot
  387. with anything other than --action or -a.
  388. """
  389. self.assertRaises(
  390. SaltCloudSystemExit, vmware.create_snapshot, name=VM_NAME, call="function"
  391. )
  392. def test_revert_to_snapshot_call(self):
  393. """
  394. Tests that a SaltCloudSystemExit is raised when trying to call revert_to_snapshot
  395. with anything other than --action or -a.
  396. """
  397. self.assertRaises(
  398. SaltCloudSystemExit,
  399. vmware.revert_to_snapshot,
  400. name=VM_NAME,
  401. call="function",
  402. )
  403. def test_remove_snapshot_call(self):
  404. """
  405. Tests that a SaltCloudSystemExit is raised when trying to call remove_snapshot
  406. with anything other than --action or -a.
  407. """
  408. self.assertRaises(
  409. SaltCloudSystemExit,
  410. vmware.remove_snapshot,
  411. name=VM_NAME,
  412. kwargs={"snapshot_name": "mySnapshot"},
  413. call="function",
  414. )
  415. def test_remove_snapshot_call_no_snapshot_name_in_kwargs(self):
  416. """
  417. Tests that a SaltCloudSystemExit is raised when name is not present in kwargs.
  418. """
  419. self.assertRaises(
  420. SaltCloudSystemExit, vmware.remove_snapshot, name=VM_NAME, call="action"
  421. )
  422. def test_remove_all_snapshots_call(self):
  423. """
  424. Tests that a SaltCloudSystemExit is raised when trying to call remove_all_snapshots
  425. with anything other than --action or -a.
  426. """
  427. self.assertRaises(
  428. SaltCloudSystemExit,
  429. vmware.remove_all_snapshots,
  430. name=VM_NAME,
  431. call="function",
  432. )
  433. def test_convert_to_template_call(self):
  434. """
  435. Tests that a SaltCloudSystemExit is raised when trying to call convert_to_template
  436. with anything other than --action or -a.
  437. """
  438. self.assertRaises(
  439. SaltCloudSystemExit,
  440. vmware.convert_to_template,
  441. name=VM_NAME,
  442. call="function",
  443. )
  444. def test_avail_sizes(self):
  445. """
  446. Tests that avail_sizes returns an empty dictionary.
  447. """
  448. self.assertEqual(vmware.avail_sizes(call="foo"), {})
  449. def test_create_datacenter_no_kwargs(self):
  450. """
  451. Tests that a SaltCloudSystemExit is raised when no kwargs are provided to
  452. create_datacenter.
  453. """
  454. self.assertRaises(
  455. SaltCloudSystemExit, vmware.create_datacenter, kwargs=None, call="function"
  456. )
  457. def test_create_datacenter_no_name_in_kwargs(self):
  458. """
  459. Tests that a SaltCloudSystemExit is raised when name is not present in
  460. kwargs that are provided to create_datacenter.
  461. """
  462. self.assertRaises(
  463. SaltCloudSystemExit,
  464. vmware.create_datacenter,
  465. kwargs={"foo": "bar"},
  466. call="function",
  467. )
  468. def test_create_datacenter_name_too_short(self):
  469. """
  470. Tests that a SaltCloudSystemExit is raised when name is present in kwargs
  471. that are provided to create_datacenter but is an empty string.
  472. """
  473. self.assertRaises(
  474. SaltCloudSystemExit,
  475. vmware.create_datacenter,
  476. kwargs={"name": ""},
  477. call="function",
  478. )
  479. def test_create_datacenter_name_too_long(self):
  480. """
  481. Tests that a SaltCloudSystemExit is raised when name is present in kwargs
  482. that are provided to create_datacenter but is a string with length <= 80.
  483. """
  484. self.assertRaises(
  485. SaltCloudSystemExit,
  486. vmware.create_datacenter,
  487. kwargs={
  488. "name": "cCD2GgJGPG1DUnPeFBoPeqtdmUxIWxDoVFbA14vIG0BPoUECkgbRMnnY6gaUPBvIDCcsZ5HU48ubgQu5c"
  489. },
  490. call="function",
  491. )
  492. def test_create_cluster_no_kwargs(self):
  493. """
  494. Tests that a SaltCloudSystemExit is raised when no kwargs are provided to
  495. create_cluster.
  496. """
  497. self.assertRaises(
  498. SaltCloudSystemExit, vmware.create_cluster, kwargs=None, call="function"
  499. )
  500. def test_create_cluster_no_name_no_datacenter_in_kwargs(self):
  501. """
  502. Tests that a SaltCloudSystemExit is raised when neither the name nor the
  503. datacenter is present in kwargs that are provided to create_cluster.
  504. """
  505. self.assertRaises(
  506. SaltCloudSystemExit,
  507. vmware.create_cluster,
  508. kwargs={"foo": "bar"},
  509. call="function",
  510. )
  511. def test_create_cluster_no_datacenter_in_kwargs(self):
  512. """
  513. Tests that a SaltCloudSystemExit is raised when the name is present but the
  514. datacenter is not present in kwargs that are provided to create_cluster.
  515. """
  516. self.assertRaises(
  517. SaltCloudSystemExit,
  518. vmware.create_cluster,
  519. kwargs={"name": "my-cluster"},
  520. call="function",
  521. )
  522. def test_create_cluster_no_name_in_kwargs(self):
  523. """
  524. Tests that a SaltCloudSystemExit is raised when the datacenter is present
  525. but the name is not present in kwargs that are provided to create_cluster.
  526. """
  527. self.assertRaises(
  528. SaltCloudSystemExit,
  529. vmware.create_cluster,
  530. kwargs={"datacenter": "my-datacenter"},
  531. call="function",
  532. )
  533. def test_rescan_hba_no_kwargs(self):
  534. """
  535. Tests that a SaltCloudSystemExit is raised when no kwargs are provided to
  536. rescan_hba.
  537. """
  538. self.assertRaises(
  539. SaltCloudSystemExit, vmware.rescan_hba, kwargs=None, call="function"
  540. )
  541. def test_rescan_hba_no_host_in_kwargs(self):
  542. """
  543. Tests that a SaltCloudSystemExit is raised when host is not present in
  544. kwargs that are provided to rescan_hba.
  545. """
  546. self.assertRaises(
  547. SaltCloudSystemExit,
  548. vmware.rescan_hba,
  549. kwargs={"foo": "bar"},
  550. call="function",
  551. )
  552. def test_create_snapshot_no_kwargs(self):
  553. """
  554. Tests that a SaltCloudSystemExit is raised when no kwargs are provided to
  555. create_snapshot.
  556. """
  557. self.assertRaises(
  558. SaltCloudSystemExit,
  559. vmware.create_snapshot,
  560. name=VM_NAME,
  561. kwargs=None,
  562. call="action",
  563. )
  564. def test_create_snapshot_no_snapshot_name_in_kwargs(self):
  565. """
  566. Tests that a SaltCloudSystemExit is raised when snapshot_name is not present
  567. in kwargs that are provided to create_snapshot.
  568. """
  569. self.assertRaises(
  570. SaltCloudSystemExit,
  571. vmware.create_snapshot,
  572. name=VM_NAME,
  573. kwargs={"foo": "bar"},
  574. call="action",
  575. )
  576. def test_add_host_no_esxi_host_user_in_config(self):
  577. """
  578. Tests that a SaltCloudSystemExit is raised when esxi_host_user is not
  579. specified in the cloud provider configuration when calling add_host.
  580. """
  581. with patch.dict(vmware.__opts__, {"providers": PROVIDER_CONFIG}, clean=True):
  582. self.assertRaisesWithMessage(
  583. SaltCloudSystemExit,
  584. "You must specify the ESXi host username in your providers config.",
  585. vmware.add_host,
  586. kwargs=None,
  587. call="function",
  588. )
  589. def test_add_host_no_esxi_host_password_in_config(self):
  590. """
  591. Tests that a SaltCloudSystemExit is raised when esxi_host_password is not
  592. specified in the cloud provider configuration when calling add_host.
  593. """
  594. provider_config_additions = {
  595. "esxi_host_user": "root",
  596. }
  597. provider_config = copy.deepcopy(PROVIDER_CONFIG)
  598. provider_config["vcenter01"]["vmware"].update(provider_config_additions)
  599. with patch.dict(vmware.__opts__, {"providers": provider_config}, clean=True):
  600. self.assertRaisesWithMessage(
  601. SaltCloudSystemExit,
  602. "You must specify the ESXi host password in your providers config.",
  603. vmware.add_host,
  604. kwargs=None,
  605. call="function",
  606. )
  607. def test_no_clonefrom_just_image(self):
  608. """
  609. Tests that the profile is configured correctly when deploying using an image
  610. """
  611. profile_additions = {"image": "some-image.iso"}
  612. provider_config = copy.deepcopy(PROVIDER_CONFIG)
  613. profile = copy.deepcopy(PROFILE)
  614. profile["base-gold"].update(profile_additions)
  615. provider_config_additions = {"profiles": profile}
  616. provider_config["vcenter01"]["vmware"].update(provider_config_additions)
  617. vm_ = {"profile": profile}
  618. with patch.dict(vmware.__opts__, {"providers": provider_config}, clean=True):
  619. self.assertEqual(
  620. config.is_profile_configured(
  621. vmware.__opts__, "vcenter01:vmware", "base-gold", vm_=vm_
  622. ),
  623. True,
  624. )
  625. def test_just_clonefrom(self):
  626. """
  627. Tests that the profile is configured correctly when deploying by cloning from a template
  628. """
  629. profile_additions = {
  630. "clonefrom": "test-template",
  631. "image": "should ignore image",
  632. }
  633. provider_config = copy.deepcopy(PROVIDER_CONFIG)
  634. profile = copy.deepcopy(PROFILE)
  635. profile["base-gold"].update(profile_additions)
  636. provider_config_additions = {"profiles": profile}
  637. provider_config["vcenter01"]["vmware"].update(provider_config_additions)
  638. vm_ = {"profile": profile}
  639. with patch.dict(vmware.__opts__, {"providers": provider_config}, clean=True):
  640. self.assertEqual(
  641. config.is_profile_configured(
  642. vmware.__opts__, "vcenter01:vmware", "base-gold", vm_=vm_
  643. ),
  644. True,
  645. )
  646. def test_add_new_ide_controller_helper(self):
  647. """
  648. Tests that creating a new controller, ensuring that it will generate a controller key
  649. if one is not provided
  650. """
  651. with patch(
  652. "salt.cloud.clouds.vmware.randint", return_value=101
  653. ) as randint_mock:
  654. controller_label = "Some label"
  655. bus_number = 1
  656. spec = vmware._add_new_ide_controller_helper(
  657. controller_label, None, bus_number
  658. )
  659. self.assertEqual(spec.device.key, randint_mock.return_value)
  660. spec = vmware._add_new_ide_controller_helper(
  661. controller_label, 200, bus_number
  662. )
  663. self.assertEqual(spec.device.key, 200)
  664. self.assertEqual(spec.device.busNumber, bus_number)
  665. self.assertEqual(spec.device.deviceInfo.label, controller_label)
  666. self.assertEqual(spec.device.deviceInfo.summary, controller_label)
  667. def test_manage_devices_just_cd(self):
  668. """
  669. Tests that when adding IDE/CD drives, controller keys will be in the apparent
  670. safe-range on ESX 5.5 but randomly generated on other versions (i.e. 6)
  671. """
  672. device_map = {
  673. "ide": {"IDE 0": {}, "IDE 1": {}},
  674. "cd": {"CD/DVD Drive 1": {"controller": "IDE 0"}},
  675. }
  676. with patch(
  677. "salt.cloud.clouds.vmware.get_vcenter_version",
  678. return_value="VMware ESXi 5.5.0",
  679. ):
  680. specs = vmware._manage_devices(device_map, vm=None)["device_specs"]
  681. self.assertEqual(
  682. specs[0].device.key, vmware.SAFE_ESX_5_5_CONTROLLER_KEY_INDEX
  683. )
  684. self.assertEqual(
  685. specs[1].device.key, vmware.SAFE_ESX_5_5_CONTROLLER_KEY_INDEX + 1
  686. )
  687. self.assertEqual(
  688. specs[2].device.controllerKey, vmware.SAFE_ESX_5_5_CONTROLLER_KEY_INDEX
  689. )
  690. with patch(
  691. "salt.cloud.clouds.vmware.get_vcenter_version", return_value="VMware ESXi 6"
  692. ):
  693. with patch(
  694. "salt.cloud.clouds.vmware.randint", return_value=100
  695. ) as first_key:
  696. specs = vmware._manage_devices(device_map, vm=None)["device_specs"]
  697. self.assertEqual(specs[0].device.key, first_key.return_value)
  698. self.assertEqual(specs[2].device.controllerKey, first_key.return_value)
  699. def test_add_host_no_host_in_kwargs(self):
  700. """
  701. Tests that a SaltCloudSystemExit is raised when host is not present in
  702. kwargs that are provided to add_host.
  703. """
  704. provider_config_additions = {
  705. "esxi_host_user": "root",
  706. "esxi_host_password": "myhostpassword",
  707. }
  708. provider_config = copy.deepcopy(PROVIDER_CONFIG)
  709. provider_config["vcenter01"]["vmware"].update(provider_config_additions)
  710. with patch.dict(vmware.__opts__, {"providers": provider_config}, clean=True):
  711. self.assertRaisesWithMessage(
  712. SaltCloudSystemExit,
  713. "You must specify either the IP or DNS name of the host system.",
  714. vmware.add_host,
  715. kwargs={"foo": "bar"},
  716. call="function",
  717. )
  718. def test_add_host_both_cluster_and_datacenter_in_kwargs(self):
  719. """
  720. Tests that a SaltCloudSystemExit is raised when both cluster and datacenter
  721. are present in kwargs that are provided to add_host.
  722. """
  723. provider_config_additions = {
  724. "esxi_host_user": "root",
  725. "esxi_host_password": "myhostpassword",
  726. }
  727. provider_config = copy.deepcopy(PROVIDER_CONFIG)
  728. provider_config["vcenter01"]["vmware"].update(provider_config_additions)
  729. with patch.dict(vmware.__opts__, {"providers": provider_config}, clean=True):
  730. self.assertRaisesWithMessage(
  731. SaltCloudSystemExit,
  732. "You must specify either the cluster name or the datacenter name.",
  733. vmware.add_host,
  734. kwargs={
  735. "host": "my-esxi-host",
  736. "datacenter": "my-datacenter",
  737. "cluster": "my-cluster",
  738. },
  739. call="function",
  740. )
  741. def test_add_host_neither_cluster_nor_datacenter_in_kwargs(self):
  742. """
  743. Tests that a SaltCloudSystemExit is raised when neither cluster nor
  744. datacenter is present in kwargs that are provided to add_host.
  745. """
  746. provider_config_additions = {
  747. "esxi_host_user": "root",
  748. "esxi_host_password": "myhostpassword",
  749. }
  750. provider_config = copy.deepcopy(PROVIDER_CONFIG)
  751. provider_config["vcenter01"]["vmware"].update(provider_config_additions)
  752. with patch.dict(vmware.__opts__, {"providers": provider_config}, clean=True):
  753. self.assertRaisesWithMessage(
  754. SaltCloudSystemExit,
  755. "You must specify either the cluster name or the datacenter name.",
  756. vmware.add_host,
  757. kwargs={"host": "my-esxi-host"},
  758. call="function",
  759. )
  760. @skipIf(HAS_LIBS is False, "Install pyVmomi to be able to run this unit test.")
  761. def test_add_host_cluster_not_exists(self):
  762. """
  763. Tests that a SaltCloudSystemExit is raised when the specified cluster present
  764. in kwargs that are provided to add_host does not exist in the VMware
  765. environment.
  766. """
  767. with patch("salt.cloud.clouds.vmware._get_si", MagicMock(return_value=None)):
  768. with patch(
  769. "salt.utils.vmware.get_mor_by_property", MagicMock(return_value=None)
  770. ):
  771. provider_config_additions = {
  772. "esxi_host_user": "root",
  773. "esxi_host_password": "myhostpassword",
  774. }
  775. provider_config = copy.deepcopy(PROVIDER_CONFIG)
  776. provider_config["vcenter01"]["vmware"].update(provider_config_additions)
  777. with patch.dict(
  778. vmware.__opts__, {"providers": provider_config}, clean=True
  779. ):
  780. self.assertRaisesWithMessage(
  781. SaltCloudSystemExit,
  782. "Specified cluster does not exist.",
  783. vmware.add_host,
  784. kwargs={"host": "my-esxi-host", "cluster": "my-cluster"},
  785. call="function",
  786. )
  787. @skipIf(HAS_LIBS is False, "Install pyVmomi to be able to run this unit test.")
  788. def test_add_host_datacenter_not_exists(self):
  789. """
  790. Tests that a SaltCloudSystemExit is raised when the specified datacenter
  791. present in kwargs that are provided to add_host does not exist in the VMware
  792. environment.
  793. """
  794. with patch("salt.cloud.clouds.vmware._get_si", MagicMock(return_value=None)):
  795. with patch(
  796. "salt.utils.vmware.get_mor_by_property", MagicMock(return_value=None)
  797. ):
  798. provider_config_additions = {
  799. "esxi_host_user": "root",
  800. "esxi_host_password": "myhostpassword",
  801. }
  802. provider_config = copy.deepcopy(PROVIDER_CONFIG)
  803. provider_config["vcenter01"]["vmware"].update(provider_config_additions)
  804. with patch.dict(
  805. vmware.__opts__, {"providers": provider_config}, clean=True
  806. ):
  807. self.assertRaisesWithMessage(
  808. SaltCloudSystemExit,
  809. "Specified datacenter does not exist.",
  810. vmware.add_host,
  811. kwargs={"host": "my-esxi-host", "datacenter": "my-datacenter"},
  812. call="function",
  813. )
  814. def test_remove_host_no_kwargs(self):
  815. """
  816. Tests that a SaltCloudSystemExit is raised when no kwargs are provided to
  817. remove_host.
  818. """
  819. self.assertRaises(
  820. SaltCloudSystemExit, vmware.remove_host, kwargs=None, call="function"
  821. )
  822. def test_remove_host_no_host_in_kwargs(self):
  823. """
  824. Tests that a SaltCloudSystemExit is raised when host is not present in
  825. kwargs that are provided to remove_host.
  826. """
  827. self.assertRaises(
  828. SaltCloudSystemExit,
  829. vmware.remove_host,
  830. kwargs={"foo": "bar"},
  831. call="function",
  832. )
  833. @skipIf(HAS_LIBS is False, "Install pyVmomi to be able to run this unit test.")
  834. def test_remove_host_not_exists(self):
  835. """
  836. Tests that a SaltCloudSystemExit is raised when the specified host present
  837. in kwargs that are provided to remove_host does not exist in the VMware
  838. environment.
  839. """
  840. with patch("salt.cloud.clouds.vmware._get_si", MagicMock(return_value=None)):
  841. with patch(
  842. "salt.utils.vmware.get_mor_by_property", MagicMock(return_value=None)
  843. ):
  844. self.assertRaises(
  845. SaltCloudSystemExit,
  846. vmware.remove_host,
  847. kwargs={"host": "my-host"},
  848. call="function",
  849. )
  850. def test_connect_host_no_kwargs(self):
  851. """
  852. Tests that a SaltCloudSystemExit is raised when no kwargs are provided to
  853. connect_host.
  854. """
  855. self.assertRaises(
  856. SaltCloudSystemExit, vmware.connect_host, kwargs=None, call="function"
  857. )
  858. def test_connect_host_no_host_in_kwargs(self):
  859. """
  860. Tests that a SaltCloudSystemExit is raised when host is not present in
  861. kwargs that are provided to connect_host.
  862. """
  863. self.assertRaises(
  864. SaltCloudSystemExit,
  865. vmware.connect_host,
  866. kwargs={"foo": "bar"},
  867. call="function",
  868. )
  869. @skipIf(HAS_LIBS is False, "Install pyVmomi to be able to run this unit test.")
  870. def test_connect_host_not_exists(self):
  871. """
  872. Tests that a SaltCloudSystemExit is raised when the specified host present
  873. in kwargs that are provided to connect_host does not exist in the VMware
  874. environment.
  875. """
  876. with patch("salt.cloud.clouds.vmware._get_si", MagicMock(return_value=None)):
  877. with patch(
  878. "salt.utils.vmware.get_mor_by_property", MagicMock(return_value=None)
  879. ):
  880. self.assertRaises(
  881. SaltCloudSystemExit,
  882. vmware.connect_host,
  883. kwargs={"host": "my-host"},
  884. call="function",
  885. )
  886. def test_disconnect_host_no_kwargs(self):
  887. """
  888. Tests that a SaltCloudSystemExit is raised when no kwargs are provided to
  889. disconnect_host.
  890. """
  891. self.assertRaises(
  892. SaltCloudSystemExit, vmware.disconnect_host, kwargs=None, call="function"
  893. )
  894. def test_disconnect_host_no_host_in_kwargs(self):
  895. """
  896. Tests that a SaltCloudSystemExit is raised when host is not present in
  897. kwargs that are provided to disconnect_host.
  898. """
  899. self.assertRaises(
  900. SaltCloudSystemExit,
  901. vmware.disconnect_host,
  902. kwargs={"foo": "bar"},
  903. call="function",
  904. )
  905. @skipIf(HAS_LIBS is False, "Install pyVmomi to be able to run this unit test.")
  906. def test_disconnect_host_not_exists(self):
  907. """
  908. Tests that a SaltCloudSystemExit is raised when the specified host present
  909. in kwargs that are provided to disconnect_host does not exist in the VMware
  910. environment.
  911. """
  912. with patch("salt.cloud.clouds.vmware._get_si", MagicMock(return_value=None)):
  913. with patch(
  914. "salt.utils.vmware.get_mor_by_property", MagicMock(return_value=None)
  915. ):
  916. self.assertRaises(
  917. SaltCloudSystemExit,
  918. vmware.disconnect_host,
  919. kwargs={"host": "my-host"},
  920. call="function",
  921. )
  922. def test_reboot_host_no_kwargs(self):
  923. """
  924. Tests that a SaltCloudSystemExit is raised when no kwargs are provided to
  925. reboot_host.
  926. """
  927. self.assertRaises(
  928. SaltCloudSystemExit, vmware.reboot_host, kwargs=None, call="function"
  929. )
  930. def test_reboot_host_no_host_in_kwargs(self):
  931. """
  932. Tests that a SaltCloudSystemExit is raised when host is not present in
  933. kwargs that are provided to reboot_host.
  934. """
  935. self.assertRaises(
  936. SaltCloudSystemExit,
  937. vmware.reboot_host,
  938. kwargs={"foo": "bar"},
  939. call="function",
  940. )
  941. @skipIf(HAS_LIBS is False, "Install pyVmomi to be able to run this unit test.")
  942. def test_reboot_host_not_exists(self):
  943. """
  944. Tests that a SaltCloudSystemExit is raised when the specified host present
  945. in kwargs that are provided to connect_host does not exist in the VMware
  946. environment.
  947. """
  948. with patch("salt.cloud.clouds.vmware._get_si", MagicMock(return_value=None)):
  949. with patch(
  950. "salt.utils.vmware.get_mor_by_property", MagicMock(return_value=None)
  951. ):
  952. self.assertRaises(
  953. SaltCloudSystemExit,
  954. vmware.reboot_host,
  955. kwargs={"host": "my-host"},
  956. call="function",
  957. )
  958. def test_create_datastore_cluster_no_kwargs(self):
  959. """
  960. Tests that a SaltCloudSystemExit is raised when no kwargs are provided to
  961. create_datastore_cluster.
  962. """
  963. self.assertRaises(
  964. SaltCloudSystemExit,
  965. vmware.create_datastore_cluster,
  966. kwargs=None,
  967. call="function",
  968. )
  969. def test_create_datastore_cluster_no_name_in_kwargs(self):
  970. """
  971. Tests that a SaltCloudSystemExit is raised when name is not present in
  972. kwargs that are provided to create_datastore_cluster.
  973. """
  974. self.assertRaises(
  975. SaltCloudSystemExit,
  976. vmware.create_datastore_cluster,
  977. kwargs={"foo": "bar"},
  978. call="function",
  979. )
  980. def test_create_datastore_cluster_name_too_short(self):
  981. """
  982. Tests that a SaltCloudSystemExit is raised when name is present in kwargs
  983. that are provided to create_datastore_cluster but is an empty string.
  984. """
  985. self.assertRaises(
  986. SaltCloudSystemExit,
  987. vmware.create_datastore_cluster,
  988. kwargs={"name": ""},
  989. call="function",
  990. )
  991. def test_create_datastore_cluster_name_too_long(self):
  992. """
  993. Tests that a SaltCloudSystemExit is raised when name is present in kwargs
  994. that are provided to create_datastore_cluster but is a string with length <= 80.
  995. """
  996. self.assertRaises(
  997. SaltCloudSystemExit,
  998. vmware.create_datastore_cluster,
  999. kwargs={
  1000. "name": "cCD2GgJGPG1DUnPeFBoPeqtdmUxIWxDoVFbA14vIG0BPoUECkgbRMnnY6gaUPBvIDCcsZ5HU48ubgQu5c"
  1001. },
  1002. call="function",
  1003. )
  1004. def test__add_new_hard_disk_helper(self):
  1005. with patch("salt.cloud.clouds.vmware._get_si", MagicMock(return_value=None)):
  1006. with patch(
  1007. "salt.utils.vmware.get_mor_using_container_view",
  1008. side_effect=[None, None],
  1009. ):
  1010. self.assertRaises(
  1011. SaltCloudSystemExit,
  1012. vmware._add_new_hard_disk_helper,
  1013. disk_label="test",
  1014. size_gb=100,
  1015. unit_number=0,
  1016. datastore="whatever",
  1017. )
  1018. with patch(
  1019. "salt.utils.vmware.get_mor_using_container_view",
  1020. side_effect=["Datastore", None],
  1021. ):
  1022. self.assertRaises(
  1023. AttributeError,
  1024. vmware._add_new_hard_disk_helper,
  1025. disk_label="test",
  1026. size_gb=100,
  1027. unit_number=0,
  1028. datastore="whatever",
  1029. )
  1030. vmware.salt.utils.vmware.get_mor_using_container_view.assert_called_with(
  1031. None, vim.Datastore, "whatever"
  1032. )
  1033. with patch(
  1034. "salt.utils.vmware.get_mor_using_container_view",
  1035. side_effect=[None, "Cluster"],
  1036. ):
  1037. self.assertRaises(
  1038. AttributeError,
  1039. vmware._add_new_hard_disk_helper,
  1040. disk_label="test",
  1041. size_gb=100,
  1042. unit_number=0,
  1043. datastore="whatever",
  1044. )
  1045. vmware.salt.utils.vmware.get_mor_using_container_view.assert_called_with(
  1046. None, vim.StoragePod, "whatever"
  1047. )
  1048. class CloneFromSnapshotTest(TestCase):
  1049. """
  1050. Test functionality to clone from snapshot
  1051. """
  1052. @skipIf(HAS_LIBS is False, "Install pyVmomi to be able to run this unit test.")
  1053. def test_quick_linked_clone(self):
  1054. """
  1055. Test that disk move type is
  1056. set to createNewChildDiskBacking
  1057. """
  1058. self._test_clone_type(vmware.QUICK_LINKED_CLONE)
  1059. @skipIf(HAS_LIBS is False, "Install pyVmomi to be able to run this unit test.")
  1060. def test_current_state_linked_clone(self):
  1061. """
  1062. Test that disk move type is
  1063. set to moveChildMostDiskBacking
  1064. """
  1065. self._test_clone_type(vmware.CURRENT_STATE_LINKED_CLONE)
  1066. @skipIf(HAS_LIBS is False, "Install pyVmomi to be able to run this unit test.")
  1067. def test_copy_all_disks_full_clone(self):
  1068. """
  1069. Test that disk move type is
  1070. set to moveAllDiskBackingsAndAllowSharing
  1071. """
  1072. self._test_clone_type(vmware.COPY_ALL_DISKS_FULL_CLONE)
  1073. @skipIf(HAS_LIBS is False, "Install pyVmomi to be able to run this unit test.")
  1074. def test_flatten_all_all_disks_full_clone(self):
  1075. """
  1076. Test that disk move type is
  1077. set to moveAllDiskBackingsAndDisallowSharing
  1078. """
  1079. self._test_clone_type(vmware.FLATTEN_DISK_FULL_CLONE)
  1080. @skipIf(HAS_LIBS is False, "Install pyVmomi to be able to run this unit test.")
  1081. def test_raises_error_for_invalid_disk_move_type(self):
  1082. """
  1083. Test that invalid disk move type
  1084. raises error
  1085. """
  1086. with self.assertRaises(SaltCloudSystemExit):
  1087. self._test_clone_type("foobar")
  1088. def _test_clone_type(self, clone_type):
  1089. """
  1090. Assertions for checking that a certain clone type
  1091. works
  1092. """
  1093. obj_ref = MagicMock()
  1094. obj_ref.snapshot = vim.vm.Snapshot(None, None)
  1095. obj_ref.snapshot.currentSnapshot = vim.vm.Snapshot(None, None)
  1096. clone_spec = vmware.handle_snapshot(
  1097. vim.vm.ConfigSpec(),
  1098. obj_ref,
  1099. vim.vm.RelocateSpec(),
  1100. False,
  1101. {"snapshot": {"disk_move_type": clone_type}},
  1102. )
  1103. self.assertEqual(clone_spec.location.diskMoveType, clone_type)
  1104. obj_ref2 = MagicMock()
  1105. obj_ref2.snapshot = vim.vm.Snapshot(None, None)
  1106. obj_ref2.snapshot.currentSnapshot = vim.vm.Snapshot(None, None)
  1107. clone_spec2 = vmware.handle_snapshot(
  1108. vim.vm.ConfigSpec(),
  1109. obj_ref2,
  1110. vim.vm.RelocateSpec(),
  1111. True,
  1112. {"snapshot": {"disk_move_type": clone_type}},
  1113. )
  1114. self.assertEqual(clone_spec2.location.diskMoveType, clone_type)