test_vmware.py 203 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Alexandru Bleotu <alexandru.bleotu@morganstanley.com>
  4. Tests for cluster related functions in salt.utils.vmware
  5. '''
  6. # Import python libraries
  7. from __future__ import absolute_import, print_function, unicode_literals
  8. import base64
  9. import logging
  10. import ssl
  11. import sys
  12. # Import Salt testing libraries
  13. from tests.support.runtests import RUNTIME_VARS
  14. from tests.support.unit import TestCase, skipIf
  15. from tests.support.mixins import LoaderModuleMockMixin
  16. from tests.support.mock import (
  17. patch,
  18. MagicMock,
  19. PropertyMock,
  20. call,
  21. )
  22. # Import Salt libraries
  23. from salt.exceptions import (
  24. ArgumentValueError,
  25. CommandExecutionError,
  26. VMwareApiError,
  27. VMwareConnectionError,
  28. VMwareRuntimeError,
  29. VMwareObjectRetrievalError,
  30. VMwareSystemError,
  31. )
  32. import salt.utils.vmware
  33. # Import Third Party Libs
  34. from salt.ext import six
  35. try:
  36. from pyVmomi import vim, vmodl # pylint: disable=no-name-in-module
  37. HAS_PYVMOMI = True
  38. except ImportError:
  39. HAS_PYVMOMI = False
  40. try:
  41. import gssapi
  42. HAS_GSSAPI = True
  43. except ImportError:
  44. HAS_GSSAPI = False
  45. if sys.version_info[:3] > (2, 7, 8):
  46. SSL_VALIDATION = True
  47. else:
  48. SSL_VALIDATION = False
  49. if hasattr(ssl, '_create_unverified_context'):
  50. ssl_context = 'ssl._create_unverified_context'
  51. else:
  52. ssl_context = 'ssl._create_stdlib_context'
  53. # Get Logging Started
  54. log = logging.getLogger(__name__)
  55. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  56. class GetClusterTestCase(TestCase):
  57. '''
  58. Tests for salt.utils.vmware.get_cluster
  59. '''
  60. def setUp(self):
  61. patches = (
  62. ('salt.utils.vmware.get_managed_object_name', MagicMock()),
  63. ('salt.utils.vmware.get_service_instance_from_managed_object', MagicMock()),
  64. ('salt.utils.vmware.get_mors_with_properties', MagicMock(return_value=[{'name': 'fake_cluster',
  65. 'object': MagicMock()}]))
  66. )
  67. for mod, mock in patches:
  68. patcher = patch(mod, mock)
  69. patcher.start()
  70. self.addCleanup(patcher.stop)
  71. self.mock_si = MagicMock()
  72. self.mock_dc = MagicMock()
  73. self.mock_cluster1 = MagicMock()
  74. self.mock_cluster2 = MagicMock()
  75. self.mock_entries = [{'name': 'fake_cluster1',
  76. 'object': self.mock_cluster1},
  77. {'name': 'fake_cluster2',
  78. 'object': self.mock_cluster2}]
  79. for attr in ('mock_si', 'mock_dc', 'mock_cluster1', 'mock_cluster2', 'mock_entries'):
  80. self.addCleanup(delattr, self, attr)
  81. def test_get_managed_object_name_call(self):
  82. mock_get_managed_object_name = MagicMock()
  83. with patch('salt.utils.vmware.get_managed_object_name',
  84. mock_get_managed_object_name):
  85. salt.utils.vmware.get_cluster(self.mock_dc, 'fake_cluster')
  86. mock_get_managed_object_name.assert_called_once_with(self.mock_dc)
  87. def test_get_service_instance_from_managed_object(self):
  88. mock_dc_name = MagicMock()
  89. mock_get_service_instance_from_managed_object = MagicMock()
  90. with patch('salt.utils.vmware.get_managed_object_name',
  91. MagicMock(return_value=mock_dc_name)):
  92. with patch(
  93. 'salt.utils.vmware.get_service_instance_from_managed_object',
  94. mock_get_service_instance_from_managed_object):
  95. salt.utils.vmware.get_cluster(self.mock_dc, 'fake_cluster')
  96. mock_get_service_instance_from_managed_object.assert_called_once_with(
  97. self.mock_dc, name=mock_dc_name)
  98. def test_traversal_spec_init(self):
  99. mock_dc_name = MagicMock()
  100. mock_traversal_spec = MagicMock()
  101. mock_traversal_spec_ini = MagicMock(return_value=mock_traversal_spec)
  102. mock_get_service_instance_from_managed_object = MagicMock()
  103. patch_traversal_spec_str = \
  104. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec'
  105. with patch(patch_traversal_spec_str, mock_traversal_spec_ini):
  106. salt.utils.vmware.get_cluster(self.mock_dc, 'fake_cluster')
  107. mock_traversal_spec_ini.assert_has_calls(
  108. [call(path='childEntity',
  109. skip=False,
  110. type=vim.Folder),
  111. call(path='hostFolder',
  112. skip=True,
  113. type=vim.Datacenter,
  114. selectSet=[mock_traversal_spec])])
  115. def test_get_mors_with_properties_call(self):
  116. mock_get_mors_with_properties = MagicMock(
  117. return_value=[{'name': 'fake_cluster', 'object': MagicMock()}])
  118. mock_traversal_spec = MagicMock()
  119. patch_traversal_spec_str = \
  120. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec'
  121. with patch(
  122. 'salt.utils.vmware.get_service_instance_from_managed_object',
  123. MagicMock(return_value=self.mock_si)):
  124. with patch('salt.utils.vmware.get_mors_with_properties',
  125. mock_get_mors_with_properties):
  126. with patch(patch_traversal_spec_str,
  127. MagicMock(return_value=mock_traversal_spec)):
  128. salt.utils.vmware.get_cluster(self.mock_dc, 'fake_cluster')
  129. mock_get_mors_with_properties.assert_called_once_with(
  130. self.mock_si, vim.ClusterComputeResource,
  131. container_ref=self.mock_dc,
  132. property_list=['name'],
  133. traversal_spec=mock_traversal_spec)
  134. def test_get_mors_with_properties_returns_empty_array(self):
  135. with patch('salt.utils.vmware.get_managed_object_name',
  136. MagicMock(return_value='fake_dc')):
  137. with patch('salt.utils.vmware.get_mors_with_properties',
  138. MagicMock(return_value=[])):
  139. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  140. salt.utils.vmware.get_cluster(self.mock_dc, 'fake_cluster')
  141. self.assertEqual(excinfo.exception.strerror,
  142. 'Cluster \'fake_cluster\' was not found in '
  143. 'datacenter \'fake_dc\'')
  144. def test_cluster_not_found(self):
  145. with patch('salt.utils.vmware.get_managed_object_name',
  146. MagicMock(return_value='fake_dc')):
  147. with patch('salt.utils.vmware.get_mors_with_properties',
  148. MagicMock(return_value=self.mock_entries)):
  149. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  150. salt.utils.vmware.get_cluster(self.mock_dc, 'fake_cluster')
  151. self.assertEqual(excinfo.exception.strerror,
  152. 'Cluster \'fake_cluster\' was not found in '
  153. 'datacenter \'fake_dc\'')
  154. def test_cluster_found(self):
  155. with patch('salt.utils.vmware.get_managed_object_name',
  156. MagicMock(return_value='fake_dc')):
  157. with patch('salt.utils.vmware.get_mors_with_properties',
  158. MagicMock(return_value=self.mock_entries)):
  159. res = salt.utils.vmware.get_cluster(self.mock_dc, 'fake_cluster2')
  160. self.assertEqual(res, self.mock_cluster2)
  161. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  162. class CreateClusterTestCase(TestCase):
  163. '''
  164. Tests for salt.utils.vmware.create_cluster
  165. '''
  166. def setUp(self):
  167. patches = (
  168. ('salt.utils.vmware.get_managed_object_name', MagicMock()),
  169. )
  170. for mod, mock in patches:
  171. patcher = patch(mod, mock)
  172. patcher.start()
  173. self.addCleanup(patcher.stop)
  174. self.mock_create_cluster_ex = MagicMock()
  175. self.mock_dc = MagicMock(
  176. hostFolder=MagicMock(CreateClusterEx=self.mock_create_cluster_ex))
  177. self.mock_cluster_spec = MagicMock()
  178. for attr in ('mock_create_cluster_ex', 'mock_dc', 'mock_cluster_spec'):
  179. self.addCleanup(delattr, self, attr)
  180. def test_get_managed_object_name(self):
  181. mock_get_managed_object_name = MagicMock()
  182. with patch('salt.utils.vmware.get_managed_object_name',
  183. mock_get_managed_object_name):
  184. salt.utils.vmware.create_cluster(self.mock_dc, 'fake_cluster',
  185. self.mock_cluster_spec)
  186. mock_get_managed_object_name.assert_called_once_with(self.mock_dc)
  187. def test_create_cluster_call(self):
  188. salt.utils.vmware.create_cluster(self.mock_dc, 'fake_cluster',
  189. self.mock_cluster_spec)
  190. self.mock_create_cluster_ex.assert_called_once_with(
  191. 'fake_cluster', self.mock_cluster_spec)
  192. def test_create_cluster_raise_no_permission(self):
  193. exc = vim.fault.NoPermission()
  194. exc.privilegeId = 'Fake privilege'
  195. self.mock_dc.hostFolder.CreateClusterEx = MagicMock(
  196. side_effect=exc)
  197. with self.assertRaises(VMwareApiError) as excinfo:
  198. salt.utils.vmware.create_cluster(self.mock_dc, 'fake_cluster',
  199. self.mock_cluster_spec)
  200. self.assertEqual(excinfo.exception.strerror,
  201. 'Not enough permissions. Required privilege: '
  202. 'Fake privilege')
  203. def test_create_cluster_raise_vim_fault(self):
  204. exc = vim.fault.VimFault()
  205. exc.msg = 'VimFault msg'
  206. self.mock_dc.hostFolder.CreateClusterEx = MagicMock(
  207. side_effect=exc)
  208. with self.assertRaises(VMwareApiError) as excinfo:
  209. salt.utils.vmware.create_cluster(self.mock_dc, 'fake_cluster',
  210. self.mock_cluster_spec)
  211. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  212. def test_create_cluster_raise_runtime_fault(self):
  213. exc = vmodl.RuntimeFault()
  214. exc.msg = 'RuntimeFault msg'
  215. self.mock_dc.hostFolder.CreateClusterEx = MagicMock(
  216. side_effect=exc)
  217. with self.assertRaises(VMwareRuntimeError) as excinfo:
  218. salt.utils.vmware.create_cluster(self.mock_dc, 'fake_cluster',
  219. self.mock_cluster_spec)
  220. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  221. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  222. class UpdateClusterTestCase(TestCase):
  223. '''
  224. Tests for salt.utils.vmware.update_cluster
  225. '''
  226. def setUp(self):
  227. patches = (
  228. ('salt.utils.vmware.get_managed_object_name', MagicMock()),
  229. ('salt.utils.vmware.wait_for_task', MagicMock()),
  230. )
  231. for mod, mock in patches:
  232. patcher = patch(mod, mock)
  233. patcher.start()
  234. self.addCleanup(patcher.stop)
  235. self.mock_task = MagicMock()
  236. self.mock_reconfigure_compute_resource_task = \
  237. MagicMock(return_value=self.mock_task)
  238. self.mock_cluster = MagicMock(ReconfigureComputeResource_Task=
  239. self.mock_reconfigure_compute_resource_task)
  240. self.mock_cluster_spec = MagicMock()
  241. for attr in ('mock_task', 'mock_reconfigure_compute_resource_task', 'mock_cluster', 'mock_cluster_spec'):
  242. self.addCleanup(delattr, self, attr)
  243. def test_get_managed_object_name(self):
  244. mock_get_managed_object_name = MagicMock()
  245. with patch('salt.utils.vmware.get_managed_object_name',
  246. mock_get_managed_object_name):
  247. salt.utils.vmware.update_cluster(self.mock_cluster, self.mock_cluster_spec)
  248. mock_get_managed_object_name.assert_called_once_with(self.mock_cluster)
  249. def test_reconfigure_compute_resource_task_call(self):
  250. salt.utils.vmware.update_cluster(self.mock_cluster, self.mock_cluster_spec)
  251. self.mock_reconfigure_compute_resource_task.assert_called_once_with(
  252. self.mock_cluster_spec, modify=True)
  253. def test_reconfigure_compute_resource_raise_no_permission(self):
  254. exc = vim.fault.NoPermission()
  255. exc.privilegeId = 'Fake privilege'
  256. self.mock_cluster.ReconfigureComputeResource_Task = \
  257. MagicMock(side_effect=exc)
  258. with self.assertRaises(VMwareApiError) as excinfo:
  259. salt.utils.vmware.update_cluster(self.mock_cluster, self.mock_cluster_spec)
  260. self.assertEqual(excinfo.exception.strerror,
  261. 'Not enough permissions. Required privilege: '
  262. 'Fake privilege')
  263. def test_reconfigure_compute_resource_raise_vim_fault(self):
  264. exc = vim.fault.VimFault()
  265. exc.msg = 'VimFault msg'
  266. self.mock_cluster.ReconfigureComputeResource_Task = \
  267. MagicMock(side_effect=exc)
  268. with self.assertRaises(VMwareApiError) as excinfo:
  269. salt.utils.vmware.update_cluster(self.mock_cluster, self.mock_cluster_spec)
  270. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  271. def test_reconfigure_compute_resource_raise_runtime_fault(self):
  272. exc = vmodl.RuntimeFault()
  273. exc.msg = 'RuntimeFault msg'
  274. self.mock_cluster.ReconfigureComputeResource_Task = \
  275. MagicMock(side_effect=exc)
  276. with self.assertRaises(VMwareRuntimeError) as excinfo:
  277. salt.utils.vmware.update_cluster(self.mock_cluster, self.mock_cluster_spec)
  278. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  279. def test_wait_for_task_call(self):
  280. mock_wait_for_task = MagicMock()
  281. with patch('salt.utils.vmware.get_managed_object_name',
  282. MagicMock(return_value='fake_cluster')):
  283. with patch('salt.utils.vmware.wait_for_task', mock_wait_for_task):
  284. salt.utils.vmware.update_cluster(self.mock_cluster, self.mock_cluster_spec)
  285. mock_wait_for_task.assert_called_once_with(
  286. self.mock_task, 'fake_cluster', 'ClusterUpdateTask')
  287. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  288. class WaitForTaskTestCase(TestCase):
  289. '''
  290. Tests for salt.utils.vmware.wait_for_task
  291. '''
  292. def setUp(self):
  293. patches = (
  294. ('salt.utils.vmware.time.time', MagicMock(return_value=1)),
  295. ('salt.utils.vmware.time.sleep', MagicMock(return_value=None))
  296. )
  297. for mod, mock in patches:
  298. patcher = patch(mod, mock)
  299. patcher.start()
  300. self.addCleanup(patcher.stop)
  301. def test_first_task_info_raise_no_permission(self):
  302. exc = vim.fault.NoPermission()
  303. exc.privilegeId = 'Fake privilege'
  304. mock_task = MagicMock()
  305. type(mock_task).info = PropertyMock(side_effect=exc)
  306. with self.assertRaises(VMwareApiError) as excinfo:
  307. salt.utils.vmware.wait_for_task(mock_task,
  308. 'fake_instance_name',
  309. 'task_type')
  310. self.assertEqual(excinfo.exception.strerror,
  311. 'Not enough permissions. Required privilege: '
  312. 'Fake privilege')
  313. def test_first_task_info_raise_vim_fault(self):
  314. exc = vim.fault.VimFault()
  315. exc.msg = 'VimFault msg'
  316. mock_task = MagicMock()
  317. type(mock_task).info = PropertyMock(side_effect=exc)
  318. with self.assertRaises(VMwareApiError) as excinfo:
  319. salt.utils.vmware.wait_for_task(mock_task,
  320. 'fake_instance_name',
  321. 'task_type')
  322. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  323. def test_first_task_info_raise_runtime_fault(self):
  324. exc = vmodl.RuntimeFault()
  325. exc.msg = 'RuntimeFault msg'
  326. mock_task = MagicMock()
  327. type(mock_task).info = PropertyMock(side_effect=exc)
  328. with self.assertRaises(VMwareRuntimeError) as excinfo:
  329. salt.utils.vmware.wait_for_task(mock_task,
  330. 'fake_instance_name',
  331. 'task_type')
  332. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  333. def test_inner_loop_task_info_raise_no_permission(self):
  334. exc = vim.fault.NoPermission()
  335. exc.privilegeId = 'Fake privilege'
  336. mock_task = MagicMock()
  337. mock_info1 = MagicMock()
  338. type(mock_task).info = PropertyMock(
  339. side_effect=[mock_info1, exc])
  340. type(mock_info1).state = PropertyMock(side_effect=['running', 'bad'])
  341. with self.assertRaises(VMwareApiError) as excinfo:
  342. salt.utils.vmware.wait_for_task(mock_task,
  343. 'fake_instance_name',
  344. 'task_type')
  345. self.assertEqual(excinfo.exception.strerror,
  346. 'Not enough permissions. Required privilege: '
  347. 'Fake privilege')
  348. def test_inner_loop_task_info_raise_vim_fault(self):
  349. exc = vim.fault.VimFault()
  350. exc.msg = 'VimFault msg'
  351. mock_task = MagicMock()
  352. mock_info1 = MagicMock()
  353. type(mock_task).info = PropertyMock(
  354. side_effect=[mock_info1, exc])
  355. type(mock_info1).state = PropertyMock(side_effect=['running', 'bad'])
  356. with self.assertRaises(VMwareApiError) as excinfo:
  357. salt.utils.vmware.wait_for_task(mock_task,
  358. 'fake_instance_name',
  359. 'task_type')
  360. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  361. def test_inner_loop_task_info_raise_runtime_fault(self):
  362. exc = vmodl.RuntimeFault()
  363. exc.msg = 'RuntimeFault msg'
  364. mock_task = MagicMock()
  365. mock_info1 = MagicMock()
  366. type(mock_task).info = PropertyMock(
  367. side_effect=[mock_info1, exc])
  368. type(mock_info1).state = PropertyMock(side_effect=['running', 'bad'])
  369. with self.assertRaises(VMwareRuntimeError) as excinfo:
  370. salt.utils.vmware.wait_for_task(mock_task,
  371. 'fake_instance_name',
  372. 'task_type')
  373. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  374. def test_info_state_running(self):
  375. # The 'bad' values are invalid in the while loop
  376. mock_task = MagicMock()
  377. prop_mock_state = PropertyMock(side_effect=['running', 'bad', 'bad',
  378. 'success'])
  379. prop_mock_result = PropertyMock()
  380. type(mock_task.info).state = prop_mock_state
  381. type(mock_task.info).result = prop_mock_result
  382. salt.utils.vmware.wait_for_task(mock_task,
  383. 'fake_instance_name',
  384. 'task_type')
  385. self.assertEqual(prop_mock_state.call_count, 4)
  386. self.assertEqual(prop_mock_result.call_count, 1)
  387. def test_info_state_running_continues_loop(self):
  388. mock_task = MagicMock()
  389. # The 'fake' values are required to match all the lookups and end the
  390. # loop
  391. prop_mock_state = PropertyMock(side_effect=['running', 'fake', 'fake',
  392. 'success'])
  393. prop_mock_result = PropertyMock()
  394. type(mock_task.info).state = prop_mock_state
  395. type(mock_task.info).result = prop_mock_result
  396. salt.utils.vmware.wait_for_task(mock_task,
  397. 'fake_instance_name',
  398. 'task_type')
  399. self.assertEqual(prop_mock_state.call_count, 4)
  400. self.assertEqual(prop_mock_result.call_count, 1)
  401. def test_info_state_queued_continues_loop(self):
  402. mock_task = MagicMock()
  403. # The 'fake' values are required to match all the lookups and end the
  404. # loop
  405. prop_mock_state = PropertyMock(side_effect=['fake', 'queued', 'fake',
  406. 'fake', 'success'])
  407. prop_mock_result = PropertyMock()
  408. type(mock_task.info).state = prop_mock_state
  409. type(mock_task.info).result = prop_mock_result
  410. salt.utils.vmware.wait_for_task(mock_task,
  411. 'fake_instance_name',
  412. 'task_type')
  413. self.assertEqual(prop_mock_state.call_count, 5)
  414. self.assertEqual(prop_mock_result.call_count, 1)
  415. def test_info_state_success(self):
  416. mock_task = MagicMock()
  417. prop_mock_state = PropertyMock(return_value='success')
  418. prop_mock_result = PropertyMock()
  419. type(mock_task.info).state = prop_mock_state
  420. type(mock_task.info).result = prop_mock_result
  421. salt.utils.vmware.wait_for_task(mock_task,
  422. 'fake_instance_name',
  423. 'task_type')
  424. self.assertEqual(prop_mock_state.call_count, 3)
  425. self.assertEqual(prop_mock_result.call_count, 1)
  426. def test_info_error_exception(self):
  427. mock_task = MagicMock()
  428. prop_mock_state = PropertyMock(return_value='error')
  429. prop_mock_error = PropertyMock(side_effect=Exception('error exc'))
  430. type(mock_task.info).state = prop_mock_state
  431. type(mock_task.info).error = prop_mock_error
  432. with self.assertRaises(Exception) as excinfo:
  433. salt.utils.vmware.wait_for_task(mock_task,
  434. 'fake_instance_name',
  435. 'task_type')
  436. self.assertEqual(six.text_type(excinfo.exception), 'error exc')
  437. def test_info_error_no_permission(self):
  438. exc = vim.fault.NoPermission()
  439. exc.privilegeId = 'Fake privilege'
  440. mock_task = MagicMock()
  441. prop_mock_state = PropertyMock(return_value='error')
  442. prop_mock_error = PropertyMock(side_effect=exc)
  443. type(mock_task.info).state = prop_mock_state
  444. type(mock_task.info).error = prop_mock_error
  445. with self.assertRaises(VMwareApiError) as excinfo:
  446. salt.utils.vmware.wait_for_task(mock_task,
  447. 'fake_instance_name',
  448. 'task_type')
  449. self.assertEqual(excinfo.exception.strerror,
  450. 'Not enough permissions. Required privilege: '
  451. 'Fake privilege')
  452. def test_info_error_vim_fault(self):
  453. exc = vim.fault.VimFault()
  454. exc.msg = 'VimFault msg'
  455. mock_task = MagicMock()
  456. prop_mock_state = PropertyMock(return_value='error')
  457. prop_mock_error = PropertyMock(side_effect=exc)
  458. type(mock_task.info).state = prop_mock_state
  459. type(mock_task.info).error = prop_mock_error
  460. with self.assertRaises(VMwareApiError) as excinfo:
  461. salt.utils.vmware.wait_for_task(mock_task,
  462. 'fake_instance_name',
  463. 'task_type')
  464. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  465. def test_info_error_system_fault(self):
  466. exc = vmodl.fault.SystemError()
  467. exc.msg = 'SystemError msg'
  468. mock_task = MagicMock()
  469. prop_mock_state = PropertyMock(return_value='error')
  470. prop_mock_error = PropertyMock(side_effect=exc)
  471. type(mock_task.info).state = prop_mock_state
  472. type(mock_task.info).error = prop_mock_error
  473. with self.assertRaises(VMwareSystemError) as excinfo:
  474. salt.utils.vmware.wait_for_task(mock_task,
  475. 'fake_instance_name',
  476. 'task_type')
  477. self.assertEqual(excinfo.exception.strerror, 'SystemError msg')
  478. def test_info_error_invalid_argument_no_fault_message(self):
  479. exc = vmodl.fault.InvalidArgument()
  480. exc.faultMessage = None
  481. exc.msg = 'InvalidArgumentFault msg'
  482. mock_task = MagicMock()
  483. prop_mock_state = PropertyMock(return_value='error')
  484. prop_mock_error = PropertyMock(side_effect=exc)
  485. type(mock_task.info).state = prop_mock_state
  486. type(mock_task.info).error = prop_mock_error
  487. with self.assertRaises(VMwareApiError) as excinfo:
  488. salt.utils.vmware.wait_for_task(mock_task,
  489. 'fake_instance_name',
  490. 'task_type')
  491. self.assertEqual(excinfo.exception.strerror,
  492. 'InvalidArgumentFault msg')
  493. def test_info_error_invalid_argument_with_fault_message(self):
  494. exc = vmodl.fault.InvalidArgument()
  495. fault_message = vim.LocalizableMessage()
  496. fault_message.message = 'LocalFault msg'
  497. exc.faultMessage = [fault_message]
  498. exc.msg = 'InvalidArgumentFault msg'
  499. mock_task = MagicMock()
  500. prop_mock_state = PropertyMock(return_value='error')
  501. prop_mock_error = PropertyMock(side_effect=exc)
  502. type(mock_task.info).state = prop_mock_state
  503. type(mock_task.info).error = prop_mock_error
  504. with self.assertRaises(VMwareApiError) as excinfo:
  505. salt.utils.vmware.wait_for_task(mock_task,
  506. 'fake_instance_name',
  507. 'task_type')
  508. self.assertEqual(excinfo.exception.strerror,
  509. 'InvalidArgumentFault msg (LocalFault msg)')
  510. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  511. class GetMorsWithPropertiesTestCase(TestCase):
  512. '''
  513. Tests for salt.utils.get_mors_with_properties
  514. '''
  515. si = None
  516. obj_type = None
  517. prop_list = None
  518. container_ref = None
  519. traversal_spec = None
  520. def setUp(self):
  521. self.si = MagicMock()
  522. self.obj_type = MagicMock()
  523. self.prop_list = MagicMock()
  524. self.container_ref = MagicMock()
  525. self.traversal_spec = MagicMock()
  526. def test_empty_content(self):
  527. get_content = MagicMock(return_value=[])
  528. with patch('salt.utils.vmware.get_content', get_content):
  529. ret = salt.utils.vmware.get_mors_with_properties(
  530. self.si, self.obj_type, self.prop_list,
  531. self.container_ref, self.traversal_spec)
  532. get_content.assert_called_once_with(
  533. self.si, self.obj_type,
  534. property_list=self.prop_list,
  535. container_ref=self.container_ref,
  536. traversal_spec=self.traversal_spec,
  537. local_properties=False)
  538. self.assertEqual(ret, [])
  539. def test_local_properties_set(self):
  540. obj_mock = MagicMock()
  541. # obj.propSet
  542. propSet_prop = PropertyMock(return_value=[])
  543. type(obj_mock).propSet = propSet_prop
  544. # obj.obj
  545. inner_obj_mock = MagicMock()
  546. obj_prop = PropertyMock(return_value=inner_obj_mock)
  547. type(obj_mock).obj = obj_prop
  548. get_content = MagicMock(return_value=[obj_mock])
  549. with patch('salt.utils.vmware.get_content', get_content):
  550. ret = salt.utils.vmware.get_mors_with_properties(
  551. self.si, self.obj_type, self.prop_list,
  552. self.container_ref, self.traversal_spec,
  553. local_properties=True)
  554. get_content.assert_called_once_with(
  555. self.si, self.obj_type,
  556. property_list=self.prop_list,
  557. container_ref=self.container_ref,
  558. traversal_spec=self.traversal_spec,
  559. local_properties=True)
  560. def test_one_element_content(self):
  561. obj_mock = MagicMock()
  562. # obj.propSet
  563. propSet_prop = PropertyMock(return_value=[])
  564. type(obj_mock).propSet = propSet_prop
  565. # obj.obj
  566. inner_obj_mock = MagicMock()
  567. obj_prop = PropertyMock(return_value=inner_obj_mock)
  568. type(obj_mock).obj = obj_prop
  569. get_content = MagicMock(return_value=[obj_mock])
  570. with patch('salt.utils.vmware.get_content', get_content):
  571. ret = salt.utils.vmware.get_mors_with_properties(
  572. self.si, self.obj_type, self.prop_list,
  573. self.container_ref, self.traversal_spec)
  574. get_content.assert_called_once_with(
  575. self.si, self.obj_type,
  576. property_list=self.prop_list,
  577. container_ref=self.container_ref,
  578. traversal_spec=self.traversal_spec,
  579. local_properties=False)
  580. self.assertEqual(propSet_prop.call_count, 1)
  581. self.assertEqual(obj_prop.call_count, 1)
  582. self.assertEqual(len(ret), 1)
  583. self.assertDictEqual(ret[0], {'object': inner_obj_mock})
  584. def test_multiple_element_content(self):
  585. # obj1
  586. obj1_mock = MagicMock()
  587. # obj1.propSet
  588. obj1_propSet_prop = PropertyMock(return_value=[])
  589. type(obj1_mock).propSet = obj1_propSet_prop
  590. # obj1.obj
  591. obj1_inner_obj_mock = MagicMock()
  592. obj1_obj_prop = PropertyMock(return_value=obj1_inner_obj_mock)
  593. type(obj1_mock).obj = obj1_obj_prop
  594. # obj2
  595. obj2_mock = MagicMock()
  596. # obj2.propSet
  597. obj2_propSet_prop = PropertyMock(return_value=[])
  598. type(obj2_mock).propSet = obj2_propSet_prop
  599. # obj2.obj
  600. obj2_inner_obj_mock = MagicMock()
  601. obj2_obj_prop = PropertyMock(return_value=obj2_inner_obj_mock)
  602. type(obj2_mock).obj = obj2_obj_prop
  603. get_content = MagicMock(return_value=[obj1_mock, obj2_mock])
  604. with patch('salt.utils.vmware.get_content', get_content):
  605. ret = salt.utils.vmware.get_mors_with_properties(
  606. self.si, self.obj_type, self.prop_list,
  607. self.container_ref, self.traversal_spec)
  608. get_content.assert_called_once_with(
  609. self.si, self.obj_type,
  610. property_list=self.prop_list,
  611. container_ref=self.container_ref,
  612. traversal_spec=self.traversal_spec,
  613. local_properties=False)
  614. self.assertEqual(obj1_propSet_prop.call_count, 1)
  615. self.assertEqual(obj2_propSet_prop.call_count, 1)
  616. self.assertEqual(obj1_obj_prop.call_count, 1)
  617. self.assertEqual(obj2_obj_prop.call_count, 1)
  618. self.assertEqual(len(ret), 2)
  619. self.assertDictEqual(ret[0], {'object': obj1_inner_obj_mock})
  620. self.assertDictEqual(ret[1], {'object': obj2_inner_obj_mock})
  621. def test_one_elem_one_property(self):
  622. obj_mock = MagicMock()
  623. # property mock
  624. prop_set_obj_mock = MagicMock()
  625. prop_set_obj_name_prop = PropertyMock(return_value='prop_name')
  626. prop_set_obj_val_prop = PropertyMock(return_value='prop_value')
  627. type(prop_set_obj_mock).name = prop_set_obj_name_prop
  628. type(prop_set_obj_mock).val = prop_set_obj_val_prop
  629. # obj.propSet
  630. propSet_prop = PropertyMock(return_value=[prop_set_obj_mock])
  631. type(obj_mock).propSet = propSet_prop
  632. # obj.obj
  633. inner_obj_mock = MagicMock()
  634. obj_prop = PropertyMock(return_value=inner_obj_mock)
  635. type(obj_mock).obj = obj_prop
  636. get_content = MagicMock(return_value=[obj_mock])
  637. with patch('salt.utils.vmware.get_content', get_content):
  638. ret = salt.utils.vmware.get_mors_with_properties(
  639. self.si, self.obj_type, self.prop_list,
  640. self.container_ref, self.traversal_spec,
  641. local_properties=False)
  642. get_content.assert_called_once_with(
  643. self.si, self.obj_type,
  644. property_list=self.prop_list,
  645. container_ref=self.container_ref,
  646. traversal_spec=self.traversal_spec,
  647. local_properties=False)
  648. self.assertEqual(propSet_prop.call_count, 1)
  649. self.assertEqual(prop_set_obj_name_prop.call_count, 1)
  650. self.assertEqual(prop_set_obj_val_prop.call_count, 1)
  651. self.assertEqual(obj_prop.call_count, 1)
  652. self.assertEqual(len(ret), 1)
  653. self.assertDictEqual(ret[0], {'prop_name': 'prop_value',
  654. 'object': inner_obj_mock})
  655. def test_one_elem_multiple_properties(self):
  656. obj_mock = MagicMock()
  657. # property1 mock
  658. prop_set_obj1_mock = MagicMock()
  659. prop_set_obj1_name_prop = PropertyMock(return_value='prop_name1')
  660. prop_set_obj1_val_prop = PropertyMock(return_value='prop_value1')
  661. type(prop_set_obj1_mock).name = prop_set_obj1_name_prop
  662. type(prop_set_obj1_mock).val = prop_set_obj1_val_prop
  663. # property2 mock
  664. prop_set_obj2_mock = MagicMock()
  665. prop_set_obj2_name_prop = PropertyMock(return_value='prop_name2')
  666. prop_set_obj2_val_prop = PropertyMock(return_value='prop_value2')
  667. type(prop_set_obj2_mock).name = prop_set_obj2_name_prop
  668. type(prop_set_obj2_mock).val = prop_set_obj2_val_prop
  669. # obj.propSet
  670. propSet_prop = PropertyMock(return_value=[prop_set_obj1_mock,
  671. prop_set_obj2_mock])
  672. type(obj_mock).propSet = propSet_prop
  673. # obj.obj
  674. inner_obj_mock = MagicMock()
  675. obj_prop = PropertyMock(return_value=inner_obj_mock)
  676. type(obj_mock).obj = obj_prop
  677. get_content = MagicMock(return_value=[obj_mock])
  678. with patch('salt.utils.vmware.get_content', get_content):
  679. ret = salt.utils.vmware.get_mors_with_properties(
  680. self.si, self.obj_type, self.prop_list,
  681. self.container_ref, self.traversal_spec)
  682. get_content.assert_called_once_with(
  683. self.si, self.obj_type,
  684. property_list=self.prop_list,
  685. container_ref=self.container_ref,
  686. traversal_spec=self.traversal_spec,
  687. local_properties=False)
  688. self.assertEqual(propSet_prop.call_count, 1)
  689. self.assertEqual(prop_set_obj1_name_prop.call_count, 1)
  690. self.assertEqual(prop_set_obj1_val_prop.call_count, 1)
  691. self.assertEqual(prop_set_obj2_name_prop.call_count, 1)
  692. self.assertEqual(prop_set_obj2_val_prop.call_count, 1)
  693. self.assertEqual(obj_prop.call_count, 1)
  694. self.assertEqual(len(ret), 1)
  695. self.assertDictEqual(ret[0], {'prop_name1': 'prop_value1',
  696. 'prop_name2': 'prop_value2',
  697. 'object': inner_obj_mock})
  698. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  699. class GetPropertiesOfManagedObjectTestCase(TestCase):
  700. '''
  701. Tests for salt.utils.get_properties_of_managed_object
  702. '''
  703. def setUp(self):
  704. patches = (
  705. ('salt.utils.vmware.get_service_instance_from_managed_object', MagicMock()),
  706. ('salt.utils.vmware.get_mors_with_properties', MagicMock(return_value=[MagicMock()]))
  707. )
  708. for mod, mock in patches:
  709. patcher = patch(mod, mock)
  710. patcher.start()
  711. self.addCleanup(patcher.stop)
  712. self.mock_si = MagicMock()
  713. self.fake_mo_ref = vim.ManagedEntity('Fake')
  714. self.mock_props = MagicMock()
  715. self.mock_item_name = {'name': 'fake_name'}
  716. self.mock_item = MagicMock()
  717. def test_get_service_instance_from_managed_object_call(self):
  718. mock_get_instance_from_managed_object = MagicMock()
  719. with patch(
  720. 'salt.utils.vmware.get_service_instance_from_managed_object',
  721. mock_get_instance_from_managed_object):
  722. salt.utils.vmware.get_properties_of_managed_object(
  723. self.fake_mo_ref, self.mock_props)
  724. mock_get_instance_from_managed_object.assert_called_once_with(
  725. self.fake_mo_ref)
  726. def test_get_mors_with_properties_calls(self):
  727. mock_get_mors_with_properties = MagicMock(return_value=[MagicMock()])
  728. with patch(
  729. 'salt.utils.vmware.get_service_instance_from_managed_object',
  730. MagicMock(return_value=self.mock_si)):
  731. with patch('salt.utils.vmware.get_mors_with_properties',
  732. mock_get_mors_with_properties):
  733. salt.utils.vmware.get_properties_of_managed_object(
  734. self.fake_mo_ref, self.mock_props)
  735. mock_get_mors_with_properties.assert_has_calls(
  736. [call(self.mock_si, vim.ManagedEntity,
  737. container_ref=self.fake_mo_ref,
  738. property_list=['name'],
  739. local_properties=True),
  740. call(self.mock_si, vim.ManagedEntity,
  741. container_ref=self.fake_mo_ref,
  742. property_list=self.mock_props,
  743. local_properties=True)])
  744. def test_managed_object_no_name_property(self):
  745. with patch('salt.utils.vmware.get_mors_with_properties',
  746. MagicMock(side_effect=[vmodl.query.InvalidProperty(), []])):
  747. with self.assertRaises(VMwareApiError) as excinfo:
  748. salt.utils.vmware.get_properties_of_managed_object(
  749. self.fake_mo_ref, self.mock_props)
  750. self.assertEqual('Properties of managed object \'<unnamed>\' weren\'t '
  751. 'retrieved', excinfo.exception.strerror)
  752. def test_no_items_named_object(self):
  753. with patch('salt.utils.vmware.get_mors_with_properties',
  754. MagicMock(side_effect=[[self.mock_item_name], []])):
  755. with self.assertRaises(VMwareApiError) as excinfo:
  756. salt.utils.vmware.get_properties_of_managed_object(
  757. self.fake_mo_ref, self.mock_props)
  758. self.assertEqual('Properties of managed object \'fake_name\' weren\'t '
  759. 'retrieved', excinfo.exception.strerror)
  760. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  761. class GetManagedObjectName(TestCase):
  762. '''
  763. Tests for salt.utils.get_managed_object_name
  764. '''
  765. def setUp(self):
  766. patches = (
  767. ('salt.utils.vmware.get_properties_of_managed_object', MagicMock(return_value={'key': 'value'})),
  768. )
  769. for mod, mock in patches:
  770. patcher = patch(mod, mock)
  771. patcher.start()
  772. self.addCleanup(patcher.stop)
  773. self.mock_mo_ref = MagicMock()
  774. def test_get_properties_of_managed_object_call(self):
  775. mock_get_properties_of_managed_object = MagicMock()
  776. with patch('salt.utils.vmware.get_properties_of_managed_object',
  777. mock_get_properties_of_managed_object):
  778. salt.utils.vmware.get_managed_object_name(self.mock_mo_ref)
  779. mock_get_properties_of_managed_object.assert_called_once_with(
  780. self.mock_mo_ref, ['name'])
  781. def test_no_name_in_property_dict(self):
  782. ret = salt.utils.vmware.get_managed_object_name(self.mock_mo_ref)
  783. self.assertIsNone(ret)
  784. def test_return_managed_object_name(self):
  785. mock_get_properties_of_managed_object = MagicMock()
  786. with patch('salt.utils.vmware.get_properties_of_managed_object',
  787. MagicMock(return_value={'name': 'fake_name'})):
  788. ret = salt.utils.vmware.get_managed_object_name(self.mock_mo_ref)
  789. self.assertEqual(ret, 'fake_name')
  790. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  791. class GetContentTestCase(TestCase):
  792. '''
  793. Tests for salt.utils.get_content
  794. '''
  795. # Method names to be patched
  796. traversal_spec_method_name = \
  797. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec'
  798. property_spec_method_name = \
  799. 'salt.utils.vmware.vmodl.query.PropertyCollector.PropertySpec'
  800. obj_spec_method_name = \
  801. 'salt.utils.vmware.vmodl.query.PropertyCollector.ObjectSpec'
  802. filter_spec_method_name = \
  803. 'salt.utils.vmware.vmodl.query.PropertyCollector.FilterSpec'
  804. # Class variables
  805. si_mock = None
  806. root_folder_mock = None
  807. root_folder_prop = None
  808. container_view_mock = None
  809. create_container_view_mock = None
  810. result_mock = None
  811. retrieve_contents_mock = None
  812. destroy_mock = None
  813. obj_type_mock = None
  814. traversal_spec_ret_mock = None
  815. traversal_spec_mock = None
  816. property_spec_ret_mock = None
  817. property_spec_mock = None
  818. obj_spec_ret_mock = None
  819. obj_spec_mock = None
  820. filter_spec_ret_mock = None
  821. filter_spec_mock = None
  822. def setUp(self):
  823. patches = (
  824. ('salt.utils.vmware.get_root_folder', MagicMock()),
  825. ('salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec', MagicMock(return_value=MagicMock())),
  826. ('salt.utils.vmware.vmodl.query.PropertyCollector.PropertySpec', MagicMock(return_value=MagicMock())),
  827. ('salt.utils.vmware.vmodl.query.PropertyCollector.ObjectSpec', MagicMock(return_value=MagicMock())),
  828. ('salt.utils.vmware.vmodl.query.PropertyCollector.FilterSpec', MagicMock(return_value=MagicMock()))
  829. )
  830. for mod, mock in patches:
  831. patcher = patch(mod, mock)
  832. patcher.start()
  833. self.addCleanup(patcher.stop)
  834. # setup the service instance
  835. self.si_mock = MagicMock()
  836. # RootFolder
  837. self.root_folder_mock = MagicMock()
  838. self.get_root_folder_mock = \
  839. MagicMock(return_value=self.root_folder_mock)
  840. # CreateContainerView()
  841. self.container_view_mock = MagicMock()
  842. self.create_container_view_mock = \
  843. MagicMock(return_value=self.container_view_mock)
  844. self.si_mock.content.viewManager.CreateContainerView = \
  845. self.create_container_view_mock
  846. # RetrieveContents()
  847. self.result_mock = MagicMock()
  848. self.retrieve_contents_mock = MagicMock(return_value=self.result_mock)
  849. self.si_mock.content.propertyCollector.RetrieveContents = \
  850. self.retrieve_contents_mock
  851. # Destroy()
  852. self.destroy_mock = MagicMock()
  853. self.container_view_mock.Destroy = self.destroy_mock
  854. # override mocks
  855. self.obj_type_mock = MagicMock()
  856. self.traversal_spec_ret_mock = MagicMock()
  857. self.traversal_spec_mock = \
  858. MagicMock(return_value=self.traversal_spec_ret_mock)
  859. self.property_spec_ret_mock = MagicMock()
  860. self.property_spec_mock = \
  861. MagicMock(return_value=self.property_spec_ret_mock)
  862. self.obj_spec_ret_mock = MagicMock()
  863. self.obj_spec_mock = \
  864. MagicMock(return_value=self.obj_spec_ret_mock)
  865. self.filter_spec_ret_mock = MagicMock()
  866. self.filter_spec_mock = \
  867. MagicMock(return_value=self.filter_spec_ret_mock)
  868. def test_empty_container_ref(self):
  869. with patch('salt.utils.vmware.get_root_folder',
  870. self.get_root_folder_mock):
  871. salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
  872. self.get_root_folder_mock.assert_called_once_with(self.si_mock)
  873. self.create_container_view_mock.assert_called_once_with(
  874. self.root_folder_mock, [self.obj_type_mock], True)
  875. def test_defined_container_ref(self):
  876. container_ref_mock = MagicMock()
  877. with patch('salt.utils.vmware.get_root_folder',
  878. self.get_root_folder_mock):
  879. with patch(self.obj_spec_method_name, self.obj_type_mock):
  880. salt.utils.vmware.get_content(
  881. self.si_mock, self.obj_type_mock,
  882. container_ref=container_ref_mock)
  883. self.assertEqual(self.get_root_folder_mock.call_count, 0)
  884. self.create_container_view_mock.assert_called_once_with(
  885. container_ref_mock, [self.obj_type_mock], True)
  886. # Also checks destroy is called
  887. def test_local_traversal_spec(self):
  888. with patch('salt.utils.vmware.get_root_folder',
  889. self.get_root_folder_mock):
  890. with patch(self.traversal_spec_method_name,
  891. self.traversal_spec_mock):
  892. with patch(self.obj_spec_method_name, self.obj_spec_mock):
  893. ret = salt.utils.vmware.get_content(self.si_mock,
  894. self.obj_type_mock)
  895. self.create_container_view_mock.assert_called_once_with(
  896. self.root_folder_mock, [self.obj_type_mock], True)
  897. self.traversal_spec_mock.assert_called_once_with(
  898. name='traverseEntities', path='view', skip=False,
  899. type=vim.view.ContainerView)
  900. self.obj_spec_mock.assert_called_once_with(
  901. obj=self.container_view_mock,
  902. skip=True,
  903. selectSet=[self.traversal_spec_ret_mock])
  904. # check destroy is called
  905. self.assertEqual(self.destroy_mock.call_count, 1)
  906. def test_create_container_view_raise_no_permission(self):
  907. exc = vim.fault.NoPermission()
  908. exc.privilegeId = 'Fake privilege'
  909. self.si_mock.content.viewManager.CreateContainerView = \
  910. MagicMock(side_effect=exc)
  911. with patch('salt.utils.vmware.get_root_folder',
  912. self.get_root_folder_mock):
  913. with self.assertRaises(VMwareApiError) as excinfo:
  914. salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
  915. self.assertEqual(excinfo.exception.strerror,
  916. 'Not enough permissions. Required privilege: '
  917. 'Fake privilege')
  918. def test_create_container_view_raise_vim_fault(self):
  919. exc = vim.fault.VimFault()
  920. exc.msg = 'VimFault msg'
  921. self.si_mock.content.viewManager.CreateContainerView = \
  922. MagicMock(side_effect=exc)
  923. with patch('salt.utils.vmware.get_root_folder',
  924. self.get_root_folder_mock):
  925. with self.assertRaises(VMwareApiError) as excinfo:
  926. salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
  927. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  928. def test_create_container_view_raise_runtime_fault(self):
  929. exc = vmodl.RuntimeFault()
  930. exc.msg = 'RuntimeFault msg'
  931. self.si_mock.content.viewManager.CreateContainerView = \
  932. MagicMock(side_effect=exc)
  933. with patch('salt.utils.vmware.get_root_folder',
  934. self.get_root_folder_mock):
  935. with self.assertRaises(VMwareRuntimeError) as excinfo:
  936. salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
  937. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  938. def test_destroy_raise_no_permission(self):
  939. exc = vim.fault.NoPermission()
  940. exc.privilegeId = 'Fake privilege'
  941. self.si_mock.content.viewManager.CreateContainerView = MagicMock(
  942. return_value=MagicMock(Destroy=MagicMock(side_effect=exc)))
  943. with patch('salt.utils.vmware.get_root_folder',
  944. self.get_root_folder_mock):
  945. with self.assertRaises(VMwareApiError) as excinfo:
  946. salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
  947. self.assertEqual(excinfo.exception.strerror,
  948. 'Not enough permissions. Required privilege: '
  949. 'Fake privilege')
  950. def test_destroy_raise_vim_fault(self):
  951. exc = vim.fault.VimFault()
  952. exc.msg = 'VimFault msg'
  953. self.si_mock.content.viewManager.CreateContainerView = MagicMock(
  954. return_value=MagicMock(Destroy=MagicMock(side_effect=exc)))
  955. with patch('salt.utils.vmware.get_root_folder',
  956. self.get_root_folder_mock):
  957. with self.assertRaises(VMwareApiError) as excinfo:
  958. salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
  959. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  960. def test_destroy_raise_runtime_fault(self):
  961. exc = vmodl.RuntimeFault()
  962. exc.msg = 'RuntimeFault msg'
  963. self.si_mock.content.viewManager.CreateContainerView = MagicMock(
  964. return_value=MagicMock(Destroy=MagicMock(side_effect=exc)))
  965. with patch('salt.utils.vmware.get_root_folder',
  966. self.get_root_folder_mock):
  967. with self.assertRaises(VMwareRuntimeError) as excinfo:
  968. salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
  969. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  970. # Also checks destroy is not called
  971. def test_external_traversal_spec(self):
  972. traversal_spec_obj_mock = MagicMock()
  973. with patch('salt.utils.vmware.get_root_folder',
  974. self.get_root_folder_mock):
  975. with patch(self.traversal_spec_method_name,
  976. self.traversal_spec_mock):
  977. with patch(self.obj_spec_method_name, self.obj_spec_mock):
  978. salt.utils.vmware.get_content(
  979. self.si_mock,
  980. self.obj_type_mock,
  981. traversal_spec=traversal_spec_obj_mock)
  982. self.obj_spec_mock.assert_called_once_with(
  983. obj=self.root_folder_mock,
  984. skip=True,
  985. selectSet=[traversal_spec_obj_mock])
  986. # Check local traversal methods are not called
  987. self.assertEqual(self.create_container_view_mock.call_count, 0)
  988. self.assertEqual(self.traversal_spec_mock.call_count, 0)
  989. # check destroy is not called
  990. self.assertEqual(self.destroy_mock.call_count, 0)
  991. def test_property_obj_filter_specs_and_contents(self):
  992. with patch(self.traversal_spec_method_name, self.traversal_spec_mock):
  993. with patch(self.property_spec_method_name, self.property_spec_mock):
  994. with patch(self.obj_spec_method_name, self.obj_spec_mock):
  995. with patch(self.filter_spec_method_name,
  996. self.filter_spec_mock):
  997. ret = salt.utils.vmware.get_content(
  998. self.si_mock,
  999. self.obj_type_mock)
  1000. self.traversal_spec_mock.assert_called_once_with(
  1001. name='traverseEntities', path='view', skip=False,
  1002. type=vim.view.ContainerView)
  1003. self.property_spec_mock.assert_called_once_with(
  1004. type=self.obj_type_mock, all=True, pathSet=None)
  1005. self.obj_spec_mock.assert_called_once_with(
  1006. obj=self.container_view_mock, skip=True,
  1007. selectSet=[self.traversal_spec_ret_mock])
  1008. self.retrieve_contents_mock.assert_called_once_with(
  1009. [self.filter_spec_ret_mock])
  1010. self.assertEqual(ret, self.result_mock)
  1011. def test_retrieve_contents_raise_no_permission(self):
  1012. exc = vim.fault.NoPermission()
  1013. exc.privilegeId = 'Fake privilege'
  1014. self.si_mock.content.propertyCollector.RetrieveContents = \
  1015. MagicMock(side_effect=exc)
  1016. with self.assertRaises(VMwareApiError) as excinfo:
  1017. salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
  1018. self.assertEqual(excinfo.exception.strerror,
  1019. 'Not enough permissions. Required privilege: '
  1020. 'Fake privilege')
  1021. def test_retrieve_contents_raise_vim_fault(self):
  1022. exc = vim.fault.VimFault()
  1023. exc.msg = 'VimFault msg'
  1024. self.si_mock.content.propertyCollector.RetrieveContents = \
  1025. MagicMock(side_effect=exc)
  1026. with self.assertRaises(VMwareApiError) as excinfo:
  1027. salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
  1028. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  1029. def test_retrieve_contents_raise_runtime_fault(self):
  1030. exc = vmodl.RuntimeFault()
  1031. exc.msg = 'RuntimeFault msg'
  1032. self.si_mock.content.propertyCollector.RetrieveContents = \
  1033. MagicMock(side_effect=exc)
  1034. with self.assertRaises(VMwareRuntimeError) as excinfo:
  1035. salt.utils.vmware.get_content(self.si_mock, self.obj_type_mock)
  1036. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  1037. def test_local_properties_set(self):
  1038. container_ref_mock = MagicMock()
  1039. with patch(self.traversal_spec_method_name, self.traversal_spec_mock):
  1040. with patch(self.property_spec_method_name, self.property_spec_mock):
  1041. with patch(self.obj_spec_method_name, self.obj_spec_mock):
  1042. salt.utils.vmware.get_content(
  1043. self.si_mock,
  1044. self.obj_type_mock,
  1045. container_ref=container_ref_mock,
  1046. local_properties=True)
  1047. self.assertEqual(self.traversal_spec_mock.call_count, 0)
  1048. self.obj_spec_mock.assert_called_once_with(
  1049. obj=container_ref_mock, skip=False, selectSet=None)
  1050. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1051. class GetRootFolderTestCase(TestCase):
  1052. '''
  1053. Tests for salt.utils.get_root_folder
  1054. '''
  1055. def setUp(self):
  1056. self.mock_root_folder = MagicMock()
  1057. self.mock_content = MagicMock(rootFolder=self.mock_root_folder)
  1058. self.mock_si = MagicMock(
  1059. RetrieveContent=MagicMock(return_value=self.mock_content))
  1060. def test_raise_no_permission(self):
  1061. exc = vim.fault.NoPermission()
  1062. exc.privilegeId = 'Fake privilege'
  1063. type(self.mock_content).rootFolder = PropertyMock(side_effect=exc)
  1064. with self.assertRaises(VMwareApiError) as excinfo:
  1065. salt.utils.vmware.get_root_folder(self.mock_si)
  1066. self.assertEqual(excinfo.exception.strerror,
  1067. 'Not enough permissions. Required privilege: '
  1068. 'Fake privilege')
  1069. def test_raise_vim_fault(self):
  1070. exc = vim.fault.VimFault()
  1071. exc.msg = 'VimFault msg'
  1072. type(self.mock_content).rootFolder = PropertyMock(side_effect=exc)
  1073. with self.assertRaises(VMwareApiError) as excinfo:
  1074. salt.utils.vmware.get_root_folder(self.mock_si)
  1075. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  1076. def test_raise_runtime_fault(self):
  1077. exc = vmodl.RuntimeFault()
  1078. exc.msg = 'RuntimeFault msg'
  1079. type(self.mock_content).rootFolder = PropertyMock(side_effect=exc)
  1080. with self.assertRaises(VMwareRuntimeError) as excinfo:
  1081. salt.utils.vmware.get_root_folder(self.mock_si)
  1082. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  1083. def test_return(self):
  1084. ret = salt.utils.vmware.get_root_folder(self.mock_si)
  1085. self.assertEqual(ret, self.mock_root_folder)
  1086. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1087. class GetServiceInfoTestCase(TestCase):
  1088. '''
  1089. Tests for salt.utils.vmware.get_service_info
  1090. '''
  1091. def setUp(self):
  1092. self.mock_about = MagicMock()
  1093. self.mock_si = MagicMock(content=MagicMock())
  1094. type(self.mock_si.content).about = \
  1095. PropertyMock(return_value=self.mock_about)
  1096. def tearDown(self):
  1097. for attr in ('mock_si', 'mock_about'):
  1098. delattr(self, attr)
  1099. def test_about_ret(self):
  1100. ret = salt.utils.vmware.get_service_info(self.mock_si)
  1101. self.assertEqual(ret, self.mock_about)
  1102. def test_about_raises_no_permission(self):
  1103. exc = vim.fault.NoPermission()
  1104. exc.privilegeId = 'Fake privilege'
  1105. type(self.mock_si.content).about = \
  1106. PropertyMock(side_effect=exc)
  1107. with self.assertRaises(VMwareApiError) as excinfo:
  1108. salt.utils.vmware.get_service_info(self.mock_si)
  1109. self.assertEqual(excinfo.exception.strerror,
  1110. 'Not enough permissions. Required privilege: '
  1111. 'Fake privilege')
  1112. def test_about_raises_vim_fault(self):
  1113. exc = vim.fault.VimFault()
  1114. exc.msg = 'VimFault msg'
  1115. type(self.mock_si.content).about = \
  1116. PropertyMock(side_effect=exc)
  1117. with self.assertRaises(VMwareApiError) as excinfo:
  1118. salt.utils.vmware.get_service_info(self.mock_si)
  1119. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  1120. def test_about_raises_runtime_fault(self):
  1121. exc = vmodl.RuntimeFault()
  1122. exc.msg = 'RuntimeFault msg'
  1123. type(self.mock_si.content).about = \
  1124. PropertyMock(side_effect=exc)
  1125. with self.assertRaises(VMwareRuntimeError) as excinfo:
  1126. salt.utils.vmware.get_service_info(self.mock_si)
  1127. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  1128. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1129. @skipIf(not HAS_GSSAPI, 'The \'gssapi\' library is missing')
  1130. class GssapiTokenTest(TestCase):
  1131. '''
  1132. Test cases for salt.utils.vmware.get_gssapi_token
  1133. '''
  1134. def setUp(self):
  1135. patches = (
  1136. ('gssapi.Name', MagicMock(return_value='service')),
  1137. ('gssapi.InitContext', MagicMock())
  1138. )
  1139. for mod, mock in patches:
  1140. patcher = patch(mod, mock)
  1141. patcher.start()
  1142. self.addCleanup(patcher.stop)
  1143. def test_no_gssapi(self):
  1144. with patch('salt.utils.vmware.HAS_GSSAPI', False):
  1145. with self.assertRaises(ImportError) as excinfo:
  1146. salt.utils.vmware.get_gssapi_token('principal', 'host', 'domain')
  1147. self.assertIn('The gssapi library is not imported.',
  1148. excinfo.exception.message)
  1149. @skipIf(not HAS_GSSAPI, 'The \'gssapi\' library is missing')
  1150. def test_service_name(self):
  1151. mock_name = MagicMock()
  1152. with patch.object(salt.utils.vmware.gssapi, 'Name', mock_name):
  1153. with self.assertRaises(CommandExecutionError):
  1154. salt.utils.vmware.get_gssapi_token('principal', 'host',
  1155. 'domain')
  1156. mock_name.assert_called_once_with('principal/host@domain',
  1157. gssapi.C_NT_USER_NAME)
  1158. @skipIf(not HAS_GSSAPI, 'The \'gssapi\' library is missing')
  1159. def test_out_token_defined(self):
  1160. mock_context = MagicMock(return_value=MagicMock())
  1161. mock_context.return_value.established = False
  1162. mock_context.return_value.step = MagicMock(return_value='out_token')
  1163. with patch.object(salt.utils.vmware.gssapi, 'InitContext',
  1164. mock_context):
  1165. ret = salt.utils.vmware.get_gssapi_token('principal', 'host',
  1166. 'domain')
  1167. self.assertEqual(mock_context.return_value.step.called, 1)
  1168. self.assertEqual(ret, base64.b64encode(b'out_token'))
  1169. @skipIf(not HAS_GSSAPI, 'The \'gssapi\' library is missing')
  1170. def test_out_token_undefined(self):
  1171. mock_context = MagicMock(return_value=MagicMock())
  1172. mock_context.return_value.established = False
  1173. mock_context.return_value.step = MagicMock(return_value=None)
  1174. with patch.object(salt.utils.vmware.gssapi, 'InitContext',
  1175. mock_context):
  1176. with self.assertRaises(CommandExecutionError) as excinfo:
  1177. salt.utils.vmware.get_gssapi_token('principal', 'host',
  1178. 'domain')
  1179. self.assertEqual(mock_context.return_value.step.called, 1)
  1180. self.assertIn('Can\'t receive token',
  1181. excinfo.exception.strerror)
  1182. @skipIf(not HAS_GSSAPI, 'The \'gssapi\' library is missing')
  1183. def test_context_extablished(self):
  1184. mock_context = MagicMock(return_value=MagicMock())
  1185. mock_context.return_value.established = True
  1186. mock_context.return_value.step = MagicMock(return_value='out_token')
  1187. with patch.object(salt.utils.vmware.gssapi, 'InitContext',
  1188. mock_context):
  1189. mock_context.established = True
  1190. mock_context.step = MagicMock(return_value=None)
  1191. with self.assertRaises(CommandExecutionError) as excinfo:
  1192. salt.utils.vmware.get_gssapi_token('principal', 'host',
  1193. 'domain')
  1194. self.assertEqual(mock_context.step.called, 0)
  1195. self.assertIn('Context established, but didn\'t receive token',
  1196. excinfo.exception.strerror)
  1197. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1198. class PrivateGetServiceInstanceTestCase(TestCase):
  1199. '''
  1200. Tests for salt.utils.vmware._get_service_instance
  1201. '''
  1202. def setUp(self):
  1203. patches = (
  1204. ('salt.utils.vmware.SmartConnect', MagicMock()),
  1205. ('salt.utils.vmware.Disconnect', MagicMock()),
  1206. ('salt.utils.vmware.get_gssapi_token', MagicMock(return_value='fake_token'))
  1207. )
  1208. for mod, mock in patches:
  1209. patcher = patch(mod, mock)
  1210. patcher.start()
  1211. self.addCleanup(patcher.stop)
  1212. def test_invalid_mechianism(self):
  1213. with self.assertRaises(CommandExecutionError) as excinfo:
  1214. salt.utils.vmware._get_service_instance(
  1215. host='fake_host.fqdn',
  1216. username='fake_username',
  1217. password='fake_password',
  1218. protocol='fake_protocol',
  1219. port=1,
  1220. mechanism='invalid_mechanism',
  1221. principal='fake principal',
  1222. domain='fake_domain')
  1223. self.assertIn('Unsupported mechanism', excinfo.exception.strerror)
  1224. def test_userpass_mechanism_empty_username(self):
  1225. with self.assertRaises(CommandExecutionError) as excinfo:
  1226. salt.utils.vmware._get_service_instance(
  1227. host='fake_host.fqdn',
  1228. username=None,
  1229. password='fake_password',
  1230. protocol='fake_protocol',
  1231. port=1,
  1232. mechanism='userpass',
  1233. principal='fake principal',
  1234. domain='fake_domain')
  1235. self.assertIn('mandatory parameter \'username\'',
  1236. excinfo.exception.strerror)
  1237. def test_userpass_mechanism_empty_password(self):
  1238. with self.assertRaises(CommandExecutionError) as excinfo:
  1239. salt.utils.vmware._get_service_instance(
  1240. host='fake_host.fqdn',
  1241. username='fake_username',
  1242. password=None,
  1243. protocol='fake_protocol',
  1244. port=1,
  1245. mechanism='userpass',
  1246. principal='fake principal',
  1247. domain='fake_domain')
  1248. self.assertIn('mandatory parameter \'password\'',
  1249. excinfo.exception.strerror)
  1250. def test_userpass_mechanism_no_domain(self):
  1251. mock_sc = MagicMock()
  1252. with patch('salt.utils.vmware.SmartConnect', mock_sc):
  1253. salt.utils.vmware._get_service_instance(
  1254. host='fake_host.fqdn',
  1255. username='fake_username',
  1256. password='fake_password',
  1257. protocol='fake_protocol',
  1258. port=1,
  1259. mechanism='userpass',
  1260. principal='fake principal',
  1261. domain=None)
  1262. mock_sc.assert_called_once_with(
  1263. host='fake_host.fqdn',
  1264. user='fake_username',
  1265. pwd='fake_password',
  1266. protocol='fake_protocol',
  1267. port=1,
  1268. b64token=None,
  1269. mechanism='userpass')
  1270. def test_userpass_mech_domain_unused(self):
  1271. mock_sc = MagicMock()
  1272. with patch('salt.utils.vmware.SmartConnect', mock_sc):
  1273. salt.utils.vmware._get_service_instance(
  1274. host='fake_host.fqdn',
  1275. username='fake_username@domain',
  1276. password='fake_password',
  1277. protocol='fake_protocol',
  1278. port=1,
  1279. mechanism='userpass',
  1280. principal='fake principal',
  1281. domain='fake_domain')
  1282. mock_sc.assert_called_once_with(
  1283. host='fake_host.fqdn',
  1284. user='fake_username@domain',
  1285. pwd='fake_password',
  1286. protocol='fake_protocol',
  1287. port=1,
  1288. b64token=None,
  1289. mechanism='userpass')
  1290. mock_sc.reset_mock()
  1291. salt.utils.vmware._get_service_instance(
  1292. host='fake_host.fqdn',
  1293. username='domain\\fake_username',
  1294. password='fake_password',
  1295. protocol='fake_protocol',
  1296. port=1,
  1297. mechanism='userpass',
  1298. principal='fake principal',
  1299. domain='fake_domain')
  1300. mock_sc.assert_called_once_with(
  1301. host='fake_host.fqdn',
  1302. user='domain\\fake_username',
  1303. pwd='fake_password',
  1304. protocol='fake_protocol',
  1305. port=1,
  1306. b64token=None,
  1307. mechanism='userpass')
  1308. def test_sspi_empty_principal(self):
  1309. with self.assertRaises(CommandExecutionError) as excinfo:
  1310. salt.utils.vmware._get_service_instance(
  1311. host='fake_host.fqdn',
  1312. username='fake_username',
  1313. password='fake_password',
  1314. protocol='fake_protocol',
  1315. port=1,
  1316. mechanism='sspi',
  1317. principal=None,
  1318. domain='fake_domain')
  1319. self.assertIn('mandatory parameters are missing',
  1320. excinfo.exception.strerror)
  1321. def test_sspi_empty_domain(self):
  1322. with self.assertRaises(CommandExecutionError) as excinfo:
  1323. salt.utils.vmware._get_service_instance(
  1324. host='fake_host.fqdn',
  1325. username='fake_username',
  1326. password='fake_password',
  1327. protocol='fake_protocol',
  1328. port=1,
  1329. mechanism='sspi',
  1330. principal='fake_principal',
  1331. domain=None)
  1332. self.assertIn('mandatory parameters are missing',
  1333. excinfo.exception.strerror)
  1334. def test_sspi_get_token_error(self):
  1335. mock_token = MagicMock(side_effect=Exception('Exception'))
  1336. with patch('salt.utils.vmware.get_gssapi_token', mock_token):
  1337. with self.assertRaises(VMwareConnectionError) as excinfo:
  1338. salt.utils.vmware._get_service_instance(
  1339. host='fake_host.fqdn',
  1340. username='fake_username',
  1341. password='fake_password',
  1342. protocol='fake_protocol',
  1343. port=1,
  1344. mechanism='sspi',
  1345. principal='fake_principal',
  1346. domain='fake_domain')
  1347. mock_token.assert_called_once_with('fake_principal',
  1348. 'fake_host.fqdn',
  1349. 'fake_domain')
  1350. self.assertEqual('Exception', excinfo.exception.strerror)
  1351. def test_sspi_get_token_success_(self):
  1352. mock_token = MagicMock(return_value='fake_token')
  1353. mock_sc = MagicMock()
  1354. with patch('salt.utils.vmware.get_gssapi_token', mock_token):
  1355. with patch('salt.utils.vmware.SmartConnect', mock_sc):
  1356. salt.utils.vmware._get_service_instance(
  1357. host='fake_host.fqdn',
  1358. username='fake_username',
  1359. password='fake_password',
  1360. protocol='fake_protocol',
  1361. port=1,
  1362. mechanism='sspi',
  1363. principal='fake_principal',
  1364. domain='fake_domain')
  1365. mock_token.assert_called_once_with('fake_principal',
  1366. 'fake_host.fqdn',
  1367. 'fake_domain')
  1368. mock_sc.assert_called_once_with(
  1369. host='fake_host.fqdn',
  1370. user='fake_username',
  1371. pwd='fake_password',
  1372. protocol='fake_protocol',
  1373. port=1,
  1374. b64token='fake_token',
  1375. mechanism='sspi')
  1376. def test_first_attempt_successful_connection(self):
  1377. mock_sc = MagicMock()
  1378. with patch('salt.utils.vmware.SmartConnect', mock_sc):
  1379. salt.utils.vmware._get_service_instance(
  1380. host='fake_host.fqdn',
  1381. username='fake_username',
  1382. password='fake_password',
  1383. protocol='fake_protocol',
  1384. port=1,
  1385. mechanism='sspi',
  1386. principal='fake_principal',
  1387. domain='fake_domain')
  1388. mock_sc.assert_called_once_with(
  1389. host='fake_host.fqdn',
  1390. user='fake_username',
  1391. pwd='fake_password',
  1392. protocol='fake_protocol',
  1393. port=1,
  1394. b64token='fake_token',
  1395. mechanism='sspi')
  1396. @skipIf(not SSL_VALIDATION, 'SSL validation is not enabled')
  1397. def test_second_attempt_successful_connection(self):
  1398. with patch('ssl.SSLContext', MagicMock()), \
  1399. patch(ssl_context, MagicMock()):
  1400. exc = vim.fault.HostConnectFault()
  1401. exc.msg = '[SSL: CERTIFICATE_VERIFY_FAILED]'
  1402. mock_sc = MagicMock(side_effect=[exc, None])
  1403. mock_ssl = MagicMock()
  1404. with patch('salt.utils.vmware.SmartConnect', mock_sc):
  1405. with patch(ssl_context,
  1406. mock_ssl):
  1407. salt.utils.vmware._get_service_instance(
  1408. host='fake_host.fqdn',
  1409. username='fake_username',
  1410. password='fake_password',
  1411. protocol='fake_protocol',
  1412. port=1,
  1413. mechanism='sspi',
  1414. principal='fake_principal',
  1415. domain='fake_domain')
  1416. mock_ssl.assert_called_once_with()
  1417. calls = [call(host='fake_host.fqdn',
  1418. user='fake_username',
  1419. pwd='fake_password',
  1420. protocol='fake_protocol',
  1421. port=1,
  1422. b64token='fake_token',
  1423. mechanism='sspi'),
  1424. call(host='fake_host.fqdn',
  1425. user='fake_username',
  1426. pwd='fake_password',
  1427. protocol='fake_protocol',
  1428. port=1,
  1429. sslContext=mock_ssl.return_value,
  1430. b64token='fake_token',
  1431. mechanism='sspi')]
  1432. mock_sc.assert_has_calls(calls)
  1433. @skipIf(not SSL_VALIDATION, 'SSL validation is not enabled')
  1434. def test_third_attempt_successful_connection(self):
  1435. with patch('ssl.SSLContext', MagicMock()), \
  1436. patch(ssl_context, MagicMock()):
  1437. exc = vim.fault.HostConnectFault()
  1438. exc.msg = '[SSL: CERTIFICATE_VERIFY_FAILED]'
  1439. exc2 = Exception('certificate verify failed')
  1440. mock_sc = MagicMock(side_effect=[exc, exc2, None])
  1441. mock_ssl_unverif = MagicMock()
  1442. mock_ssl_context = MagicMock()
  1443. with patch('salt.utils.vmware.SmartConnect', mock_sc):
  1444. with patch(ssl_context, mock_ssl_unverif):
  1445. with patch('ssl.SSLContext', mock_ssl_context):
  1446. salt.utils.vmware._get_service_instance(
  1447. host='fake_host.fqdn',
  1448. username='fake_username',
  1449. password='fake_password',
  1450. protocol='fake_protocol',
  1451. port=1,
  1452. mechanism='sspi',
  1453. principal='fake_principal',
  1454. domain='fake_domain')
  1455. mock_ssl_context.assert_called_once_with(ssl.PROTOCOL_TLSv1)
  1456. mock_ssl_unverif.assert_called_once_with()
  1457. calls = [call(host='fake_host.fqdn',
  1458. user='fake_username',
  1459. pwd='fake_password',
  1460. protocol='fake_protocol',
  1461. port=1,
  1462. b64token='fake_token',
  1463. mechanism='sspi'),
  1464. call(host='fake_host.fqdn',
  1465. user='fake_username',
  1466. pwd='fake_password',
  1467. protocol='fake_protocol',
  1468. port=1,
  1469. sslContext=mock_ssl_unverif.return_value,
  1470. b64token='fake_token',
  1471. mechanism='sspi'),
  1472. call(host='fake_host.fqdn',
  1473. user='fake_username',
  1474. pwd='fake_password',
  1475. protocol='fake_protocol',
  1476. port=1,
  1477. sslContext=mock_ssl_context.return_value,
  1478. b64token='fake_token',
  1479. mechanism='sspi'),
  1480. ]
  1481. mock_sc.assert_has_calls(calls)
  1482. def test_first_attempt_unsuccessful_connection_default_error(self):
  1483. exc = Exception('Exception')
  1484. mock_sc = MagicMock(side_effect=exc)
  1485. with patch('salt.utils.vmware.SmartConnect', mock_sc):
  1486. with self.assertRaises(VMwareConnectionError) as excinfo:
  1487. salt.utils.vmware._get_service_instance(
  1488. host='fake_host.fqdn',
  1489. username='fake_username',
  1490. password='fake_password',
  1491. protocol='fake_protocol',
  1492. port=1,
  1493. mechanism='sspi',
  1494. principal='fake_principal',
  1495. domain='fake_domain')
  1496. self.assertEqual(mock_sc.call_count, 1)
  1497. self.assertIn('Could not connect to host \'fake_host.fqdn\'',
  1498. excinfo.Exception.message)
  1499. def test_first_attempt_unsuccessful_connection_vim_fault(self):
  1500. exc = vim.fault.VimFault()
  1501. exc.msg = 'VimFault'
  1502. mock_sc = MagicMock(side_effect=exc)
  1503. with patch('salt.utils.vmware.SmartConnect', mock_sc):
  1504. with self.assertRaises(VMwareConnectionError) as excinfo:
  1505. salt.utils.vmware._get_service_instance(
  1506. host='fake_host.fqdn',
  1507. username='fake_username',
  1508. password='fake_password',
  1509. protocol='fake_protocol',
  1510. port=1,
  1511. mechanism='sspi',
  1512. principal='fake_principal',
  1513. domain='fake_domain')
  1514. self.assertEqual(mock_sc.call_count, 1)
  1515. self.assertEqual('VimFault', excinfo.Exception.message)
  1516. @skipIf(not SSL_VALIDATION, 'SSL validation is not enabled')
  1517. def test_second_attempt_unsuccsessful_connection_default_error(self):
  1518. with patch('ssl.SSLContext', MagicMock()), \
  1519. patch(ssl_context, MagicMock()):
  1520. exc = vim.fault.HostConnectFault()
  1521. exc.msg = '[SSL: CERTIFICATE_VERIFY_FAILED]'
  1522. exc2 = Exception('Exception')
  1523. mock_sc = MagicMock(side_effect=[exc, exc2])
  1524. with patch('salt.utils.vmware.SmartConnect', mock_sc):
  1525. with self.assertRaises(VMwareConnectionError) as excinfo:
  1526. salt.utils.vmware._get_service_instance(
  1527. host='fake_host.fqdn',
  1528. username='fake_username',
  1529. password='fake_password',
  1530. protocol='fake_protocol',
  1531. port=1,
  1532. mechanism='sspi',
  1533. principal='fake_principal',
  1534. domain='fake_domain')
  1535. self.assertEqual(mock_sc.call_count, 2)
  1536. self.assertIn('Could not connect to host \'fake_host.fqdn\'',
  1537. excinfo.Exception.message)
  1538. @skipIf(not SSL_VALIDATION, 'SSL validation is not enabled')
  1539. def test_second_attempt_unsuccsessful_connection_vim_fault(self):
  1540. with patch('ssl.SSLContext', MagicMock()), \
  1541. patch(ssl_context, MagicMock()):
  1542. exc = vim.fault.HostConnectFault()
  1543. exc.msg = '[SSL: CERTIFICATE_VERIFY_FAILED]'
  1544. exc2 = vim.fault.VimFault()
  1545. exc2.msg = 'VimFault'
  1546. mock_sc = MagicMock(side_effect=[exc, exc2])
  1547. with patch('salt.utils.vmware.SmartConnect', mock_sc):
  1548. with self.assertRaises(VMwareConnectionError) as excinfo:
  1549. salt.utils.vmware._get_service_instance(
  1550. host='fake_host.fqdn',
  1551. username='fake_username',
  1552. password='fake_password',
  1553. protocol='fake_protocol',
  1554. port=1,
  1555. mechanism='sspi',
  1556. principal='fake_principal',
  1557. domain='fake_domain')
  1558. self.assertEqual(mock_sc.call_count, 2)
  1559. self.assertIn('VimFault', excinfo.Exception.message)
  1560. @skipIf(not SSL_VALIDATION, 'SSL validation is not enabled')
  1561. def test_third_attempt_unsuccessful_connection_detault_error(self):
  1562. with patch('ssl.SSLContext', MagicMock()), \
  1563. patch(ssl_context, MagicMock()):
  1564. exc = vim.fault.HostConnectFault()
  1565. exc.msg = '[SSL: CERTIFICATE_VERIFY_FAILED]'
  1566. exc2 = Exception('certificate verify failed')
  1567. exc3 = Exception('Exception')
  1568. mock_sc = MagicMock(side_effect=[exc, exc2, exc3])
  1569. with patch('salt.utils.vmware.SmartConnect', mock_sc):
  1570. with self.assertRaises(VMwareConnectionError) as excinfo:
  1571. salt.utils.vmware._get_service_instance(
  1572. host='fake_host.fqdn',
  1573. username='fake_username',
  1574. password='fake_password',
  1575. protocol='fake_protocol',
  1576. port=1,
  1577. mechanism='sspi',
  1578. principal='fake_principal',
  1579. domain='fake_domain')
  1580. self.assertEqual(mock_sc.call_count, 3)
  1581. self.assertIn('Exception', excinfo.Exception.message)
  1582. @skipIf(not SSL_VALIDATION, 'SSL validation is not enabled')
  1583. def test_third_attempt_unsuccessful_connection_vim_fault(self):
  1584. with patch('ssl.SSLContext', MagicMock()), \
  1585. patch(ssl_context, MagicMock()):
  1586. exc = vim.fault.HostConnectFault()
  1587. exc.msg = '[SSL: CERTIFICATE_VERIFY_FAILED]'
  1588. exc2 = Exception('certificate verify failed')
  1589. exc3 = vim.fault.VimFault()
  1590. exc3.msg = 'VimFault'
  1591. mock_sc = MagicMock(side_effect=[exc, exc2, exc3])
  1592. with patch('salt.utils.vmware.SmartConnect', mock_sc):
  1593. with self.assertRaises(VMwareConnectionError) as excinfo:
  1594. salt.utils.vmware._get_service_instance(
  1595. host='fake_host.fqdn',
  1596. username='fake_username',
  1597. password='fake_password',
  1598. protocol='fake_protocol',
  1599. port=1,
  1600. mechanism='sspi',
  1601. principal='fake_principal',
  1602. domain='fake_domain')
  1603. self.assertEqual(mock_sc.call_count, 3)
  1604. self.assertIn('VimFault', excinfo.Exception.message)
  1605. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1606. class GetServiceInstanceTestCase(TestCase):
  1607. '''
  1608. Tests for salt.utils.vmware.get_service_instance
  1609. '''
  1610. def setUp(self):
  1611. patches = (
  1612. ('salt.utils.vmware.GetSi', MagicMock(return_value=None)),
  1613. ('salt.utils.vmware._get_service_instance', MagicMock(return_value=MagicMock()))
  1614. )
  1615. for mod, mock in patches:
  1616. patcher = patch(mod, mock)
  1617. patcher.start()
  1618. self.addCleanup(patcher.stop)
  1619. def test_default_params(self):
  1620. mock_get_si = MagicMock()
  1621. with patch('salt.utils.vmware._get_service_instance', mock_get_si):
  1622. salt.utils.vmware.get_service_instance(
  1623. host='fake_host'
  1624. )
  1625. mock_get_si.assert_called_once_with('fake_host', None, None,
  1626. 'https', 443, 'userpass', None,
  1627. None)
  1628. def test_no_cached_service_instance_same_host_on_proxy(self):
  1629. with patch('salt.utils.platform.is_proxy', MagicMock(return_value=True)):
  1630. # Service instance is uncached when using class default mock objs
  1631. mock_get_si = MagicMock()
  1632. with patch('salt.utils.vmware._get_service_instance', mock_get_si):
  1633. salt.utils.vmware.get_service_instance(
  1634. host='fake_host',
  1635. username='fake_username',
  1636. password='fake_password',
  1637. protocol='fake_protocol',
  1638. port=1,
  1639. mechanism='fake_mechanism',
  1640. principal='fake_principal',
  1641. domain='fake_domain'
  1642. )
  1643. mock_get_si.assert_called_once_with('fake_host',
  1644. 'fake_username',
  1645. 'fake_password',
  1646. 'fake_protocol',
  1647. 1,
  1648. 'fake_mechanism',
  1649. 'fake_principal',
  1650. 'fake_domain')
  1651. def test_cached_service_instance_different_host(self):
  1652. mock_si = MagicMock()
  1653. mock_si_stub = MagicMock()
  1654. mock_disconnect = MagicMock()
  1655. mock_get_si = MagicMock(return_value=mock_si)
  1656. mock_getstub = MagicMock()
  1657. with patch('salt.utils.vmware.GetSi', mock_get_si):
  1658. with patch('salt.utils.vmware.GetStub', mock_getstub):
  1659. with patch('salt.utils.vmware.Disconnect', mock_disconnect):
  1660. salt.utils.vmware.get_service_instance(
  1661. host='fake_host',
  1662. username='fake_username',
  1663. password='fake_password',
  1664. protocol='fake_protocol',
  1665. port=1,
  1666. mechanism='fake_mechanism',
  1667. principal='fake_principal',
  1668. domain='fake_domain'
  1669. )
  1670. self.assertEqual(mock_get_si.call_count, 1)
  1671. self.assertEqual(mock_getstub.call_count, 1)
  1672. self.assertEqual(mock_disconnect.call_count, 1)
  1673. def test_uncached_service_instance(self):
  1674. # Service instance is uncached when using class default mock objs
  1675. mock_get_si = MagicMock()
  1676. with patch('salt.utils.vmware._get_service_instance', mock_get_si):
  1677. salt.utils.vmware.get_service_instance(
  1678. host='fake_host',
  1679. username='fake_username',
  1680. password='fake_password',
  1681. protocol='fake_protocol',
  1682. port=1,
  1683. mechanism='fake_mechanism',
  1684. principal='fake_principal',
  1685. domain='fake_domain'
  1686. )
  1687. mock_get_si.assert_called_once_with('fake_host',
  1688. 'fake_username',
  1689. 'fake_password',
  1690. 'fake_protocol',
  1691. 1,
  1692. 'fake_mechanism',
  1693. 'fake_principal',
  1694. 'fake_domain')
  1695. def test_unauthenticated_service_instance(self):
  1696. mock_si_current_time = MagicMock(side_effect=vim.fault.NotAuthenticated)
  1697. mock_si = MagicMock()
  1698. mock_get_si = MagicMock(return_value=mock_si)
  1699. mock_si.CurrentTime = mock_si_current_time
  1700. mock_disconnect = MagicMock()
  1701. with patch('salt.utils.vmware._get_service_instance', mock_get_si):
  1702. with patch('salt.utils.vmware.Disconnect', mock_disconnect):
  1703. salt.utils.vmware.get_service_instance(
  1704. host='fake_host',
  1705. username='fake_username',
  1706. password='fake_password',
  1707. protocol='fake_protocol',
  1708. port=1,
  1709. mechanism='fake_mechanism',
  1710. principal='fake_principal',
  1711. domain='fake_domain'
  1712. )
  1713. self.assertEqual(mock_si_current_time.call_count, 1)
  1714. self.assertEqual(mock_disconnect.call_count, 1)
  1715. self.assertEqual(mock_get_si.call_count, 2)
  1716. def test_current_time_raise_no_permission(self):
  1717. exc = vim.fault.NoPermission()
  1718. exc.privilegeId = 'Fake privilege'
  1719. with patch('salt.utils.vmware._get_service_instance',
  1720. MagicMock(return_value=MagicMock(
  1721. CurrentTime=MagicMock(side_effect=exc)))):
  1722. with self.assertRaises(VMwareApiError) as excinfo:
  1723. salt.utils.vmware.get_service_instance(
  1724. host='fake_host',
  1725. username='fake_username',
  1726. password='fake_password',
  1727. protocol='fake_protocol',
  1728. port=1,
  1729. mechanism='fake_mechanism',
  1730. principal='fake_principal',
  1731. domain='fake_domain')
  1732. self.assertEqual(excinfo.exception.strerror,
  1733. 'Not enough permissions. Required privilege: '
  1734. 'Fake privilege')
  1735. def test_current_time_raise_vim_fault(self):
  1736. exc = vim.fault.VimFault()
  1737. exc.msg = 'VimFault msg'
  1738. with patch('salt.utils.vmware._get_service_instance',
  1739. MagicMock(return_value=MagicMock(
  1740. CurrentTime=MagicMock(side_effect=exc)))):
  1741. with self.assertRaises(VMwareApiError) as excinfo:
  1742. salt.utils.vmware.get_service_instance(
  1743. host='fake_host',
  1744. username='fake_username',
  1745. password='fake_password',
  1746. protocol='fake_protocol',
  1747. port=1,
  1748. mechanism='fake_mechanism',
  1749. principal='fake_principal',
  1750. domain='fake_domain')
  1751. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  1752. def test_current_time_raise_runtime_fault(self):
  1753. exc = vmodl.RuntimeFault()
  1754. exc.msg = 'RuntimeFault msg'
  1755. with patch('salt.utils.vmware._get_service_instance',
  1756. MagicMock(return_value=MagicMock(
  1757. CurrentTime=MagicMock(side_effect=exc)))):
  1758. with self.assertRaises(VMwareRuntimeError) as excinfo:
  1759. salt.utils.vmware.get_service_instance(
  1760. host='fake_host',
  1761. username='fake_username',
  1762. password='fake_password',
  1763. protocol='fake_protocol',
  1764. port=1,
  1765. mechanism='fake_mechanism',
  1766. principal='fake_principal',
  1767. domain='fake_domain')
  1768. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  1769. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1770. class DisconnectTestCase(TestCase):
  1771. '''
  1772. Tests for salt.utils.vmware.disconnect
  1773. '''
  1774. def setUp(self):
  1775. self.mock_si = MagicMock()
  1776. self.addCleanup(delattr, self, 'mock_si')
  1777. def test_disconnect(self):
  1778. mock_disconnect = MagicMock()
  1779. with patch('salt.utils.vmware.Disconnect', mock_disconnect):
  1780. salt.utils.vmware.disconnect(
  1781. service_instance=self.mock_si)
  1782. mock_disconnect.assert_called_once_with(self.mock_si)
  1783. def test_disconnect_raise_no_permission(self):
  1784. exc = vim.fault.NoPermission()
  1785. exc.privilegeId = 'Fake privilege'
  1786. with patch('salt.utils.vmware.Disconnect', MagicMock(side_effect=exc)):
  1787. with self.assertRaises(VMwareApiError) as excinfo:
  1788. salt.utils.vmware.disconnect(
  1789. service_instance=self.mock_si)
  1790. self.assertEqual(excinfo.exception.strerror,
  1791. 'Not enough permissions. Required privilege: '
  1792. 'Fake privilege')
  1793. def test_disconnect_raise_vim_fault(self):
  1794. exc = vim.fault.VimFault()
  1795. exc.msg = 'VimFault msg'
  1796. with patch('salt.utils.vmware.Disconnect', MagicMock(side_effect=exc)):
  1797. with self.assertRaises(VMwareApiError) as excinfo:
  1798. salt.utils.vmware.disconnect(
  1799. service_instance=self.mock_si)
  1800. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  1801. def test_disconnect_raise_runtime_fault(self):
  1802. exc = vmodl.RuntimeFault()
  1803. exc.msg = 'RuntimeFault msg'
  1804. with patch('salt.utils.vmware.Disconnect', MagicMock(side_effect=exc)):
  1805. with self.assertRaises(VMwareRuntimeError) as excinfo:
  1806. salt.utils.vmware.disconnect(
  1807. service_instance=self.mock_si)
  1808. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  1809. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1810. class IsConnectionToAVCenterTestCase(TestCase):
  1811. '''
  1812. Tests for salt.utils.vmware.is_connection_to_a_vcenter
  1813. '''
  1814. def test_api_type_raise_no_permission(self):
  1815. exc = vim.fault.NoPermission()
  1816. exc.privilegeId = 'Fake privilege'
  1817. mock_si = MagicMock()
  1818. type(mock_si.content.about).apiType = PropertyMock(side_effect=exc)
  1819. with self.assertRaises(VMwareApiError) as excinfo:
  1820. salt.utils.vmware.is_connection_to_a_vcenter(mock_si)
  1821. self.assertEqual(excinfo.exception.strerror,
  1822. 'Not enough permissions. Required privilege: '
  1823. 'Fake privilege')
  1824. def test_api_type_raise_vim_fault(self):
  1825. exc = vim.fault.VimFault()
  1826. exc.msg = 'VimFault msg'
  1827. mock_si = MagicMock()
  1828. type(mock_si.content.about).apiType = PropertyMock(side_effect=exc)
  1829. with self.assertRaises(VMwareApiError) as excinfo:
  1830. salt.utils.vmware.is_connection_to_a_vcenter(mock_si)
  1831. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  1832. def test_api_type_raise_runtime_fault(self):
  1833. exc = vmodl.RuntimeFault()
  1834. exc.msg = 'RuntimeFault msg'
  1835. mock_si = MagicMock()
  1836. type(mock_si.content.about).apiType = PropertyMock(side_effect=exc)
  1837. with self.assertRaises(VMwareRuntimeError) as excinfo:
  1838. salt.utils.vmware.is_connection_to_a_vcenter(mock_si)
  1839. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  1840. def test_connected_to_a_vcenter(self):
  1841. mock_si = MagicMock()
  1842. mock_si.content.about.apiType = 'VirtualCenter'
  1843. ret = salt.utils.vmware.is_connection_to_a_vcenter(mock_si)
  1844. self.assertTrue(ret)
  1845. def test_connected_to_a_host(self):
  1846. mock_si = MagicMock()
  1847. mock_si.content.about.apiType = 'HostAgent'
  1848. ret = salt.utils.vmware.is_connection_to_a_vcenter(mock_si)
  1849. self.assertFalse(ret)
  1850. def test_connected_to_invalid_entity(self):
  1851. mock_si = MagicMock()
  1852. mock_si.content.about.apiType = 'UnsupportedType'
  1853. with self.assertRaises(VMwareApiError) as excinfo:
  1854. salt.utils.vmware.is_connection_to_a_vcenter(mock_si)
  1855. self.assertIn('Unexpected api type \'UnsupportedType\'',
  1856. excinfo.exception.strerror)
  1857. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1858. class GetNewServiceInstanceStub(TestCase, LoaderModuleMockMixin):
  1859. '''
  1860. Tests for salt.utils.vmware.get_new_service_instance_stub
  1861. '''
  1862. def setup_loader_modules(self):
  1863. return {salt.utils.vmware: {
  1864. '__virtual__': MagicMock(return_value='vmware'),
  1865. 'sys': MagicMock(),
  1866. 'ssl': MagicMock()}}
  1867. def setUp(self):
  1868. self.mock_stub = MagicMock(
  1869. host='fake_host:1000',
  1870. cookie='ignore"fake_cookie')
  1871. self.mock_si = MagicMock(
  1872. _stub=self.mock_stub)
  1873. self.mock_ret = MagicMock()
  1874. self.mock_new_stub = MagicMock()
  1875. self.context_dict = {}
  1876. patches = (('salt.utils.vmware.VmomiSupport.GetRequestContext',
  1877. MagicMock(
  1878. return_value=self.context_dict)),
  1879. ('salt.utils.vmware.SoapStubAdapter',
  1880. MagicMock(return_value=self.mock_new_stub)))
  1881. for mod, mock in patches:
  1882. patcher = patch(mod, mock)
  1883. patcher.start()
  1884. self.addCleanup(patcher.stop)
  1885. type(salt.utils.vmware.sys).version_info = \
  1886. PropertyMock(return_value=(2, 7, 9))
  1887. self.mock_context = MagicMock()
  1888. self.mock_create_default_context = \
  1889. MagicMock(return_value=self.mock_context)
  1890. salt.utils.vmware.ssl.create_default_context = \
  1891. self.mock_create_default_context
  1892. def tearDown(self):
  1893. for attr in ('mock_stub', 'mock_si', 'mock_ret', 'mock_new_stub',
  1894. 'context_dict', 'mock_context',
  1895. 'mock_create_default_context'):
  1896. delattr(self, attr)
  1897. def test_ssl_default_context_loaded(self):
  1898. salt.utils.vmware.get_new_service_instance_stub(
  1899. self.mock_si, 'fake_path')
  1900. self.mock_create_default_context.assert_called_once_with()
  1901. self.assertFalse(self.mock_context.check_hostname)
  1902. self.assertEqual(self.mock_context.verify_mode,
  1903. salt.utils.vmware.ssl.CERT_NONE)
  1904. def test_ssl_default_context_not_loaded(self):
  1905. type(salt.utils.vmware.sys).version_info = \
  1906. PropertyMock(return_value=(2, 7, 8))
  1907. salt.utils.vmware.get_new_service_instance_stub(
  1908. self.mock_si, 'fake_path')
  1909. self.assertEqual(self.mock_create_default_context.call_count, 0)
  1910. def test_session_cookie_in_context(self):
  1911. salt.utils.vmware.get_new_service_instance_stub(
  1912. self.mock_si, 'fake_path')
  1913. self.assertEqual(self.context_dict['vcSessionCookie'], 'fake_cookie')
  1914. def test_get_new_stub(self):
  1915. mock_get_new_stub = MagicMock()
  1916. with patch('salt.utils.vmware.SoapStubAdapter', mock_get_new_stub):
  1917. salt.utils.vmware.get_new_service_instance_stub(
  1918. self.mock_si, 'fake_path', 'fake_ns', 'fake_version')
  1919. mock_get_new_stub.assert_called_once_with(
  1920. host='fake_host', ns='fake_ns', path='fake_path',
  1921. version='fake_version', poolSize=0, sslContext=self.mock_context)
  1922. def test_get_new_stub_2_7_8_python(self):
  1923. type(salt.utils.vmware.sys).version_info = \
  1924. PropertyMock(return_value=(2, 7, 8))
  1925. mock_get_new_stub = MagicMock()
  1926. with patch('salt.utils.vmware.SoapStubAdapter', mock_get_new_stub):
  1927. salt.utils.vmware.get_new_service_instance_stub(
  1928. self.mock_si, 'fake_path', 'fake_ns', 'fake_version')
  1929. mock_get_new_stub.assert_called_once_with(
  1930. host='fake_host', ns='fake_ns', path='fake_path',
  1931. version='fake_version', poolSize=0, sslContext=None)
  1932. def test_new_stub_returned(self):
  1933. ret = salt.utils.vmware.get_new_service_instance_stub(
  1934. self.mock_si, 'fake_path')
  1935. self.assertEqual(self.mock_new_stub.cookie, 'ignore"fake_cookie')
  1936. self.assertEqual(ret, self.mock_new_stub)
  1937. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1938. class GetServiceInstanceFromManagedObjectTestCase(TestCase):
  1939. '''
  1940. Tests for salt.utils.vmware.get_managed_instance_from_managed_object
  1941. '''
  1942. def setUp(self):
  1943. patches = (
  1944. ('salt.utils.vmware.vim.ServiceInstance', MagicMock()),
  1945. )
  1946. for mod, mock in patches:
  1947. patcher = patch(mod, mock)
  1948. patcher.start()
  1949. self.addCleanup(patcher.stop)
  1950. self.mock_si = MagicMock()
  1951. self.mock_stub = PropertyMock()
  1952. self.mock_mo_ref = MagicMock(_stub=self.mock_stub)
  1953. for attr in ('mock_si', 'mock_stub', 'mock_mo_ref'):
  1954. self.addCleanup(delattr, self, attr)
  1955. def test_default_name_parameter(self):
  1956. mock_trace = MagicMock()
  1957. type(salt.utils.vmware.log).trace = mock_trace
  1958. salt.utils.vmware.get_service_instance_from_managed_object(
  1959. self.mock_mo_ref)
  1960. mock_trace.assert_called_once_with(
  1961. '[%s] Retrieving service instance from managed object',
  1962. '<unnamed>')
  1963. def test_name_parameter_passed_in(self):
  1964. mock_trace = MagicMock()
  1965. type(salt.utils.vmware.log).trace = mock_trace
  1966. salt.utils.vmware.get_service_instance_from_managed_object(
  1967. self.mock_mo_ref, 'fake_mo_name')
  1968. mock_trace.assert_called_once_with(
  1969. '[%s] Retrieving service instance from managed object',
  1970. 'fake_mo_name')
  1971. def test_service_instance_instantiation(self):
  1972. mock_service_instance_ini = MagicMock()
  1973. with patch('salt.utils.vmware.vim.ServiceInstance',
  1974. mock_service_instance_ini):
  1975. salt.utils.vmware.get_service_instance_from_managed_object(
  1976. self.mock_mo_ref)
  1977. mock_service_instance_ini.assert_called_once_with('ServiceInstance')
  1978. def test_si_return_and_stub_assignment(self):
  1979. with patch('salt.utils.vmware.vim.ServiceInstance',
  1980. MagicMock(return_value=self.mock_si)):
  1981. ret = salt.utils.vmware.get_service_instance_from_managed_object(
  1982. self.mock_mo_ref)
  1983. self.assertEqual(ret, self.mock_si)
  1984. self.assertEqual(ret._stub, self.mock_stub)
  1985. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  1986. class GetDatacentersTestCase(TestCase):
  1987. '''
  1988. Tests for salt.utils.vmware.get_datacenters
  1989. '''
  1990. def setUp(self):
  1991. patches = (
  1992. ('salt.utils.vmware.get_mors_with_properties',
  1993. MagicMock(return_value=[{'name': 'fake_dc', 'object': MagicMock()}])),
  1994. )
  1995. for mod, mock in patches:
  1996. patcher = patch(mod, mock)
  1997. patcher.start()
  1998. self.addCleanup(patcher.stop)
  1999. self.mock_si = MagicMock()
  2000. self.mock_dc1 = MagicMock()
  2001. self.mock_dc2 = MagicMock()
  2002. self.mock_entries = [{'name': 'fake_dc1',
  2003. 'object': self.mock_dc1},
  2004. {'name': 'fake_dc2',
  2005. 'object': self.mock_dc2}]
  2006. def test_get_mors_with_properties_call(self):
  2007. mock_get_mors_with_properties = MagicMock(
  2008. return_value=[{'name': 'fake_dc', 'object': MagicMock()}])
  2009. with patch('salt.utils.vmware.get_mors_with_properties',
  2010. mock_get_mors_with_properties):
  2011. salt.utils.vmware.get_datacenters(self.mock_si, datacenter_names=['fake_dc1'])
  2012. mock_get_mors_with_properties.assert_called_once_with(
  2013. self.mock_si, vim.Datacenter, property_list=['name'])
  2014. def test_get_mors_with_properties_returns_empty_array(self):
  2015. with patch('salt.utils.vmware.get_mors_with_properties',
  2016. MagicMock(return_value=[])):
  2017. res = salt.utils.vmware.get_datacenters(self.mock_si,
  2018. datacenter_names=['fake_dc1'])
  2019. self.assertEqual(res, [])
  2020. def test_no_parameters(self):
  2021. with patch('salt.utils.vmware.get_mors_with_properties',
  2022. MagicMock(return_value=self.mock_entries)):
  2023. res = salt.utils.vmware.get_datacenters(self.mock_si)
  2024. self.assertEqual(res, [])
  2025. def test_datastore_not_found(self):
  2026. with patch('salt.utils.vmware.get_mors_with_properties',
  2027. MagicMock(return_value=self.mock_entries)):
  2028. res = salt.utils.vmware.get_datacenters(self.mock_si,
  2029. datacenter_names=['fake_dc'])
  2030. self.assertEqual(res, [])
  2031. def test_datastore_found(self):
  2032. with patch('salt.utils.vmware.get_mors_with_properties',
  2033. MagicMock(return_value=self.mock_entries)):
  2034. res = salt.utils.vmware.get_datacenters(
  2035. self.mock_si, datacenter_names=['fake_dc2'])
  2036. self.assertEqual(res, [self.mock_dc2])
  2037. def test_get_all_datastores(self):
  2038. with patch('salt.utils.vmware.get_mors_with_properties',
  2039. MagicMock(return_value=self.mock_entries)):
  2040. res = salt.utils.vmware.get_datacenters(
  2041. self.mock_si, get_all_datacenters=True)
  2042. self.assertEqual(res, [self.mock_dc1, self.mock_dc2])
  2043. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2044. class GetDatacenterTestCase(TestCase):
  2045. '''
  2046. Tests for salt.utils.vmware.get_datacenter
  2047. '''
  2048. def setUp(self):
  2049. patches = (
  2050. ('salt.utils.vmware.get_datacenters', MagicMock(return_value=[MagicMock()])),
  2051. )
  2052. for mod, mock in patches:
  2053. patcher = patch(mod, mock)
  2054. patcher.start()
  2055. self.addCleanup(patcher.stop)
  2056. self.mock_si = MagicMock()
  2057. self.mock_dc = MagicMock()
  2058. def test_get_datacenters_call(self):
  2059. mock_get_datacenters = MagicMock(return_value=[MagicMock()])
  2060. with patch('salt.utils.vmware.get_datacenters',
  2061. mock_get_datacenters):
  2062. salt.utils.vmware.get_datacenter(self.mock_si, 'fake_dc1')
  2063. mock_get_datacenters.assert_called_once_with(
  2064. self.mock_si, datacenter_names=['fake_dc1'])
  2065. def test_no_datacenters_returned(self):
  2066. with patch('salt.utils.vmware.get_datacenters',
  2067. MagicMock(return_value=[])):
  2068. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  2069. salt.utils.vmware.get_datacenter(self.mock_si, 'fake_dc1')
  2070. self.assertEqual('Datacenter \'fake_dc1\' was not found',
  2071. excinfo.exception.strerror)
  2072. def test_get_datacenter_return(self):
  2073. with patch('salt.utils.vmware.get_datacenters',
  2074. MagicMock(return_value=[self.mock_dc])):
  2075. res = salt.utils.vmware.get_datacenter(self.mock_si, 'fake_dc1')
  2076. self.assertEqual(res, self.mock_dc)
  2077. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2078. class CreateDatacenterTestCase(TestCase):
  2079. '''
  2080. Tests for salt.utils.vmware.create_datacenter
  2081. '''
  2082. def setUp(self):
  2083. patches = (
  2084. ('salt.utils.vmware.get_root_folder', MagicMock()),
  2085. )
  2086. for mod, mock in patches:
  2087. patcher = patch(mod, mock)
  2088. patcher.start()
  2089. self.addCleanup(patcher.stop)
  2090. self.mock_si = MagicMock()
  2091. self.mock_dc = MagicMock()
  2092. self.mock_create_datacenter = MagicMock(return_value=self.mock_dc)
  2093. self.mock_root_folder = MagicMock(
  2094. CreateDatacenter=self.mock_create_datacenter)
  2095. def test_get_root_folder(self):
  2096. mock_get_root_folder = MagicMock()
  2097. with patch('salt.utils.vmware.get_root_folder', mock_get_root_folder):
  2098. salt.utils.vmware.create_datacenter(self.mock_si, 'fake_dc')
  2099. mock_get_root_folder.assert_called_once_with(self.mock_si)
  2100. def test_create_datacenter_call(self):
  2101. with patch('salt.utils.vmware.get_root_folder',
  2102. MagicMock(return_value=self.mock_root_folder)):
  2103. salt.utils.vmware.create_datacenter(self.mock_si, 'fake_dc')
  2104. self.mock_create_datacenter.assert_called_once_with('fake_dc')
  2105. def test_create_datacenter_raise_no_permission(self):
  2106. exc = vim.fault.NoPermission()
  2107. exc.privilegeId = 'Fake privilege'
  2108. self.mock_root_folder = MagicMock(
  2109. CreateDatacenter=MagicMock(side_effect=exc))
  2110. with patch('salt.utils.vmware.get_root_folder',
  2111. MagicMock(return_value=self.mock_root_folder)):
  2112. with self.assertRaises(VMwareApiError) as excinfo:
  2113. salt.utils.vmware.create_datacenter(self.mock_si, 'fake_dc')
  2114. self.assertEqual(excinfo.exception.strerror,
  2115. 'Not enough permissions. Required privilege: '
  2116. 'Fake privilege')
  2117. def test_create_datacenter_raise_vim_fault(self):
  2118. exc = vim.VimFault()
  2119. exc.msg = 'VimFault msg'
  2120. self.mock_root_folder = MagicMock(
  2121. CreateDatacenter=MagicMock(side_effect=exc))
  2122. with patch('salt.utils.vmware.get_root_folder',
  2123. MagicMock(return_value=self.mock_root_folder)):
  2124. with self.assertRaises(VMwareApiError) as excinfo:
  2125. salt.utils.vmware.create_datacenter(self.mock_si, 'fake_dc')
  2126. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  2127. def test_create_datacenter_runtime_fault(self):
  2128. exc = vmodl.RuntimeFault()
  2129. exc.msg = 'RuntimeFault msg'
  2130. self.mock_root_folder = MagicMock(
  2131. CreateDatacenter=MagicMock(side_effect=exc))
  2132. with patch('salt.utils.vmware.get_root_folder',
  2133. MagicMock(return_value=self.mock_root_folder)):
  2134. with self.assertRaises(VMwareRuntimeError) as excinfo:
  2135. salt.utils.vmware.create_datacenter(self.mock_si, 'fake_dc')
  2136. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  2137. def test_datastore_successfully_created(self):
  2138. with patch('salt.utils.vmware.get_root_folder',
  2139. MagicMock(return_value=self.mock_root_folder)):
  2140. res = salt.utils.vmware.create_datacenter(self.mock_si, 'fake_dc')
  2141. self.assertEqual(res, self.mock_dc)
  2142. class FakeTaskClass(object):
  2143. pass
  2144. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2145. class GetDvssTestCase(TestCase):
  2146. def setUp(self):
  2147. self.mock_si = MagicMock()
  2148. self.mock_dc_ref = MagicMock()
  2149. self.mock_traversal_spec = MagicMock()
  2150. self.mock_items = [{'object': MagicMock(),
  2151. 'name': 'fake_dvs1'},
  2152. {'object': MagicMock(),
  2153. 'name': 'fake_dvs2'},
  2154. {'object': MagicMock(),
  2155. 'name': 'fake_dvs3'}]
  2156. self.mock_get_mors = MagicMock(return_value=self.mock_items)
  2157. patches = (
  2158. ('salt.utils.vmware.get_managed_object_name',
  2159. MagicMock()),
  2160. ('salt.utils.vmware.get_mors_with_properties',
  2161. self.mock_get_mors),
  2162. ('salt.utils.vmware.get_service_instance_from_managed_object',
  2163. MagicMock(return_value=self.mock_si)),
  2164. ('salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  2165. MagicMock(return_value=self.mock_traversal_spec)))
  2166. for mod, mock in patches:
  2167. patcher = patch(mod, mock)
  2168. patcher.start()
  2169. self.addCleanup(patcher.stop)
  2170. def tearDown(self):
  2171. for attr in ('mock_si', 'mock_dc_ref', 'mock_traversal_spec',
  2172. 'mock_items', 'mock_get_mors'):
  2173. delattr(self, attr)
  2174. def test_get_managed_object_name_call(self):
  2175. mock_get_managed_object_name = MagicMock()
  2176. with patch('salt.utils.vmware.get_managed_object_name',
  2177. mock_get_managed_object_name):
  2178. salt.utils.vmware.get_dvss(self.mock_dc_ref)
  2179. mock_get_managed_object_name.assert_called_once_with(self.mock_dc_ref)
  2180. def test_traversal_spec(self):
  2181. mock_traversal_spec = MagicMock(return_value='traversal_spec')
  2182. with patch(
  2183. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  2184. mock_traversal_spec):
  2185. salt.utils.vmware.get_dvss(self.mock_dc_ref)
  2186. mock_traversal_spec.assert_has_calls(
  2187. [call(path='childEntity', skip=False, type=vim.Folder),
  2188. call(path='networkFolder', skip=True, type=vim.Datacenter,
  2189. selectSet=['traversal_spec'])])
  2190. def test_get_mors_with_properties(self):
  2191. salt.utils.vmware.get_dvss(self.mock_dc_ref)
  2192. self.mock_get_mors.assert_called_once_with(
  2193. self.mock_si, vim.DistributedVirtualSwitch,
  2194. container_ref=self.mock_dc_ref, property_list=['name'],
  2195. traversal_spec=self.mock_traversal_spec)
  2196. def test_get_no_dvss(self):
  2197. ret = salt.utils.vmware.get_dvss(self.mock_dc_ref)
  2198. self.assertEqual(ret, [])
  2199. def test_get_all_dvss(self):
  2200. ret = salt.utils.vmware.get_dvss(self.mock_dc_ref, get_all_dvss=True)
  2201. self.assertEqual(ret, [i['object'] for i in self.mock_items])
  2202. def test_filtered_all_dvss(self):
  2203. ret = salt.utils.vmware.get_dvss(self.mock_dc_ref,
  2204. dvs_names=['fake_dvs1', 'fake_dvs3', 'no_dvs'])
  2205. self.assertEqual(ret, [self.mock_items[0]['object'],
  2206. self.mock_items[2]['object']])
  2207. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2208. class GetNetworkFolderTestCase(TestCase):
  2209. def setUp(self):
  2210. self.mock_si = MagicMock()
  2211. self.mock_dc_ref = MagicMock()
  2212. self.mock_traversal_spec = MagicMock()
  2213. self.mock_entries = [{'object': MagicMock(),
  2214. 'name': 'fake_netw_folder'}]
  2215. self.mock_get_mors = MagicMock(return_value=self.mock_entries)
  2216. patches = (
  2217. ('salt.utils.vmware.get_managed_object_name',
  2218. MagicMock(return_value='fake_dc')),
  2219. ('salt.utils.vmware.get_service_instance_from_managed_object',
  2220. MagicMock(return_value=self.mock_si)),
  2221. ('salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  2222. MagicMock(return_value=self.mock_traversal_spec)),
  2223. ('salt.utils.vmware.get_mors_with_properties',
  2224. self.mock_get_mors))
  2225. for mod, mock in patches:
  2226. patcher = patch(mod, mock)
  2227. patcher.start()
  2228. self.addCleanup(patcher.stop)
  2229. def tearDown(self):
  2230. for attr in ('mock_si', 'mock_dc_ref', 'mock_traversal_spec',
  2231. 'mock_entries', 'mock_get_mors'):
  2232. delattr(self, attr)
  2233. def test_get_managed_object_name_call(self):
  2234. mock_get_managed_object_name = MagicMock()
  2235. with patch('salt.utils.vmware.get_managed_object_name',
  2236. mock_get_managed_object_name):
  2237. salt.utils.vmware.get_network_folder(self.mock_dc_ref)
  2238. mock_get_managed_object_name.assert_called_once_with(self.mock_dc_ref)
  2239. def test_traversal_spec(self):
  2240. mock_traversal_spec = MagicMock(return_value='traversal_spec')
  2241. with patch(
  2242. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  2243. mock_traversal_spec):
  2244. salt.utils.vmware.get_network_folder(self.mock_dc_ref)
  2245. mock_traversal_spec.assert_called_once_with(
  2246. path='networkFolder', skip=False, type=vim.Datacenter)
  2247. def test_get_mors_with_properties(self):
  2248. salt.utils.vmware.get_network_folder(self.mock_dc_ref)
  2249. self.mock_get_mors.assert_called_once_with(
  2250. self.mock_si, vim.Folder, container_ref=self.mock_dc_ref,
  2251. property_list=['name'], traversal_spec=self.mock_traversal_spec)
  2252. def test_get_no_network_folder(self):
  2253. with patch('salt.utils.vmware.get_mors_with_properties',
  2254. MagicMock(return_value=[])):
  2255. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  2256. salt.utils.vmware.get_network_folder(self.mock_dc_ref)
  2257. self.assertEqual(excinfo.exception.strerror,
  2258. 'Network folder in datacenter \'fake_dc\' wasn\'t '
  2259. 'retrieved')
  2260. def test_get_network_folder(self):
  2261. ret = salt.utils.vmware.get_network_folder(self.mock_dc_ref)
  2262. self.assertEqual(ret, self.mock_entries[0]['object'])
  2263. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2264. class CreateDvsTestCase(TestCase):
  2265. def setUp(self):
  2266. self.mock_dc_ref = MagicMock()
  2267. self.mock_dvs_create_spec = MagicMock()
  2268. self.mock_task = MagicMock(spec=FakeTaskClass)
  2269. self.mock_netw_folder = \
  2270. MagicMock(CreateDVS_Task=MagicMock(
  2271. return_value=self.mock_task))
  2272. self.mock_wait_for_task = MagicMock()
  2273. patches = (
  2274. ('salt.utils.vmware.get_managed_object_name',
  2275. MagicMock(return_value='fake_dc')),
  2276. ('salt.utils.vmware.get_network_folder',
  2277. MagicMock(return_value=self.mock_netw_folder)),
  2278. ('salt.utils.vmware.wait_for_task', self.mock_wait_for_task))
  2279. for mod, mock in patches:
  2280. patcher = patch(mod, mock)
  2281. patcher.start()
  2282. self.addCleanup(patcher.stop)
  2283. def tearDown(self):
  2284. for attr in ('mock_dc_ref', 'mock_dvs_create_spec',
  2285. 'mock_task', 'mock_netw_folder', 'mock_wait_for_task'):
  2286. delattr(self, attr)
  2287. def test_get_managed_object_name_call(self):
  2288. mock_get_managed_object_name = MagicMock()
  2289. with patch('salt.utils.vmware.get_managed_object_name',
  2290. mock_get_managed_object_name):
  2291. salt.utils.vmware.create_dvs(self.mock_dc_ref, 'fake_dvs')
  2292. mock_get_managed_object_name.assert_called_once_with(self.mock_dc_ref)
  2293. def test_no_dvs_create_spec(self):
  2294. mock_spec = MagicMock(configSpec=None)
  2295. mock_config_spec = MagicMock()
  2296. mock_dvs_create_spec = MagicMock(return_value=mock_spec)
  2297. mock_vmware_dvs_config_spec = \
  2298. MagicMock(return_value=mock_config_spec)
  2299. with patch('salt.utils.vmware.vim.DVSCreateSpec',
  2300. mock_dvs_create_spec):
  2301. with patch('salt.utils.vmware.vim.VMwareDVSConfigSpec',
  2302. mock_vmware_dvs_config_spec):
  2303. salt.utils.vmware.create_dvs(self.mock_dc_ref, 'fake_dvs')
  2304. mock_dvs_create_spec.assert_called_once_with()
  2305. mock_vmware_dvs_config_spec.assert_called_once_with()
  2306. self.assertEqual(mock_spec.configSpec, mock_config_spec)
  2307. self.assertEqual(mock_config_spec.name, 'fake_dvs')
  2308. self.mock_netw_folder.CreateDVS_Task.assert_called_once_with(mock_spec)
  2309. def test_get_network_folder(self):
  2310. mock_get_network_folder = MagicMock()
  2311. with patch('salt.utils.vmware.get_network_folder',
  2312. mock_get_network_folder):
  2313. salt.utils.vmware.create_dvs(self.mock_dc_ref, 'fake_dvs')
  2314. mock_get_network_folder.assert_called_once_with(self.mock_dc_ref)
  2315. def test_create_dvs_task_passed_in_spec(self):
  2316. salt.utils.vmware.create_dvs(self.mock_dc_ref, 'fake_dvs',
  2317. dvs_create_spec=self.mock_dvs_create_spec)
  2318. self.mock_netw_folder.CreateDVS_Task.assert_called_once_with(
  2319. self.mock_dvs_create_spec)
  2320. def test_create_dvs_task_raises_no_permission(self):
  2321. exc = vim.fault.NoPermission()
  2322. exc.privilegeId = 'Fake privilege'
  2323. self.mock_netw_folder.CreateDVS_Task = MagicMock(side_effect=exc)
  2324. with self.assertRaises(VMwareApiError) as excinfo:
  2325. salt.utils.vmware.create_dvs(self.mock_dc_ref, 'fake_dvs',
  2326. dvs_create_spec=self.mock_dvs_create_spec)
  2327. self.assertEqual(excinfo.exception.strerror,
  2328. 'Not enough permissions. Required privilege: '
  2329. 'Fake privilege')
  2330. def test_create_dvs_task_raises_vim_fault(self):
  2331. exc = vim.fault.VimFault()
  2332. exc.msg = 'VimFault msg'
  2333. self.mock_netw_folder.CreateDVS_Task = MagicMock(side_effect=exc)
  2334. with self.assertRaises(VMwareApiError) as excinfo:
  2335. salt.utils.vmware.create_dvs(self.mock_dc_ref, 'fake_dvs',
  2336. dvs_create_spec=self.mock_dvs_create_spec)
  2337. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  2338. def test_create_dvs_task_raises_runtime_fault(self):
  2339. exc = vmodl.RuntimeFault()
  2340. exc.msg = 'RuntimeFault msg'
  2341. self.mock_netw_folder.CreateDVS_Task = MagicMock(side_effect=exc)
  2342. with self.assertRaises(VMwareRuntimeError) as excinfo:
  2343. salt.utils.vmware.create_dvs(self.mock_dc_ref, 'fake_dvs',
  2344. dvs_create_spec=self.mock_dvs_create_spec)
  2345. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  2346. def test_wait_for_tasks(self):
  2347. salt.utils.vmware.create_dvs(self.mock_dc_ref, 'fake_dvs',
  2348. dvs_create_spec=self.mock_dvs_create_spec)
  2349. self.mock_wait_for_task.assert_called_once_with(
  2350. self.mock_task, 'fake_dvs',
  2351. '<class \'{}unit.utils.test_vmware.FakeTaskClass\'>'.format(
  2352. 'tests.' if RUNTIME_VARS.PYTEST_SESSION else '')
  2353. )
  2354. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2355. class UpdateDvsTestCase(TestCase):
  2356. def setUp(self):
  2357. self.mock_task = MagicMock(spec=FakeTaskClass)
  2358. self.mock_dvs_ref = MagicMock(
  2359. ReconfigureDvs_Task=MagicMock(return_value=self.mock_task))
  2360. self.mock_dvs_spec = MagicMock()
  2361. self.mock_wait_for_task = MagicMock()
  2362. patches = (
  2363. ('salt.utils.vmware.get_managed_object_name',
  2364. MagicMock(return_value='fake_dvs')),
  2365. ('salt.utils.vmware.wait_for_task', self.mock_wait_for_task))
  2366. for mod, mock in patches:
  2367. patcher = patch(mod, mock)
  2368. patcher.start()
  2369. self.addCleanup(patcher.stop)
  2370. def tearDown(self):
  2371. for attr in ('mock_dvs_ref', 'mock_task', 'mock_dvs_spec',
  2372. 'mock_wait_for_task'):
  2373. delattr(self, attr)
  2374. def test_get_managed_object_name_call(self):
  2375. mock_get_managed_object_name = MagicMock()
  2376. with patch('salt.utils.vmware.get_managed_object_name',
  2377. mock_get_managed_object_name):
  2378. salt.utils.vmware.update_dvs(self.mock_dvs_ref, self.mock_dvs_spec)
  2379. mock_get_managed_object_name.assert_called_once_with(self.mock_dvs_ref)
  2380. def test_reconfigure_dvs_task(self):
  2381. salt.utils.vmware.update_dvs(self.mock_dvs_ref, self.mock_dvs_spec)
  2382. self.mock_dvs_ref.ReconfigureDvs_Task.assert_called_once_with(
  2383. self.mock_dvs_spec)
  2384. def test_reconfigure_dvs_task_raises_no_permission(self):
  2385. exc = vim.fault.NoPermission()
  2386. exc.privilegeId = 'Fake privilege'
  2387. self.mock_dvs_ref.ReconfigureDvs_Task = MagicMock(side_effect=exc)
  2388. with self.assertRaises(VMwareApiError) as excinfo:
  2389. salt.utils.vmware.update_dvs(self.mock_dvs_ref, self.mock_dvs_spec)
  2390. self.assertEqual(excinfo.exception.strerror,
  2391. 'Not enough permissions. Required privilege: '
  2392. 'Fake privilege')
  2393. def test_reconfigure_dvs_task_raises_vim_fault(self):
  2394. exc = vim.fault.VimFault()
  2395. exc.msg = 'VimFault msg'
  2396. self.mock_dvs_ref.ReconfigureDvs_Task = MagicMock(side_effect=exc)
  2397. with self.assertRaises(VMwareApiError) as excinfo:
  2398. salt.utils.vmware.update_dvs(self.mock_dvs_ref, self.mock_dvs_spec)
  2399. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  2400. def test_reconfigure_dvs_task_raises_runtime_fault(self):
  2401. exc = vmodl.RuntimeFault()
  2402. exc.msg = 'RuntimeFault msg'
  2403. self.mock_dvs_ref.ReconfigureDvs_Task = MagicMock(side_effect=exc)
  2404. with self.assertRaises(VMwareRuntimeError) as excinfo:
  2405. salt.utils.vmware.update_dvs(self.mock_dvs_ref, self.mock_dvs_spec)
  2406. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  2407. def test_wait_for_tasks(self):
  2408. salt.utils.vmware.update_dvs(self.mock_dvs_ref, self.mock_dvs_spec)
  2409. self.mock_wait_for_task.assert_called_once_with(
  2410. self.mock_task, 'fake_dvs',
  2411. '<class \'{}unit.utils.test_vmware.FakeTaskClass\'>'.format(
  2412. 'tests.' if RUNTIME_VARS.PYTEST_SESSION else '')
  2413. )
  2414. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2415. class SetDvsNetworkResourceManagementEnabledTestCase(TestCase):
  2416. def setUp(self):
  2417. self.mock_enabled = MagicMock()
  2418. self.mock_dvs_ref = MagicMock(
  2419. EnableNetworkResourceManagement=MagicMock())
  2420. patches = (
  2421. ('salt.utils.vmware.get_managed_object_name',
  2422. MagicMock(return_value='fake_dvs')),)
  2423. for mod, mock in patches:
  2424. patcher = patch(mod, mock)
  2425. patcher.start()
  2426. self.addCleanup(patcher.stop)
  2427. def tearDown(self):
  2428. for attr in ('mock_dvs_ref', 'mock_enabled'):
  2429. delattr(self, attr)
  2430. def test_get_managed_object_name_call(self):
  2431. mock_get_managed_object_name = MagicMock()
  2432. with patch('salt.utils.vmware.get_managed_object_name',
  2433. mock_get_managed_object_name):
  2434. salt.utils.vmware.set_dvs_network_resource_management_enabled(
  2435. self.mock_dvs_ref, self.mock_enabled)
  2436. mock_get_managed_object_name.assert_called_once_with(self.mock_dvs_ref)
  2437. def test_enable_network_resource_management(self):
  2438. salt.utils.vmware.set_dvs_network_resource_management_enabled(
  2439. self.mock_dvs_ref, self.mock_enabled)
  2440. self.mock_dvs_ref.EnableNetworkResourceManagement.assert_called_once_with(
  2441. enable=self.mock_enabled)
  2442. def test_enable_network_resource_management_raises_no_permission(self):
  2443. exc = vim.fault.NoPermission()
  2444. exc.privilegeId = 'Fake privilege'
  2445. self.mock_dvs_ref.EnableNetworkResourceManagement = \
  2446. MagicMock(side_effect=exc)
  2447. with self.assertRaises(VMwareApiError) as excinfo:
  2448. salt.utils.vmware.set_dvs_network_resource_management_enabled(
  2449. self.mock_dvs_ref, self.mock_enabled)
  2450. self.assertEqual(excinfo.exception.strerror,
  2451. 'Not enough permissions. Required privilege: '
  2452. 'Fake privilege')
  2453. def test_enable_network_resource_management_raises_vim_fault(self):
  2454. exc = vim.fault.VimFault()
  2455. exc.msg = 'VimFault msg'
  2456. self.mock_dvs_ref.EnableNetworkResourceManagement = \
  2457. MagicMock(side_effect=exc)
  2458. with self.assertRaises(VMwareApiError) as excinfo:
  2459. salt.utils.vmware.set_dvs_network_resource_management_enabled(
  2460. self.mock_dvs_ref, self.mock_enabled)
  2461. def test_enable_network_resource_management_raises_runtime_fault(self):
  2462. exc = vmodl.RuntimeFault()
  2463. exc.msg = 'RuntimeFault msg'
  2464. self.mock_dvs_ref.EnableNetworkResourceManagement = \
  2465. MagicMock(side_effect=exc)
  2466. with self.assertRaises(VMwareRuntimeError) as excinfo:
  2467. salt.utils.vmware.set_dvs_network_resource_management_enabled(
  2468. self.mock_dvs_ref, self.mock_enabled)
  2469. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  2470. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2471. class GetDvportgroupsTestCase(TestCase):
  2472. def setUp(self):
  2473. self.mock_si = MagicMock()
  2474. self.mock_dc_ref = MagicMock(spec=vim.Datacenter)
  2475. self.mock_dvs_ref = MagicMock(spec=vim.DistributedVirtualSwitch)
  2476. self.mock_traversal_spec = MagicMock()
  2477. self.mock_items = [{'object': MagicMock(),
  2478. 'name': 'fake_pg1'},
  2479. {'object': MagicMock(),
  2480. 'name': 'fake_pg2'},
  2481. {'object': MagicMock(),
  2482. 'name': 'fake_pg3'}]
  2483. self.mock_get_mors = MagicMock(return_value=self.mock_items)
  2484. patches = (
  2485. ('salt.utils.vmware.get_managed_object_name',
  2486. MagicMock()),
  2487. ('salt.utils.vmware.get_mors_with_properties',
  2488. self.mock_get_mors),
  2489. ('salt.utils.vmware.get_service_instance_from_managed_object',
  2490. MagicMock(return_value=self.mock_si)),
  2491. ('salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  2492. MagicMock(return_value=self.mock_traversal_spec)))
  2493. for mod, mock in patches:
  2494. patcher = patch(mod, mock)
  2495. patcher.start()
  2496. self.addCleanup(patcher.stop)
  2497. def tearDown(self):
  2498. for attr in ('mock_si', 'mock_dc_ref', 'mock_dvs_ref',
  2499. 'mock_traversal_spec', 'mock_items', 'mock_get_mors'):
  2500. delattr(self, attr)
  2501. def test_unsupported_parrent(self):
  2502. with self.assertRaises(ArgumentValueError) as excinfo:
  2503. salt.utils.vmware.get_dvportgroups(MagicMock())
  2504. self.assertEqual(excinfo.exception.strerror,
  2505. 'Parent has to be either a datacenter, or a '
  2506. 'distributed virtual switch')
  2507. def test_get_managed_object_name_call(self):
  2508. mock_get_managed_object_name = MagicMock()
  2509. with patch('salt.utils.vmware.get_managed_object_name',
  2510. mock_get_managed_object_name):
  2511. salt.utils.vmware.get_dvportgroups(self.mock_dc_ref)
  2512. mock_get_managed_object_name.assert_called_once_with(self.mock_dc_ref)
  2513. def test_traversal_spec_datacenter_parent(self):
  2514. mock_traversal_spec = MagicMock(return_value='traversal_spec')
  2515. with patch(
  2516. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  2517. mock_traversal_spec):
  2518. salt.utils.vmware.get_dvportgroups(self.mock_dc_ref)
  2519. mock_traversal_spec.assert_has_calls(
  2520. [call(path='childEntity', skip=False, type=vim.Folder),
  2521. call(path='networkFolder', skip=True, type=vim.Datacenter,
  2522. selectSet=['traversal_spec'])])
  2523. def test_traversal_spec_dvs_parent(self):
  2524. mock_traversal_spec = MagicMock(return_value='traversal_spec')
  2525. with patch(
  2526. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  2527. mock_traversal_spec):
  2528. salt.utils.vmware.get_dvportgroups(self.mock_dvs_ref)
  2529. mock_traversal_spec.assert_called_once_with(
  2530. path='portgroup', skip=False, type=vim.DistributedVirtualSwitch)
  2531. def test_get_mors_with_properties(self):
  2532. salt.utils.vmware.get_dvportgroups(self.mock_dvs_ref)
  2533. self.mock_get_mors.assert_called_once_with(
  2534. self.mock_si, vim.DistributedVirtualPortgroup,
  2535. container_ref=self.mock_dvs_ref, property_list=['name'],
  2536. traversal_spec=self.mock_traversal_spec)
  2537. def test_get_no_pgs(self):
  2538. ret = salt.utils.vmware.get_dvportgroups(self.mock_dvs_ref)
  2539. self.assertEqual(ret, [])
  2540. def test_get_all_pgs(self):
  2541. ret = salt.utils.vmware.get_dvportgroups(self.mock_dvs_ref,
  2542. get_all_portgroups=True)
  2543. self.assertEqual(ret, [i['object'] for i in self.mock_items])
  2544. def test_filtered_pgs(self):
  2545. ret = salt.utils.vmware.get_dvss(self.mock_dc_ref,
  2546. dvs_names=['fake_pg1', 'fake_pg3', 'no_pg'])
  2547. self.assertEqual(ret, [self.mock_items[0]['object'],
  2548. self.mock_items[2]['object']])
  2549. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2550. class GetUplinkDvportgroupTestCase(TestCase):
  2551. def setUp(self):
  2552. self.mock_si = MagicMock()
  2553. self.mock_dvs_ref = MagicMock(spec=vim.DistributedVirtualSwitch)
  2554. self.mock_traversal_spec = MagicMock()
  2555. self.mock_items = [{'object': MagicMock(),
  2556. 'tag': [MagicMock(key='fake_tag')]},
  2557. {'object': MagicMock(),
  2558. 'tag': [MagicMock(key='SYSTEM/DVS.UPLINKPG')]}]
  2559. self.mock_get_mors = MagicMock(return_value=self.mock_items)
  2560. patches = (
  2561. ('salt.utils.vmware.get_managed_object_name',
  2562. MagicMock(return_value='fake_dvs')),
  2563. ('salt.utils.vmware.get_mors_with_properties',
  2564. self.mock_get_mors),
  2565. ('salt.utils.vmware.get_service_instance_from_managed_object',
  2566. MagicMock(return_value=self.mock_si)),
  2567. ('salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  2568. MagicMock(return_value=self.mock_traversal_spec)))
  2569. for mod, mock in patches:
  2570. patcher = patch(mod, mock)
  2571. patcher.start()
  2572. self.addCleanup(patcher.stop)
  2573. def tearDown(self):
  2574. for attr in ('mock_si', 'mock_dvs_ref', 'mock_traversal_spec',
  2575. 'mock_items', 'mock_get_mors'):
  2576. delattr(self, attr)
  2577. def test_get_managed_object_name_call(self):
  2578. mock_get_managed_object_name = MagicMock()
  2579. with patch('salt.utils.vmware.get_managed_object_name',
  2580. mock_get_managed_object_name):
  2581. salt.utils.vmware.get_uplink_dvportgroup(self.mock_dvs_ref)
  2582. mock_get_managed_object_name.assert_called_once_with(self.mock_dvs_ref)
  2583. def test_traversal_spec(self):
  2584. mock_traversal_spec = MagicMock(return_value='traversal_spec')
  2585. with patch(
  2586. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  2587. mock_traversal_spec):
  2588. salt.utils.vmware.get_uplink_dvportgroup(self.mock_dvs_ref)
  2589. mock_traversal_spec.assert_called_once_with(
  2590. path='portgroup', skip=False, type=vim.DistributedVirtualSwitch)
  2591. def test_get_mors_with_properties(self):
  2592. salt.utils.vmware.get_uplink_dvportgroup(self.mock_dvs_ref)
  2593. self.mock_get_mors.assert_called_once_with(
  2594. self.mock_si, vim.DistributedVirtualPortgroup,
  2595. container_ref=self.mock_dvs_ref, property_list=['tag'],
  2596. traversal_spec=self.mock_traversal_spec)
  2597. def test_get_no_uplink_pg(self):
  2598. with patch('salt.utils.vmware.get_mors_with_properties',
  2599. MagicMock(return_value=[])):
  2600. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  2601. salt.utils.vmware.get_uplink_dvportgroup(self.mock_dvs_ref)
  2602. self.assertEqual(excinfo.exception.strerror,
  2603. 'Uplink portgroup of DVS \'fake_dvs\' wasn\'t found')
  2604. def test_get_uplink_pg(self):
  2605. ret = salt.utils.vmware.get_uplink_dvportgroup(self.mock_dvs_ref)
  2606. self.assertEqual(ret, self.mock_items[1]['object'])
  2607. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2608. class CreateDvportgroupTestCase(TestCase):
  2609. def setUp(self):
  2610. self.mock_pg_spec = MagicMock()
  2611. self.mock_task = MagicMock(spec=FakeTaskClass)
  2612. self.mock_dvs_ref = \
  2613. MagicMock(CreateDVPortgroup_Task=MagicMock(
  2614. return_value=self.mock_task))
  2615. self.mock_wait_for_task = MagicMock()
  2616. patches = (
  2617. ('salt.utils.vmware.get_managed_object_name',
  2618. MagicMock(return_value='fake_dvs')),
  2619. ('salt.utils.vmware.wait_for_task', self.mock_wait_for_task))
  2620. for mod, mock in patches:
  2621. patcher = patch(mod, mock)
  2622. patcher.start()
  2623. self.addCleanup(patcher.stop)
  2624. def tearDown(self):
  2625. for attr in ('mock_pg_spec', 'mock_dvs_ref', 'mock_task',
  2626. 'mock_wait_for_task'):
  2627. delattr(self, attr)
  2628. def test_get_managed_object_name_call(self):
  2629. mock_get_managed_object_name = MagicMock()
  2630. with patch('salt.utils.vmware.get_managed_object_name',
  2631. mock_get_managed_object_name):
  2632. salt.utils.vmware.create_dvportgroup(self.mock_dvs_ref, self.mock_pg_spec)
  2633. mock_get_managed_object_name.assert_called_once_with(self.mock_dvs_ref)
  2634. def test_create_dvporgroup_task(self):
  2635. salt.utils.vmware.create_dvportgroup(self.mock_dvs_ref, self.mock_pg_spec)
  2636. self.mock_dvs_ref.CreateDVPortgroup_Task.assert_called_once_with(
  2637. self.mock_pg_spec)
  2638. def test_create_dvporgroup_task_raises_no_permission(self):
  2639. exc = vim.fault.NoPermission()
  2640. exc.privilegeId = 'Fake privilege'
  2641. self.mock_dvs_ref.CreateDVPortgroup_Task = MagicMock(side_effect=exc)
  2642. with self.assertRaises(VMwareApiError) as excinfo:
  2643. salt.utils.vmware.create_dvportgroup(self.mock_dvs_ref, self.mock_pg_spec)
  2644. self.assertEqual(excinfo.exception.strerror,
  2645. 'Not enough permissions. Required privilege: '
  2646. 'Fake privilege')
  2647. def test_create_dvporgroup_task_raises_vim_fault(self):
  2648. exc = vim.fault.VimFault()
  2649. exc.msg = 'VimFault msg'
  2650. self.mock_dvs_ref.CreateDVPortgroup_Task = MagicMock(side_effect=exc)
  2651. with self.assertRaises(VMwareApiError) as excinfo:
  2652. salt.utils.vmware.create_dvportgroup(self.mock_dvs_ref, self.mock_pg_spec)
  2653. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  2654. def test_create_dvporgroup_task_raises_runtime_fault(self):
  2655. exc = vmodl.RuntimeFault()
  2656. exc.msg = 'RuntimeFault msg'
  2657. self.mock_dvs_ref.CreateDVPortgroup_Task = MagicMock(side_effect=exc)
  2658. with self.assertRaises(VMwareRuntimeError) as excinfo:
  2659. salt.utils.vmware.create_dvportgroup(self.mock_dvs_ref, self.mock_pg_spec)
  2660. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  2661. def test_wait_for_tasks(self):
  2662. salt.utils.vmware.create_dvportgroup(self.mock_dvs_ref, self.mock_pg_spec)
  2663. self.mock_wait_for_task.assert_called_once_with(
  2664. self.mock_task, 'fake_dvs',
  2665. '<class \'{}unit.utils.test_vmware.FakeTaskClass\'>'.format(
  2666. 'tests.' if RUNTIME_VARS.PYTEST_SESSION else '')
  2667. )
  2668. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2669. class UpdateDvportgroupTestCase(TestCase):
  2670. def setUp(self):
  2671. self.mock_pg_spec = MagicMock()
  2672. self.mock_task = MagicMock(spec=FakeTaskClass)
  2673. self.mock_pg_ref = \
  2674. MagicMock(ReconfigureDVPortgroup_Task=MagicMock(
  2675. return_value=self.mock_task))
  2676. self.mock_wait_for_task = MagicMock()
  2677. patches = (
  2678. ('salt.utils.vmware.get_managed_object_name',
  2679. MagicMock(return_value='fake_pg')),
  2680. ('salt.utils.vmware.wait_for_task', self.mock_wait_for_task))
  2681. for mod, mock in patches:
  2682. patcher = patch(mod, mock)
  2683. patcher.start()
  2684. self.addCleanup(patcher.stop)
  2685. def tearDown(self):
  2686. for attr in ('mock_pg_spec', 'mock_pg_ref', 'mock_task',
  2687. 'mock_wait_for_task'):
  2688. delattr(self, attr)
  2689. def test_get_managed_object_name_call(self):
  2690. mock_get_managed_object_name = MagicMock()
  2691. with patch('salt.utils.vmware.get_managed_object_name',
  2692. mock_get_managed_object_name):
  2693. salt.utils.vmware.update_dvportgroup(self.mock_pg_ref, self.mock_pg_spec)
  2694. mock_get_managed_object_name.assert_called_once_with(self.mock_pg_ref)
  2695. def test_reconfigure_dvporgroup_task(self):
  2696. salt.utils.vmware.update_dvportgroup(self.mock_pg_ref, self.mock_pg_spec)
  2697. self.mock_pg_ref.ReconfigureDVPortgroup_Task.assert_called_once_with(
  2698. self.mock_pg_spec)
  2699. def test_reconfigure_dvporgroup_task_raises_no_permission(self):
  2700. exc = vim.fault.NoPermission()
  2701. exc.privilegeId = 'Fake privilege'
  2702. self.mock_pg_ref.ReconfigureDVPortgroup_Task = \
  2703. MagicMock(side_effect=exc)
  2704. with self.assertRaises(VMwareApiError) as excinfo:
  2705. salt.utils.vmware.update_dvportgroup(self.mock_pg_ref, self.mock_pg_spec)
  2706. self.assertEqual(excinfo.exception.strerror,
  2707. 'Not enough permissions. Required privilege: '
  2708. 'Fake privilege')
  2709. def test_reconfigure_dvporgroup_task_raises_vim_fault(self):
  2710. exc = vim.fault.VimFault()
  2711. exc.msg = 'VimFault msg'
  2712. self.mock_pg_ref.ReconfigureDVPortgroup_Task = \
  2713. MagicMock(side_effect=exc)
  2714. with self.assertRaises(VMwareApiError) as excinfo:
  2715. salt.utils.vmware.update_dvportgroup(self.mock_pg_ref, self.mock_pg_spec)
  2716. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  2717. def test_reconfigure_dvporgroup_task_raises_runtime_fault(self):
  2718. exc = vmodl.RuntimeFault()
  2719. exc.msg = 'RuntimeFault msg'
  2720. self.mock_pg_ref.ReconfigureDVPortgroup_Task = \
  2721. MagicMock(side_effect=exc)
  2722. with self.assertRaises(VMwareRuntimeError) as excinfo:
  2723. salt.utils.vmware.update_dvportgroup(self.mock_pg_ref, self.mock_pg_spec)
  2724. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  2725. def test_wait_for_tasks(self):
  2726. salt.utils.vmware.update_dvportgroup(self.mock_pg_ref, self.mock_pg_spec)
  2727. self.mock_wait_for_task.assert_called_once_with(
  2728. self.mock_task, 'fake_pg',
  2729. '<class \'{}unit.utils.test_vmware.FakeTaskClass\'>'.format(
  2730. 'tests.' if RUNTIME_VARS.PYTEST_SESSION else '')
  2731. )
  2732. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2733. class RemoveDvportgroupTestCase(TestCase):
  2734. def setUp(self):
  2735. self.mock_task = MagicMock(spec=FakeTaskClass)
  2736. self.mock_pg_ref = \
  2737. MagicMock(Destroy_Task=MagicMock(
  2738. return_value=self.mock_task))
  2739. self.mock_wait_for_task = MagicMock()
  2740. patches = (
  2741. ('salt.utils.vmware.get_managed_object_name',
  2742. MagicMock(return_value='fake_pg')),
  2743. ('salt.utils.vmware.wait_for_task', self.mock_wait_for_task))
  2744. for mod, mock in patches:
  2745. patcher = patch(mod, mock)
  2746. patcher.start()
  2747. self.addCleanup(patcher.stop)
  2748. def tearDown(self):
  2749. for attr in ('mock_pg_ref', 'mock_task', 'mock_wait_for_task'):
  2750. delattr(self, attr)
  2751. def test_get_managed_object_name_call(self):
  2752. mock_get_managed_object_name = MagicMock()
  2753. with patch('salt.utils.vmware.get_managed_object_name',
  2754. mock_get_managed_object_name):
  2755. salt.utils.vmware.remove_dvportgroup(self.mock_pg_ref)
  2756. mock_get_managed_object_name.assert_called_once_with(self.mock_pg_ref)
  2757. def test_destroy_task(self):
  2758. salt.utils.vmware.remove_dvportgroup(self.mock_pg_ref)
  2759. self.mock_pg_ref.Destroy_Task.assert_called_once_with()
  2760. def test_destroy_task_raises_no_permission(self):
  2761. exc = vim.fault.NoPermission()
  2762. exc.privilegeId = 'Fake privilege'
  2763. self.mock_pg_ref.Destroy_Task = MagicMock(side_effect=exc)
  2764. with self.assertRaises(VMwareApiError) as excinfo:
  2765. salt.utils.vmware.remove_dvportgroup(self.mock_pg_ref)
  2766. self.assertEqual(excinfo.exception.strerror,
  2767. 'Not enough permissions. Required privilege: '
  2768. 'Fake privilege')
  2769. def test_destroy_treconfigure_dvporgroup_task_raises_vim_fault(self):
  2770. exc = vim.fault.VimFault()
  2771. exc.msg = 'VimFault msg'
  2772. self.mock_pg_ref.Destroy_Task = MagicMock(side_effect=exc)
  2773. with self.assertRaises(VMwareApiError) as excinfo:
  2774. salt.utils.vmware.remove_dvportgroup(self.mock_pg_ref)
  2775. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  2776. def test_destroy_treconfigure_dvporgroup_task_raises_runtime_fault(self):
  2777. exc = vmodl.RuntimeFault()
  2778. exc.msg = 'RuntimeFault msg'
  2779. self.mock_pg_ref.Destroy_Task = MagicMock(side_effect=exc)
  2780. with self.assertRaises(VMwareRuntimeError) as excinfo:
  2781. salt.utils.vmware.remove_dvportgroup(self.mock_pg_ref)
  2782. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  2783. def test_wait_for_tasks(self):
  2784. salt.utils.vmware.remove_dvportgroup(self.mock_pg_ref)
  2785. self.mock_wait_for_task.assert_called_once_with(
  2786. self.mock_task, 'fake_pg',
  2787. '<class \'{}unit.utils.test_vmware.FakeTaskClass\'>'.format(
  2788. 'tests.' if RUNTIME_VARS.PYTEST_SESSION else '')
  2789. )
  2790. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2791. class GetHostsTestCase(TestCase):
  2792. '''
  2793. Tests for salt.utils.vmware.get_hosts
  2794. '''
  2795. def setUp(self):
  2796. patches = (
  2797. ('salt.utils.vmware.get_mors_with_properties', MagicMock(return_value=[])),
  2798. ('salt.utils.vmware.get_datacenter', MagicMock(return_value=None)),
  2799. ('salt.utils.vmware.get_cluster', MagicMock(return_value=None))
  2800. )
  2801. for mod, mock in patches:
  2802. patcher = patch(mod, mock)
  2803. patcher.start()
  2804. self.addCleanup(patcher.stop)
  2805. self.mock_root_folder = MagicMock()
  2806. self.mock_si = MagicMock()
  2807. self.mock_host1, self.mock_host2, self.mock_host3 = MagicMock(), \
  2808. MagicMock(), MagicMock()
  2809. self.mock_prop_host1 = {'name': 'fake_hostname1',
  2810. 'object': self.mock_host1}
  2811. self.mock_prop_host2 = {'name': 'fake_hostname2',
  2812. 'object': self.mock_host2}
  2813. self.mock_prop_host3 = {'name': 'fake_hostname3',
  2814. 'object': self.mock_host3}
  2815. self.mock_prop_hosts = [self.mock_prop_host1, self.mock_prop_host2,
  2816. self.mock_prop_host3]
  2817. def test_cluster_no_datacenter(self):
  2818. with self.assertRaises(ArgumentValueError) as excinfo:
  2819. salt.utils.vmware.get_hosts(self.mock_si,
  2820. cluster_name='fake_cluster')
  2821. self.assertEqual(excinfo.exception.strerror,
  2822. 'Must specify the datacenter when specifying the '
  2823. 'cluster')
  2824. def test_get_si_no_datacenter_no_cluster(self):
  2825. mock_get_mors = MagicMock()
  2826. mock_get_root_folder = MagicMock(return_value=self.mock_root_folder)
  2827. with patch('salt.utils.vmware.get_root_folder', mock_get_root_folder):
  2828. with patch('salt.utils.vmware.get_mors_with_properties',
  2829. mock_get_mors):
  2830. salt.utils.vmware.get_hosts(self.mock_si)
  2831. mock_get_root_folder.assert_called_once_with(self.mock_si)
  2832. mock_get_mors.assert_called_once_with(
  2833. self.mock_si, vim.HostSystem, container_ref=self.mock_root_folder,
  2834. property_list=['name'])
  2835. def test_get_si_datacenter_name_no_cluster_name(self):
  2836. mock_dc = MagicMock()
  2837. mock_get_dc = MagicMock(return_value=mock_dc)
  2838. mock_get_mors = MagicMock()
  2839. with patch('salt.utils.vmware.get_datacenter', mock_get_dc):
  2840. with patch('salt.utils.vmware.get_mors_with_properties',
  2841. mock_get_mors):
  2842. salt.utils.vmware.get_hosts(self.mock_si,
  2843. datacenter_name='fake_datacenter')
  2844. mock_get_dc.assert_called_once_with(self.mock_si, 'fake_datacenter')
  2845. mock_get_mors.assert_called_once_with(self.mock_si,
  2846. vim.HostSystem,
  2847. container_ref=mock_dc,
  2848. property_list=['name'])
  2849. def test_get_si_datacenter_name_and_cluster_name(self):
  2850. mock_dc = MagicMock()
  2851. mock_get_dc = MagicMock(return_value=mock_dc)
  2852. mock_get_cl = MagicMock()
  2853. mock_get_mors = MagicMock()
  2854. with patch('salt.utils.vmware.get_datacenter', mock_get_dc):
  2855. with patch('salt.utils.vmware.get_cluster', mock_get_cl):
  2856. with patch('salt.utils.vmware.get_mors_with_properties',
  2857. mock_get_mors):
  2858. salt.utils.vmware.get_hosts(
  2859. self.mock_si, datacenter_name='fake_datacenter',
  2860. cluster_name='fake_cluster')
  2861. mock_get_dc.assert_called_once_with(self.mock_si, 'fake_datacenter')
  2862. mock_get_mors.assert_called_once_with(self.mock_si,
  2863. vim.HostSystem,
  2864. container_ref=mock_dc,
  2865. property_list=['name', 'parent'])
  2866. def test_host_get_all_hosts(self):
  2867. with patch('salt.utils.vmware.get_root_folder',
  2868. MagicMock(return_value=self.mock_root_folder)):
  2869. with patch('salt.utils.vmware.get_mors_with_properties',
  2870. MagicMock(return_value=self.mock_prop_hosts)):
  2871. res = salt.utils.vmware.get_hosts(self.mock_si, get_all_hosts=True)
  2872. self.assertEqual(res, [self.mock_host1, self.mock_host2,
  2873. self.mock_host3])
  2874. def test_filter_hostname(self):
  2875. with patch('salt.utils.vmware.get_mors_with_properties',
  2876. MagicMock(return_value=self.mock_prop_hosts)):
  2877. res = salt.utils.vmware.get_hosts(self.mock_si,
  2878. host_names=['fake_hostname1',
  2879. 'fake_hostname2'])
  2880. self.assertEqual(res, [self.mock_host1, self.mock_host2])
  2881. def test_get_all_host_flag_not_set_and_no_host_names(self):
  2882. with patch('salt.utils.vmware.get_mors_with_properties',
  2883. MagicMock(return_value=self.mock_prop_hosts)):
  2884. res = salt.utils.vmware.get_hosts(self.mock_si)
  2885. self.assertEqual(res, [])
  2886. def test_filter_cluster(self):
  2887. self.mock_prop_host1['parent'] = vim.ClusterComputeResource('cluster')
  2888. self.mock_prop_host2['parent'] = vim.ClusterComputeResource('cluster')
  2889. self.mock_prop_host3['parent'] = vim.Datacenter('dc')
  2890. mock_get_cl_name = MagicMock(
  2891. side_effect=['fake_bad_cluster', 'fake_good_cluster'])
  2892. with patch('salt.utils.vmware.get_mors_with_properties',
  2893. MagicMock(return_value=self.mock_prop_hosts)):
  2894. with patch('salt.utils.vmware.get_managed_object_name',
  2895. mock_get_cl_name):
  2896. res = salt.utils.vmware.get_hosts(
  2897. self.mock_si, datacenter_name='fake_datacenter',
  2898. cluster_name='fake_good_cluster', get_all_hosts=True)
  2899. self.assertEqual(mock_get_cl_name.call_count, 2)
  2900. self.assertEqual(res, [self.mock_host2])
  2901. def test_no_hosts(self):
  2902. with patch('salt.utils.vmware.get_mors_with_properties',
  2903. MagicMock(return_value=[])):
  2904. res = salt.utils.vmware.get_hosts(self.mock_si, get_all_hosts=True)
  2905. self.assertEqual(res, [])
  2906. def test_one_host_returned(self):
  2907. with patch('salt.utils.vmware.get_mors_with_properties',
  2908. MagicMock(return_value=[self.mock_prop_host1])):
  2909. res = salt.utils.vmware.get_hosts(self.mock_si, get_all_hosts=True)
  2910. self.assertEqual(res, [self.mock_host1])
  2911. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2912. class GetLicenseManagerTestCase(TestCase):
  2913. '''
  2914. Tests for salt.utils.vmware.get_license_manager
  2915. '''
  2916. def setUp(self):
  2917. self.mock_si = MagicMock()
  2918. self.mock_lic_mgr = MagicMock()
  2919. type(self.mock_si.content).licenseManager = PropertyMock(
  2920. return_value=self.mock_lic_mgr)
  2921. def tearDown(self):
  2922. for attr in ('mock_si', 'mock_lic_mgr'):
  2923. delattr(self, attr)
  2924. def test_raise_no_permission(self):
  2925. exc = vim.fault.NoPermission()
  2926. exc.privilegeId = 'Fake privilege'
  2927. type(self.mock_si.content).licenseManager = PropertyMock(
  2928. side_effect=exc)
  2929. with self.assertRaises(VMwareApiError) as excinfo:
  2930. salt.utils.vmware.get_license_manager(self.mock_si)
  2931. self.assertEqual(excinfo.exception.strerror,
  2932. 'Not enough permissions. Required privilege: '
  2933. 'Fake privilege')
  2934. def test_raise_vim_fault(self):
  2935. exc = vim.fault.VimFault()
  2936. exc.msg = 'VimFault msg'
  2937. type(self.mock_si.content).licenseManager = PropertyMock(
  2938. side_effect=exc)
  2939. with self.assertRaises(VMwareApiError) as excinfo:
  2940. salt.utils.vmware.get_license_manager(self.mock_si)
  2941. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  2942. def test_raise_runtime_fault(self):
  2943. exc = vmodl.RuntimeFault()
  2944. exc.msg = 'RuntimeFault msg'
  2945. type(self.mock_si.content).licenseManager = PropertyMock(
  2946. side_effect=exc)
  2947. with self.assertRaises(VMwareRuntimeError) as excinfo:
  2948. salt.utils.vmware.get_license_manager(self.mock_si)
  2949. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  2950. def test_valid_assignment_manager(self):
  2951. ret = salt.utils.vmware.get_license_manager(self.mock_si)
  2952. self.assertEqual(ret, self.mock_lic_mgr)
  2953. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  2954. class GetLicenseAssignmentManagerTestCase(TestCase):
  2955. '''
  2956. Tests for salt.utils.vmware.get_license_assignment_manager
  2957. '''
  2958. def setUp(self):
  2959. self.mock_si = MagicMock()
  2960. self.mock_lic_assign_mgr = MagicMock()
  2961. type(self.mock_si.content.licenseManager).licenseAssignmentManager = \
  2962. PropertyMock(return_value=self.mock_lic_assign_mgr)
  2963. def tearDown(self):
  2964. for attr in ('mock_si', 'mock_lic_assign_mgr'):
  2965. delattr(self, attr)
  2966. def test_raise_no_permission(self):
  2967. exc = vim.fault.NoPermission()
  2968. exc.privilegeId = 'Fake privilege'
  2969. type(self.mock_si.content.licenseManager).licenseAssignmentManager = \
  2970. PropertyMock(side_effect=exc)
  2971. with self.assertRaises(VMwareApiError) as excinfo:
  2972. salt.utils.vmware.get_license_assignment_manager(self.mock_si)
  2973. self.assertEqual(excinfo.exception.strerror,
  2974. 'Not enough permissions. Required privilege: '
  2975. 'Fake privilege')
  2976. def test_raise_vim_fault(self):
  2977. exc = vim.fault.VimFault()
  2978. exc.msg = 'VimFault msg'
  2979. type(self.mock_si.content.licenseManager).licenseAssignmentManager = \
  2980. PropertyMock(side_effect=exc)
  2981. with self.assertRaises(VMwareApiError) as excinfo:
  2982. salt.utils.vmware.get_license_assignment_manager(self.mock_si)
  2983. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  2984. def test_raise_runtime_fault(self):
  2985. exc = vmodl.RuntimeFault()
  2986. exc.msg = 'RuntimeFault msg'
  2987. type(self.mock_si.content.licenseManager).licenseAssignmentManager = \
  2988. PropertyMock(side_effect=exc)
  2989. with self.assertRaises(VMwareRuntimeError) as excinfo:
  2990. salt.utils.vmware.get_license_assignment_manager(self.mock_si)
  2991. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  2992. def test_empty_license_assignment_manager(self):
  2993. type(self.mock_si.content.licenseManager).licenseAssignmentManager = \
  2994. PropertyMock(return_value=None)
  2995. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  2996. salt.utils.vmware.get_license_assignment_manager(self.mock_si)
  2997. self.assertEqual(excinfo.exception.strerror,
  2998. 'License assignment manager was not retrieved')
  2999. def test_valid_assignment_manager(self):
  3000. ret = salt.utils.vmware.get_license_assignment_manager(self.mock_si)
  3001. self.assertEqual(ret, self.mock_lic_assign_mgr)
  3002. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  3003. class GetLicensesTestCase(TestCase):
  3004. '''
  3005. Tests for salt.utils.vmware.get_licenses
  3006. '''
  3007. def setUp(self):
  3008. self.mock_si = MagicMock()
  3009. self.mock_licenses = [MagicMock(), MagicMock()]
  3010. self.mock_lic_mgr = MagicMock()
  3011. type(self.mock_lic_mgr).licenses = \
  3012. PropertyMock(return_value=self.mock_licenses)
  3013. patches = (
  3014. ('salt.utils.vmware.get_license_manager',
  3015. MagicMock(return_value=self.mock_lic_mgr)),)
  3016. for mod, mock in patches:
  3017. patcher = patch(mod, mock)
  3018. patcher.start()
  3019. self.addCleanup(patcher.stop)
  3020. def tearDown(self):
  3021. for attr in ('mock_si', 'mock_lic_mgr', 'mock_licenses'):
  3022. delattr(self, attr)
  3023. def test_no_license_manager_passed_in(self):
  3024. mock_get_license_manager = MagicMock()
  3025. with patch('salt.utils.vmware.get_license_manager',
  3026. mock_get_license_manager):
  3027. salt.utils.vmware.get_licenses(self.mock_si)
  3028. mock_get_license_manager.assert_called_once_with(self.mock_si)
  3029. def test_license_manager_passed_in(self):
  3030. mock_licenses = PropertyMock()
  3031. mock_lic_mgr = MagicMock()
  3032. type(mock_lic_mgr).licenses = mock_licenses
  3033. mock_get_license_manager = MagicMock()
  3034. with patch('salt.utils.vmware.get_license_manager',
  3035. mock_get_license_manager):
  3036. salt.utils.vmware.get_licenses(self.mock_si,
  3037. license_manager=mock_lic_mgr)
  3038. self.assertEqual(mock_get_license_manager.call_count, 0)
  3039. self.assertEqual(mock_licenses.call_count, 1)
  3040. def test_raise_no_permission(self):
  3041. exc = vim.fault.NoPermission()
  3042. exc.privilegeId = 'Fake privilege'
  3043. type(self.mock_lic_mgr).licenses = PropertyMock(side_effect=exc)
  3044. with self.assertRaises(VMwareApiError) as excinfo:
  3045. salt.utils.vmware.get_licenses(self.mock_si)
  3046. self.assertEqual(excinfo.exception.strerror,
  3047. 'Not enough permissions. Required privilege: '
  3048. 'Fake privilege')
  3049. def test_raise_vim_fault(self):
  3050. exc = vim.fault.VimFault()
  3051. exc.msg = 'VimFault msg'
  3052. type(self.mock_lic_mgr).licenses = PropertyMock(side_effect=exc)
  3053. with self.assertRaises(VMwareApiError) as excinfo:
  3054. salt.utils.vmware.get_licenses(self.mock_si)
  3055. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  3056. def test_raise_runtime_fault(self):
  3057. exc = vmodl.RuntimeFault()
  3058. exc.msg = 'RuntimeFault msg'
  3059. type(self.mock_lic_mgr).licenses = PropertyMock(side_effect=exc)
  3060. with self.assertRaises(VMwareRuntimeError) as excinfo:
  3061. salt.utils.vmware.get_licenses(self.mock_si)
  3062. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  3063. def test_valid_licenses(self):
  3064. ret = salt.utils.vmware.get_licenses(self.mock_si)
  3065. self.assertEqual(ret, self.mock_licenses)
  3066. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  3067. class AddLicenseTestCase(TestCase):
  3068. '''
  3069. Tests for salt.utils.vmware.add_license
  3070. '''
  3071. def setUp(self):
  3072. self.mock_si = MagicMock()
  3073. self.mock_license = MagicMock()
  3074. self.mock_add_license = MagicMock(return_value=self.mock_license)
  3075. self.mock_lic_mgr = MagicMock(AddLicense=self.mock_add_license)
  3076. self.mock_label = MagicMock()
  3077. patches = (
  3078. ('salt.utils.vmware.get_license_manager',
  3079. MagicMock(return_value=self.mock_lic_mgr)),
  3080. ('salt.utils.vmware.vim.KeyValue',
  3081. MagicMock(return_value=self.mock_label)))
  3082. for mod, mock in patches:
  3083. patcher = patch(mod, mock)
  3084. patcher.start()
  3085. self.addCleanup(patcher.stop)
  3086. def tearDown(self):
  3087. for attr in ('mock_si', 'mock_lic_mgr', 'mock_license',
  3088. 'mock_add_license', 'mock_label'):
  3089. delattr(self, attr)
  3090. def test_no_license_manager_passed_in(self):
  3091. mock_get_license_manager = MagicMock()
  3092. with patch('salt.utils.vmware.get_license_manager',
  3093. mock_get_license_manager):
  3094. salt.utils.vmware.add_license(self.mock_si,
  3095. 'fake_license_key',
  3096. 'fake_license_description')
  3097. mock_get_license_manager.assert_called_once_with(self.mock_si)
  3098. def test_license_manager_passed_in(self):
  3099. mock_get_license_manager = MagicMock()
  3100. with patch('salt.utils.vmware.get_license_manager',
  3101. mock_get_license_manager):
  3102. salt.utils.vmware.add_license(self.mock_si,
  3103. 'fake_license_key',
  3104. 'fake_license_description',
  3105. license_manager=self.mock_lic_mgr)
  3106. self.assertEqual(mock_get_license_manager.call_count, 0)
  3107. self.assertEqual(self.mock_add_license.call_count, 1)
  3108. def test_label_settings(self):
  3109. salt.utils.vmware.add_license(self.mock_si,
  3110. 'fake_license_key',
  3111. 'fake_license_description')
  3112. self.assertEqual(self.mock_label.key, 'VpxClientLicenseLabel')
  3113. self.assertEqual(self.mock_label.value, 'fake_license_description')
  3114. def test_add_license_arguments(self):
  3115. salt.utils.vmware.add_license(self.mock_si,
  3116. 'fake_license_key',
  3117. 'fake_license_description')
  3118. self.mock_add_license.assert_called_once_with('fake_license_key',
  3119. [self.mock_label])
  3120. def test_add_license_raises_no_permission(self):
  3121. exc = vim.fault.NoPermission()
  3122. exc.privilegeId = 'Fake privilege'
  3123. self.mock_lic_mgr.AddLicense = MagicMock(side_effect=exc)
  3124. with self.assertRaises(VMwareApiError) as excinfo:
  3125. salt.utils.vmware.add_license(self.mock_si,
  3126. 'fake_license_key',
  3127. 'fake_license_description')
  3128. self.assertEqual(excinfo.exception.strerror,
  3129. 'Not enough permissions. Required privilege: '
  3130. 'Fake privilege')
  3131. def test_add_license_raises_vim_fault(self):
  3132. exc = vim.fault.VimFault()
  3133. exc.msg = 'VimFault msg'
  3134. self.mock_lic_mgr.AddLicense = MagicMock(side_effect=exc)
  3135. with self.assertRaises(VMwareApiError) as excinfo:
  3136. salt.utils.vmware.add_license(self.mock_si,
  3137. 'fake_license_key',
  3138. 'fake_license_description')
  3139. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  3140. def test_add_license_raises_runtime_fault(self):
  3141. exc = vmodl.RuntimeFault()
  3142. exc.msg = 'RuntimeFault msg'
  3143. self.mock_lic_mgr.AddLicense = MagicMock(side_effect=exc)
  3144. with self.assertRaises(VMwareRuntimeError) as excinfo:
  3145. salt.utils.vmware.add_license(self.mock_si,
  3146. 'fake_license_key',
  3147. 'fake_license_description')
  3148. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  3149. def test_valid_license_added(self):
  3150. ret = salt.utils.vmware.add_license(self.mock_si,
  3151. 'fake_license_key',
  3152. 'fake_license_description')
  3153. self.assertEqual(ret, self.mock_license)
  3154. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  3155. class GetAssignedLicensesTestCase(TestCase):
  3156. '''
  3157. Tests for salt.utils.vmware.get_assigned_licenses
  3158. '''
  3159. def setUp(self):
  3160. self.mock_ent_id = MagicMock()
  3161. self.mock_si = MagicMock()
  3162. type(self.mock_si.content.about).instanceUuid = \
  3163. PropertyMock(return_value=self.mock_ent_id)
  3164. self.mock_moid = MagicMock()
  3165. self.prop_mock_moid = PropertyMock(return_value=self.mock_moid)
  3166. self.mock_entity_ref = MagicMock()
  3167. type(self.mock_entity_ref)._moId = self.prop_mock_moid
  3168. self.mock_assignments = [MagicMock(entityDisplayName='fake_ent1'),
  3169. MagicMock(entityDisplayName='fake_ent2')]
  3170. self.mock_query_assigned_licenses = MagicMock(
  3171. return_value=[MagicMock(assignedLicense=self.mock_assignments[0]),
  3172. MagicMock(assignedLicense=self.mock_assignments[1])])
  3173. self.mock_lic_assign_mgr = MagicMock(
  3174. QueryAssignedLicenses=self.mock_query_assigned_licenses)
  3175. patches = (
  3176. ('salt.utils.vmware.get_license_assignment_manager',
  3177. MagicMock(return_value=self.mock_lic_assign_mgr)),)
  3178. for mod, mock in patches:
  3179. patcher = patch(mod, mock)
  3180. patcher.start()
  3181. self.addCleanup(patcher.stop)
  3182. def tearDown(self):
  3183. for attr in ('mock_ent_id', 'mock_si', 'mock_moid', 'prop_mock_moid',
  3184. 'mock_entity_ref', 'mock_assignments',
  3185. 'mock_query_assigned_licenses', 'mock_lic_assign_mgr'):
  3186. delattr(self, attr)
  3187. def test_no_license_assignment_manager_passed_in(self):
  3188. mock_get_license_assign_manager = MagicMock()
  3189. with patch('salt.utils.vmware.get_license_assignment_manager',
  3190. mock_get_license_assign_manager):
  3191. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3192. self.mock_entity_ref,
  3193. 'fake_entity_name')
  3194. mock_get_license_assign_manager.assert_called_once_with(self.mock_si)
  3195. def test_license_assignment_manager_passed_in(self):
  3196. mock_get_license_assign_manager = MagicMock()
  3197. with patch('salt.utils.vmware.get_license_assignment_manager',
  3198. mock_get_license_assign_manager):
  3199. salt.utils.vmware.get_assigned_licenses(
  3200. self.mock_si, self.mock_entity_ref, 'fake_entity_name',
  3201. license_assignment_manager=self.mock_lic_assign_mgr)
  3202. self.assertEqual(mock_get_license_assign_manager.call_count, 0)
  3203. def test_entity_name(self):
  3204. mock_trace = MagicMock()
  3205. with patch('salt._logging.impl.SaltLoggingClass.trace', mock_trace):
  3206. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3207. self.mock_entity_ref,
  3208. 'fake_entity_name')
  3209. mock_trace.assert_called_once_with(
  3210. "Retrieving licenses assigned to '%s'", 'fake_entity_name')
  3211. def test_instance_uuid(self):
  3212. mock_instance_uuid_prop = PropertyMock()
  3213. type(self.mock_si.content.about).instanceUuid = mock_instance_uuid_prop
  3214. self.mock_lic_assign_mgr.QueryAssignedLicenses = MagicMock(
  3215. return_value=[MagicMock(entityDisplayName='fake_vcenter')])
  3216. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3217. entity_name='fake_vcenter')
  3218. self.assertEqual(mock_instance_uuid_prop.call_count, 1)
  3219. def test_instance_uuid_raises_no_permission(self):
  3220. exc = vim.fault.NoPermission()
  3221. exc.privilegeId = 'Fake privilege'
  3222. type(self.mock_si.content.about).instanceUuid = \
  3223. PropertyMock(side_effect=exc)
  3224. with self.assertRaises(VMwareApiError) as excinfo:
  3225. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3226. entity_name='fake_vcenter')
  3227. self.assertEqual(excinfo.exception.strerror,
  3228. 'Not enough permissions. Required privilege: '
  3229. 'Fake privilege')
  3230. def test_instance_uuid_raises_vim_fault(self):
  3231. exc = vim.fault.VimFault()
  3232. exc.msg = 'VimFault msg'
  3233. type(self.mock_si.content.about).instanceUuid = \
  3234. PropertyMock(side_effect=exc)
  3235. with self.assertRaises(VMwareApiError) as excinfo:
  3236. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3237. entity_name='fake_vcenter')
  3238. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  3239. def test_instance_uuid_raises_runtime_fault(self):
  3240. exc = vmodl.RuntimeFault()
  3241. exc.msg = 'RuntimeFault msg'
  3242. type(self.mock_si.content.about).instanceUuid = \
  3243. PropertyMock(side_effect=exc)
  3244. with self.assertRaises(VMwareRuntimeError) as excinfo:
  3245. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3246. entity_name='fake_vcenter')
  3247. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  3248. def test_vcenter_entity_too_many_assignements(self):
  3249. self.mock_lic_assign_mgr.QueryAssignedLicenses = MagicMock(
  3250. return_value=[MagicMock(), MagicMock()])
  3251. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  3252. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3253. entity_name='fake_vcenter')
  3254. self.assertEqual(excinfo.exception.strerror,
  3255. 'Unexpected return. Expect only a single assignment')
  3256. def test_wrong_vcenter_name(self):
  3257. self.mock_lic_assign_mgr.QueryAssignedLicenses = MagicMock(
  3258. return_value=[MagicMock(entityDisplayName='bad_vcenter')])
  3259. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  3260. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3261. entity_name='fake_vcenter')
  3262. self.assertEqual(excinfo.exception.strerror,
  3263. 'Got license assignment info for a different vcenter')
  3264. def test_query_assigned_licenses_vcenter(self):
  3265. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  3266. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3267. entity_name='fake_vcenter')
  3268. self.mock_query_assigned_licenses.assert_called_once_with(
  3269. self.mock_ent_id)
  3270. def test_query_assigned_licenses_with_entity(self):
  3271. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3272. self.mock_entity_ref,
  3273. 'fake_entity_name')
  3274. self.mock_query_assigned_licenses.assert_called_once_with(
  3275. self.mock_moid)
  3276. def test_query_assigned_licenses_raises_no_permission(self):
  3277. exc = vim.fault.NoPermission()
  3278. exc.privilegeId = 'Fake privilege'
  3279. self.mock_lic_assign_mgr.QueryAssignedLicenses = \
  3280. MagicMock(side_effect=exc)
  3281. with self.assertRaises(VMwareApiError) as excinfo:
  3282. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3283. self.mock_entity_ref,
  3284. 'fake_entity_name')
  3285. self.assertEqual(excinfo.exception.strerror,
  3286. 'Not enough permissions. Required privilege: '
  3287. 'Fake privilege')
  3288. def test_query_assigned_licenses_raises_vim_fault(self):
  3289. exc = vim.fault.VimFault()
  3290. exc.msg = 'VimFault msg'
  3291. self.mock_lic_assign_mgr.QueryAssignedLicenses = \
  3292. MagicMock(side_effect=exc)
  3293. with self.assertRaises(VMwareApiError) as excinfo:
  3294. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3295. self.mock_entity_ref,
  3296. 'fake_entity_name')
  3297. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  3298. def test_query_assigned_licenses_raises_runtime_fault(self):
  3299. exc = vmodl.RuntimeFault()
  3300. exc.msg = 'RuntimeFault msg'
  3301. self.mock_lic_assign_mgr.QueryAssignedLicenses = \
  3302. MagicMock(side_effect=exc)
  3303. with self.assertRaises(VMwareRuntimeError) as excinfo:
  3304. salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3305. self.mock_entity_ref,
  3306. 'fake_entity_name')
  3307. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  3308. def test_valid_assignments(self):
  3309. ret = salt.utils.vmware.get_assigned_licenses(self.mock_si,
  3310. self.mock_entity_ref,
  3311. 'fake_entity_name')
  3312. self.assertEqual(ret, self.mock_assignments)
  3313. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  3314. class AssignLicenseTestCase(TestCase):
  3315. '''
  3316. Tests for salt.utils.vmware.assign_license
  3317. '''
  3318. def setUp(self):
  3319. self.mock_ent_id = MagicMock()
  3320. self.mock_si = MagicMock()
  3321. type(self.mock_si.content.about).instanceUuid = \
  3322. PropertyMock(return_value=self.mock_ent_id)
  3323. self.mock_lic_key = MagicMock()
  3324. self.mock_moid = MagicMock()
  3325. self.prop_mock_moid = PropertyMock(return_value=self.mock_moid)
  3326. self.mock_entity_ref = MagicMock()
  3327. type(self.mock_entity_ref)._moId = self.prop_mock_moid
  3328. self.mock_license = MagicMock()
  3329. self.mock_update_assigned_license = MagicMock(
  3330. return_value=self.mock_license)
  3331. self.mock_lic_assign_mgr = MagicMock(
  3332. UpdateAssignedLicense=self.mock_update_assigned_license)
  3333. patches = (
  3334. ('salt.utils.vmware.get_license_assignment_manager',
  3335. MagicMock(return_value=self.mock_lic_assign_mgr)),)
  3336. for mod, mock in patches:
  3337. patcher = patch(mod, mock)
  3338. patcher.start()
  3339. self.addCleanup(patcher.stop)
  3340. def test_no_license_assignment_manager_passed_in(self):
  3341. mock_get_license_assign_manager = MagicMock()
  3342. with patch('salt.utils.vmware.get_license_assignment_manager',
  3343. mock_get_license_assign_manager):
  3344. salt.utils.vmware.assign_license(self.mock_si,
  3345. self.mock_lic_key,
  3346. 'fake_license_name',
  3347. self.mock_entity_ref,
  3348. 'fake_entity_name')
  3349. mock_get_license_assign_manager.assert_called_once_with(self.mock_si)
  3350. def test_license_assignment_manager_passed_in(self):
  3351. mock_get_license_assign_manager = MagicMock()
  3352. with patch('salt.utils.vmware.get_license_assignment_manager',
  3353. mock_get_license_assign_manager):
  3354. salt.utils.vmware.assign_license(
  3355. self.mock_si, self.mock_lic_key, 'fake_license_name',
  3356. self.mock_entity_ref, 'fake_entity_name',
  3357. license_assignment_manager=self.mock_lic_assign_mgr)
  3358. self.assertEqual(mock_get_license_assign_manager.call_count, 0)
  3359. self.assertEqual(self.mock_update_assigned_license.call_count, 1)
  3360. def test_entity_name(self):
  3361. mock_trace = MagicMock()
  3362. with patch('salt._logging.impl.SaltLoggingClass.trace', mock_trace):
  3363. salt.utils.vmware.assign_license(self.mock_si,
  3364. self.mock_lic_key,
  3365. 'fake_license_name',
  3366. self.mock_entity_ref,
  3367. 'fake_entity_name')
  3368. mock_trace.assert_called_once_with(
  3369. "Assigning license to '%s'", 'fake_entity_name')
  3370. def test_instance_uuid(self):
  3371. mock_instance_uuid_prop = PropertyMock()
  3372. type(self.mock_si.content.about).instanceUuid = mock_instance_uuid_prop
  3373. self.mock_lic_assign_mgr.UpdateAssignedLicense = MagicMock(
  3374. return_value=[MagicMock(entityDisplayName='fake_vcenter')])
  3375. salt.utils.vmware.assign_license(self.mock_si,
  3376. self.mock_lic_key,
  3377. 'fake_license_name',
  3378. entity_name='fake_entity_name')
  3379. self.assertEqual(mock_instance_uuid_prop.call_count, 1)
  3380. def test_instance_uuid_raises_no_permission(self):
  3381. exc = vim.fault.NoPermission()
  3382. exc.privilegeId = 'Fake privilege'
  3383. type(self.mock_si.content.about).instanceUuid = \
  3384. PropertyMock(side_effect=exc)
  3385. with self.assertRaises(VMwareApiError) as excinfo:
  3386. salt.utils.vmware.assign_license(self.mock_si,
  3387. self.mock_lic_key,
  3388. 'fake_license_name',
  3389. entity_name='fake_entity_name')
  3390. self.assertEqual(excinfo.exception.strerror,
  3391. 'Not enough permissions. Required privilege: '
  3392. 'Fake privilege')
  3393. def test_instance_uuid_raises_vim_fault(self):
  3394. exc = vim.fault.VimFault()
  3395. exc.msg = 'VimFault msg'
  3396. type(self.mock_si.content.about).instanceUuid = \
  3397. PropertyMock(side_effect=exc)
  3398. with self.assertRaises(VMwareApiError) as excinfo:
  3399. salt.utils.vmware.assign_license(self.mock_si,
  3400. self.mock_lic_key,
  3401. 'fake_license_name',
  3402. entity_name='fake_entity_name')
  3403. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  3404. def test_instance_uuid_raises_runtime_fault(self):
  3405. exc = vmodl.RuntimeFault()
  3406. exc.msg = 'RuntimeFault msg'
  3407. type(self.mock_si.content.about).instanceUuid = \
  3408. PropertyMock(side_effect=exc)
  3409. with self.assertRaises(VMwareRuntimeError) as excinfo:
  3410. salt.utils.vmware.assign_license(self.mock_si,
  3411. self.mock_lic_key,
  3412. 'fake_license_name',
  3413. entity_name='fake_entity_name')
  3414. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  3415. def test_update_assigned_licenses_vcenter(self):
  3416. salt.utils.vmware.assign_license(self.mock_si,
  3417. self.mock_lic_key,
  3418. 'fake_license_name',
  3419. entity_name='fake_entity_name')
  3420. self.mock_update_assigned_license.assert_called_once_with(
  3421. self.mock_ent_id, self.mock_lic_key, 'fake_license_name')
  3422. def test_update_assigned_licenses_call_with_entity(self):
  3423. salt.utils.vmware.assign_license(self.mock_si,
  3424. self.mock_lic_key,
  3425. 'fake_license_name',
  3426. self.mock_entity_ref,
  3427. 'fake_entity_name')
  3428. self.mock_update_assigned_license.assert_called_once_with(
  3429. self.mock_moid, self.mock_lic_key, 'fake_license_name')
  3430. def test_update_assigned_licenses_raises_no_permission(self):
  3431. exc = vim.fault.NoPermission()
  3432. exc.privilegeId = 'Fake privilege'
  3433. self.mock_lic_assign_mgr.UpdateAssignedLicense = \
  3434. MagicMock(side_effect=exc)
  3435. with self.assertRaises(VMwareApiError) as excinfo:
  3436. salt.utils.vmware.assign_license(self.mock_si,
  3437. self.mock_lic_key,
  3438. 'fake_license_name',
  3439. self.mock_entity_ref,
  3440. 'fake_entity_name')
  3441. self.assertEqual(excinfo.exception.strerror,
  3442. 'Not enough permissions. Required privilege: '
  3443. 'Fake privilege')
  3444. def test_update_assigned_licenses_raises_vim_fault(self):
  3445. exc = vim.fault.VimFault()
  3446. exc.msg = 'VimFault msg'
  3447. self.mock_lic_assign_mgr.UpdateAssignedLicense = \
  3448. MagicMock(side_effect=exc)
  3449. with self.assertRaises(VMwareApiError) as excinfo:
  3450. salt.utils.vmware.assign_license(self.mock_si,
  3451. self.mock_lic_key,
  3452. 'fake_license_name',
  3453. self.mock_entity_ref,
  3454. 'fake_entity_name')
  3455. self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
  3456. def test_update_assigned_licenses_raises_runtime_fault(self):
  3457. exc = vmodl.RuntimeFault()
  3458. exc.msg = 'RuntimeFault msg'
  3459. self.mock_lic_assign_mgr.UpdateAssignedLicense = \
  3460. MagicMock(side_effect=exc)
  3461. with self.assertRaises(VMwareRuntimeError) as excinfo:
  3462. salt.utils.vmware.assign_license(self.mock_si,
  3463. self.mock_lic_key,
  3464. 'fake_license_name',
  3465. self.mock_entity_ref,
  3466. 'fake_entity_name')
  3467. self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
  3468. def test_valid_assignments(self):
  3469. ret = salt.utils.vmware.assign_license(self.mock_si,
  3470. self.mock_lic_key,
  3471. 'fake_license_name',
  3472. self.mock_entity_ref,
  3473. 'fake_entity_name')
  3474. self.assertEqual(ret, self.mock_license)
  3475. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  3476. class GetStorageSystemTestCase(TestCase):
  3477. '''
  3478. Tests for salt.utils.vmware.get_storage_system
  3479. '''
  3480. def setUp(self):
  3481. self.mock_si = MagicMock(content=MagicMock())
  3482. self.mock_host_ref = MagicMock()
  3483. self.mock_get_managed_object_name = MagicMock(return_value='fake_host')
  3484. self.mock_traversal_spec = MagicMock()
  3485. self.mock_obj = MagicMock()
  3486. self.mock_get_mors = \
  3487. MagicMock(return_value=[{'object': self.mock_obj}])
  3488. patches = (
  3489. ('salt.utils.vmware.get_managed_object_name',
  3490. self.mock_get_managed_object_name),
  3491. ('salt.utils.vmware.get_mors_with_properties',
  3492. self.mock_get_mors),
  3493. ('salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  3494. MagicMock(return_value=self.mock_traversal_spec)))
  3495. for mod, mock in patches:
  3496. patcher = patch(mod, mock)
  3497. patcher.start()
  3498. self.addCleanup(patcher.stop)
  3499. def tearDown(self):
  3500. for attr in ('mock_si', 'mock_host_ref',
  3501. 'mock_get_managed_object_name',
  3502. 'mock_traversal_spec', 'mock_obj'):
  3503. delattr(self, attr)
  3504. def test_no_hostname_argument(self):
  3505. salt.utils.vmware.get_storage_system(self.mock_si,
  3506. self.mock_host_ref)
  3507. self.mock_get_managed_object_name.assert_called_once_with(
  3508. self.mock_host_ref)
  3509. def test_hostname_argument(self):
  3510. salt.utils.vmware.get_storage_system(self.mock_si,
  3511. self.mock_host_ref,
  3512. hostname='fake_host')
  3513. self.assertEqual(self.mock_get_managed_object_name.call_count, 0)
  3514. def test_traversal_spec(self):
  3515. mock_traversal_spec = MagicMock(return_value=[{'object':
  3516. self.mock_obj}])
  3517. with patch(
  3518. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  3519. mock_traversal_spec):
  3520. salt.utils.vmware.get_storage_system(self.mock_si,
  3521. self.mock_host_ref)
  3522. mock_traversal_spec.assert_called_once_with(
  3523. path='configManager.storageSystem',
  3524. type=vim.HostSystem,
  3525. skip=False)
  3526. def test_get_mors_with_properties(self):
  3527. salt.utils.vmware.get_storage_system(self.mock_si,
  3528. self.mock_host_ref)
  3529. self.mock_get_mors.assert_called_once_with(
  3530. self.mock_si,
  3531. vim.HostStorageSystem,
  3532. property_list=['systemFile'],
  3533. container_ref=self.mock_host_ref,
  3534. traversal_spec=self.mock_traversal_spec)
  3535. def test_empty_mors_result(self):
  3536. with patch('salt.utils.vmware.get_mors_with_properties',
  3537. MagicMock(return_value=[])):
  3538. with self.assertRaises(VMwareObjectRetrievalError) as excinfo:
  3539. salt.utils.vmware.get_storage_system(self.mock_si,
  3540. self.mock_host_ref)
  3541. self.assertEqual(excinfo.exception.strerror,
  3542. 'Host\'s \'fake_host\' storage system was '
  3543. 'not retrieved')
  3544. def test_valid_mors_result(self):
  3545. res = salt.utils.vmware.get_storage_system(self.mock_si,
  3546. self.mock_host_ref)
  3547. self.assertEqual(res, self.mock_obj)
  3548. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  3549. class GetDatastoresTestCase(TestCase):
  3550. '''
  3551. Tests for salt.utils.vmware.get_datastores
  3552. '''
  3553. def setUp(self):
  3554. self.mock_si = MagicMock()
  3555. self.mock_reference = MagicMock(spec=vim.HostSystem)
  3556. self.mock_mount_infos = [
  3557. MagicMock(volume=MagicMock(spec=vim.HostVmfsVolume,
  3558. extent=[MagicMock(
  3559. diskName='fake_disk2')])),
  3560. MagicMock(volume=MagicMock(spec=vim.HostVmfsVolume,
  3561. extent=[MagicMock(
  3562. diskName='fake_disk3')]))]
  3563. self.mock_mount_infos[0].volume.name = 'fake_ds2'
  3564. self.mock_mount_infos[1].volume.name = 'fake_ds3'
  3565. self.mock_entries = [{'name': 'fake_ds1', 'object': MagicMock()},
  3566. {'name': 'fake_ds2', 'object': MagicMock()},
  3567. {'name': 'fake_ds3', 'object': MagicMock()}]
  3568. self.mock_storage_system = MagicMock()
  3569. self.mock_get_storage_system = MagicMock(
  3570. return_value=self.mock_storage_system)
  3571. self.mock_get_managed_object_name = MagicMock(return_value='fake_host')
  3572. self.mock_traversal_spec = MagicMock()
  3573. patches = (
  3574. ('salt.utils.vmware.get_managed_object_name',
  3575. self.mock_get_managed_object_name),
  3576. ('salt.utils.vmware.get_storage_system',
  3577. self.mock_get_storage_system),
  3578. ('salt.utils.vmware.get_properties_of_managed_object',
  3579. MagicMock(return_value={'fileSystemVolumeInfo.mountInfo':
  3580. self.mock_mount_infos})),
  3581. ('salt.utils.vmware.get_mors_with_properties',
  3582. MagicMock(return_value=self.mock_entries)),
  3583. ('salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  3584. MagicMock(return_value=self.mock_traversal_spec)))
  3585. for mod, mock in patches:
  3586. patcher = patch(mod, mock)
  3587. patcher.start()
  3588. self.addCleanup(patcher.stop)
  3589. def tearDown(self):
  3590. for attr in ('mock_si', 'mock_reference', 'mock_storage_system',
  3591. 'mock_get_storage_system', 'mock_mount_infos',
  3592. 'mock_entries', 'mock_get_managed_object_name',
  3593. 'mock_traversal_spec'):
  3594. delattr(self, attr)
  3595. def test_get_reference_name_call(self):
  3596. salt.utils.vmware.get_datastores(self.mock_si,
  3597. self.mock_reference)
  3598. self.mock_get_managed_object_name.assert_called_once_with(
  3599. self.mock_reference)
  3600. def test_get_no_datastores(self):
  3601. res = salt.utils.vmware.get_datastores(self.mock_si,
  3602. self.mock_reference)
  3603. self.assertEqual(res, [])
  3604. def test_get_storage_system_call(self):
  3605. salt.utils.vmware.get_datastores(self.mock_si,
  3606. self.mock_reference,
  3607. backing_disk_ids=['fake_disk1'])
  3608. self.mock_get_storage_system.assert_called_once_with(
  3609. self.mock_si, self.mock_reference, 'fake_host')
  3610. def test_get_mount_info_call(self):
  3611. mock_get_properties_of_managed_object = MagicMock()
  3612. with patch('salt.utils.vmware.get_properties_of_managed_object',
  3613. mock_get_properties_of_managed_object):
  3614. salt.utils.vmware.get_datastores(self.mock_si,
  3615. self.mock_reference,
  3616. backing_disk_ids=['fake_disk1'])
  3617. mock_get_properties_of_managed_object.assert_called_once_with(
  3618. self.mock_storage_system, ['fileSystemVolumeInfo.mountInfo'])
  3619. def test_backing_disks_no_mount_info(self):
  3620. with patch('salt.utils.vmware.get_properties_of_managed_object',
  3621. MagicMock(return_value={})):
  3622. res = salt.utils.vmware.get_datastores(
  3623. self.mock_si, self.mock_reference,
  3624. backing_disk_ids=['fake_disk_id'])
  3625. self.assertEqual(res, [])
  3626. def test_host_traversal_spec(self):
  3627. # Reference is of type vim.HostSystem
  3628. mock_traversal_spec_init = MagicMock()
  3629. with patch(
  3630. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  3631. mock_traversal_spec_init):
  3632. salt.utils.vmware.get_datastores(
  3633. self.mock_si,
  3634. self.mock_reference,
  3635. get_all_datastores=True)
  3636. mock_traversal_spec_init.assert_called_once_with(
  3637. name='host_datastore_traversal',
  3638. path='datastore',
  3639. skip=False,
  3640. type=vim.HostSystem)
  3641. def test_cluster_traversal_spec(self):
  3642. mock_traversal_spec_init = MagicMock()
  3643. # Reference is of type vim.ClusterComputeResource
  3644. mock_reference = MagicMock(spec=vim.ClusterComputeResource)
  3645. with patch(
  3646. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  3647. mock_traversal_spec_init):
  3648. salt.utils.vmware.get_datastores(
  3649. self.mock_si,
  3650. mock_reference,
  3651. get_all_datastores=True)
  3652. mock_traversal_spec_init.assert_called_once_with(
  3653. name='cluster_datastore_traversal',
  3654. path='datastore',
  3655. skip=False,
  3656. type=vim.ClusterComputeResource)
  3657. def test_datacenter_traversal_spec(self):
  3658. mock_traversal_spec_init = MagicMock()
  3659. # Reference is of type vim.ClusterComputeResource
  3660. mock_reference = MagicMock(spec=vim.Datacenter)
  3661. with patch(
  3662. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  3663. mock_traversal_spec_init):
  3664. salt.utils.vmware.get_datastores(
  3665. self.mock_si,
  3666. mock_reference,
  3667. get_all_datastores=True)
  3668. mock_traversal_spec_init.assert_called_once_with(
  3669. name='datacenter_datastore_traversal',
  3670. path='datastore',
  3671. skip=False,
  3672. type=vim.Datacenter)
  3673. def test_root_folder_traversal_spec(self):
  3674. mock_traversal_spec_init = MagicMock(return_value='traversal')
  3675. mock_reference = MagicMock(spec=vim.Folder)
  3676. with patch('salt.utils.vmware.get_managed_object_name',
  3677. MagicMock(side_effect=['fake_host', 'Datacenters'])):
  3678. with patch(
  3679. 'salt.utils.vmware.vmodl.query.PropertyCollector.TraversalSpec',
  3680. mock_traversal_spec_init):
  3681. salt.utils.vmware.get_datastores(
  3682. self.mock_si,
  3683. mock_reference,
  3684. get_all_datastores=True)
  3685. mock_traversal_spec_init.assert_has_calls([
  3686. call(path='datastore',
  3687. skip=False,
  3688. type=vim.Datacenter),
  3689. call(path='childEntity',
  3690. selectSet=['traversal'],
  3691. skip=False,
  3692. type=vim.Folder)])
  3693. def test_unsupported_reference_type(self):
  3694. class FakeClass(object):
  3695. pass
  3696. mock_reference = MagicMock(spec=FakeClass)
  3697. with self.assertRaises(ArgumentValueError) as excinfo:
  3698. salt.utils.vmware.get_datastores(
  3699. self.mock_si,
  3700. mock_reference,
  3701. get_all_datastores=True)
  3702. self.assertEqual(excinfo.exception.strerror,
  3703. 'Unsupported reference type \'FakeClass\'')
  3704. def test_get_mors_with_properties(self):
  3705. mock_get_mors_with_properties = MagicMock()
  3706. with patch('salt.utils.vmware.get_mors_with_properties',
  3707. mock_get_mors_with_properties):
  3708. salt.utils.vmware.get_datastores(
  3709. self.mock_si,
  3710. self.mock_reference,
  3711. get_all_datastores=True)
  3712. mock_get_mors_with_properties.assert_called_once_with(
  3713. self.mock_si,
  3714. object_type=vim.Datastore,
  3715. property_list=['name'],
  3716. container_ref=self.mock_reference,
  3717. traversal_spec=self.mock_traversal_spec)
  3718. def test_get_all_datastores(self):
  3719. res = salt.utils.vmware.get_datastores(self.mock_si,
  3720. self.mock_reference,
  3721. get_all_datastores=True)
  3722. self.assertEqual(res, [self.mock_entries[0]['object'],
  3723. self.mock_entries[1]['object'],
  3724. self.mock_entries[2]['object']])
  3725. def test_get_datastores_filtered_by_name(self):
  3726. res = salt.utils.vmware.get_datastores(self.mock_si,
  3727. self.mock_reference,
  3728. datastore_names=['fake_ds1',
  3729. 'fake_ds2'])
  3730. self.assertEqual(res, [self.mock_entries[0]['object'],
  3731. self.mock_entries[1]['object']])
  3732. def test_get_datastores_filtered_by_backing_disk(self):
  3733. res = salt.utils.vmware.get_datastores(
  3734. self.mock_si, self.mock_reference,
  3735. backing_disk_ids=['fake_disk2', 'fake_disk3'])
  3736. self.assertEqual(res, [self.mock_entries[1]['object'],
  3737. self.mock_entries[2]['object']])
  3738. def test_get_datastores_filtered_by_both_name_and_backing_disk(self):
  3739. # Simulate VMware data model for volumes fake_ds2, fake_ds3
  3740. res = salt.utils.vmware.get_datastores(
  3741. self.mock_si, self.mock_reference,
  3742. datastore_names=['fake_ds1'],
  3743. backing_disk_ids=['fake_disk3'])
  3744. self.assertEqual(res, [self.mock_entries[0]['object'],
  3745. self.mock_entries[2]['object']])
  3746. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  3747. class RenameDatastoreTestCase(TestCase):
  3748. '''
  3749. Tests for salt.utils.vmware.rename_datastore
  3750. '''
  3751. def setUp(self):
  3752. self.mock_ds_ref = MagicMock()
  3753. self.mock_get_managed_object_name = MagicMock(return_value='fake_ds')
  3754. patches = (
  3755. ('salt.utils.vmware.get_managed_object_name',
  3756. self.mock_get_managed_object_name),)
  3757. for mod, mock in patches:
  3758. patcher = patch(mod, mock)
  3759. patcher.start()
  3760. self.addCleanup(patcher.stop)
  3761. def tearDown(self):
  3762. for attr in ('mock_ds_ref', 'mock_get_managed_object_name'):
  3763. delattr(self, attr)
  3764. def test_datastore_name_call(self):
  3765. salt.utils.vmware.rename_datastore(self.mock_ds_ref,
  3766. 'fake_new_name')
  3767. self.mock_get_managed_object_name.assert_called_once_with(
  3768. self.mock_ds_ref)
  3769. def test_rename_datastore_raise_no_permission(self):
  3770. exc = vim.fault.NoPermission()
  3771. exc.privilegeId = 'Fake privilege'
  3772. type(self.mock_ds_ref).RenameDatastore = MagicMock(side_effect=exc)
  3773. with self.assertRaises(VMwareApiError) as excinfo:
  3774. salt.utils.vmware.rename_datastore(self.mock_ds_ref,
  3775. 'fake_new_name')
  3776. self.assertEqual(excinfo.exception.strerror,
  3777. 'Not enough permissions. Required privilege: '
  3778. 'Fake privilege')
  3779. def test_rename_datastore_raise_vim_fault(self):
  3780. exc = vim.VimFault()
  3781. exc.msg = 'vim_fault'
  3782. type(self.mock_ds_ref).RenameDatastore = MagicMock(side_effect=exc)
  3783. with self.assertRaises(VMwareApiError) as excinfo:
  3784. salt.utils.vmware.rename_datastore(self.mock_ds_ref,
  3785. 'fake_new_name')
  3786. self.assertEqual(excinfo.exception.strerror, 'vim_fault')
  3787. def test_rename_datastore_raise_runtime_fault(self):
  3788. exc = vmodl.RuntimeFault()
  3789. exc.msg = 'runtime_fault'
  3790. type(self.mock_ds_ref).RenameDatastore = MagicMock(side_effect=exc)
  3791. with self.assertRaises(VMwareRuntimeError) as excinfo:
  3792. salt.utils.vmware.rename_datastore(self.mock_ds_ref,
  3793. 'fake_new_name')
  3794. self.assertEqual(excinfo.exception.strerror, 'runtime_fault')
  3795. def test_rename_datastore(self):
  3796. salt.utils.vmware.rename_datastore(self.mock_ds_ref, 'fake_new_name')
  3797. self.mock_ds_ref.RenameDatastore.assert_called_once_with(
  3798. 'fake_new_name')
  3799. class ConvertToKbTestCase(TestCase):
  3800. '''
  3801. Tests for converting units
  3802. '''
  3803. def setUp(self):
  3804. pass
  3805. def test_gb_conversion_call(self):
  3806. self.assertEqual(salt.utils.vmware.convert_to_kb('Gb', 10), {'size': int(10485760), 'unit': 'KB'})
  3807. def test_mb_conversion_call(self):
  3808. self.assertEqual(salt.utils.vmware.convert_to_kb('Mb', 10), {'size': int(10240), 'unit': 'KB'})
  3809. def test_kb_conversion_call(self):
  3810. self.assertEqual(salt.utils.vmware.convert_to_kb('Kb', 10), {'size': int(10), 'unit': 'KB'})
  3811. def test_conversion_bad_input_argument_fault(self):
  3812. self.assertRaises(ArgumentValueError, salt.utils.vmware.convert_to_kb, 'test', 10)
  3813. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  3814. @patch('salt.utils.vmware.get_managed_object_name', MagicMock())
  3815. @patch('salt.utils.vmware.wait_for_task', MagicMock())
  3816. class CreateVirtualMachineTestCase(TestCase):
  3817. '''
  3818. Tests for salt.utils.vmware.create_vm
  3819. '''
  3820. def setUp(self):
  3821. self.vm_name = 'fake_vm'
  3822. self.mock_task = MagicMock()
  3823. self.mock_config_spec = MagicMock()
  3824. self.mock_resourcepool_object = MagicMock()
  3825. self.mock_host_object = MagicMock()
  3826. self.mock_vm_create_task = MagicMock(return_value=self.mock_task)
  3827. self.mock_folder_object = MagicMock(CreateVM_Task=self.mock_vm_create_task)
  3828. def test_create_vm_pool_task_call(self):
  3829. salt.utils.vmware.create_vm(self.vm_name, self.mock_config_spec,
  3830. self.mock_folder_object,
  3831. self.mock_resourcepool_object)
  3832. self.assert_called_once(self.mock_vm_create_task)
  3833. def test_create_vm_host_task_call(self):
  3834. salt.utils.vmware.create_vm(self.vm_name, self.mock_config_spec,
  3835. self.mock_folder_object,
  3836. self.mock_resourcepool_object,
  3837. host_object=self.mock_host_object)
  3838. self.assert_called_once(self.mock_vm_create_task)
  3839. def test_create_vm_raise_no_permission(self):
  3840. exception = vim.fault.NoPermission()
  3841. exception.msg = 'vim.fault.NoPermission msg'
  3842. self.mock_folder_object.CreateVM_Task = MagicMock(side_effect=exception)
  3843. with self.assertRaises(VMwareApiError) as exc:
  3844. salt.utils.vmware.create_vm(self.vm_name, self.mock_config_spec,
  3845. self.mock_folder_object,
  3846. self.mock_resourcepool_object)
  3847. self.assertEqual(exc.exception.strerror,
  3848. 'Not enough permissions. Required privilege: ')
  3849. def test_create_vm_raise_vim_fault(self):
  3850. exception = vim.fault.VimFault()
  3851. exception.msg = 'vim.fault.VimFault msg'
  3852. self.mock_folder_object.CreateVM_Task = MagicMock(side_effect=exception)
  3853. with self.assertRaises(VMwareApiError) as exc:
  3854. salt.utils.vmware.create_vm(self.vm_name, self.mock_config_spec,
  3855. self.mock_folder_object,
  3856. self.mock_resourcepool_object)
  3857. self.assertEqual(exc.exception.strerror, 'vim.fault.VimFault msg')
  3858. def test_create_vm_raise_runtime_fault(self):
  3859. exception = vmodl.RuntimeFault()
  3860. exception.msg = 'vmodl.RuntimeFault msg'
  3861. self.mock_folder_object.CreateVM_Task = MagicMock(side_effect=exception)
  3862. with self.assertRaises(VMwareRuntimeError) as exc:
  3863. salt.utils.vmware.create_vm(self.vm_name, self.mock_config_spec,
  3864. self.mock_folder_object,
  3865. self.mock_resourcepool_object)
  3866. self.assertEqual(exc.exception.strerror, 'vmodl.RuntimeFault msg')
  3867. def test_create_vm_wait_for_task(self):
  3868. mock_wait_for_task = MagicMock()
  3869. with patch('salt.utils.vmware.wait_for_task', mock_wait_for_task):
  3870. salt.utils.vmware.create_vm(self.vm_name, self.mock_config_spec,
  3871. self.mock_folder_object,
  3872. self.mock_resourcepool_object)
  3873. mock_wait_for_task.assert_called_once_with(
  3874. self.mock_task, self.vm_name, 'CreateVM Task', 10, 'info')
  3875. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  3876. @patch('salt.utils.vmware.get_managed_object_name', MagicMock())
  3877. @patch('salt.utils.vmware.wait_for_task', MagicMock())
  3878. class RegisterVirtualMachineTestCase(TestCase):
  3879. '''
  3880. Tests for salt.utils.vmware.register_vm
  3881. '''
  3882. def setUp(self):
  3883. self.vm_name = 'fake_vm'
  3884. self.mock_task = MagicMock()
  3885. self.mock_vmx_path = MagicMock()
  3886. self.mock_resourcepool_object = MagicMock()
  3887. self.mock_host_object = MagicMock()
  3888. self.mock_vm_register_task = MagicMock(return_value=self.mock_task)
  3889. self.vm_folder_object = MagicMock(RegisterVM_Task=self.mock_vm_register_task)
  3890. self.datacenter = MagicMock(vmFolder=self.vm_folder_object)
  3891. def test_register_vm_pool_task_call(self):
  3892. salt.utils.vmware.register_vm(self.datacenter, self.vm_name,
  3893. self.mock_vmx_path,
  3894. self.mock_resourcepool_object)
  3895. self.assert_called_once(self.mock_vm_register_task)
  3896. def test_register_vm_host_task_call(self):
  3897. salt.utils.vmware.register_vm(self.datacenter, self.vm_name,
  3898. self.mock_vmx_path,
  3899. self.mock_resourcepool_object,
  3900. host_object=self.mock_host_object)
  3901. self.assert_called_once(self.mock_vm_register_task)
  3902. def test_register_vm_raise_no_permission(self):
  3903. exception = vim.fault.NoPermission()
  3904. self.vm_folder_object.RegisterVM_Task = MagicMock(side_effect=exception)
  3905. with self.assertRaises(VMwareApiError) as exc:
  3906. salt.utils.vmware.register_vm(self.datacenter, self.vm_name,
  3907. self.mock_vmx_path,
  3908. self.mock_resourcepool_object)
  3909. self.assertEqual(exc.exception.strerror,
  3910. 'Not enough permissions. Required privilege: ')
  3911. def test_register_vm_raise_vim_fault(self):
  3912. exception = vim.fault.VimFault()
  3913. exception.msg = 'vim.fault.VimFault msg'
  3914. self.vm_folder_object.RegisterVM_Task = MagicMock(side_effect=exception)
  3915. with self.assertRaises(VMwareApiError) as exc:
  3916. salt.utils.vmware.register_vm(self.datacenter, self.vm_name,
  3917. self.mock_vmx_path,
  3918. self.mock_resourcepool_object)
  3919. self.assertEqual(exc.exception.strerror, 'vim.fault.VimFault msg')
  3920. def test_register_vm_raise_runtime_fault(self):
  3921. exception = vmodl.RuntimeFault()
  3922. exception.msg = 'vmodl.RuntimeFault msg'
  3923. self.vm_folder_object.RegisterVM_Task = MagicMock(side_effect=exception)
  3924. with self.assertRaises(VMwareRuntimeError) as exc:
  3925. salt.utils.vmware.register_vm(self.datacenter, self.vm_name,
  3926. self.mock_vmx_path,
  3927. self.mock_resourcepool_object)
  3928. self.assertEqual(exc.exception.strerror, 'vmodl.RuntimeFault msg')
  3929. def test_register_vm_wait_for_task(self):
  3930. mock_wait_for_task = MagicMock()
  3931. with patch('salt.utils.vmware.wait_for_task', mock_wait_for_task):
  3932. salt.utils.vmware.register_vm(self.datacenter, self.vm_name,
  3933. self.mock_vmx_path,
  3934. self.mock_resourcepool_object)
  3935. mock_wait_for_task.assert_called_once_with(
  3936. self.mock_task, self.vm_name, 'RegisterVM Task')
  3937. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  3938. @patch('salt.utils.vmware.get_managed_object_name', MagicMock())
  3939. @patch('salt.utils.vmware.wait_for_task', MagicMock())
  3940. class UpdateVirtualMachineTestCase(TestCase):
  3941. '''
  3942. Tests for salt.utils.vmware.update_vm
  3943. '''
  3944. def setUp(self):
  3945. self.mock_task = MagicMock()
  3946. self.mock_config_spec = MagicMock()
  3947. self.mock_vm_update_task = MagicMock(return_value=self.mock_task)
  3948. self.mock_vm_ref = MagicMock(ReconfigVM_Task=self.mock_vm_update_task)
  3949. def test_update_vm_task_call(self):
  3950. salt.utils.vmware.update_vm(self.mock_vm_ref, self.mock_config_spec)
  3951. self.assert_called_once(self.mock_vm_update_task)
  3952. def test_update_vm_raise_vim_fault(self):
  3953. exception = vim.fault.VimFault()
  3954. exception.msg = 'vim.fault.VimFault'
  3955. self.mock_vm_ref.ReconfigVM_Task = MagicMock(side_effect=exception)
  3956. with self.assertRaises(VMwareApiError) as exc:
  3957. salt.utils.vmware.update_vm(self.mock_vm_ref, self.mock_config_spec)
  3958. self.assertEqual(exc.exception.strerror, 'vim.fault.VimFault')
  3959. def test_update_vm_raise_runtime_fault(self):
  3960. exception = vmodl.RuntimeFault()
  3961. exception.msg = 'vmodl.RuntimeFault'
  3962. self.mock_vm_ref.ReconfigVM_Task = MagicMock(side_effect=exception)
  3963. with self.assertRaises(VMwareRuntimeError) as exc:
  3964. salt.utils.vmware.update_vm(self.mock_vm_ref, self.mock_config_spec)
  3965. self.assertEqual(exc.exception.strerror, 'vmodl.RuntimeFault')
  3966. def test_update_vm_wait_for_task(self):
  3967. mock_wait_for_task = MagicMock()
  3968. with patch('salt.utils.vmware.get_managed_object_name',
  3969. MagicMock(return_value='my_vm')):
  3970. with patch('salt.utils.vmware.wait_for_task', mock_wait_for_task):
  3971. salt.utils.vmware.update_vm(self.mock_vm_ref, self.mock_config_spec)
  3972. mock_wait_for_task.assert_called_once_with(
  3973. self.mock_task, 'my_vm', 'ReconfigureVM Task')
  3974. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  3975. @patch('salt.utils.vmware.get_managed_object_name', MagicMock())
  3976. @patch('salt.utils.vmware.wait_for_task', MagicMock())
  3977. class DeleteVirtualMachineTestCase(TestCase):
  3978. '''
  3979. Tests for salt.utils.vmware.delete_vm
  3980. '''
  3981. def setUp(self):
  3982. self.mock_task = MagicMock()
  3983. self.mock_vm_destroy_task = MagicMock(return_value=self.mock_task)
  3984. self.mock_vm_ref = MagicMock(Destroy_Task=self.mock_vm_destroy_task)
  3985. def test_destroy_vm_task_call(self):
  3986. salt.utils.vmware.delete_vm(self.mock_vm_ref)
  3987. self.assert_called_once(self.mock_vm_destroy_task)
  3988. def test_destroy_vm_raise_vim_fault(self):
  3989. exception = vim.fault.VimFault()
  3990. exception.msg = 'vim.fault.VimFault'
  3991. self.mock_vm_ref.Destroy_Task = MagicMock(side_effect=exception)
  3992. with self.assertRaises(VMwareApiError) as exc:
  3993. salt.utils.vmware.delete_vm(self.mock_vm_ref)
  3994. self.assertEqual(exc.exception.strerror, 'vim.fault.VimFault')
  3995. def test_destroy_vm_raise_runtime_fault(self):
  3996. exception = vmodl.RuntimeFault()
  3997. exception.msg = 'vmodl.RuntimeFault'
  3998. self.mock_vm_ref.Destroy_Task = MagicMock(side_effect=exception)
  3999. with self.assertRaises(VMwareRuntimeError) as exc:
  4000. salt.utils.vmware.delete_vm(self.mock_vm_ref)
  4001. self.assertEqual(exc.exception.strerror, 'vmodl.RuntimeFault')
  4002. def test_destroy_vm_wait_for_task(self):
  4003. mock_wait_for_task = MagicMock()
  4004. with patch('salt.utils.vmware.get_managed_object_name',
  4005. MagicMock(return_value='my_vm')):
  4006. with patch('salt.utils.vmware.wait_for_task', mock_wait_for_task):
  4007. salt.utils.vmware.delete_vm(self.mock_vm_ref)
  4008. mock_wait_for_task.assert_called_once_with(
  4009. self.mock_task, 'my_vm', 'Destroy Task')
  4010. @skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
  4011. @patch('salt.utils.vmware.get_managed_object_name', MagicMock())
  4012. class UnregisterVirtualMachineTestCase(TestCase):
  4013. '''
  4014. Tests for salt.utils.vmware.unregister_vm
  4015. '''
  4016. def setUp(self):
  4017. self.mock_vm_unregister = MagicMock()
  4018. self.mock_vm_ref = MagicMock(UnregisterVM=self.mock_vm_unregister)
  4019. def test_unregister_vm_task_call(self):
  4020. salt.utils.vmware.unregister_vm(self.mock_vm_ref)
  4021. self.assert_called_once(self.mock_vm_unregister)
  4022. def test_unregister_vm_raise_vim_fault(self):
  4023. exception = vim.fault.VimFault()
  4024. exception.msg = 'vim.fault.VimFault'
  4025. self.mock_vm_ref.UnregisterVM = MagicMock(side_effect=exception)
  4026. with self.assertRaises(VMwareApiError) as exc:
  4027. salt.utils.vmware.unregister_vm(self.mock_vm_ref)
  4028. self.assertEqual(exc.exception.strerror, 'vim.fault.VimFault')
  4029. def test_unregister_vm_raise_runtime_fault(self):
  4030. exception = vmodl.RuntimeFault()
  4031. exception.msg = 'vmodl.RuntimeFault'
  4032. self.mock_vm_ref.UnregisterVM = MagicMock(side_effect=exception)
  4033. with self.assertRaises(VMwareRuntimeError) as exc:
  4034. salt.utils.vmware.unregister_vm(self.mock_vm_ref)
  4035. self.assertEqual(exc.exception.strerror, 'vmodl.RuntimeFault')