test_vmware.py 46 KB

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