test_vmware.py 46 KB

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