1
0

test_vmware.py 204 KB

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