qvm_template.py 169 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097
  1. import re
  2. from unittest import mock
  3. import argparse
  4. import asyncio
  5. import datetime
  6. import io
  7. import os
  8. import pathlib
  9. import subprocess
  10. import tempfile
  11. import fcntl
  12. import rpm
  13. import qubesadmin.tests
  14. import qubesadmin.tools.qvm_template
  15. class re_str(str):
  16. def __eq__(self, other):
  17. return bool(re.match(self, other))
  18. def __hash__(self):
  19. return super().__hash__()
  20. class TC_00_qvm_template(qubesadmin.tests.QubesTestCase):
  21. def setUp(self):
  22. # Print str(list) directly so that the output is consistent no matter
  23. # which implementation of `column` we use
  24. self.mock_table = mock.patch('qubesadmin.tools.print_table')
  25. mock_table = self.mock_table.start()
  26. def print_table(table, *args):
  27. print(str(table))
  28. mock_table.side_effect = print_table
  29. super().setUp()
  30. def tearDown(self):
  31. self.mock_table.stop()
  32. super().tearDown()
  33. @mock.patch('rpm.TransactionSet')
  34. @mock.patch('subprocess.check_call')
  35. @mock.patch('subprocess.check_output')
  36. def test_000_verify_rpm_success(self, mock_proc, mock_call, mock_ts):
  37. # Just return a dict instead of rpm.hdr
  38. hdr = {
  39. rpm.RPMTAG_SIGPGP: 'xxx', # non-empty
  40. rpm.RPMTAG_SIGGPG: 'xxx', # non-empty
  41. rpm.RPMTAG_NAME: 'qubes-template-test-vm',
  42. }
  43. mock_ts.return_value.hdrFromFdno.return_value = hdr
  44. mock_proc.return_value = b'dummy.rpm: digests signatures OK\n'
  45. ret = qubesadmin.tools.qvm_template.verify_rpm('/dev/null',
  46. '/path/to/key', template_name='test-vm')
  47. mock_call.assert_called_once()
  48. mock_proc.assert_called_once()
  49. self.assertEqual(hdr, ret)
  50. self.assertAllCalled()
  51. @mock.patch('rpm.TransactionSet')
  52. @mock.patch('subprocess.check_call')
  53. @mock.patch('subprocess.check_output')
  54. def test_001_verify_rpm_nosig_fail(self, mock_proc, mock_call, mock_ts):
  55. # Just return a dict instead of rpm.hdr
  56. hdr = {
  57. rpm.RPMTAG_SIGPGP: None, # empty
  58. rpm.RPMTAG_SIGGPG: None, # empty
  59. }
  60. mock_ts.return_value.hdrFromFdno.return_value = hdr
  61. mock_proc.return_value = b'dummy.rpm: digests OK\n'
  62. with self.assertRaises(Exception) as e:
  63. qubesadmin.tools.qvm_template.verify_rpm('/dev/null',
  64. '/path/to/key')
  65. mock_call.assert_called_once()
  66. mock_proc.assert_called_once()
  67. self.assertIn('Signature verification failed', e.exception.args[0])
  68. mock_ts.assert_not_called()
  69. self.assertAllCalled()
  70. @mock.patch('rpm.TransactionSet')
  71. @mock.patch('subprocess.check_call')
  72. @mock.patch('subprocess.check_output')
  73. def test_002_verify_rpm_nosig_success(self, mock_proc, mock_call, mock_ts):
  74. # Just return a dict instead of rpm.hdr
  75. hdr = {
  76. rpm.RPMTAG_SIGPGP: None, # empty
  77. rpm.RPMTAG_SIGGPG: None, # empty
  78. }
  79. mock_ts.return_value.hdrFromFdno.return_value = hdr
  80. mock_proc.return_value = b'dummy.rpm: digests OK\n'
  81. ret = qubesadmin.tools.qvm_template.verify_rpm('/dev/null',
  82. '/path/to/key', True)
  83. mock_proc.assert_not_called()
  84. mock_call.assert_not_called()
  85. self.assertEqual(ret, hdr)
  86. self.assertAllCalled()
  87. @mock.patch('rpm.TransactionSet')
  88. @mock.patch('subprocess.check_call')
  89. @mock.patch('subprocess.check_output')
  90. def test_003_verify_rpm_badsig_fail(self, mock_proc, mock_call, mock_ts):
  91. mock_proc.side_effect = subprocess.CalledProcessError(1,
  92. ['rpmkeys', '--checksig'], b'/dev/null: digests SIGNATURES NOT OK\n')
  93. with self.assertRaises(Exception) as e:
  94. qubesadmin.tools.qvm_template.verify_rpm('/dev/null',
  95. '/path/to/key')
  96. mock_call.assert_called_once()
  97. mock_proc.assert_called_once()
  98. self.assertIn('Signature verification failed', e.exception.args[0])
  99. mock_ts.assert_not_called()
  100. self.assertAllCalled()
  101. @mock.patch('rpm.TransactionSet')
  102. @mock.patch('subprocess.check_call')
  103. @mock.patch('subprocess.check_output')
  104. def test_004_verify_rpm_badname(self, mock_proc, mock_call, mock_ts):
  105. mock_proc.return_value = b'/dev/null: digests signatures OK\n'
  106. hdr = {
  107. rpm.RPMTAG_SIGPGP: 'xxx', # non-empty
  108. rpm.RPMTAG_SIGGPG: 'xxx', # non-empty
  109. rpm.RPMTAG_NAME: 'qubes-template-unexpected',
  110. }
  111. mock_ts.return_value.hdrFromFdno.return_value = hdr
  112. with self.assertRaises(
  113. qubesadmin.tools.qvm_template.SignatureVerificationError) as e:
  114. qubesadmin.tools.qvm_template.verify_rpm('/dev/null',
  115. '/path/to/key', template_name='test-vm')
  116. mock_call.assert_called_once()
  117. mock_proc.assert_called_once()
  118. self.assertIn('package does not match expected template name',
  119. e.exception.args[0])
  120. mock_ts.assert_called_once()
  121. self.assertAllCalled()
  122. @mock.patch('subprocess.Popen')
  123. def test_010_extract_rpm_success(self, mock_popen):
  124. pipe = mock.Mock()
  125. mock_popen.return_value.stdout = pipe
  126. mock_popen.return_value.wait.return_value = 0
  127. with tempfile.NamedTemporaryFile() as fd, \
  128. tempfile.TemporaryDirectory() as dir:
  129. path = fd.name
  130. dirpath = dir
  131. ret = qubesadmin.tools.qvm_template.extract_rpm(
  132. 'test-vm', path, dirpath)
  133. self.assertEqual(ret, True)
  134. self.assertEqual(mock_popen.mock_calls, [
  135. mock.call(['rpm2cpio', path], stdout=subprocess.PIPE),
  136. mock.call([
  137. 'cpio',
  138. '-idm',
  139. '-D',
  140. dirpath,
  141. './var/lib/qubes/vm-templates/test-vm/*'
  142. ], stdin=pipe, stdout=subprocess.DEVNULL),
  143. mock.call().wait(),
  144. mock.call().wait()
  145. ])
  146. self.assertAllCalled()
  147. @mock.patch('subprocess.Popen')
  148. def test_011_extract_rpm_fail(self, mock_popen):
  149. pipe = mock.Mock()
  150. mock_popen.return_value.stdout = pipe
  151. mock_popen.return_value.wait.return_value = 1
  152. with tempfile.NamedTemporaryFile() as fd, \
  153. tempfile.TemporaryDirectory() as dir:
  154. path = fd.name
  155. dirpath = dir
  156. ret = qubesadmin.tools.qvm_template.extract_rpm(
  157. 'test-vm', path, dirpath)
  158. self.assertEqual(ret, False)
  159. self.assertEqual(mock_popen.mock_calls, [
  160. mock.call(['rpm2cpio', path], stdout=subprocess.PIPE),
  161. mock.call([
  162. 'cpio',
  163. '-idm',
  164. '-D',
  165. dirpath,
  166. './var/lib/qubes/vm-templates/test-vm/*'
  167. ], stdin=pipe, stdout=subprocess.DEVNULL),
  168. mock.call().wait()
  169. ])
  170. self.assertAllCalled()
  171. @mock.patch('qubesadmin.tools.qvm_template.get_keys_for_repos')
  172. def test_090_install_lock(self, mock_get_keys):
  173. class SuccessError(Exception):
  174. pass
  175. mock_get_keys.side_effect = SuccessError
  176. with mock.patch('qubesadmin.tools.qvm_template.LOCK_FILE', '/tmp/test.lock'):
  177. with self.subTest('not locked'):
  178. with self.assertRaises(SuccessError):
  179. # args don't matter
  180. qubesadmin.tools.qvm_template.install(mock.MagicMock(), None)
  181. self.assertFalse(os.path.exists('/tmp/test.lock'))
  182. with self.subTest('lock exists but unlocked'):
  183. with open('/tmp/test.lock', 'w') as f:
  184. with self.assertRaises(SuccessError):
  185. # args don't matter
  186. qubesadmin.tools.qvm_template.install(mock.MagicMock(), None)
  187. self.assertFalse(os.path.exists('/tmp/test.lock'))
  188. with self.subTest('locked'):
  189. with open('/tmp/test.lock', 'w') as f:
  190. fcntl.flock(f, fcntl.LOCK_EX)
  191. with self.assertRaises(
  192. qubesadmin.tools.qvm_template.AlreadyRunning):
  193. # args don't matter
  194. qubesadmin.tools.qvm_template.install(mock.MagicMock(), None)
  195. # and not cleaned up then
  196. self.assertTrue(os.path.exists('/tmp/test.lock'))
  197. def add_new_vm_side_effect(self, *args, **kwargs):
  198. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  199. b'0\0test-vm class=TemplateVM state=Halted\n'
  200. self.app.domains.clear_cache()
  201. return self.app.domains['test-vm']
  202. @mock.patch('os.rename')
  203. @mock.patch('os.makedirs')
  204. @mock.patch('subprocess.check_call')
  205. @mock.patch('qubesadmin.tools.qvm_template.confirm_action')
  206. @mock.patch('qubesadmin.tools.qvm_template.extract_rpm')
  207. @mock.patch('qubesadmin.tools.qvm_template.download')
  208. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  209. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  210. def test_100_install_local_success(
  211. self,
  212. mock_verify,
  213. mock_dl_list,
  214. mock_dl,
  215. mock_extract,
  216. mock_confirm,
  217. mock_call,
  218. mock_mkdirs,
  219. mock_rename):
  220. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = b'0\0'
  221. build_time = '2020-09-01 14:30:00' # 1598970600
  222. install_time = '2020-09-01 15:30:00'
  223. for key, val in [
  224. ('name', 'test-vm'),
  225. ('epoch', '2'),
  226. ('version', '4.1'),
  227. ('release', '2020'),
  228. ('reponame', '@commandline'),
  229. ('buildtime', build_time),
  230. ('installtime', install_time),
  231. ('license', 'GPL'),
  232. ('url', 'https://qubes-os.org'),
  233. ('summary', 'Summary'),
  234. ('description', 'Desc|desc')]:
  235. self.app.expected_calls[(
  236. 'test-vm',
  237. 'admin.vm.feature.Set',
  238. f'template-{key}',
  239. val.encode())] = b'0\0'
  240. mock_verify.return_value = {
  241. rpm.RPMTAG_NAME : 'qubes-template-test-vm',
  242. rpm.RPMTAG_BUILDTIME : 1598970600,
  243. rpm.RPMTAG_DESCRIPTION : 'Desc\ndesc',
  244. rpm.RPMTAG_EPOCHNUM : 2,
  245. rpm.RPMTAG_LICENSE : 'GPL',
  246. rpm.RPMTAG_RELEASE : '2020',
  247. rpm.RPMTAG_SUMMARY : 'Summary',
  248. rpm.RPMTAG_URL : 'https://qubes-os.org',
  249. rpm.RPMTAG_VERSION : '4.1'
  250. }
  251. mock_dl_list.return_value = {}
  252. mock_call.side_effect = self.add_new_vm_side_effect
  253. mock_time = mock.Mock(wraps=datetime.datetime)
  254. mock_time.now.return_value = \
  255. datetime.datetime(2020, 9, 1, 15, 30, tzinfo=datetime.timezone.utc)
  256. with mock.patch('qubesadmin.tools.qvm_template.LOCK_FILE', '/tmp/test.lock'), \
  257. mock.patch('datetime.datetime', new=mock_time), \
  258. mock.patch('tempfile.TemporaryDirectory') as mock_tmpdir, \
  259. mock.patch('sys.stderr', new=io.StringIO()) as mock_err, \
  260. tempfile.NamedTemporaryFile(suffix='.rpm') as template_file:
  261. path = template_file.name
  262. args = argparse.Namespace(
  263. templates=[path],
  264. keyring='/tmp/keyring.gpg',
  265. nogpgcheck=False,
  266. cachedir='/var/cache/qvm-template',
  267. repo_files=[],
  268. releasever='4.1',
  269. yes=False,
  270. allow_pv=False,
  271. pool=None
  272. )
  273. mock_tmpdir.return_value.__enter__.return_value = \
  274. '/var/tmp/qvm-template-tmpdir'
  275. qubesadmin.tools.qvm_template.install(args, self.app)
  276. # Attempt to get download list
  277. selector = qubesadmin.tools.qvm_template.VersionSelector.LATEST
  278. self.assertEqual(mock_dl_list.mock_calls, [
  279. mock.call(args, self.app, version_selector=selector)
  280. ])
  281. # Nothing downloaded
  282. mock_dl.assert_called_with(args, self.app,
  283. dl_list={}, version_selector=selector)
  284. mock_verify.assert_called_once_with(template_file.name, '/tmp/keyring.gpg',
  285. nogpgcheck=False)
  286. # Package is extracted
  287. mock_extract.assert_called_with('test-vm', path,
  288. '/var/tmp/qvm-template-tmpdir')
  289. # No packages overwritten, so no confirm needed
  290. self.assertEqual(mock_confirm.mock_calls, [])
  291. # qvm-template-postprocess is called
  292. self.assertEqual(mock_call.mock_calls, [
  293. mock.call([
  294. 'qvm-template-postprocess',
  295. '--really',
  296. '--no-installed-by-rpm',
  297. 'post-install',
  298. 'test-vm',
  299. '/var/tmp/qvm-template-tmpdir'
  300. '/var/lib/qubes/vm-templates/test-vm'
  301. ])
  302. ])
  303. # Cache directory created
  304. self.assertEqual(mock_mkdirs.mock_calls, [
  305. mock.call(args.cachedir, exist_ok=True)
  306. ])
  307. # No templates downloaded, thus no renames needed
  308. self.assertEqual(mock_rename.mock_calls, [])
  309. self.assertAllCalled()
  310. @mock.patch('os.rename')
  311. @mock.patch('os.makedirs')
  312. @mock.patch('subprocess.check_call')
  313. @mock.patch('qubesadmin.tools.qvm_template.confirm_action')
  314. @mock.patch('qubesadmin.tools.qvm_template.extract_rpm')
  315. @mock.patch('qubesadmin.tools.qvm_template.download')
  316. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  317. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  318. def test_101_install_local_postprocargs_success(
  319. self,
  320. mock_verify,
  321. mock_dl_list,
  322. mock_dl,
  323. mock_extract,
  324. mock_confirm,
  325. mock_call,
  326. mock_mkdirs,
  327. mock_rename):
  328. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = b'0\0'
  329. build_time = '2020-09-01 14:30:00' # 1598970600
  330. install_time = '2020-09-01 15:30:00'
  331. for key, val in [
  332. ('name', 'test-vm'),
  333. ('epoch', '2'),
  334. ('version', '4.1'),
  335. ('release', '2020'),
  336. ('reponame', '@commandline'),
  337. ('buildtime', build_time),
  338. ('installtime', install_time),
  339. ('license', 'GPL'),
  340. ('url', 'https://qubes-os.org'),
  341. ('summary', 'Summary'),
  342. ('description', 'Desc|desc')]:
  343. self.app.expected_calls[(
  344. 'test-vm',
  345. 'admin.vm.feature.Set',
  346. f'template-{key}',
  347. val.encode())] = b'0\0'
  348. mock_verify.return_value = {
  349. rpm.RPMTAG_NAME : 'qubes-template-test-vm',
  350. rpm.RPMTAG_BUILDTIME : 1598970600,
  351. rpm.RPMTAG_DESCRIPTION : 'Desc\ndesc',
  352. rpm.RPMTAG_EPOCHNUM : 2,
  353. rpm.RPMTAG_LICENSE : 'GPL',
  354. rpm.RPMTAG_RELEASE : '2020',
  355. rpm.RPMTAG_SUMMARY : 'Summary',
  356. rpm.RPMTAG_URL : 'https://qubes-os.org',
  357. rpm.RPMTAG_VERSION : '4.1'
  358. }
  359. mock_dl_list.return_value = {}
  360. mock_call.side_effect = self.add_new_vm_side_effect
  361. mock_time = mock.Mock(wraps=datetime.datetime)
  362. mock_time.now.return_value = \
  363. datetime.datetime(2020, 9, 1, 15, 30, tzinfo=datetime.timezone.utc)
  364. with mock.patch('qubesadmin.tools.qvm_template.LOCK_FILE', '/tmp/test.lock'), \
  365. mock.patch('datetime.datetime', new=mock_time), \
  366. mock.patch('tempfile.TemporaryDirectory') as mock_tmpdir, \
  367. mock.patch('sys.stderr', new=io.StringIO()) as mock_err, \
  368. tempfile.NamedTemporaryFile(suffix='.rpm') as template_file:
  369. path = template_file.name
  370. args = argparse.Namespace(
  371. templates=[path],
  372. keyring='/tmp',
  373. nogpgcheck=False,
  374. cachedir='/var/cache/qvm-template',
  375. repo_files=[],
  376. releasever='4.1',
  377. yes=False,
  378. allow_pv=True,
  379. pool='my-pool'
  380. )
  381. mock_tmpdir.return_value.__enter__.return_value = \
  382. '/var/tmp/qvm-template-tmpdir'
  383. qubesadmin.tools.qvm_template.install(args, self.app)
  384. # Attempt to get download list
  385. selector = qubesadmin.tools.qvm_template.VersionSelector.LATEST
  386. self.assertEqual(mock_dl_list.mock_calls, [
  387. mock.call(args, self.app, version_selector=selector)
  388. ])
  389. # Nothing downloaded
  390. mock_dl.assert_called_with(args, self.app,
  391. dl_list={}, version_selector=selector)
  392. # Package is extracted
  393. mock_extract.assert_called_with('test-vm', path,
  394. '/var/tmp/qvm-template-tmpdir')
  395. # No packages overwritten, so no confirm needed
  396. self.assertEqual(mock_confirm.mock_calls, [])
  397. # qvm-template-postprocess is called
  398. self.assertEqual(mock_call.mock_calls, [
  399. mock.call([
  400. 'qvm-template-postprocess',
  401. '--really',
  402. '--no-installed-by-rpm',
  403. '--allow-pv',
  404. '--pool',
  405. 'my-pool',
  406. 'post-install',
  407. 'test-vm',
  408. '/var/tmp/qvm-template-tmpdir'
  409. '/var/lib/qubes/vm-templates/test-vm'
  410. ])
  411. ])
  412. # Cache directory created
  413. self.assertEqual(mock_mkdirs.mock_calls, [
  414. mock.call(args.cachedir, exist_ok=True)
  415. ])
  416. # No templates downloaded, thus no renames needed
  417. self.assertEqual(mock_rename.mock_calls, [])
  418. self.assertAllCalled()
  419. @mock.patch('os.rename')
  420. @mock.patch('os.makedirs')
  421. @mock.patch('subprocess.check_call')
  422. @mock.patch('qubesadmin.tools.qvm_template.confirm_action')
  423. @mock.patch('qubesadmin.tools.qvm_template.extract_rpm')
  424. @mock.patch('qubesadmin.tools.qvm_template.download')
  425. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  426. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  427. def test_102_install_local_badsig_fail(
  428. self,
  429. mock_verify,
  430. mock_dl_list,
  431. mock_dl,
  432. mock_extract,
  433. mock_confirm,
  434. mock_call,
  435. mock_mkdirs,
  436. mock_rename):
  437. mock_verify.return_value = None
  438. mock_time = mock.Mock(wraps=datetime.datetime)
  439. with mock.patch('qubesadmin.tools.qvm_template.LOCK_FILE', '/tmp/test.lock'), \
  440. mock.patch('datetime.datetime', new=mock_time), \
  441. mock.patch('tempfile.TemporaryDirectory') as mock_tmpdir, \
  442. mock.patch('sys.stderr', new=io.StringIO()) as mock_err, \
  443. tempfile.NamedTemporaryFile(suffix='.rpm') as template_file:
  444. path = template_file.name
  445. args = argparse.Namespace(
  446. templates=[path],
  447. keyring='/tmp',
  448. nogpgcheck=False,
  449. cachedir='/var/cache/qvm-template',
  450. repo_files=[],
  451. releasever='4.1',
  452. yes=False,
  453. allow_pv=False,
  454. pool=None
  455. )
  456. mock_tmpdir.return_value.__enter__.return_value = \
  457. '/var/tmp/qvm-template-tmpdir'
  458. # Should raise parser.error
  459. with self.assertRaises(SystemExit):
  460. qubesadmin.tools.qvm_template.install(args, self.app)
  461. # Check error message
  462. self.assertTrue('verification failed' in mock_err.getvalue())
  463. # Should not be executed:
  464. self.assertEqual(mock_dl_list.mock_calls, [])
  465. self.assertEqual(mock_dl.mock_calls, [])
  466. self.assertEqual(mock_extract.mock_calls, [])
  467. self.assertEqual(mock_confirm.mock_calls, [])
  468. self.assertEqual(mock_call.mock_calls, [])
  469. self.assertEqual(mock_rename.mock_calls, [])
  470. self.assertAllCalled()
  471. @mock.patch('os.rename')
  472. @mock.patch('os.makedirs')
  473. @mock.patch('subprocess.check_call')
  474. @mock.patch('qubesadmin.tools.qvm_template.confirm_action')
  475. @mock.patch('qubesadmin.tools.qvm_template.extract_rpm')
  476. @mock.patch('qubesadmin.tools.qvm_template.download')
  477. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  478. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  479. def test_103_install_local_exists_fail(
  480. self,
  481. mock_verify,
  482. mock_dl_list,
  483. mock_dl,
  484. mock_extract,
  485. mock_confirm,
  486. mock_call,
  487. mock_mkdirs,
  488. mock_rename):
  489. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  490. b'0\0test-vm class=TemplateVM state=Halted\n'
  491. mock_verify.return_value = {
  492. rpm.RPMTAG_NAME : 'qubes-template-test-vm',
  493. rpm.RPMTAG_BUILDTIME : 1598970600,
  494. rpm.RPMTAG_DESCRIPTION : 'Desc\ndesc',
  495. rpm.RPMTAG_EPOCHNUM : 2,
  496. rpm.RPMTAG_LICENSE : 'GPL',
  497. rpm.RPMTAG_RELEASE : '2020',
  498. rpm.RPMTAG_SUMMARY : 'Summary',
  499. rpm.RPMTAG_URL : 'https://qubes-os.org',
  500. rpm.RPMTAG_VERSION : '4.1'
  501. }
  502. mock_dl_list.return_value = {}
  503. mock_time = mock.Mock(wraps=datetime.datetime)
  504. with mock.patch('qubesadmin.tools.qvm_template.LOCK_FILE', '/tmp/test.lock'), \
  505. mock.patch('datetime.datetime', new=mock_time), \
  506. mock.patch('tempfile.TemporaryDirectory') as mock_tmpdir, \
  507. mock.patch('sys.stderr', new=io.StringIO()) as mock_err, \
  508. tempfile.NamedTemporaryFile(suffix='.rpm') as template_file:
  509. path = template_file.name
  510. args = argparse.Namespace(
  511. templates=[path],
  512. keyring='/tmp',
  513. nogpgcheck=False,
  514. cachedir='/var/cache/qvm-template',
  515. repo_files=[],
  516. releasever='4.1',
  517. yes=False,
  518. allow_pv=False,
  519. pool=None
  520. )
  521. mock_tmpdir.return_value.__enter__.return_value = \
  522. '/var/tmp/qvm-template-tmpdir'
  523. qubesadmin.tools.qvm_template.install(args, self.app)
  524. # Check warning message
  525. self.assertTrue('already installed' in mock_err.getvalue())
  526. # Attempt to get download list
  527. selector = qubesadmin.tools.qvm_template.VersionSelector.LATEST
  528. self.assertEqual(mock_dl_list.mock_calls, [
  529. mock.call(args, self.app, version_selector=selector)
  530. ])
  531. # Nothing downloaded
  532. self.assertEqual(mock_dl.mock_calls, [
  533. mock.call(args, self.app,
  534. dl_list={}, version_selector=selector)
  535. ])
  536. # Should not be executed:
  537. self.assertEqual(mock_extract.mock_calls, [])
  538. self.assertEqual(mock_confirm.mock_calls, [])
  539. self.assertEqual(mock_call.mock_calls, [])
  540. self.assertEqual(mock_rename.mock_calls, [])
  541. self.assertAllCalled()
  542. @mock.patch('os.rename')
  543. @mock.patch('os.makedirs')
  544. @mock.patch('subprocess.check_call')
  545. @mock.patch('qubesadmin.tools.qvm_template.confirm_action')
  546. @mock.patch('qubesadmin.tools.qvm_template.extract_rpm')
  547. @mock.patch('qubesadmin.tools.qvm_template.download')
  548. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  549. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  550. def test_104_install_local_badpkgname_fail(
  551. self,
  552. mock_verify,
  553. mock_dl_list,
  554. mock_dl,
  555. mock_extract,
  556. mock_confirm,
  557. mock_call,
  558. mock_mkdirs,
  559. mock_rename):
  560. mock_verify.return_value = {
  561. rpm.RPMTAG_NAME : 'Xqubes-template-test-vm',
  562. rpm.RPMTAG_BUILDTIME : 1598970600,
  563. rpm.RPMTAG_DESCRIPTION : 'Desc\ndesc',
  564. rpm.RPMTAG_EPOCHNUM : 2,
  565. rpm.RPMTAG_LICENSE : 'GPL',
  566. rpm.RPMTAG_RELEASE : '2020',
  567. rpm.RPMTAG_SUMMARY : 'Summary',
  568. rpm.RPMTAG_URL : 'https://qubes-os.org',
  569. rpm.RPMTAG_VERSION : '4.1'
  570. }
  571. mock_time = mock.Mock(wraps=datetime.datetime)
  572. with mock.patch('qubesadmin.tools.qvm_template.LOCK_FILE', '/tmp/test.lock'), \
  573. mock.patch('datetime.datetime', new=mock_time), \
  574. mock.patch('tempfile.TemporaryDirectory') as mock_tmpdir, \
  575. mock.patch('sys.stderr', new=io.StringIO()) as mock_err, \
  576. tempfile.NamedTemporaryFile(suffix='.rpm') as template_file:
  577. path = template_file.name
  578. args = argparse.Namespace(
  579. templates=[path],
  580. keyring='/tmp',
  581. nogpgcheck=False,
  582. cachedir='/var/cache/qvm-template',
  583. repo_files=[],
  584. releasever='4.1',
  585. yes=False,
  586. allow_pv=False,
  587. pool=None
  588. )
  589. mock_tmpdir.return_value.__enter__.return_value = \
  590. '/var/tmp/qvm-template-tmpdir'
  591. with self.assertRaises(SystemExit):
  592. qubesadmin.tools.qvm_template.install(args, self.app)
  593. # Check error message
  594. self.assertTrue('Illegal package name' in mock_err.getvalue())
  595. # Should not be executed:
  596. self.assertEqual(mock_dl_list.mock_calls, [])
  597. self.assertEqual(mock_dl.mock_calls, [])
  598. self.assertEqual(mock_extract.mock_calls, [])
  599. self.assertEqual(mock_confirm.mock_calls, [])
  600. self.assertEqual(mock_call.mock_calls, [])
  601. self.assertEqual(mock_rename.mock_calls, [])
  602. self.assertAllCalled()
  603. @mock.patch('os.rename')
  604. @mock.patch('os.makedirs')
  605. @mock.patch('subprocess.check_call')
  606. @mock.patch('qubesadmin.tools.qvm_template.confirm_action')
  607. @mock.patch('qubesadmin.tools.qvm_template.extract_rpm')
  608. @mock.patch('qubesadmin.tools.qvm_template.download')
  609. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  610. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  611. def test_106_install_local_badpath_fail(
  612. self,
  613. mock_verify,
  614. mock_dl_list,
  615. mock_dl,
  616. mock_extract,
  617. mock_confirm,
  618. mock_call,
  619. mock_mkdirs,
  620. mock_rename):
  621. mock_time = mock.Mock(wraps=datetime.datetime)
  622. with mock.patch('qubesadmin.tools.qvm_template.LOCK_FILE', '/tmp/test.lock'), \
  623. mock.patch('datetime.datetime', new=mock_time), \
  624. mock.patch('tempfile.TemporaryDirectory') as mock_tmpdir, \
  625. mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  626. path = '/var/tmp/ShOulD-NoT-ExIsT.rpm'
  627. args = argparse.Namespace(
  628. templates=[path],
  629. keyring='/tmp',
  630. nogpgcheck=False,
  631. cachedir='/var/cache/qvm-template',
  632. repo_files=[],
  633. releasever='4.1',
  634. yes=False,
  635. allow_pv=False,
  636. pool=None
  637. )
  638. mock_tmpdir.return_value.__enter__.return_value = \
  639. '/var/tmp/qvm-template-tmpdir'
  640. with self.assertRaises(SystemExit):
  641. qubesadmin.tools.qvm_template.install(args, self.app)
  642. # Check error message
  643. self.assertTrue(f"RPM file '{path}' not found" \
  644. in mock_err.getvalue())
  645. # Should not be executed:
  646. self.assertEqual(mock_verify.mock_calls, [])
  647. self.assertEqual(mock_dl_list.mock_calls, [])
  648. self.assertEqual(mock_dl.mock_calls, [])
  649. self.assertEqual(mock_extract.mock_calls, [])
  650. self.assertEqual(mock_confirm.mock_calls, [])
  651. self.assertEqual(mock_call.mock_calls, [])
  652. self.assertEqual(mock_rename.mock_calls, [])
  653. self.assertAllCalled()
  654. @mock.patch('os.rename')
  655. @mock.patch('os.makedirs')
  656. @mock.patch('subprocess.check_call')
  657. @mock.patch('qubesadmin.tools.qvm_template.confirm_action')
  658. @mock.patch('qubesadmin.tools.qvm_template.extract_rpm')
  659. @mock.patch('qubesadmin.tools.qvm_template.download')
  660. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  661. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  662. def test_107_install_download_success(
  663. self,
  664. mock_verify,
  665. mock_dl_list,
  666. mock_dl,
  667. mock_extract,
  668. mock_confirm,
  669. mock_call,
  670. mock_mkdirs,
  671. mock_rename):
  672. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = b'0\0'
  673. build_time = '2020-09-01 14:30:00' # 1598970600
  674. install_time = '2020-09-01 15:30:00'
  675. for key, val in [
  676. ('name', 'test-vm'),
  677. ('epoch', '2'),
  678. ('version', '4.1'),
  679. ('release', '2020'),
  680. ('reponame', 'qubes-templates-itl'),
  681. ('buildtime', build_time),
  682. ('installtime', install_time),
  683. ('license', 'GPL'),
  684. ('url', 'https://qubes-os.org'),
  685. ('summary', 'Summary'),
  686. ('description', 'Desc|desc')]:
  687. self.app.expected_calls[(
  688. 'test-vm',
  689. 'admin.vm.feature.Set',
  690. f'template-{key}',
  691. val.encode())] = b'0\0'
  692. mock_dl.return_value = {'test-vm': {
  693. rpm.RPMTAG_NAME : 'qubes-template-test-vm',
  694. rpm.RPMTAG_BUILDTIME : 1598970600,
  695. rpm.RPMTAG_DESCRIPTION : 'Desc\ndesc',
  696. rpm.RPMTAG_EPOCHNUM : 2,
  697. rpm.RPMTAG_LICENSE : 'GPL',
  698. rpm.RPMTAG_RELEASE : '2020',
  699. rpm.RPMTAG_SUMMARY : 'Summary',
  700. rpm.RPMTAG_URL : 'https://qubes-os.org',
  701. rpm.RPMTAG_VERSION : '4.1'
  702. }}
  703. dl_list = {
  704. 'test-vm': qubesadmin.tools.qvm_template.DlEntry(
  705. ('1', '4.1', '20200101'), 'qubes-templates-itl', 1048576)
  706. }
  707. mock_dl_list.return_value = dl_list
  708. mock_call.side_effect = self.add_new_vm_side_effect
  709. mock_time = mock.Mock(wraps=datetime.datetime)
  710. mock_time.now.return_value = \
  711. datetime.datetime(2020, 9, 1, 15, 30, tzinfo=datetime.timezone.utc)
  712. with mock.patch('qubesadmin.tools.qvm_template.LOCK_FILE', '/tmp/test.lock'), \
  713. mock.patch('datetime.datetime', new=mock_time), \
  714. mock.patch('tempfile.TemporaryDirectory') as mock_tmpdir, \
  715. mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  716. args = argparse.Namespace(
  717. templates='test-vm',
  718. keyring='/tmp/keyring.gpg',
  719. nogpgcheck=False,
  720. cachedir='/var/cache/qvm-template',
  721. repo_files=[],
  722. releasever='4.1',
  723. yes=False,
  724. keep_cache=True,
  725. allow_pv=False,
  726. pool=None
  727. )
  728. mock_tmpdir.return_value.__enter__.return_value = \
  729. '/var/tmp/qvm-template-tmpdir'
  730. qubesadmin.tools.qvm_template.install(args, self.app)
  731. # Attempt to get download list
  732. selector = qubesadmin.tools.qvm_template.VersionSelector.LATEST
  733. self.assertEqual(mock_dl_list.mock_calls, [
  734. mock.call(args, self.app, version_selector=selector)
  735. ])
  736. # Nothing downloaded
  737. mock_dl.assert_called_with(args, self.app,
  738. dl_list=dl_list, version_selector=selector)
  739. # download already verify the package internally
  740. self.assertEqual(mock_verify.mock_calls, [])
  741. # Package is extracted
  742. mock_extract.assert_called_with('test-vm',
  743. '/var/cache/qvm-template/qubes-template-test-vm-1:4.1-20200101.rpm',
  744. '/var/tmp/qvm-template-tmpdir')
  745. # No packages overwritten, so no confirm needed
  746. self.assertEqual(mock_confirm.mock_calls, [])
  747. # qvm-template-postprocess is called
  748. self.assertEqual(mock_call.mock_calls, [
  749. mock.call([
  750. 'qvm-template-postprocess',
  751. '--really',
  752. '--no-installed-by-rpm',
  753. 'post-install',
  754. 'test-vm',
  755. '/var/tmp/qvm-template-tmpdir'
  756. '/var/lib/qubes/vm-templates/test-vm'
  757. ])
  758. ])
  759. # Cache directory created
  760. self.assertEqual(mock_mkdirs.mock_calls, [
  761. mock.call(args.cachedir, exist_ok=True)
  762. ])
  763. # No templates downloaded, thus no renames needed
  764. self.assertEqual(mock_rename.mock_calls, [])
  765. self.assertAllCalled()
  766. def test_110_qrexec_payload_refresh_success(self):
  767. with tempfile.NamedTemporaryFile() as repo_conf1, \
  768. tempfile.NamedTemporaryFile() as repo_conf2:
  769. repo_str1 = \
  770. '''[qubes-templates-itl]
  771. name = Qubes Templates repository
  772. #baseurl = https://yum.qubes-os.org/r$releasever/templates-itl
  773. #baseurl = http://yum.qubesosfasa4zl44o4tws22di6kepyzfeqv3tg4e3ztknltfxqrymdad.onion/r$releasever/templates-itl
  774. metalink = https://yum.qubes-os.org/r$releasever/templates-itl/repodata/repomd.xml.metalink
  775. enabled = 1
  776. fastestmirror = 1
  777. metadata_expire = 7d
  778. gpgcheck = 1
  779. gpgkey = file:///etc/qubes/repo-templates/keys/RPM-GPG-KEY-qubes-$releasever-primary
  780. '''
  781. repo_str2 = \
  782. '''[qubes-templates-itl-testing]
  783. name = Qubes Templates repository
  784. #baseurl = https://yum.qubes-os.org/r$releasever/templates-itl-testing
  785. #baseurl = http://yum.qubesosfasa4zl44o4tws22di6kepyzfeqv3tg4e3ztknltfxqrymdad.onion/r$releasever/templates-itl-testing
  786. metalink = https://yum.qubes-os.org/r$releasever/templates-itl-testing/repodata/repomd.xml.metalink
  787. enabled = 0
  788. fastestmirror = 1
  789. gpgcheck = 1
  790. gpgkey = file:///etc/qubes/repo-templates/keys/RPM-GPG-KEY-qubes-$releasever-primary
  791. '''
  792. repo_conf1.write(repo_str1.encode())
  793. repo_conf1.flush()
  794. repo_conf2.write(repo_str2.encode())
  795. repo_conf2.flush()
  796. args = argparse.Namespace(
  797. enablerepo=['repo1', 'repo2'],
  798. disablerepo=['repo3', 'repo4', 'repo5'],
  799. repoid=[],
  800. releasever='4.1',
  801. repo_files=[repo_conf1.name, repo_conf2.name]
  802. )
  803. res = qubesadmin.tools.qvm_template.qrexec_payload(args, self.app,
  804. 'qubes-template-fedora-32', True)
  805. self.assertEqual(res,
  806. '''--enablerepo=repo1
  807. --enablerepo=repo2
  808. --disablerepo=repo3
  809. --disablerepo=repo4
  810. --disablerepo=repo5
  811. --refresh
  812. --releasever=4.1
  813. qubes-template-fedora-32
  814. ---
  815. ''' + repo_str1 + '\n' + repo_str2 + '\n')
  816. self.assertAllCalled()
  817. def test_111_qrexec_payload_norefresh_success(self):
  818. with tempfile.NamedTemporaryFile() as repo_conf1:
  819. repo_str1 = \
  820. '''[qubes-templates-itl]
  821. name = Qubes Templates repository
  822. #baseurl = https://yum.qubes-os.org/r$releasever/templates-itl
  823. #baseurl = http://yum.qubesosfasa4zl44o4tws22di6kepyzfeqv3tg4e3ztknltfxqrymdad.onion/r$releasever/templates-itl
  824. metalink = https://yum.qubes-os.org/r$releasever/templates-itl/repodata/repomd.xml.metalink
  825. enabled = 1
  826. fastestmirror = 1
  827. metadata_expire = 7d
  828. gpgcheck = 1
  829. gpgkey = file:///etc/qubes/repo-templates/keys/RPM-GPG-KEY-qubes-$releasever-primary
  830. '''
  831. repo_conf1.write(repo_str1.encode())
  832. repo_conf1.flush()
  833. args = argparse.Namespace(
  834. enablerepo=[],
  835. disablerepo=[],
  836. repoid=['repo1', 'repo2'],
  837. releasever='4.1',
  838. repo_files=[repo_conf1.name]
  839. )
  840. res = qubesadmin.tools.qvm_template.qrexec_payload(args, self.app,
  841. 'qubes-template-fedora-32', False)
  842. self.assertEqual(res,
  843. '''--repoid=repo1
  844. --repoid=repo2
  845. --releasever=4.1
  846. qubes-template-fedora-32
  847. ---
  848. ''' + repo_str1 + '\n')
  849. self.assertAllCalled()
  850. def test_112_qrexec_payload_specnewline_fail(self):
  851. with tempfile.NamedTemporaryFile() as repo_conf1:
  852. repo_str1 = \
  853. '''[qubes-templates-itl]
  854. name = Qubes Templates repository
  855. #baseurl = https://yum.qubes-os.org/r$releasever/templates-itl
  856. #baseurl = http://yum.qubesosfasa4zl44o4tws22di6kepyzfeqv3tg4e3ztknltfxqrymdad.onion/r$releasever/templates-itl
  857. metalink = https://yum.qubes-os.org/r$releasever/templates-itl/repodata/repomd.xml.metalink
  858. enabled = 1
  859. fastestmirror = 1
  860. metadata_expire = 7d
  861. gpgcheck = 1
  862. gpgkey = file:///etc/qubes/repo-templates/keys/RPM-GPG-KEY-qubes-$releasever-primary
  863. '''
  864. repo_conf1.write(repo_str1.encode())
  865. repo_conf1.flush()
  866. args = argparse.Namespace(
  867. enablerepo=[],
  868. disablerepo=[],
  869. repoid=['repo1', 'repo2'],
  870. releasever='4.1',
  871. repo_files=[repo_conf1.name]
  872. )
  873. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  874. with self.assertRaises(SystemExit):
  875. qubesadmin.tools.qvm_template.qrexec_payload(args,
  876. self.app, 'qubes-template-fedora\n-32', False)
  877. # Check error message
  878. self.assertTrue('Malformed template name'
  879. in mock_err.getvalue())
  880. self.assertTrue("argument should not contain '\\n'"
  881. in mock_err.getvalue())
  882. self.assertAllCalled()
  883. def test_113_qrexec_payload_enablereponewline_fail(self):
  884. with tempfile.NamedTemporaryFile() as repo_conf1:
  885. repo_str1 = \
  886. '''[qubes-templates-itl]
  887. name = Qubes Templates repository
  888. #baseurl = https://yum.qubes-os.org/r$releasever/templates-itl
  889. #baseurl = http://yum.qubesosfasa4zl44o4tws22di6kepyzfeqv3tg4e3ztknltfxqrymdad.onion/r$releasever/templates-itl
  890. metalink = https://yum.qubes-os.org/r$releasever/templates-itl/repodata/repomd.xml.metalink
  891. enabled = 1
  892. fastestmirror = 1
  893. metadata_expire = 7d
  894. gpgcheck = 1
  895. gpgkey = file:///etc/qubes/repo-templates/keys/RPM-GPG-KEY-qubes-$releasever-primary
  896. '''
  897. repo_conf1.write(repo_str1.encode())
  898. repo_conf1.flush()
  899. args = argparse.Namespace(
  900. enablerepo=['repo\n0'],
  901. disablerepo=[],
  902. repoid=['repo1', 'repo2'],
  903. releasever='4.1',
  904. repo_files=[repo_conf1.name]
  905. )
  906. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  907. with self.assertRaises(SystemExit):
  908. qubesadmin.tools.qvm_template.qrexec_payload(args,
  909. self.app, 'qubes-template-fedora-32', False)
  910. # Check error message
  911. self.assertTrue('Malformed --enablerepo'
  912. in mock_err.getvalue())
  913. self.assertTrue("argument should not contain '\\n'"
  914. in mock_err.getvalue())
  915. self.assertAllCalled()
  916. def test_114_qrexec_payload_disablereponewline_fail(self):
  917. with tempfile.NamedTemporaryFile() as repo_conf1:
  918. repo_str1 = \
  919. '''[qubes-templates-itl]
  920. name = Qubes Templates repository
  921. #baseurl = https://yum.qubes-os.org/r$releasever/templates-itl
  922. #baseurl = http://yum.qubesosfasa4zl44o4tws22di6kepyzfeqv3tg4e3ztknltfxqrymdad.onion/r$releasever/templates-itl
  923. metalink = https://yum.qubes-os.org/r$releasever/templates-itl/repodata/repomd.xml.metalink
  924. enabled = 1
  925. fastestmirror = 1
  926. metadata_expire = 7d
  927. gpgcheck = 1
  928. gpgkey = file:///etc/qubes/repo-templates/keys/RPM-GPG-KEY-qubes-$releasever-primary
  929. '''
  930. repo_conf1.write(repo_str1.encode())
  931. repo_conf1.flush()
  932. args = argparse.Namespace(
  933. enablerepo=[],
  934. disablerepo=['repo\n0'],
  935. repoid=['repo1', 'repo2'],
  936. releasever='4.1',
  937. repo_files=[repo_conf1.name]
  938. )
  939. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  940. with self.assertRaises(SystemExit):
  941. qubesadmin.tools.qvm_template.qrexec_payload(args,
  942. self.app, 'qubes-template-fedora-32', False)
  943. # Check error message
  944. self.assertTrue('Malformed --disablerepo'
  945. in mock_err.getvalue())
  946. self.assertTrue("argument should not contain '\\n'"
  947. in mock_err.getvalue())
  948. self.assertAllCalled()
  949. def test_115_qrexec_payload_repoidnewline_fail(self):
  950. with tempfile.NamedTemporaryFile() as repo_conf1:
  951. repo_str1 = \
  952. '''[qubes-templates-itl]
  953. name = Qubes Templates repository
  954. #baseurl = https://yum.qubes-os.org/r$releasever/templates-itl
  955. #baseurl = http://yum.qubesosfasa4zl44o4tws22di6kepyzfeqv3tg4e3ztknltfxqrymdad.onion/r$releasever/templates-itl
  956. metalink = https://yum.qubes-os.org/r$releasever/templates-itl/repodata/repomd.xml.metalink
  957. enabled = 1
  958. fastestmirror = 1
  959. metadata_expire = 7d
  960. gpgcheck = 1
  961. gpgkey = file:///etc/qubes/repo-templates/keys/RPM-GPG-KEY-qubes-$releasever-primary
  962. '''
  963. repo_conf1.write(repo_str1.encode())
  964. repo_conf1.flush()
  965. args = argparse.Namespace(
  966. enablerepo=[],
  967. disablerepo=[],
  968. repoid=['repo\n1', 'repo2'],
  969. releasever='4.1',
  970. repo_files=[repo_conf1.name]
  971. )
  972. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  973. with self.assertRaises(SystemExit):
  974. qubesadmin.tools.qvm_template.qrexec_payload(args,
  975. self.app, 'qubes-template-fedora-32', False)
  976. # Check error message
  977. self.assertTrue('Malformed --repoid'
  978. in mock_err.getvalue())
  979. self.assertTrue("argument should not contain '\\n'"
  980. in mock_err.getvalue())
  981. self.assertAllCalled()
  982. def test_116_qrexec_payload_releasevernewline_fail(self):
  983. with tempfile.NamedTemporaryFile() as repo_conf1:
  984. repo_str1 = \
  985. '''[qubes-templates-itl]
  986. name = Qubes Templates repository
  987. #baseurl = https://yum.qubes-os.org/r$releasever/templates-itl
  988. #baseurl = http://yum.qubesosfasa4zl44o4tws22di6kepyzfeqv3tg4e3ztknltfxqrymdad.onion/r$releasever/templates-itl
  989. metalink = https://yum.qubes-os.org/r$releasever/templates-itl/repodata/repomd.xml.metalink
  990. enabled = 1
  991. fastestmirror = 1
  992. metadata_expire = 7d
  993. gpgcheck = 1
  994. gpgkey = file:///etc/qubes/repo-templates/keys/RPM-GPG-KEY-qubes-$releasever-primary
  995. '''
  996. repo_conf1.write(repo_str1.encode())
  997. repo_conf1.flush()
  998. args = argparse.Namespace(
  999. enablerepo=[],
  1000. disablerepo=[],
  1001. repoid=['repo1', 'repo2'],
  1002. releasever='4\n.1',
  1003. repo_files=[repo_conf1.name]
  1004. )
  1005. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  1006. with self.assertRaises(SystemExit):
  1007. qubesadmin.tools.qvm_template.qrexec_payload(args,
  1008. self.app, 'qubes-template-fedora-32', False)
  1009. # Check error message
  1010. self.assertTrue('Malformed --releasever'
  1011. in mock_err.getvalue())
  1012. self.assertTrue("argument should not contain '\\n'"
  1013. in mock_err.getvalue())
  1014. self.assertAllCalled()
  1015. def test_117_qrexec_payload_specdash_fail(self):
  1016. with tempfile.NamedTemporaryFile() as repo_conf1:
  1017. repo_str1 = \
  1018. '''[qubes-templates-itl]
  1019. name = Qubes Templates repository
  1020. #baseurl = https://yum.qubes-os.org/r$releasever/templates-itl
  1021. #baseurl = http://yum.qubesosfasa4zl44o4tws22di6kepyzfeqv3tg4e3ztknltfxqrymdad.onion/r$releasever/templates-itl
  1022. metalink = https://yum.qubes-os.org/r$releasever/templates-itl/repodata/repomd.xml.metalink
  1023. enabled = 1
  1024. fastestmirror = 1
  1025. metadata_expire = 7d
  1026. gpgcheck = 1
  1027. gpgkey = file:///etc/qubes/repo-templates/keys/RPM-GPG-KEY-qubes-$releasever-primary
  1028. '''
  1029. repo_conf1.write(repo_str1.encode())
  1030. repo_conf1.flush()
  1031. args = argparse.Namespace(
  1032. enablerepo=[],
  1033. disablerepo=[],
  1034. repoid=['repo1', 'repo2'],
  1035. releasever='4.1',
  1036. repo_files=[repo_conf1.name]
  1037. )
  1038. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  1039. with self.assertRaises(SystemExit):
  1040. qubesadmin.tools.qvm_template.qrexec_payload(args,
  1041. self.app, '---', False)
  1042. # Check error message
  1043. self.assertTrue('Malformed template name'
  1044. in mock_err.getvalue())
  1045. self.assertTrue("argument should not be '---'"
  1046. in mock_err.getvalue())
  1047. self.assertAllCalled()
  1048. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1049. def test_120_qrexec_repoquery_success(self, mock_payload):
  1050. args = argparse.Namespace(updatevm='test-vm')
  1051. mock_payload.return_value = 'str1\nstr2'
  1052. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1053. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1054. self.app.expected_service_calls[
  1055. ('test-vm', 'qubes.TemplateSearch')] = \
  1056. b'''qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 04:56|GPL|https://qubes-os.org|Qubes template for fedora-32|Qubes template\n for fedora-32\n|
  1057. qubes-template-fedora-32|1|4.2|20200201|qubes-templates-itl-testing|2048576|2020-02-23 04:56|GPLv2|https://qubes-os.org/?|Qubes template for fedora-32 v2|Qubes template\n for fedora-32 v2\n|
  1058. '''
  1059. res = qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1060. 'qubes-template-fedora-32')
  1061. self.assertEqual(res, [
  1062. qubesadmin.tools.qvm_template.Template(
  1063. 'fedora-32',
  1064. '0',
  1065. '4.1',
  1066. '20200101',
  1067. 'qubes-templates-itl',
  1068. 1048576,
  1069. datetime.datetime(2020, 1, 23, 4, 56),
  1070. 'GPL',
  1071. 'https://qubes-os.org',
  1072. 'Qubes template for fedora-32',
  1073. 'Qubes template\n for fedora-32\n'
  1074. ),
  1075. qubesadmin.tools.qvm_template.Template(
  1076. 'fedora-32',
  1077. '1',
  1078. '4.2',
  1079. '20200201',
  1080. 'qubes-templates-itl-testing',
  1081. 2048576,
  1082. datetime.datetime(2020, 2, 23, 4, 56),
  1083. 'GPLv2',
  1084. 'https://qubes-os.org/?',
  1085. 'Qubes template for fedora-32 v2',
  1086. 'Qubes template\n for fedora-32 v2\n'
  1087. )
  1088. ])
  1089. self.assertEqual(self.app.service_calls, [
  1090. ('test-vm', 'qubes.TemplateSearch',
  1091. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1092. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1093. ])
  1094. self.assertEqual(mock_payload.mock_calls, [
  1095. mock.call(args, self.app, 'qubes-template-fedora-32', False)
  1096. ])
  1097. self.assertAllCalled()
  1098. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1099. def test_121_qrexec_repoquery_refresh_success(self, mock_payload):
  1100. args = argparse.Namespace(updatevm='test-vm')
  1101. mock_payload.return_value = 'str1\nstr2'
  1102. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1103. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1104. self.app.expected_service_calls[
  1105. ('test-vm', 'qubes.TemplateSearch')] = \
  1106. b'''qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 04:56|GPL|https://qubes-os.org|Qubes template for fedora-32|Qubes template\n for fedora-32\n|
  1107. qubes-template-fedora-32|1|4.2|20200201|qubes-templates-itl-testing|2048576|2020-02-23 04:56|GPLv2|https://qubes-os.org/?|Qubes template for fedora-32 v2|Qubes template\n for fedora-32 v2\n|
  1108. '''
  1109. res = qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1110. 'qubes-template-fedora-32', True)
  1111. self.assertEqual(res, [
  1112. qubesadmin.tools.qvm_template.Template(
  1113. 'fedora-32',
  1114. '0',
  1115. '4.1',
  1116. '20200101',
  1117. 'qubes-templates-itl',
  1118. 1048576,
  1119. datetime.datetime(2020, 1, 23, 4, 56),
  1120. 'GPL',
  1121. 'https://qubes-os.org',
  1122. 'Qubes template for fedora-32',
  1123. 'Qubes template\n for fedora-32\n'
  1124. ),
  1125. qubesadmin.tools.qvm_template.Template(
  1126. 'fedora-32',
  1127. '1',
  1128. '4.2',
  1129. '20200201',
  1130. 'qubes-templates-itl-testing',
  1131. 2048576,
  1132. datetime.datetime(2020, 2, 23, 4, 56),
  1133. 'GPLv2',
  1134. 'https://qubes-os.org/?',
  1135. 'Qubes template for fedora-32 v2',
  1136. 'Qubes template\n for fedora-32 v2\n'
  1137. )
  1138. ])
  1139. self.assertEqual(self.app.service_calls, [
  1140. ('test-vm', 'qubes.TemplateSearch',
  1141. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1142. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1143. ])
  1144. self.assertEqual(mock_payload.mock_calls, [
  1145. mock.call(args, self.app, 'qubes-template-fedora-32', True)
  1146. ])
  1147. self.assertAllCalled()
  1148. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1149. def test_122_qrexec_repoquery_ignorenonspec_success(self, mock_payload):
  1150. args = argparse.Namespace(updatevm='test-vm')
  1151. mock_payload.return_value = 'str1\nstr2'
  1152. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1153. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1154. self.app.expected_service_calls[
  1155. ('test-vm', 'qubes.TemplateSearch')] = \
  1156. b'''qubes-template-debian-10|1|4.2|20200201|qubes-templates-itl-testing|2048576|2020-02-23 04:56|GPLv2|https://qubes-os.org/?|Qubes template for debian-10|Qubes template for debian-10\n|
  1157. qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 04:56|GPL|https://qubes-os.org|Qubes template for fedora-32|Qubes template for fedora-32\n|
  1158. '''
  1159. res = qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1160. 'qubes-template-fedora-32')
  1161. self.assertEqual(res, [
  1162. qubesadmin.tools.qvm_template.Template(
  1163. 'fedora-32',
  1164. '0',
  1165. '4.1',
  1166. '20200101',
  1167. 'qubes-templates-itl',
  1168. 1048576,
  1169. datetime.datetime(2020, 1, 23, 4, 56),
  1170. 'GPL',
  1171. 'https://qubes-os.org',
  1172. 'Qubes template for fedora-32',
  1173. 'Qubes template for fedora-32\n'
  1174. )
  1175. ])
  1176. self.assertEqual(self.app.service_calls, [
  1177. ('test-vm', 'qubes.TemplateSearch',
  1178. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1179. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1180. ])
  1181. self.assertEqual(mock_payload.mock_calls, [
  1182. mock.call(args, self.app, 'qubes-template-fedora-32', False)
  1183. ])
  1184. self.assertAllCalled()
  1185. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1186. def test_123_qrexec_repoquery_ignorebadname_success(self, mock_payload):
  1187. args = argparse.Namespace(updatevm='test-vm')
  1188. mock_payload.return_value = 'str1\nstr2'
  1189. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1190. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1191. self.app.expected_service_calls[
  1192. ('test-vm', 'qubes.TemplateSearch')] = \
  1193. b'''template-fedora-32|1|4.2|20200201|qubes-templates-itl-testing|2048576|2020-02-23 04:56|GPLv2|https://qubes-os.org/?|Qubes template for fedora-32 v2|Qubes template\n for fedora-32 v2\n|
  1194. qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 04:56|GPL|https://qubes-os.org|Qubes template for fedora-32|Qubes template for fedora-32\n|
  1195. '''
  1196. res = qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1197. 'qubes-template-fedora-32')
  1198. self.assertEqual(res, [
  1199. qubesadmin.tools.qvm_template.Template(
  1200. 'fedora-32',
  1201. '0',
  1202. '4.1',
  1203. '20200101',
  1204. 'qubes-templates-itl',
  1205. 1048576,
  1206. datetime.datetime(2020, 1, 23, 4, 56),
  1207. 'GPL',
  1208. 'https://qubes-os.org',
  1209. 'Qubes template for fedora-32',
  1210. 'Qubes template for fedora-32\n'
  1211. )
  1212. ])
  1213. self.assertEqual(self.app.service_calls, [
  1214. ('test-vm', 'qubes.TemplateSearch',
  1215. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1216. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1217. ])
  1218. self.assertEqual(mock_payload.mock_calls, [
  1219. mock.call(args, self.app, 'qubes-template-fedora-32', False)
  1220. ])
  1221. self.assertAllCalled()
  1222. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1223. def test_124_qrexec_repoquery_searchfail_fail(self, mock_payload):
  1224. args = argparse.Namespace(updatevm='test-vm')
  1225. mock_payload.return_value = 'str1\nstr2'
  1226. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1227. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1228. with mock.patch('qubesadmin.tests.TestProcess.wait') \
  1229. as mock_wait:
  1230. mock_wait.return_value = 1
  1231. with self.assertRaises(ConnectionError):
  1232. qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1233. 'qubes-template-fedora-32')
  1234. self.assertEqual(self.app.service_calls, [
  1235. ('test-vm', 'qubes.TemplateSearch',
  1236. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1237. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1238. ])
  1239. self.assertEqual(mock_payload.mock_calls, [
  1240. mock.call(args, self.app, 'qubes-template-fedora-32', False)
  1241. ])
  1242. self.assertAllCalled()
  1243. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1244. def test_125_qrexec_repoquery_extrafield_fail(self, mock_payload):
  1245. args = argparse.Namespace(updatevm='test-vm')
  1246. mock_payload.return_value = 'str1\nstr2'
  1247. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1248. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1249. self.app.expected_service_calls[
  1250. ('test-vm', 'qubes.TemplateSearch')] = \
  1251. b'''qubes-template-fedora-32|1|4.2|20200201|qubes-templates-itl-testing|2048576|2020-02-23 04:56|GPLv2|https://qubes-os.org/?|Qubes template for fedora-32 v2|Extra field|Qubes template\n for fedora-32 v2\n|
  1252. qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 04:56|GPL|https://qubes-os.org|Qubes template for fedora-32|Qubes template for fedora-32\n|
  1253. '''
  1254. with self.assertRaisesRegex(ConnectionError,
  1255. "unexpected data format"):
  1256. qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1257. 'qubes-template-fedora-32')
  1258. self.assertEqual(self.app.service_calls, [
  1259. ('test-vm', 'qubes.TemplateSearch',
  1260. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1261. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1262. ])
  1263. self.assertEqual(mock_payload.mock_calls, [
  1264. mock.call(args, self.app, 'qubes-template-fedora-32', False)
  1265. ])
  1266. self.assertAllCalled()
  1267. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1268. def test_125_qrexec_repoquery_missingfield_fail(self, mock_payload):
  1269. args = argparse.Namespace(updatevm='test-vm')
  1270. mock_payload.return_value = 'str1\nstr2'
  1271. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1272. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1273. self.app.expected_service_calls[
  1274. ('test-vm', 'qubes.TemplateSearch')] = \
  1275. b'''qubes-template-fedora-32|1|4.2|20200201|qubes-templates-itl-testing|2048576|2020-02-23 04:56|GPLv2|Qubes template for fedora-32 v2|Qubes template\n for fedora-32 v2\n|
  1276. qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 04:56|GPL|https://qubes-os.org|Qubes template for fedora-32|Qubes template for fedora-32\n|
  1277. '''
  1278. with self.assertRaisesRegex(ConnectionError,
  1279. "unexpected data format"):
  1280. qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1281. 'qubes-template-fedora-32')
  1282. self.assertEqual(self.app.service_calls, [
  1283. ('test-vm', 'qubes.TemplateSearch',
  1284. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1285. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1286. ])
  1287. self.assertEqual(mock_payload.mock_calls, [
  1288. mock.call(args, self.app, 'qubes-template-fedora-32', False)
  1289. ])
  1290. self.assertAllCalled()
  1291. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1292. def test_126_qrexec_repoquery_badfieldname_fail(self, mock_payload):
  1293. args = argparse.Namespace(updatevm='test-vm')
  1294. mock_payload.return_value = 'str1\nstr2'
  1295. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1296. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1297. self.app.expected_service_calls[
  1298. ('test-vm', 'qubes.TemplateSearch')] = \
  1299. b'''qubes-template-fedora-(32)|1|4.2|20200201|qubes-templates-itl-testing|2048576|2020-02-23 04:56|GPLv2|https://qubes-os.org/?|Qubes template for fedora-32 v2|Qubes template\n for fedora-32 v2\n|
  1300. qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 04:56|GPL|https://qubes-os.org|Qubes template for fedora-32|Qubes template for fedora-32\n|
  1301. '''
  1302. with self.assertRaisesRegex(ConnectionError,
  1303. "unexpected data format"):
  1304. qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1305. 'qubes-template-fedora-32')
  1306. self.assertEqual(self.app.service_calls, [
  1307. ('test-vm', 'qubes.TemplateSearch',
  1308. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1309. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1310. ])
  1311. self.assertEqual(mock_payload.mock_calls, [
  1312. mock.call(args, self.app, 'qubes-template-fedora-32', False)
  1313. ])
  1314. self.assertAllCalled()
  1315. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1316. def test_126_qrexec_repoquery_badfieldepoch_fail(self, mock_payload):
  1317. args = argparse.Namespace(updatevm='test-vm')
  1318. mock_payload.return_value = 'str1\nstr2'
  1319. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1320. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1321. self.app.expected_service_calls[
  1322. ('test-vm', 'qubes.TemplateSearch')] = \
  1323. b'''qubes-template-fedora-32|!1|4.2|20200201|qubes-templates-itl-testing|2048576|2020-02-23 04:56|GPLv2|https://qubes-os.org/?|Qubes template for fedora-32 v2|Qubes template\n for fedora-32 v2\n|
  1324. qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 04:56|GPL|https://qubes-os.org|Qubes template for fedora-32|Qubes template for fedora-32\n|
  1325. '''
  1326. with self.assertRaisesRegex(ConnectionError,
  1327. "unexpected data format"):
  1328. qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1329. 'qubes-template-fedora-32')
  1330. self.assertEqual(self.app.service_calls, [
  1331. ('test-vm', 'qubes.TemplateSearch',
  1332. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1333. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1334. ])
  1335. self.assertEqual(mock_payload.mock_calls, [
  1336. mock.call(args, self.app, 'qubes-template-fedora-32', False)
  1337. ])
  1338. self.assertAllCalled()
  1339. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1340. def test_126_qrexec_repoquery_badfieldreponame_fail(self, mock_payload):
  1341. args = argparse.Namespace(updatevm='test-vm')
  1342. mock_payload.return_value = 'str1\nstr2'
  1343. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1344. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1345. self.app.expected_service_calls[
  1346. ('test-vm', 'qubes.TemplateSearch')] = \
  1347. b'''qubes-template-fedora-32|1|4.2|20200201|qubes-templates-itl-<testing>|2048576|2020-02-23 04:56|GPLv2|https://qubes-os.org/?|Qubes template for fedora-32 v2|Qubes template\n for fedora-32 v2\n|
  1348. qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 04:56|GPL|https://qubes-os.org|Qubes template for fedora-32|Qubes template for fedora-32\n|
  1349. '''
  1350. with self.assertRaisesRegex(ConnectionError,
  1351. "unexpected data format"):
  1352. qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1353. 'qubes-template-fedora-32')
  1354. self.assertEqual(self.app.service_calls, [
  1355. ('test-vm', 'qubes.TemplateSearch',
  1356. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1357. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1358. ])
  1359. self.assertEqual(mock_payload.mock_calls, [
  1360. mock.call(args, self.app, 'qubes-template-fedora-32', False)
  1361. ])
  1362. self.assertAllCalled()
  1363. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1364. def test_126_qrexec_repoquery_badfielddlsize_fail(self, mock_payload):
  1365. args = argparse.Namespace(updatevm='test-vm')
  1366. mock_payload.return_value = 'str1\nstr2'
  1367. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1368. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1369. self.app.expected_service_calls[
  1370. ('test-vm', 'qubes.TemplateSearch')] = \
  1371. b'''qubes-template-fedora-32|1|4.2|20200201|qubes-templates-itl-testing|2048a576|2020-02-23 04:56|GPLv2|https://qubes-os.org/?|Qubes template for fedora-32 v2|Qubes template\n for fedora-32 v2\n|
  1372. qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 04:56|GPL|https://qubes-os.org|Qubes template for fedora-32|Qubes template for fedora-32\n|
  1373. '''
  1374. with self.assertRaisesRegex(ConnectionError,
  1375. "unexpected data format"):
  1376. qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1377. 'qubes-template-fedora-32')
  1378. self.assertEqual(self.app.service_calls, [
  1379. ('test-vm', 'qubes.TemplateSearch',
  1380. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1381. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1382. ])
  1383. self.assertEqual(mock_payload.mock_calls, [
  1384. mock.call(args, self.app, 'qubes-template-fedora-32', False)
  1385. ])
  1386. self.assertAllCalled()
  1387. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1388. def test_126_qrexec_repoquery_badfielddate_fail(self, mock_payload):
  1389. args = argparse.Namespace(updatevm='test-vm')
  1390. mock_payload.return_value = 'str1\nstr2'
  1391. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1392. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1393. self.app.expected_service_calls[
  1394. ('test-vm', 'qubes.TemplateSearch')] = \
  1395. b'''qubes-template-fedora-32|1|4.2|20200201|qubes-templates-itl-testing|2048576|2020-02-23|GPLv2|https://qubes-os.org/?|Qubes template for fedora-32 v2|Qubes template\n for fedora-32 v2\n|
  1396. qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 04:56|GPL|https://qubes-os.org|Qubes template for fedora-32|Qubes template for fedora-32\n|
  1397. '''
  1398. with self.assertRaisesRegex(ConnectionError,
  1399. "unexpected data format"):
  1400. qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1401. 'qubes-template-fedora-32')
  1402. self.assertEqual(self.app.service_calls, [
  1403. ('test-vm', 'qubes.TemplateSearch',
  1404. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1405. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1406. ])
  1407. self.assertEqual(mock_payload.mock_calls, [
  1408. mock.call(args, self.app, 'qubes-template-fedora-32', False)
  1409. ])
  1410. self.assertAllCalled()
  1411. @mock.patch('qubesadmin.tools.qvm_template.qrexec_payload')
  1412. def test_126_qrexec_repoquery_license_fail(self, mock_payload):
  1413. args = argparse.Namespace(updatevm='test-vm')
  1414. mock_payload.return_value = 'str1\nstr2'
  1415. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1416. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1417. self.app.expected_service_calls[
  1418. ('test-vm', 'qubes.TemplateSearch')] = \
  1419. b'''qubes-template-fedora-32|1|4.2|20200201|qubes-templates-itl-testing|2048576|2020-02-23 04:56|GPLv2:)|https://qubes-os.org/?|Qubes template for fedora-32 v2|Qubes template\n for fedora-32 v2\n|
  1420. qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 04:56|GPL|https://qubes-os.org|Qubes template for fedora-32|Qubes template for fedora-32\n|
  1421. '''
  1422. with self.assertRaisesRegex(ConnectionError,
  1423. "unexpected data format"):
  1424. qubesadmin.tools.qvm_template.qrexec_repoquery(args, self.app,
  1425. 'qubes-template-fedora-32')
  1426. self.assertEqual(self.app.service_calls, [
  1427. ('test-vm', 'qubes.TemplateSearch',
  1428. {'filter_esc': True, 'stdout': subprocess.PIPE}),
  1429. ('test-vm', 'qubes.TemplateSearch', b'str1\nstr2')
  1430. ])
  1431. self.assertEqual(mock_payload.mock_calls, [
  1432. mock.call(args, self.app, 'qubes-template-fedora-32', False)
  1433. ])
  1434. self.assertAllCalled()
  1435. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  1436. def test_130_get_dl_list_latest_success(self, mock_query):
  1437. mock_query.return_value = [
  1438. qubesadmin.tools.qvm_template.Template(
  1439. 'fedora-32',
  1440. '1',
  1441. '4.1',
  1442. '20200101',
  1443. 'qubes-templates-itl',
  1444. 1048576,
  1445. datetime.datetime(2020, 1, 23, 4, 56),
  1446. 'GPL',
  1447. 'https://qubes-os.org',
  1448. 'Qubes template for fedora-32',
  1449. 'Qubes template\n for fedora-32\n'
  1450. ),
  1451. qubesadmin.tools.qvm_template.Template(
  1452. 'fedora-32',
  1453. '0',
  1454. '4.2',
  1455. '20200201',
  1456. 'qubes-templates-itl-testing',
  1457. 2048576,
  1458. datetime.datetime(2020, 2, 23, 4, 56),
  1459. 'GPLv2',
  1460. 'https://qubes-os.org/?',
  1461. 'Qubes template for fedora-32 v2',
  1462. 'Qubes template\n for fedora-32 v2\n'
  1463. )
  1464. ]
  1465. args = argparse.Namespace(
  1466. templates=['some.local.file.rpm', 'fedora-32']
  1467. )
  1468. ret = qubesadmin.tools.qvm_template.get_dl_list(args, self.app)
  1469. self.assertEqual(ret, {
  1470. 'fedora-32': qubesadmin.tools.qvm_template.DlEntry(
  1471. ('1', '4.1', '20200101'), 'qubes-templates-itl', 1048576)
  1472. })
  1473. self.assertEqual(mock_query.mock_calls, [
  1474. mock.call(args, self.app, 'qubes-template-fedora-32')
  1475. ])
  1476. self.assertAllCalled()
  1477. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  1478. def test_131_get_dl_list_latest_notfound_fail(self, mock_query):
  1479. mock_query.return_value = []
  1480. args = argparse.Namespace(
  1481. templates=['some.local.file.rpm', 'fedora-31']
  1482. )
  1483. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  1484. with self.assertRaises(SystemExit):
  1485. qubesadmin.tools.qvm_template.get_dl_list(args, self.app)
  1486. self.assertTrue('not found' in mock_err.getvalue())
  1487. self.assertEqual(mock_query.mock_calls, [
  1488. mock.call(args, self.app, 'qubes-template-fedora-31')
  1489. ])
  1490. self.assertAllCalled()
  1491. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  1492. def test_132_get_dl_list_multimerge0_success(self, mock_query):
  1493. counter = 0
  1494. def f(*args):
  1495. nonlocal counter
  1496. counter += 1
  1497. if counter == 1:
  1498. return [
  1499. qubesadmin.tools.qvm_template.Template(
  1500. 'fedora-32',
  1501. '0',
  1502. '4.2',
  1503. '20200201',
  1504. 'qubes-templates-itl-testing',
  1505. 2048576,
  1506. datetime.datetime(2020, 2, 23, 4, 56),
  1507. 'GPLv2',
  1508. 'https://qubes-os.org/?',
  1509. 'Qubes template for fedora-32 v2',
  1510. 'Qubes template\n for fedora-32 v2\n'
  1511. )
  1512. ]
  1513. return [
  1514. qubesadmin.tools.qvm_template.Template(
  1515. 'fedora-32',
  1516. '1',
  1517. '4.1',
  1518. '20200101',
  1519. 'qubes-templates-itl',
  1520. 1048576,
  1521. datetime.datetime(2020, 1, 23, 4, 56),
  1522. 'GPL',
  1523. 'https://qubes-os.org',
  1524. 'Qubes template for fedora-32',
  1525. 'Qubes template\n for fedora-32\n'
  1526. )
  1527. ]
  1528. mock_query.side_effect = f
  1529. args = argparse.Namespace(
  1530. templates=['some.local.file.rpm', 'fedora-32:0', 'fedora-32:1']
  1531. )
  1532. ret = qubesadmin.tools.qvm_template.get_dl_list(args, self.app)
  1533. self.assertEqual(ret, {
  1534. 'fedora-32': qubesadmin.tools.qvm_template.DlEntry(
  1535. ('1', '4.1', '20200101'), 'qubes-templates-itl', 1048576)
  1536. })
  1537. self.assertEqual(mock_query.mock_calls, [
  1538. mock.call(args, self.app, 'qubes-template-fedora-32:0'),
  1539. mock.call(args, self.app, 'qubes-template-fedora-32:1')
  1540. ])
  1541. self.assertAllCalled()
  1542. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  1543. def test_132_get_dl_list_multimerge1_success(self, mock_query):
  1544. counter = 0
  1545. def f(*args):
  1546. nonlocal counter
  1547. counter += 1
  1548. if counter == 1:
  1549. return [
  1550. qubesadmin.tools.qvm_template.Template(
  1551. 'fedora-32',
  1552. '2',
  1553. '4.2',
  1554. '20200201',
  1555. 'qubes-templates-itl-testing',
  1556. 2048576,
  1557. datetime.datetime(2020, 2, 23, 4, 56),
  1558. 'GPLv2',
  1559. 'https://qubes-os.org/?',
  1560. 'Qubes template for fedora-32 v2',
  1561. 'Qubes template\n for fedora-32 v2\n'
  1562. )
  1563. ]
  1564. return [
  1565. qubesadmin.tools.qvm_template.Template(
  1566. 'fedora-32',
  1567. '1',
  1568. '4.1',
  1569. '20200101',
  1570. 'qubes-templates-itl',
  1571. 1048576,
  1572. datetime.datetime(2020, 1, 23, 4, 56),
  1573. 'GPL',
  1574. 'https://qubes-os.org',
  1575. 'Qubes template for fedora-32',
  1576. 'Qubes template\n for fedora-32\n'
  1577. )
  1578. ]
  1579. mock_query.side_effect = f
  1580. args = argparse.Namespace(
  1581. templates=['some.local.file.rpm', 'fedora-32:2', 'fedora-32:1']
  1582. )
  1583. ret = qubesadmin.tools.qvm_template.get_dl_list(args, self.app)
  1584. self.assertEqual(ret, {
  1585. 'fedora-32': qubesadmin.tools.qvm_template.DlEntry(
  1586. ('2', '4.2', '20200201'),
  1587. 'qubes-templates-itl-testing',
  1588. 2048576)
  1589. })
  1590. self.assertEqual(mock_query.mock_calls, [
  1591. mock.call(args, self.app, 'qubes-template-fedora-32:2'),
  1592. mock.call(args, self.app, 'qubes-template-fedora-32:1')
  1593. ])
  1594. self.assertAllCalled()
  1595. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  1596. def test_133_get_dl_list_reinstall_success(self, mock_query):
  1597. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1598. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1599. self.app.expected_calls[(
  1600. 'test-vm',
  1601. 'admin.vm.feature.Get',
  1602. f'template-name',
  1603. None)] = b'0\0test-vm'
  1604. self.app.expected_calls[(
  1605. 'test-vm',
  1606. 'admin.vm.feature.Get',
  1607. f'template-epoch',
  1608. None)] = b'0\x000'
  1609. self.app.expected_calls[(
  1610. 'test-vm',
  1611. 'admin.vm.feature.Get',
  1612. f'template-version',
  1613. None)] = b'0\x004.2'
  1614. self.app.expected_calls[(
  1615. 'test-vm',
  1616. 'admin.vm.feature.Get',
  1617. f'template-release',
  1618. None)] = b'0\x0020200201'
  1619. mock_query.return_value = [
  1620. qubesadmin.tools.qvm_template.Template(
  1621. 'test-vm',
  1622. '1',
  1623. '4.1',
  1624. '20200101',
  1625. 'qubes-templates-itl',
  1626. 1048576,
  1627. datetime.datetime(2020, 1, 23, 4, 56),
  1628. 'GPL',
  1629. 'https://qubes-os.org',
  1630. 'Qubes template for test-vm',
  1631. 'Qubes template\n for test-vm\n'
  1632. ),
  1633. qubesadmin.tools.qvm_template.Template(
  1634. 'test-vm',
  1635. '0',
  1636. '4.2',
  1637. '20200201',
  1638. 'qubes-templates-itl-testing',
  1639. 2048576,
  1640. datetime.datetime(2020, 2, 23, 4, 56),
  1641. 'GPLv2',
  1642. 'https://qubes-os.org/?',
  1643. 'Qubes template for test-vm v2',
  1644. 'Qubes template\n for test-vm v2\n'
  1645. )
  1646. ]
  1647. args = argparse.Namespace(
  1648. templates=['some.local.file.rpm', 'test-vm']
  1649. )
  1650. ret = qubesadmin.tools.qvm_template.get_dl_list(args, self.app,
  1651. qubesadmin.tools.qvm_template.VersionSelector.REINSTALL)
  1652. self.assertEqual(ret, {
  1653. 'test-vm': qubesadmin.tools.qvm_template.DlEntry(
  1654. ('0', '4.2', '20200201'),
  1655. 'qubes-templates-itl-testing',
  1656. 2048576
  1657. )
  1658. })
  1659. self.assertEqual(mock_query.mock_calls, [
  1660. mock.call(args, self.app, 'qubes-template-test-vm')
  1661. ])
  1662. self.assertAllCalled()
  1663. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  1664. def test_134_get_dl_list_reinstall_nolocal_fail(self, mock_query):
  1665. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1666. b'0\x00'
  1667. mock_query.return_value = [
  1668. qubesadmin.tools.qvm_template.Template(
  1669. 'test-vm',
  1670. '1',
  1671. '4.1',
  1672. '20200101',
  1673. 'qubes-templates-itl',
  1674. 1048576,
  1675. datetime.datetime(2020, 1, 23, 4, 56),
  1676. 'GPL',
  1677. 'https://qubes-os.org',
  1678. 'Qubes template for test-vm',
  1679. 'Qubes template\n for test-vm\n'
  1680. ),
  1681. qubesadmin.tools.qvm_template.Template(
  1682. 'test-vm',
  1683. '0',
  1684. '4.2',
  1685. '20200201',
  1686. 'qubes-templates-itl-testing',
  1687. 2048576,
  1688. datetime.datetime(2020, 2, 23, 4, 56),
  1689. 'GPLv2',
  1690. 'https://qubes-os.org/?',
  1691. 'Qubes template for test-vm v2',
  1692. 'Qubes template\n for test-vm v2\n'
  1693. )
  1694. ]
  1695. args = argparse.Namespace(templates=['test-vm'])
  1696. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  1697. with self.assertRaises(SystemExit):
  1698. qubesadmin.tools.qvm_template.get_dl_list(args, self.app,
  1699. qubesadmin.tools.qvm_template.VersionSelector.REINSTALL)
  1700. self.assertTrue('not already installed' in mock_err.getvalue())
  1701. self.assertEqual(mock_query.mock_calls, [
  1702. mock.call(args, self.app, 'qubes-template-test-vm')
  1703. ])
  1704. self.assertAllCalled()
  1705. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  1706. def test_135_get_dl_list_reinstall_nonmanaged_fail(self, mock_query):
  1707. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1708. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1709. mock_query.return_value = [
  1710. qubesadmin.tools.qvm_template.Template(
  1711. 'test-vm',
  1712. '1',
  1713. '4.1',
  1714. '20200101',
  1715. 'qubes-templates-itl',
  1716. 1048576,
  1717. datetime.datetime(2020, 1, 23, 4, 56),
  1718. 'GPL',
  1719. 'https://qubes-os.org',
  1720. 'Qubes template for test-vm',
  1721. 'Qubes template\n for test-vm\n'
  1722. ),
  1723. qubesadmin.tools.qvm_template.Template(
  1724. 'test-vm',
  1725. '0',
  1726. '4.2',
  1727. '20200201',
  1728. 'qubes-templates-itl-testing',
  1729. 2048576,
  1730. datetime.datetime(2020, 2, 23, 4, 56),
  1731. 'GPLv2',
  1732. 'https://qubes-os.org/?',
  1733. 'Qubes template for test-vm v2',
  1734. 'Qubes template\n for test-vm v2\n'
  1735. )
  1736. ]
  1737. args = argparse.Namespace(templates=['test-vm'])
  1738. def qubesd_call(dest, method,
  1739. arg=None, payload=None, payload_stream=None,
  1740. orig_func=self.app.qubesd_call):
  1741. if method == 'admin.vm.feature.Get':
  1742. raise KeyError
  1743. return orig_func(dest, method, arg, payload, payload_stream)
  1744. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err, \
  1745. mock.patch.object(self.app, 'qubesd_call') as mock_call:
  1746. mock_call.side_effect = qubesd_call
  1747. with self.assertRaises(SystemExit):
  1748. qubesadmin.tools.qvm_template.get_dl_list(args, self.app,
  1749. qubesadmin.tools.qvm_template.VersionSelector.REINSTALL)
  1750. self.assertTrue('not managed' in mock_err.getvalue())
  1751. self.assertEqual(mock_query.mock_calls, [
  1752. mock.call(args, self.app, 'qubes-template-test-vm')
  1753. ])
  1754. self.assertAllCalled()
  1755. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  1756. def test_135_get_dl_list_reinstall_nonmanagednoname_fail(self, mock_query):
  1757. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1758. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1759. self.app.expected_calls[(
  1760. 'test-vm',
  1761. 'admin.vm.feature.Get',
  1762. f'template-name',
  1763. None)] = b'0\0test-vm-2'
  1764. mock_query.return_value = [
  1765. qubesadmin.tools.qvm_template.Template(
  1766. 'test-vm',
  1767. '1',
  1768. '4.1',
  1769. '20200101',
  1770. 'qubes-templates-itl',
  1771. 1048576,
  1772. datetime.datetime(2020, 1, 23, 4, 56),
  1773. 'GPL',
  1774. 'https://qubes-os.org',
  1775. 'Qubes template for test-vm',
  1776. 'Qubes template\n for test-vm\n'
  1777. ),
  1778. qubesadmin.tools.qvm_template.Template(
  1779. 'test-vm',
  1780. '0',
  1781. '4.2',
  1782. '20200201',
  1783. 'qubes-templates-itl-testing',
  1784. 2048576,
  1785. datetime.datetime(2020, 2, 23, 4, 56),
  1786. 'GPLv2',
  1787. 'https://qubes-os.org/?',
  1788. 'Qubes template for test-vm v2',
  1789. 'Qubes template\n for test-vm v2\n'
  1790. )
  1791. ]
  1792. args = argparse.Namespace(templates=['test-vm'])
  1793. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  1794. with self.assertRaises(SystemExit):
  1795. qubesadmin.tools.qvm_template.get_dl_list(args, self.app,
  1796. qubesadmin.tools.qvm_template.VersionSelector.REINSTALL)
  1797. self.assertTrue('not managed' in mock_err.getvalue())
  1798. self.assertEqual(mock_query.mock_calls, [
  1799. mock.call(args, self.app, 'qubes-template-test-vm')
  1800. ])
  1801. self.assertAllCalled()
  1802. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  1803. def test_136_get_dl_list_downgrade_success(self, mock_query):
  1804. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1805. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1806. self.app.expected_calls[(
  1807. 'test-vm',
  1808. 'admin.vm.feature.Get',
  1809. f'template-name',
  1810. None)] = b'0\0test-vm'
  1811. self.app.expected_calls[(
  1812. 'test-vm',
  1813. 'admin.vm.feature.Get',
  1814. f'template-epoch',
  1815. None)] = b'0\x000'
  1816. self.app.expected_calls[(
  1817. 'test-vm',
  1818. 'admin.vm.feature.Get',
  1819. f'template-version',
  1820. None)] = b'0\x004.3'
  1821. self.app.expected_calls[(
  1822. 'test-vm',
  1823. 'admin.vm.feature.Get',
  1824. f'template-release',
  1825. None)] = b'0\x0020200201'
  1826. mock_query.return_value = [
  1827. qubesadmin.tools.qvm_template.Template(
  1828. 'test-vm',
  1829. '0',
  1830. '4.2',
  1831. '20200201',
  1832. 'qubes-templates-itl-testing',
  1833. 2048576,
  1834. datetime.datetime(2020, 2, 23, 4, 56),
  1835. 'GPLv2',
  1836. 'https://qubes-os.org/?',
  1837. 'Qubes template for test-vm v2',
  1838. 'Qubes template\n for test-vm v2\n'
  1839. ),
  1840. qubesadmin.tools.qvm_template.Template(
  1841. 'test-vm',
  1842. '0',
  1843. '4.1',
  1844. '20200101',
  1845. 'qubes-templates-itl',
  1846. 1048576,
  1847. datetime.datetime(2020, 1, 23, 4, 56),
  1848. 'GPL',
  1849. 'https://qubes-os.org',
  1850. 'Qubes template for test-vm',
  1851. 'Qubes template\n for test-vm\n'
  1852. ),
  1853. qubesadmin.tools.qvm_template.Template(
  1854. 'test-vm',
  1855. '1',
  1856. '4.1',
  1857. '20200101',
  1858. 'qubes-templates-itl',
  1859. 1048576,
  1860. datetime.datetime(2020, 1, 23, 4, 56),
  1861. 'GPL',
  1862. 'https://qubes-os.org',
  1863. 'Qubes template for test-vm',
  1864. 'Qubes template\n for test-vm\n'
  1865. )
  1866. ]
  1867. args = argparse.Namespace(templates=['test-vm'])
  1868. ret = qubesadmin.tools.qvm_template.get_dl_list(args, self.app,
  1869. qubesadmin.tools.qvm_template.VersionSelector.LATEST_LOWER)
  1870. self.assertEqual(ret, {
  1871. 'test-vm': qubesadmin.tools.qvm_template.DlEntry(
  1872. ('0', '4.2', '20200201'),
  1873. 'qubes-templates-itl-testing',
  1874. 2048576
  1875. )
  1876. })
  1877. self.assertEqual(mock_query.mock_calls, [
  1878. mock.call(args, self.app, 'qubes-template-test-vm')
  1879. ])
  1880. self.assertAllCalled()
  1881. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  1882. def test_137_get_dl_list_downgrade_nonmanaged_fail(self, mock_query):
  1883. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1884. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1885. self.app.expected_calls[(
  1886. 'test-vm',
  1887. 'admin.vm.feature.Get',
  1888. f'template-name',
  1889. None)] = b'0\0test-vm-2'
  1890. mock_query.return_value = [
  1891. qubesadmin.tools.qvm_template.Template(
  1892. 'test-vm',
  1893. '0',
  1894. '4.2',
  1895. '20200201',
  1896. 'qubes-templates-itl-testing',
  1897. 2048576,
  1898. datetime.datetime(2020, 2, 23, 4, 56),
  1899. 'GPLv2',
  1900. 'https://qubes-os.org/?',
  1901. 'Qubes template for test-vm v2',
  1902. 'Qubes template\n for test-vm v2\n'
  1903. ),
  1904. qubesadmin.tools.qvm_template.Template(
  1905. 'test-vm',
  1906. '0',
  1907. '4.1',
  1908. '20200101',
  1909. 'qubes-templates-itl',
  1910. 1048576,
  1911. datetime.datetime(2020, 1, 23, 4, 56),
  1912. 'GPL',
  1913. 'https://qubes-os.org',
  1914. 'Qubes template for test-vm',
  1915. 'Qubes template\n for test-vm\n'
  1916. ),
  1917. qubesadmin.tools.qvm_template.Template(
  1918. 'test-vm',
  1919. '1',
  1920. '4.1',
  1921. '20200101',
  1922. 'qubes-templates-itl',
  1923. 1048576,
  1924. datetime.datetime(2020, 1, 23, 4, 56),
  1925. 'GPL',
  1926. 'https://qubes-os.org',
  1927. 'Qubes template for test-vm',
  1928. 'Qubes template\n for test-vm\n'
  1929. )
  1930. ]
  1931. args = argparse.Namespace(templates=['test-vm'])
  1932. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  1933. with self.assertRaises(SystemExit):
  1934. qubesadmin.tools.qvm_template.get_dl_list(args, self.app,
  1935. qubesadmin.tools.qvm_template.VersionSelector.REINSTALL)
  1936. self.assertTrue('not managed' in mock_err.getvalue())
  1937. self.assertEqual(mock_query.mock_calls, [
  1938. mock.call(args, self.app, 'qubes-template-test-vm')
  1939. ])
  1940. self.assertAllCalled()
  1941. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  1942. def test_138_get_dl_list_downgrade_notfound_skip(self, mock_query):
  1943. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1944. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1945. self.app.expected_calls[(
  1946. 'test-vm',
  1947. 'admin.vm.feature.Get',
  1948. f'template-name',
  1949. None)] = b'0\0test-vm'
  1950. self.app.expected_calls[(
  1951. 'test-vm',
  1952. 'admin.vm.feature.Get',
  1953. f'template-epoch',
  1954. None)] = b'0\x000'
  1955. self.app.expected_calls[(
  1956. 'test-vm',
  1957. 'admin.vm.feature.Get',
  1958. f'template-version',
  1959. None)] = b'0\x004.3'
  1960. self.app.expected_calls[(
  1961. 'test-vm',
  1962. 'admin.vm.feature.Get',
  1963. f'template-release',
  1964. None)] = b'0\x0020200201'
  1965. mock_query.return_value = [
  1966. qubesadmin.tools.qvm_template.Template(
  1967. 'test-vm',
  1968. '1',
  1969. '4.1',
  1970. '20200101',
  1971. 'qubes-templates-itl',
  1972. 1048576,
  1973. datetime.datetime(2020, 1, 23, 4, 56),
  1974. 'GPL',
  1975. 'https://qubes-os.org',
  1976. 'Qubes template for test-vm',
  1977. 'Qubes template\n for test-vm\n'
  1978. )
  1979. ]
  1980. args = argparse.Namespace(templates=['test-vm'])
  1981. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  1982. ret = qubesadmin.tools.qvm_template.get_dl_list(args, self.app,
  1983. qubesadmin.tools.qvm_template.VersionSelector.LATEST_LOWER)
  1984. self.assertTrue('lowest version' in mock_err.getvalue())
  1985. self.assertEqual(ret, {})
  1986. self.assertEqual(mock_query.mock_calls, [
  1987. mock.call(args, self.app, 'qubes-template-test-vm')
  1988. ])
  1989. self.assertAllCalled()
  1990. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  1991. def test_139_get_dl_list_upgrade_success(self, mock_query):
  1992. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1993. b'0\x00test-vm class=TemplateVM state=Halted\n'
  1994. self.app.expected_calls[(
  1995. 'test-vm',
  1996. 'admin.vm.feature.Get',
  1997. f'template-name',
  1998. None)] = b'0\0test-vm'
  1999. self.app.expected_calls[(
  2000. 'test-vm',
  2001. 'admin.vm.feature.Get',
  2002. f'template-epoch',
  2003. None)] = b'0\x000'
  2004. self.app.expected_calls[(
  2005. 'test-vm',
  2006. 'admin.vm.feature.Get',
  2007. f'template-version',
  2008. None)] = b'0\x004.3'
  2009. self.app.expected_calls[(
  2010. 'test-vm',
  2011. 'admin.vm.feature.Get',
  2012. f'template-release',
  2013. None)] = b'0\x0020200201'
  2014. mock_query.return_value = [
  2015. qubesadmin.tools.qvm_template.Template(
  2016. 'test-vm',
  2017. '1',
  2018. '4.1',
  2019. '20200101',
  2020. 'qubes-templates-itl',
  2021. 1048576,
  2022. datetime.datetime(2020, 1, 23, 4, 56),
  2023. 'GPL',
  2024. 'https://qubes-os.org',
  2025. 'Qubes template for test-vm',
  2026. 'Qubes template\n for test-vm\n'
  2027. )
  2028. ]
  2029. args = argparse.Namespace(templates=['test-vm'])
  2030. ret = qubesadmin.tools.qvm_template.get_dl_list(args, self.app,
  2031. qubesadmin.tools.qvm_template.VersionSelector.LATEST_HIGHER)
  2032. self.assertEqual(ret, {
  2033. 'test-vm': qubesadmin.tools.qvm_template.DlEntry(
  2034. ('1', '4.1', '20200101'), 'qubes-templates-itl', 1048576
  2035. )
  2036. })
  2037. self.assertEqual(mock_query.mock_calls, [
  2038. mock.call(args, self.app, 'qubes-template-test-vm')
  2039. ])
  2040. self.assertAllCalled()
  2041. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2042. def test_140_get_dl_list_downgrade_notfound_skip(self, mock_query):
  2043. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  2044. b'0\x00test-vm class=TemplateVM state=Halted\n'
  2045. self.app.expected_calls[(
  2046. 'test-vm',
  2047. 'admin.vm.feature.Get',
  2048. f'template-name',
  2049. None)] = b'0\0test-vm'
  2050. self.app.expected_calls[(
  2051. 'test-vm',
  2052. 'admin.vm.feature.Get',
  2053. f'template-epoch',
  2054. None)] = b'0\x000'
  2055. self.app.expected_calls[(
  2056. 'test-vm',
  2057. 'admin.vm.feature.Get',
  2058. f'template-version',
  2059. None)] = b'0\x004.3'
  2060. self.app.expected_calls[(
  2061. 'test-vm',
  2062. 'admin.vm.feature.Get',
  2063. f'template-release',
  2064. None)] = b'0\x0020200201'
  2065. mock_query.return_value = [
  2066. qubesadmin.tools.qvm_template.Template(
  2067. 'test-vm',
  2068. '0',
  2069. '4.1',
  2070. '20200101',
  2071. 'qubes-templates-itl',
  2072. 1048576,
  2073. datetime.datetime(2020, 1, 23, 4, 56),
  2074. 'GPL',
  2075. 'https://qubes-os.org',
  2076. 'Qubes template for test-vm',
  2077. 'Qubes template\n for test-vm\n'
  2078. )
  2079. ]
  2080. args = argparse.Namespace(templates=['test-vm'])
  2081. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  2082. ret = qubesadmin.tools.qvm_template.get_dl_list(args, self.app,
  2083. qubesadmin.tools.qvm_template.VersionSelector.LATEST_HIGHER)
  2084. self.assertTrue('highest version' in mock_err.getvalue())
  2085. self.assertEqual(ret, {})
  2086. self.assertEqual(mock_query.mock_calls, [
  2087. mock.call(args, self.app, 'qubes-template-test-vm')
  2088. ])
  2089. self.assertAllCalled()
  2090. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2091. def test_141_get_dl_list_reinstall_notfound_fail(self, mock_query):
  2092. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  2093. b'0\x00test-vm class=TemplateVM state=Halted\n'
  2094. self.app.expected_calls[(
  2095. 'test-vm',
  2096. 'admin.vm.feature.Get',
  2097. f'template-name',
  2098. None)] = b'0\0test-vm'
  2099. self.app.expected_calls[(
  2100. 'test-vm',
  2101. 'admin.vm.feature.Get',
  2102. f'template-epoch',
  2103. None)] = b'0\x000'
  2104. self.app.expected_calls[(
  2105. 'test-vm',
  2106. 'admin.vm.feature.Get',
  2107. f'template-version',
  2108. None)] = b'0\x004.3'
  2109. self.app.expected_calls[(
  2110. 'test-vm',
  2111. 'admin.vm.feature.Get',
  2112. f'template-release',
  2113. None)] = b'0\x0020200201'
  2114. mock_query.return_value = [
  2115. qubesadmin.tools.qvm_template.Template(
  2116. 'test-vm',
  2117. '0',
  2118. '4.1',
  2119. '20200101',
  2120. 'qubes-templates-itl',
  2121. 1048576,
  2122. datetime.datetime(2020, 1, 23, 4, 56),
  2123. 'GPL',
  2124. 'https://qubes-os.org',
  2125. 'Qubes template for test-vm',
  2126. 'Qubes template\n for test-vm\n'
  2127. )
  2128. ]
  2129. args = argparse.Namespace(templates=['test-vm'])
  2130. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  2131. with self.assertRaises(SystemExit):
  2132. qubesadmin.tools.qvm_template.get_dl_list(args, self.app,
  2133. qubesadmin.tools.qvm_template.VersionSelector.REINSTALL)
  2134. self.assertTrue('Same version' in mock_err.getvalue())
  2135. self.assertTrue('not found' in mock_err.getvalue())
  2136. self.assertEqual(mock_query.mock_calls, [
  2137. mock.call(args, self.app, 'qubes-template-test-vm')
  2138. ])
  2139. self.assertAllCalled()
  2140. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2141. def test_150_list_templates_installed_success(self, mock_query):
  2142. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  2143. b'0\x00test-vm class=TemplateVM state=Halted\n' \
  2144. b'test-vm-2 class=TemplateVM state=Halted\n' \
  2145. b'non-spec class=TemplateVM state=Halted\n'
  2146. build_time = '2020-09-01 14:30:00' # 1598970600
  2147. install_time = '2020-09-01 15:30:00'
  2148. for key, val in [
  2149. ('name', 'test-vm'),
  2150. ('epoch', '2'),
  2151. ('version', '4.1'),
  2152. ('release', '2020'),
  2153. ('reponame', '@commandline'),
  2154. ('buildtime', build_time),
  2155. ('installtime', install_time),
  2156. ('license', 'GPL'),
  2157. ('url', 'https://qubes-os.org'),
  2158. ('summary', 'Summary'),
  2159. ('description', 'Desc|desc')]:
  2160. self.app.expected_calls[(
  2161. 'test-vm',
  2162. 'admin.vm.feature.Get',
  2163. f'template-{key}',
  2164. None)] = b'0\0' + val.encode()
  2165. for key, val in [('name', 'test-vm-2-not-managed')]:
  2166. self.app.expected_calls[(
  2167. 'test-vm-2',
  2168. 'admin.vm.feature.Get',
  2169. f'template-{key}',
  2170. None)] = b'0\0' + val.encode()
  2171. for key, val in [
  2172. ('name', 'non-spec'),
  2173. ('epoch', '0'),
  2174. ('version', '4.3'),
  2175. ('release', '20200201')]:
  2176. self.app.expected_calls[(
  2177. 'non-spec',
  2178. 'admin.vm.feature.Get',
  2179. f'template-{key}',
  2180. None)] = b'0\0' + val.encode()
  2181. args = argparse.Namespace(
  2182. all=False,
  2183. installed=True,
  2184. available=False,
  2185. extras=False,
  2186. upgrades=False,
  2187. all_versions=True,
  2188. machine_readable=False,
  2189. machine_readable_json=False,
  2190. templates=['test-vm*']
  2191. )
  2192. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out, \
  2193. mock.patch.object(self.app.domains['test-vm'],
  2194. 'get_disk_utilization') as mock_disk:
  2195. mock_disk.return_value = 1234321
  2196. qubesadmin.tools.qvm_template.list_templates(
  2197. args, self.app, 'list')
  2198. self.assertEqual(mock_out.getvalue(),
  2199. '''Installed Templates
  2200. [('test-vm', '2:4.1-2020', '@commandline')]
  2201. ''')
  2202. self.assertEqual(mock_disk.mock_calls, [mock.call()])
  2203. self.assertEqual(mock_query.mock_calls, [])
  2204. self.assertAllCalled()
  2205. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2206. def test_151_list_templates_available_success(self, mock_query):
  2207. counter = 0
  2208. def f(*args):
  2209. nonlocal counter
  2210. counter += 1
  2211. if counter == 1:
  2212. return [
  2213. qubesadmin.tools.qvm_template.Template(
  2214. 'fedora-32',
  2215. '0',
  2216. '4.2',
  2217. '20200201',
  2218. 'qubes-templates-itl-testing',
  2219. 2048576,
  2220. datetime.datetime(2020, 2, 23, 4, 56),
  2221. 'GPLv2',
  2222. 'https://qubes-os.org/?',
  2223. 'Qubes template for fedora-32 v2',
  2224. 'Qubes template\n for fedora-32 v2\n'
  2225. )
  2226. ]
  2227. return [
  2228. qubesadmin.tools.qvm_template.Template(
  2229. 'fedora-31',
  2230. '1',
  2231. '4.1',
  2232. '20200101',
  2233. 'qubes-templates-itl',
  2234. 1048576,
  2235. datetime.datetime(2020, 1, 23, 4, 56),
  2236. 'GPL',
  2237. 'https://qubes-os.org',
  2238. 'Qubes template for fedora-31',
  2239. 'Qubes template\n for fedora-31\n'
  2240. )
  2241. ]
  2242. mock_query.side_effect = f
  2243. args = argparse.Namespace(
  2244. all=False,
  2245. installed=False,
  2246. available=True,
  2247. extras=False,
  2248. upgrades=False,
  2249. all_versions=True,
  2250. machine_readable=False,
  2251. machine_readable_json=False,
  2252. templates=['fedora-32', 'fedora-31']
  2253. )
  2254. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out:
  2255. qubesadmin.tools.qvm_template.list_templates(
  2256. args, self.app, 'list')
  2257. # Order not determinstic because of sets
  2258. expected = [
  2259. ('fedora-31', '1:4.1-20200101', 'qubes-templates-itl'),
  2260. ('fedora-32', '0:4.2-20200201', 'qubes-templates-itl-testing')
  2261. ]
  2262. self.assertTrue(mock_out.getvalue() == \
  2263. f'''Available Templates
  2264. {str([expected[1], expected[0]])}
  2265. ''' \
  2266. or mock_out.getvalue() == \
  2267. f'''Available Templates
  2268. {str([expected[0], expected[1]])}
  2269. ''')
  2270. self.assertEqual(mock_query.mock_calls, [
  2271. mock.call(args, self.app, 'fedora-32'),
  2272. mock.call(args, self.app, 'fedora-31')
  2273. ])
  2274. self.assertAllCalled()
  2275. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2276. def test_151_list_templates_available_all_success(self, mock_query):
  2277. mock_query.return_value = [
  2278. qubesadmin.tools.qvm_template.Template(
  2279. 'fedora-31',
  2280. '1',
  2281. '4.1',
  2282. '20190101',
  2283. 'qubes-templates-itl',
  2284. 1048576,
  2285. datetime.datetime(2019, 1, 23, 4, 56),
  2286. 'GPL',
  2287. 'https://qubes-os.org',
  2288. 'Qubes template for fedora-31',
  2289. 'Qubes template\n for fedora-31\n'
  2290. ),
  2291. qubesadmin.tools.qvm_template.Template(
  2292. 'fedora-31',
  2293. '1',
  2294. '4.1',
  2295. '20200101',
  2296. 'qubes-templates-itl',
  2297. 1048576,
  2298. datetime.datetime(2020, 1, 23, 4, 56),
  2299. 'GPL',
  2300. 'https://qubes-os.org',
  2301. 'Qubes template for fedora-31',
  2302. 'Qubes template\n for fedora-31\n'
  2303. ),
  2304. ]
  2305. args = argparse.Namespace(
  2306. all=False,
  2307. installed=False,
  2308. available=True,
  2309. extras=False,
  2310. upgrades=False,
  2311. all_versions=True,
  2312. machine_readable=False,
  2313. machine_readable_json=False,
  2314. templates=[]
  2315. )
  2316. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out:
  2317. qubesadmin.tools.qvm_template.list_templates(
  2318. args, self.app, 'list')
  2319. self.assertEqual(mock_out.getvalue(),
  2320. '''Available Templates
  2321. [('fedora-31', '1:4.1-20190101', 'qubes-templates-itl'), ('fedora-31', '1:4.1-20200101', 'qubes-templates-itl')]
  2322. ''')
  2323. self.assertEqual(mock_query.mock_calls, [
  2324. mock.call(args, self.app)
  2325. ])
  2326. self.assertAllCalled()
  2327. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2328. def test_151_list_templates_available_only_latest_success(self, mock_query):
  2329. mock_query.return_value = [
  2330. qubesadmin.tools.qvm_template.Template(
  2331. 'fedora-31',
  2332. '1',
  2333. '4.1',
  2334. '20190101',
  2335. 'qubes-templates-itl',
  2336. 1048576,
  2337. datetime.datetime(2019, 1, 23, 4, 56),
  2338. 'GPL',
  2339. 'https://qubes-os.org',
  2340. 'Qubes template for fedora-31',
  2341. 'Qubes template\n for fedora-31\n'
  2342. ),
  2343. qubesadmin.tools.qvm_template.Template(
  2344. 'fedora-31',
  2345. '1',
  2346. '4.1',
  2347. '20200101',
  2348. 'qubes-templates-itl',
  2349. 1048576,
  2350. datetime.datetime(2020, 1, 23, 4, 56),
  2351. 'GPL',
  2352. 'https://qubes-os.org',
  2353. 'Qubes template for fedora-31',
  2354. 'Qubes template\n for fedora-31\n'
  2355. ),
  2356. ]
  2357. args = argparse.Namespace(
  2358. all=False,
  2359. installed=False,
  2360. available=True,
  2361. extras=False,
  2362. upgrades=False,
  2363. all_versions=False,
  2364. machine_readable=False,
  2365. machine_readable_json=False,
  2366. templates=[]
  2367. )
  2368. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out:
  2369. qubesadmin.tools.qvm_template.list_templates(
  2370. args, self.app, 'list')
  2371. self.assertEqual(mock_out.getvalue(),
  2372. '''Available Templates
  2373. [('fedora-31', '1:4.1-20200101', 'qubes-templates-itl')]
  2374. ''')
  2375. self.assertEqual(mock_query.mock_calls, [
  2376. mock.call(args, self.app)
  2377. ])
  2378. self.assertAllCalled()
  2379. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2380. def test_152_list_templates_extras_success(self, mock_query):
  2381. mock_query.return_value = [
  2382. qubesadmin.tools.qvm_template.Template(
  2383. 'test-vm',
  2384. '2',
  2385. '4.1',
  2386. '2020',
  2387. 'qubes-templates-itl',
  2388. 1048576,
  2389. datetime.datetime(2020, 9, 1, 14, 30,
  2390. tzinfo=datetime.timezone.utc),
  2391. 'GPL',
  2392. 'https://qubes-os.org',
  2393. 'Qubes template for fedora-31',
  2394. 'Qubes template\n for fedora-31\n'
  2395. )
  2396. ]
  2397. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  2398. b'0\x00test-vm class=TemplateVM state=Halted\n' \
  2399. b'test-vm-2 class=TemplateVM state=Halted\n' \
  2400. b'test-vm-3 class=TemplateVM state=Halted\n' \
  2401. b'non-spec class=TemplateVM state=Halted\n'
  2402. for key, val in [('name', 'test-vm')]:
  2403. self.app.expected_calls[(
  2404. 'test-vm',
  2405. 'admin.vm.feature.Get',
  2406. f'template-{key}',
  2407. None)] = b'0\0' + val.encode()
  2408. for key, val in [
  2409. ('name', 'test-vm-2'),
  2410. ('epoch', '1'),
  2411. ('version', '4.0'),
  2412. ('release', '2019'),
  2413. ('reponame', 'qubes-template-itl'),
  2414. ('buildtime', '2020-09-02 14:30:00'),
  2415. ('installtime', '2020-09-02 15:30:00'),
  2416. ('license', 'GPLv2'),
  2417. ('url', 'https://qubes-os.org/?'),
  2418. ('summary', 'Summary2'),
  2419. ('description', 'Desc|desc|2')]:
  2420. self.app.expected_calls[(
  2421. 'test-vm-2',
  2422. 'admin.vm.feature.Get',
  2423. f'template-{key}',
  2424. None)] = b'0\0' + val.encode()
  2425. for key, val in [('name', 'test-vm-3-non-managed')]:
  2426. self.app.expected_calls[(
  2427. 'test-vm-3',
  2428. 'admin.vm.feature.Get',
  2429. f'template-{key}',
  2430. None)] = b'0\0' + val.encode()
  2431. for key, val in [
  2432. ('name', 'non-spec'),
  2433. ('epoch', '1'),
  2434. ('version', '4.0'),
  2435. ('release', '2019')]:
  2436. self.app.expected_calls[(
  2437. 'non-spec',
  2438. 'admin.vm.feature.Get',
  2439. f'template-{key}',
  2440. None)] = b'0\0' + val.encode()
  2441. args = argparse.Namespace(
  2442. all=False,
  2443. installed=False,
  2444. available=False,
  2445. extras=True,
  2446. upgrades=False,
  2447. all_versions=True,
  2448. machine_readable=False,
  2449. machine_readable_json=False,
  2450. templates=['test-vm*']
  2451. )
  2452. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out, \
  2453. mock.patch.object(self.app.domains['test-vm-2'],
  2454. 'get_disk_utilization') as mock_disk:
  2455. mock_disk.return_value = 1234321
  2456. qubesadmin.tools.qvm_template.list_templates(
  2457. args, self.app, 'list')
  2458. self.assertEqual(mock_out.getvalue(),
  2459. '''Extra Templates
  2460. [('test-vm-2', '1:4.0-2019', 'qubes-template-itl')]
  2461. ''')
  2462. self.assertEqual(mock_disk.mock_calls, [mock.call()])
  2463. self.assertEqual(mock_query.mock_calls, [
  2464. mock.call(args, self.app, 'test-vm*')
  2465. ])
  2466. self.assertAllCalled()
  2467. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2468. def test_153_list_templates_upgrades_success(self, mock_query):
  2469. mock_query.return_value = [
  2470. qubesadmin.tools.qvm_template.Template(
  2471. 'test-vm',
  2472. '2',
  2473. '4.1',
  2474. '2020',
  2475. 'qubes-templates-itl',
  2476. 1048576,
  2477. datetime.datetime(2020, 9, 1, 14, 30,
  2478. tzinfo=datetime.timezone.utc),
  2479. 'GPL',
  2480. 'https://qubes-os.org',
  2481. 'Qubes template for fedora-31',
  2482. 'Qubes template\n for fedora-31\n'
  2483. ),
  2484. qubesadmin.tools.qvm_template.Template(
  2485. 'test-vm',
  2486. '0',
  2487. '4.1',
  2488. '2020',
  2489. 'qubes-templates-itl',
  2490. 1048576,
  2491. datetime.datetime(2020, 9, 1, 14, 30,
  2492. tzinfo=datetime.timezone.utc),
  2493. 'GPL',
  2494. 'https://qubes-os.org',
  2495. 'Qubes template for fedora-31',
  2496. 'Qubes template\n for fedora-31\n'
  2497. ),
  2498. qubesadmin.tools.qvm_template.Template(
  2499. 'test-vm-3',
  2500. '0',
  2501. '4.1',
  2502. '2020',
  2503. 'qubes-templates-itl',
  2504. 1048576,
  2505. datetime.datetime(2020, 9, 1, 14, 30,
  2506. tzinfo=datetime.timezone.utc),
  2507. 'GPL',
  2508. 'https://qubes-os.org',
  2509. 'Qubes template for fedora-31',
  2510. 'Qubes template\n for fedora-31\n'
  2511. )
  2512. ]
  2513. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  2514. b'0\x00test-vm class=TemplateVM state=Halted\n' \
  2515. b'test-vm-2 class=TemplateVM state=Halted\n' \
  2516. b'test-vm-3 class=TemplateVM state=Halted\n'
  2517. for key, val in [
  2518. ('name', 'test-vm'),
  2519. ('epoch', '1'),
  2520. ('version', '4.0'),
  2521. ('release', '2019')]:
  2522. self.app.expected_calls[(
  2523. 'test-vm',
  2524. 'admin.vm.feature.Get',
  2525. f'template-{key}',
  2526. None)] = b'0\0' + val.encode()
  2527. for key, val in [
  2528. ('name', 'test-vm-2'),
  2529. ('epoch', '1'),
  2530. ('version', '4.0'),
  2531. ('release', '2019')]:
  2532. self.app.expected_calls[(
  2533. 'test-vm-2',
  2534. 'admin.vm.feature.Get',
  2535. f'template-{key}',
  2536. None)] = b'0\0' + val.encode()
  2537. for key, val in [('name', 'test-vm-3-non-managed')]:
  2538. self.app.expected_calls[(
  2539. 'test-vm-3',
  2540. 'admin.vm.feature.Get',
  2541. f'template-{key}',
  2542. None)] = b'0\0' + val.encode()
  2543. args = argparse.Namespace(
  2544. all=False,
  2545. installed=False,
  2546. available=False,
  2547. extras=False,
  2548. upgrades=True,
  2549. all_versions=True,
  2550. machine_readable=False,
  2551. machine_readable_json=False,
  2552. templates=['test-vm*']
  2553. )
  2554. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out, \
  2555. mock.patch.object(self.app.domains['test-vm-2'],
  2556. 'get_disk_utilization') as mock_disk:
  2557. mock_disk.return_value = 1234321
  2558. qubesadmin.tools.qvm_template.list_templates(
  2559. args, self.app, 'list')
  2560. self.assertEqual(mock_out.getvalue(),
  2561. '''Available Upgrades
  2562. [('test-vm', '2:4.1-2020', 'qubes-templates-itl')]
  2563. ''')
  2564. self.assertEqual(mock_disk.mock_calls, [])
  2565. self.assertEqual(mock_query.mock_calls, [
  2566. mock.call(args, self.app, 'test-vm*')
  2567. ])
  2568. self.assertAllCalled()
  2569. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2570. def __test_list_templates_all_success(self, operation,
  2571. args, expected, mock_query):
  2572. mock_query.return_value = [
  2573. qubesadmin.tools.qvm_template.Template(
  2574. 'test-vm',
  2575. '2',
  2576. '4.1',
  2577. '2020',
  2578. 'qubes-templates-itl',
  2579. 1048576,
  2580. datetime.datetime(2020, 9, 1, 14, 30,
  2581. tzinfo=datetime.timezone.utc),
  2582. 'GPL',
  2583. 'https://qubes-os.org',
  2584. 'Qubes template for fedora-31',
  2585. 'Qubes template\n for fedora-31\n'
  2586. )
  2587. ]
  2588. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  2589. b'0\x00test-vm-2 class=TemplateVM state=Halted\n'
  2590. for key, val in [
  2591. ('name', 'test-vm-2'),
  2592. ('epoch', '1'),
  2593. ('version', '4.0'),
  2594. ('release', '2019'),
  2595. ('reponame', '@commandline'),
  2596. ('buildtime', '2020-09-02 14:30:00'),
  2597. ('installtime', '2020-09-02 15:30:00'),
  2598. ('license', 'GPL'),
  2599. ('url', 'https://qubes-os.org'),
  2600. ('summary', 'Summary'),
  2601. ('description', 'Desc|desc')]:
  2602. self.app.expected_calls[(
  2603. 'test-vm-2',
  2604. 'admin.vm.feature.Get',
  2605. f'template-{key}',
  2606. None)] = b'0\0' + val.encode()
  2607. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out, \
  2608. mock.patch.object(self.app.domains['test-vm-2'],
  2609. 'get_disk_utilization') as mock_disk:
  2610. mock_disk.return_value = 1234321
  2611. qubesadmin.tools.qvm_template.list_templates(
  2612. args, self.app, operation)
  2613. self.assertEqual(mock_out.getvalue(), expected)
  2614. self.assertEqual(mock_disk.mock_calls, [mock.call()])
  2615. self.assertEqual(mock_query.mock_calls, [
  2616. mock.call(args, self.app, 'test-vm*')
  2617. ])
  2618. self.assertAllCalled()
  2619. def test_154_list_templates_all_success(self):
  2620. args = argparse.Namespace(
  2621. all=True,
  2622. installed=False,
  2623. available=False,
  2624. extras=False,
  2625. upgrades=False,
  2626. all_versions=True,
  2627. machine_readable=False,
  2628. machine_readable_json=False,
  2629. templates=['test-vm*']
  2630. )
  2631. expected = \
  2632. '''Installed Templates
  2633. [('test-vm-2', '1:4.0-2019', '@commandline')]
  2634. Available Templates
  2635. [('test-vm', '2:4.1-2020', 'qubes-templates-itl')]
  2636. '''
  2637. self.__test_list_templates_all_success('list', args, expected)
  2638. def test_155_list_templates_all_implicit_success(self):
  2639. args = argparse.Namespace(
  2640. all=False,
  2641. installed=False,
  2642. available=False,
  2643. extras=False,
  2644. upgrades=False,
  2645. all_versions=True,
  2646. machine_readable=False,
  2647. machine_readable_json=False,
  2648. templates=['test-vm*']
  2649. )
  2650. expected = \
  2651. '''Installed Templates
  2652. [('test-vm-2', '1:4.0-2019', '@commandline')]
  2653. Available Templates
  2654. [('test-vm', '2:4.1-2020', 'qubes-templates-itl')]
  2655. '''
  2656. self.__test_list_templates_all_success('list', args, expected)
  2657. def test_156_list_templates_info_all_success(self):
  2658. args = argparse.Namespace(
  2659. all=False,
  2660. installed=False,
  2661. available=False,
  2662. extras=False,
  2663. upgrades=False,
  2664. all_versions=True,
  2665. machine_readable=False,
  2666. machine_readable_json=False,
  2667. templates=['test-vm*']
  2668. )
  2669. expected = \
  2670. '''Installed Templates
  2671. [('Name', ':', 'test-vm-2'), ('Epoch', ':', '1'), ('Version', ':', '4.0'), ('Release', ':', '2019'), ('Size', ':', '1.2 MiB'), ('Repository', ':', '@commandline'), ('Buildtime', ':', '2020-09-02 14:30:00'), ('Install time', ':', '2020-09-02 15:30:00'), ('URL', ':', 'https://qubes-os.org'), ('License', ':', 'GPL'), ('Summary', ':', 'Summary'), ('Description', ':', 'Desc'), ('', ':', 'desc'), (' ', ' ', ' ')]
  2672. Available Templates
  2673. [('Name', ':', 'test-vm'), ('Epoch', ':', '2'), ('Version', ':', '4.1'), ('Release', ':', '2020'), ('Size', ':', '1.0 MiB'), ('Repository', ':', 'qubes-templates-itl'), ('Buildtime', ':', '2020-09-01 14:30:00+00:00'), ('URL', ':', 'https://qubes-os.org'), ('License', ':', 'GPL'), ('Summary', ':', 'Qubes template for fedora-31'), ('Description', ':', 'Qubes template'), ('', ':', ' for fedora-31'), (' ', ' ', ' ')]
  2674. '''
  2675. self.__test_list_templates_all_success('info', args, expected)
  2676. def test_157_list_templates_list_all_machinereadable_success(self):
  2677. args = argparse.Namespace(
  2678. all=False,
  2679. installed=False,
  2680. available=False,
  2681. extras=False,
  2682. upgrades=False,
  2683. all_versions=True,
  2684. machine_readable=True,
  2685. machine_readable_json=False,
  2686. templates=['test-vm*']
  2687. )
  2688. expected = \
  2689. '''installed|test-vm-2|1:4.0-2019|@commandline
  2690. available|test-vm|2:4.1-2020|qubes-templates-itl
  2691. '''
  2692. self.__test_list_templates_all_success('list', args, expected)
  2693. def test_158_list_templates_info_all_machinereadable_success(self):
  2694. args = argparse.Namespace(
  2695. all=False,
  2696. installed=False,
  2697. available=False,
  2698. extras=False,
  2699. upgrades=False,
  2700. all_versions=True,
  2701. machine_readable=True,
  2702. machine_readable_json=False,
  2703. templates=['test-vm*']
  2704. )
  2705. expected = \
  2706. '''installed|test-vm-2|1|4.0|2019|@commandline|1234321|2020-09-02 14:30:00|2020-09-02 15:30:00|GPL|https://qubes-os.org|Summary|Desc|desc
  2707. available|test-vm|2|4.1|2020|qubes-templates-itl|1048576|2020-09-01 14:30:00||GPL|https://qubes-os.org|Qubes template for fedora-31|Qubes template| for fedora-31|
  2708. '''
  2709. self.__test_list_templates_all_success('info', args, expected)
  2710. def test_159_list_templates_list_all_machinereadablejson_success(self):
  2711. args = argparse.Namespace(
  2712. all=False,
  2713. installed=False,
  2714. available=False,
  2715. extras=False,
  2716. upgrades=False,
  2717. all_versions=True,
  2718. machine_readable=False,
  2719. machine_readable_json=True,
  2720. templates=['test-vm*']
  2721. )
  2722. expected = \
  2723. '''{"installed": [{"name": "test-vm-2", "evr": "1:4.0-2019", "reponame": "@commandline"}], "available": [{"name": "test-vm", "evr": "2:4.1-2020", "reponame": "qubes-templates-itl"}]}
  2724. '''
  2725. self.__test_list_templates_all_success('list', args, expected)
  2726. def test_160_list_templates_info_all_machinereadablejson_success(self):
  2727. args = argparse.Namespace(
  2728. all=False,
  2729. installed=False,
  2730. available=False,
  2731. extras=False,
  2732. upgrades=False,
  2733. all_versions=True,
  2734. machine_readable=False,
  2735. machine_readable_json=True,
  2736. templates=['test-vm*']
  2737. )
  2738. expected = \
  2739. r'''{"installed": [{"name": "test-vm-2", "epoch": "1", "version": "4.0", "release": "2019", "reponame": "@commandline", "size": "1234321", "buildtime": "2020-09-02 14:30:00", "installtime": "2020-09-02 15:30:00", "license": "GPL", "url": "https://qubes-os.org", "summary": "Summary", "description": "Desc\ndesc"}], "available": [{"name": "test-vm", "epoch": "2", "version": "4.1", "release": "2020", "reponame": "qubes-templates-itl", "size": "1048576", "buildtime": "2020-09-01 14:30:00", "installtime": "", "license": "GPL", "url": "https://qubes-os.org", "summary": "Qubes template for fedora-31", "description": "Qubes template\n for fedora-31\n"}]}
  2740. '''
  2741. self.__test_list_templates_all_success('info', args, expected)
  2742. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2743. def test_161_list_templates_noresults_fail(self, mock_query):
  2744. mock_query.return_value = []
  2745. args = argparse.Namespace(
  2746. all=False,
  2747. installed=False,
  2748. available=True,
  2749. extras=False,
  2750. upgrades=False,
  2751. all_versions=True,
  2752. machine_readable=False,
  2753. machine_readable_json=False,
  2754. templates=[]
  2755. )
  2756. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  2757. with self.assertRaises(SystemExit):
  2758. qubesadmin.tools.qvm_template.list_templates(
  2759. args, self.app, 'list')
  2760. self.assertTrue('No matching templates' in mock_err.getvalue())
  2761. self.assertEqual(mock_query.mock_calls, [
  2762. mock.call(args, self.app)
  2763. ])
  2764. self.assertAllCalled()
  2765. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2766. def test_170_search_success(self, mock_query):
  2767. mock_query.return_value = [
  2768. qubesadmin.tools.qvm_template.Template(
  2769. 'test-vm',
  2770. '2',
  2771. '4.1',
  2772. '2020',
  2773. 'qubes-templates-itl',
  2774. 1048576,
  2775. datetime.datetime(2020, 9, 1, 14, 30,
  2776. tzinfo=datetime.timezone.utc),
  2777. 'GPL',
  2778. 'https://qubes-os.org',
  2779. 'Qubes template for fedora-31',
  2780. 'Qubes template\n for fedora-31\n'
  2781. ),
  2782. qubesadmin.tools.qvm_template.Template(
  2783. 'test-vm',
  2784. '0',
  2785. '4.1',
  2786. '2020',
  2787. 'qubes-templates-itl',
  2788. 1048576,
  2789. datetime.datetime(2020, 9, 1, 14, 30,
  2790. tzinfo=datetime.timezone.utc),
  2791. 'GPL',
  2792. 'https://qubes-os.org',
  2793. 'Older Qubes template for fedora-31',
  2794. 'Older Qubes template\n for fedora-31\n'
  2795. ),
  2796. qubesadmin.tools.qvm_template.Template(
  2797. 'should-not-match-3',
  2798. '0',
  2799. '4.1',
  2800. '2020',
  2801. 'qubes-templates-itl',
  2802. 1048576,
  2803. datetime.datetime(2020, 9, 1, 14, 30,
  2804. tzinfo=datetime.timezone.utc),
  2805. 'GPL',
  2806. 'https://qubes-os.org/test-vm',
  2807. 'Qubes template for fedora-31',
  2808. 'test-vm Qubes template\n for fedora-31\n'
  2809. )
  2810. ]
  2811. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  2812. b'0\x00test-vm-2 class=TemplateVM state=Halted\n'
  2813. for key, val in [
  2814. ('name', 'test-vm-2'),
  2815. ('epoch', '1'),
  2816. ('version', '4.0'),
  2817. ('release', '2019'),
  2818. ('reponame', '@commandline'),
  2819. ('buildtime', '2020-09-02 14:30:00'),
  2820. ('license', 'GPL'),
  2821. ('url', 'https://qubes-os.org'),
  2822. ('summary', 'Summary'),
  2823. ('description', 'Desc|desc')]:
  2824. self.app.expected_calls[(
  2825. 'test-vm-2',
  2826. 'admin.vm.feature.Get',
  2827. f'template-{key}',
  2828. None)] = b'0\0' + val.encode()
  2829. args = argparse.Namespace(
  2830. all=False,
  2831. templates=['test-vm']
  2832. )
  2833. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out, \
  2834. mock.patch.object(self.app.domains['test-vm-2'],
  2835. 'get_disk_utilization') as mock_disk:
  2836. mock_disk.return_value = 1234321
  2837. qubesadmin.tools.qvm_template.search(args, self.app)
  2838. self.assertEqual(mock_out.getvalue(),
  2839. '''=== Name Exactly Matched: test-vm ===
  2840. test-vm : Qubes template for fedora-31
  2841. === Name Matched: test-vm ===
  2842. test-vm-2 : Summary
  2843. ''')
  2844. self.assertEqual(mock_disk.mock_calls, [mock.call()])
  2845. self.assertEqual(mock_query.mock_calls, [
  2846. mock.call(args, self.app)
  2847. ])
  2848. self.assertAllCalled()
  2849. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2850. def test_171_search_summary_success(self, mock_query):
  2851. mock_query.return_value = [
  2852. qubesadmin.tools.qvm_template.Template(
  2853. 'test-template',
  2854. '2',
  2855. '4.1',
  2856. '2020',
  2857. 'qubes-templates-itl',
  2858. 1048576,
  2859. datetime.datetime(2020, 9, 1, 14, 30,
  2860. tzinfo=datetime.timezone.utc),
  2861. 'GPL',
  2862. 'https://qubes-os.org',
  2863. 'Qubes template for test-vm :)',
  2864. 'Qubes template\n for fedora-31\n'
  2865. ),
  2866. qubesadmin.tools.qvm_template.Template(
  2867. 'test-template-exact',
  2868. '2',
  2869. '4.1',
  2870. '2020',
  2871. 'qubes-templates-itl',
  2872. 1048576,
  2873. datetime.datetime(2020, 9, 1, 14, 30,
  2874. tzinfo=datetime.timezone.utc),
  2875. 'GPL',
  2876. 'https://qubes-os.org',
  2877. 'test-vm',
  2878. 'Qubes template\n for fedora-31\n'
  2879. ),
  2880. qubesadmin.tools.qvm_template.Template(
  2881. 'test-vm',
  2882. '2',
  2883. '4.1',
  2884. '2020',
  2885. 'qubes-templates-itl',
  2886. 1048576,
  2887. datetime.datetime(2020, 9, 1, 14, 30,
  2888. tzinfo=datetime.timezone.utc),
  2889. 'GPL',
  2890. 'https://qubes-os.org',
  2891. 'Qubes template for test-vm',
  2892. 'Qubes template\n for fedora-31\n'
  2893. ),
  2894. qubesadmin.tools.qvm_template.Template(
  2895. 'test-vm-2',
  2896. '2',
  2897. '4.1',
  2898. '2020',
  2899. 'qubes-templates-itl',
  2900. 1048576,
  2901. datetime.datetime(2020, 9, 1, 14, 30,
  2902. tzinfo=datetime.timezone.utc),
  2903. 'GPL',
  2904. 'https://qubes-os.org',
  2905. 'Qubes template for test-vm-2',
  2906. 'Qubes template\n for fedora-31\n'
  2907. ),
  2908. ]
  2909. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  2910. b'0\x00'
  2911. args = argparse.Namespace(
  2912. all=False,
  2913. templates=['test-vm']
  2914. )
  2915. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out:
  2916. qubesadmin.tools.qvm_template.search(args, self.app)
  2917. self.assertEqual(mock_out.getvalue(),
  2918. '''=== Name & Summary Matched: test-vm ===
  2919. test-vm : Qubes template for test-vm
  2920. test-vm-2 : Qubes template for test-vm-2
  2921. === Summary Matched: test-vm ===
  2922. test-template : Qubes template for test-vm :)
  2923. === Summary Exactly Matched: test-vm ===
  2924. test-template-exact : test-vm
  2925. ''')
  2926. self.assertEqual(mock_query.mock_calls, [
  2927. mock.call(args, self.app)
  2928. ])
  2929. self.assertAllCalled()
  2930. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2931. def test_172_search_namesummaryexact_success(self, mock_query):
  2932. mock_query.return_value = [
  2933. qubesadmin.tools.qvm_template.Template(
  2934. 'test-template-exact',
  2935. '2',
  2936. '4.1',
  2937. '2020',
  2938. 'qubes-templates-itl',
  2939. 1048576,
  2940. datetime.datetime(2020, 9, 1, 14, 30,
  2941. tzinfo=datetime.timezone.utc),
  2942. 'GPL',
  2943. 'https://qubes-os.org',
  2944. 'test-vm',
  2945. 'Qubes template\n for fedora-31\n'
  2946. ),
  2947. qubesadmin.tools.qvm_template.Template(
  2948. 'test-vm',
  2949. '2',
  2950. '4.1',
  2951. '2020',
  2952. 'qubes-templates-itl',
  2953. 1048576,
  2954. datetime.datetime(2020, 9, 1, 14, 30,
  2955. tzinfo=datetime.timezone.utc),
  2956. 'GPL',
  2957. 'https://qubes-os.org',
  2958. 'test-vm',
  2959. 'Qubes template\n for fedora-31\n'
  2960. ),
  2961. qubesadmin.tools.qvm_template.Template(
  2962. 'test-vm-2',
  2963. '2',
  2964. '4.1',
  2965. '2020',
  2966. 'qubes-templates-itl',
  2967. 1048576,
  2968. datetime.datetime(2020, 9, 1, 14, 30,
  2969. tzinfo=datetime.timezone.utc),
  2970. 'GPL',
  2971. 'https://qubes-os.org',
  2972. 'test-vm',
  2973. 'Qubes template\n for fedora-31\n'
  2974. )
  2975. ]
  2976. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  2977. b'0\x00'
  2978. args = argparse.Namespace(
  2979. all=False,
  2980. templates=['test-vm']
  2981. )
  2982. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out:
  2983. qubesadmin.tools.qvm_template.search(args, self.app)
  2984. self.assertEqual(mock_out.getvalue(),
  2985. '''=== Name & Summary Exactly Matched: test-vm ===
  2986. test-vm : test-vm
  2987. === Name & Summary Matched: test-vm ===
  2988. test-vm-2 : test-vm
  2989. === Summary Exactly Matched: test-vm ===
  2990. test-template-exact : test-vm
  2991. ''')
  2992. self.assertEqual(mock_query.mock_calls, [
  2993. mock.call(args, self.app)
  2994. ])
  2995. self.assertAllCalled()
  2996. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  2997. def test_173_search_multiquery_success(self, mock_query):
  2998. mock_query.return_value = [
  2999. qubesadmin.tools.qvm_template.Template(
  3000. 'test-template-exact',
  3001. '2',
  3002. '4.1',
  3003. '2020',
  3004. 'qubes-templates-itl',
  3005. 1048576,
  3006. datetime.datetime(2020, 9, 1, 14, 30,
  3007. tzinfo=datetime.timezone.utc),
  3008. 'GPL',
  3009. 'https://qubes-os.org',
  3010. 'test-vm',
  3011. 'Qubes template\n for fedora-31\n'
  3012. ),
  3013. qubesadmin.tools.qvm_template.Template(
  3014. 'test-vm',
  3015. '2',
  3016. '4.1',
  3017. '2020',
  3018. 'qubes-templates-itl',
  3019. 1048576,
  3020. datetime.datetime(2020, 9, 1, 14, 30,
  3021. tzinfo=datetime.timezone.utc),
  3022. 'GPL',
  3023. 'https://qubes-os.org',
  3024. 'test-vm',
  3025. 'Qubes template\n for fedora-31\n'
  3026. ),
  3027. qubesadmin.tools.qvm_template.Template(
  3028. 'should-not-match',
  3029. '2',
  3030. '4.1',
  3031. '2020',
  3032. 'qubes-templates-itl',
  3033. 1048576,
  3034. datetime.datetime(2020, 9, 1, 14, 30,
  3035. tzinfo=datetime.timezone.utc),
  3036. 'GPL',
  3037. 'https://qubes-os.org',
  3038. 'Summary',
  3039. 'test-vm Qubes template\n for fedora-31\n'
  3040. )
  3041. ]
  3042. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  3043. b'0\x00'
  3044. args = argparse.Namespace(
  3045. all=False,
  3046. templates=['test-vm', 'test-template']
  3047. )
  3048. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out:
  3049. qubesadmin.tools.qvm_template.search(args, self.app)
  3050. self.assertEqual(mock_out.getvalue(),
  3051. '''=== Name & Summary Matched: test-template, test-vm ===
  3052. test-template-exact : test-vm
  3053. ''')
  3054. self.assertEqual(mock_query.mock_calls, [
  3055. mock.call(args, self.app)
  3056. ])
  3057. self.assertAllCalled()
  3058. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  3059. def test_174_search_multiquery_exact_success(self, mock_query):
  3060. mock_query.return_value = [
  3061. qubesadmin.tools.qvm_template.Template(
  3062. 'test-vm',
  3063. '2',
  3064. '4.1',
  3065. '2020',
  3066. 'qubes-templates-itl',
  3067. 1048576,
  3068. datetime.datetime(2020, 9, 1, 14, 30,
  3069. tzinfo=datetime.timezone.utc),
  3070. 'GPL',
  3071. 'https://qubes-os.org',
  3072. 'summary',
  3073. 'Qubes template\n for fedora-31\n'
  3074. ),
  3075. qubesadmin.tools.qvm_template.Template(
  3076. 'summary',
  3077. '2',
  3078. '4.1',
  3079. '2020',
  3080. 'qubes-templates-itl',
  3081. 1048576,
  3082. datetime.datetime(2020, 9, 1, 14, 30,
  3083. tzinfo=datetime.timezone.utc),
  3084. 'GPL',
  3085. 'https://qubes-os.org',
  3086. 'test-vm Summary',
  3087. 'Qubes template\n for fedora-31\n'
  3088. )
  3089. ]
  3090. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  3091. b'0\x00'
  3092. args = argparse.Namespace(
  3093. all=False,
  3094. templates=['test-vm', 'summary']
  3095. )
  3096. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out:
  3097. qubesadmin.tools.qvm_template.search(args, self.app)
  3098. self.assertEqual(mock_out.getvalue(),
  3099. '''=== Name & Summary Matched: summary, test-vm ===
  3100. summary : test-vm Summary
  3101. === Name & Summary Exactly Matched: summary, test-vm ===
  3102. test-vm : summary
  3103. ''')
  3104. self.assertEqual(mock_query.mock_calls, [
  3105. mock.call(args, self.app)
  3106. ])
  3107. self.assertAllCalled()
  3108. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  3109. def test_175_search_all_success(self, mock_query):
  3110. mock_query.return_value = [
  3111. qubesadmin.tools.qvm_template.Template(
  3112. 'test-vm',
  3113. '2',
  3114. '4.1',
  3115. '2020',
  3116. 'qubes-templates-itl',
  3117. 1048576,
  3118. datetime.datetime(2020, 9, 1, 14, 30,
  3119. tzinfo=datetime.timezone.utc),
  3120. 'GPL',
  3121. 'https://qubes-os.org/keyword-url',
  3122. 'summary',
  3123. 'Qubes template\n for fedora-31\n'
  3124. ),
  3125. qubesadmin.tools.qvm_template.Template(
  3126. 'test-vm-exact',
  3127. '2',
  3128. '4.1',
  3129. '2020',
  3130. 'qubes-templates-itl',
  3131. 1048576,
  3132. datetime.datetime(2020, 9, 1, 14, 30,
  3133. tzinfo=datetime.timezone.utc),
  3134. 'GPL',
  3135. 'https://qubes-os.org',
  3136. 'test-vm Summary',
  3137. 'Qubes template\n for fedora-31\n'
  3138. ),
  3139. qubesadmin.tools.qvm_template.Template(
  3140. 'test-vm-exac2',
  3141. '2',
  3142. '4.1',
  3143. '2020',
  3144. 'qubes-templates-itl',
  3145. 1048576,
  3146. datetime.datetime(2020, 9, 1, 14, 30,
  3147. tzinfo=datetime.timezone.utc),
  3148. 'GPL',
  3149. 'test-vm-exac2',
  3150. 'test-vm Summary',
  3151. 'Qubes template\n for fedora-31\n'
  3152. ),
  3153. qubesadmin.tools.qvm_template.Template(
  3154. 'test-vm-2',
  3155. '2',
  3156. '4.1',
  3157. '2020',
  3158. 'qubes-templates-itl',
  3159. 1048576,
  3160. datetime.datetime(2020, 9, 1, 14, 30,
  3161. tzinfo=datetime.timezone.utc),
  3162. 'GPL',
  3163. 'https://qubes-os.org',
  3164. 'test-vm Summary',
  3165. 'keyword-desc'
  3166. ),
  3167. qubesadmin.tools.qvm_template.Template(
  3168. 'should-not-match',
  3169. '2',
  3170. '4.1',
  3171. '2020',
  3172. 'qubes-templates-itl',
  3173. 1048576,
  3174. datetime.datetime(2020, 9, 1, 14, 30,
  3175. tzinfo=datetime.timezone.utc),
  3176. 'GPL',
  3177. 'https://qubes-os.org',
  3178. 'Summary',
  3179. 'Description'
  3180. )
  3181. ]
  3182. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  3183. b'0\x00'
  3184. args = argparse.Namespace(
  3185. all=True,
  3186. templates=['test-vm-exact', 'test-vm-exac2',
  3187. 'keyword-url', 'keyword-desc']
  3188. )
  3189. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out:
  3190. qubesadmin.tools.qvm_template.search(args, self.app)
  3191. self.assertEqual(mock_out.getvalue(),
  3192. '''=== Name & URL Exactly Matched: test-vm-exac2 ===
  3193. test-vm-exac2 : test-vm Summary
  3194. === Name Exactly Matched: test-vm-exact ===
  3195. test-vm-exact : test-vm Summary
  3196. === Description Exactly Matched: keyword-desc ===
  3197. test-vm-2 : test-vm Summary
  3198. === URL Matched: keyword-url ===
  3199. test-vm : summary
  3200. ''')
  3201. self.assertEqual(mock_query.mock_calls, [
  3202. mock.call(args, self.app)
  3203. ])
  3204. self.assertAllCalled()
  3205. @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
  3206. def test_176_search_wildcard_success(self, mock_query):
  3207. mock_query.return_value = [
  3208. qubesadmin.tools.qvm_template.Template(
  3209. 'test-vm',
  3210. '2',
  3211. '4.1',
  3212. '2020',
  3213. 'qubes-templates-itl',
  3214. 1048576,
  3215. datetime.datetime(2020, 9, 1, 14, 30,
  3216. tzinfo=datetime.timezone.utc),
  3217. 'GPL',
  3218. 'https://qubes-os.org',
  3219. 'Qubes template for fedora-31',
  3220. 'Qubes template\n for fedora-31\n'
  3221. ),
  3222. qubesadmin.tools.qvm_template.Template(
  3223. 'should-not-match-3',
  3224. '0',
  3225. '4.1',
  3226. '2020',
  3227. 'qubes-templates-itl',
  3228. 1048576,
  3229. datetime.datetime(2020, 9, 1, 14, 30,
  3230. tzinfo=datetime.timezone.utc),
  3231. 'GPL',
  3232. 'https://qubes-os.org/test-vm',
  3233. 'Qubes template for fedora-31',
  3234. 'test-vm Qubes template\n for fedora-31\n'
  3235. )
  3236. ]
  3237. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  3238. b'0\x00'
  3239. args = argparse.Namespace(
  3240. all=False,
  3241. templates=['t?st-vm']
  3242. )
  3243. with mock.patch('sys.stdout', new=io.StringIO()) as mock_out:
  3244. qubesadmin.tools.qvm_template.search(args, self.app)
  3245. self.assertEqual(mock_out.getvalue(),
  3246. '''=== Name Matched: t?st-vm ===
  3247. test-vm : Qubes template for fedora-31
  3248. ''')
  3249. self.assertEqual(mock_query.mock_calls, [
  3250. mock.call(args, self.app)
  3251. ])
  3252. self.assertAllCalled()
  3253. def _mock_qrexec_download(self, args, app, spec, path,
  3254. dlsize=None, refresh=False):
  3255. self.assertFalse(os.path.exists(path),
  3256. '{} should not exist before'.format(path))
  3257. # just create an empty file
  3258. with open(path, 'wb') as f:
  3259. if f is not None:
  3260. f.truncate(dlsize)
  3261. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3262. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3263. @mock.patch('qubesadmin.tools.qvm_template.qrexec_download')
  3264. def test_180_download_success(self, mock_qrexec, mock_dllist,
  3265. mock_verify_rpm):
  3266. mock_qrexec.side_effect = self._mock_qrexec_download
  3267. with tempfile.TemporaryDirectory() as dir:
  3268. args = argparse.Namespace(
  3269. repo_files=[],
  3270. keyring='/tmp/keyring.gpg',
  3271. releasever='4.1',
  3272. nogpgcheck=False,
  3273. retries=1
  3274. )
  3275. qubesadmin.tools.qvm_template.download(args, self.app, dir, {
  3276. 'fedora-31': qubesadmin.tools.qvm_template.DlEntry(
  3277. ('1', '2', '3'), 'qubes-templates-itl', 1048576),
  3278. 'fedora-32': qubesadmin.tools.qvm_template.DlEntry(
  3279. ('0', '1', '2'),
  3280. 'qubes-templates-itl-testing',
  3281. 2048576)
  3282. })
  3283. self.assertEqual(mock_qrexec.mock_calls, [
  3284. mock.call(args, self.app, 'qubes-template-fedora-31-1:2-3',
  3285. re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3286. 1048576),
  3287. mock.call(args, self.app, 'qubes-template-fedora-32-0:1-2',
  3288. re_str(dir + '/.*/qubes-template-fedora-32-0:1-2.rpm.UNTRUSTED'),
  3289. 2048576)
  3290. ])
  3291. self.assertEqual(mock_dllist.mock_calls, [])
  3292. self.assertEqual(mock_verify_rpm.mock_calls, [
  3293. mock.call(re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3294. '/tmp/keyring.gpg', template_name='fedora-31'),
  3295. mock.call(re_str(dir + '/.*/qubes-template-fedora-32-0:1-2.rpm.UNTRUSTED'),
  3296. '/tmp/keyring.gpg', template_name='fedora-32'),
  3297. ])
  3298. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3299. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3300. @mock.patch('qubesadmin.tools.qvm_template.qrexec_download')
  3301. def test_181_download_success_nosuffix(self, mock_qrexec, mock_dllist,
  3302. mock_verify_rpm):
  3303. mock_qrexec.side_effect = self._mock_qrexec_download
  3304. with tempfile.TemporaryDirectory() as dir:
  3305. args = argparse.Namespace(
  3306. retries=1,
  3307. repo_files=[],
  3308. keyring='/tmp/keyring.gpg',
  3309. releasever='4.1',
  3310. nogpgcheck=False,
  3311. downloaddir=dir
  3312. )
  3313. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  3314. qubesadmin.tools.qvm_template.download(args, self.app, None, {
  3315. 'fedora-31': qubesadmin.tools.qvm_template.DlEntry(
  3316. ('1', '2', '3'), 'qubes-templates-itl', 1048576)
  3317. })
  3318. self.assertEqual(mock_qrexec.mock_calls, [
  3319. mock.call(args, self.app, 'qubes-template-fedora-31-1:2-3',
  3320. re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3321. 1048576)
  3322. ])
  3323. self.assertEqual(mock_dllist.mock_calls, [])
  3324. self.assertEqual(mock_verify_rpm.mock_calls, [
  3325. mock.call(re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3326. '/tmp/keyring.gpg', template_name='fedora-31'),
  3327. ])
  3328. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3329. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3330. @mock.patch('qubesadmin.tools.qvm_template.qrexec_download')
  3331. def test_182_download_success_getdllist(self, mock_qrexec, mock_dllist,
  3332. mock_verify_rpm):
  3333. mock_qrexec.side_effect = self._mock_qrexec_download
  3334. mock_dllist.return_value = {
  3335. 'fedora-31': qubesadmin.tools.qvm_template.DlEntry(
  3336. ('1', '2', '3'), 'qubes-templates-itl', 1048576)
  3337. }
  3338. with tempfile.TemporaryDirectory() as dir:
  3339. args = argparse.Namespace(
  3340. retries=1,
  3341. repo_files=[],
  3342. keyring='/tmp/keyring.gpg',
  3343. releasever='4.1',
  3344. nogpgcheck=False,
  3345. )
  3346. qubesadmin.tools.qvm_template.download(args, self.app,
  3347. dir, None,
  3348. qubesadmin.tools.qvm_template.VersionSelector.LATEST_LOWER)
  3349. self.assertEqual(mock_qrexec.mock_calls, [
  3350. mock.call(args, self.app, 'qubes-template-fedora-31-1:2-3',
  3351. re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3352. 1048576)
  3353. ])
  3354. self.assertEqual(mock_dllist.mock_calls, [
  3355. mock.call(args, self.app,
  3356. version_selector=\
  3357. qubesadmin.tools.qvm_template.\
  3358. VersionSelector.LATEST_LOWER)
  3359. ])
  3360. self.assertEqual(mock_verify_rpm.mock_calls, [
  3361. mock.call(re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3362. '/tmp/keyring.gpg', template_name='fedora-31'),
  3363. ])
  3364. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3365. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3366. @mock.patch('qubesadmin.tools.qvm_template.qrexec_download')
  3367. def test_183_download_success_downloaddir(self, mock_qrexec, mock_dllist,
  3368. mock_verify_rpm):
  3369. mock_qrexec.side_effect = self._mock_qrexec_download
  3370. with tempfile.TemporaryDirectory() as dir:
  3371. args = argparse.Namespace(
  3372. retries=1,
  3373. repo_files=[],
  3374. keyring='/tmp/keyring.gpg',
  3375. releasever='4.1',
  3376. nogpgcheck=False,
  3377. downloaddir=dir
  3378. )
  3379. qubesadmin.tools.qvm_template.download(args, self.app, None, {
  3380. 'fedora-31': qubesadmin.tools.qvm_template.DlEntry(
  3381. ('1', '2', '3'), 'qubes-templates-itl', 1048576)
  3382. })
  3383. self.assertEqual(mock_qrexec.mock_calls, [
  3384. mock.call(args, self.app, 'qubes-template-fedora-31-1:2-3',
  3385. re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3386. 1048576)
  3387. ])
  3388. self.assertEqual(mock_dllist.mock_calls, [])
  3389. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3390. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3391. @mock.patch('qubesadmin.tools.qvm_template.qrexec_download')
  3392. def test_184_download_success_exists(self, mock_qrexec, mock_dllist,
  3393. mock_verify_rpm):
  3394. mock_qrexec.side_effect = self._mock_qrexec_download
  3395. with tempfile.TemporaryDirectory() as dir:
  3396. with open(os.path.join(
  3397. dir, 'qubes-template-fedora-31-1:2-3.rpm'),
  3398. 'w') as _:
  3399. pass
  3400. args = argparse.Namespace(
  3401. retries=1,
  3402. repo_files=[],
  3403. keyring='/tmp/keyring.gpg',
  3404. releasever='4.1',
  3405. nogpgcheck=False,
  3406. downloaddir=dir
  3407. )
  3408. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  3409. qubesadmin.tools.qvm_template.download(args, self.app, None, {
  3410. 'fedora-31': qubesadmin.tools.qvm_template.DlEntry(
  3411. ('1', '2', '3'), 'qubes-templates-itl', 1048576),
  3412. 'fedora-32': qubesadmin.tools.qvm_template.DlEntry(
  3413. ('0', '1', '2'),
  3414. 'qubes-templates-itl-testing',
  3415. 2048576)
  3416. })
  3417. self.assertTrue('already exists, skipping'
  3418. in mock_err.getvalue())
  3419. self.assertEqual(mock_qrexec.mock_calls, [
  3420. mock.call(args, self.app, 'qubes-template-fedora-32-0:1-2',
  3421. re_str(dir + '/.*/qubes-template-fedora-32-0:1-2.rpm.UNTRUSTED'),
  3422. 2048576)
  3423. ])
  3424. self.assertEqual(mock_dllist.mock_calls, [])
  3425. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3426. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3427. @mock.patch('qubesadmin.tools.qvm_template.qrexec_download')
  3428. def test_185_download_success_existsmove(self, mock_qrexec, mock_dllist,
  3429. mock_verify_rpm):
  3430. mock_qrexec.side_effect = self._mock_qrexec_download
  3431. with tempfile.TemporaryDirectory() as dir:
  3432. with open(os.path.join(
  3433. dir, 'qubes-template-fedora-31-1:2-3.rpm'),
  3434. 'w') as _:
  3435. pass
  3436. args = argparse.Namespace(
  3437. retries=1,
  3438. repo_files=[],
  3439. keyring='/tmp/keyring.gpg',
  3440. releasever='4.1',
  3441. nogpgcheck=False,
  3442. downloaddir=dir
  3443. )
  3444. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  3445. qubesadmin.tools.qvm_template.download(args, self.app, None, {
  3446. 'fedora-31': qubesadmin.tools.qvm_template.DlEntry(
  3447. ('1', '2', '3'), 'qubes-templates-itl', 1048576)
  3448. })
  3449. self.assertTrue('already exists, skipping'
  3450. in mock_err.getvalue())
  3451. self.assertEqual(mock_qrexec.mock_calls, [])
  3452. self.assertEqual(mock_dllist.mock_calls, [])
  3453. self.assertTrue(os.path.exists(
  3454. dir + '/qubes-template-fedora-31-1:2-3.rpm'))
  3455. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3456. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3457. @mock.patch('qubesadmin.tools.qvm_template.qrexec_download')
  3458. def test_186_download_success_existsnosuffix(self, mock_qrexec, mock_dllist,
  3459. mock_verify_rpm):
  3460. mock_qrexec.side_effect = self._mock_qrexec_download
  3461. with tempfile.TemporaryDirectory() as dir:
  3462. with open(os.path.join(
  3463. dir, 'qubes-template-fedora-31-1:2-3.rpm'),
  3464. 'w') as _:
  3465. pass
  3466. args = argparse.Namespace(
  3467. retries=1,
  3468. repo_files=[],
  3469. keyring='/tmp/keyring.gpg',
  3470. releasever='4.1',
  3471. nogpgcheck=False,
  3472. downloaddir=dir
  3473. )
  3474. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  3475. qubesadmin.tools.qvm_template.download(args, self.app, None, {
  3476. 'fedora-31': qubesadmin.tools.qvm_template.DlEntry(
  3477. ('1', '2', '3'), 'qubes-templates-itl', 1048576)
  3478. })
  3479. self.assertTrue('already exists, skipping'
  3480. in mock_err.getvalue())
  3481. self.assertEqual(mock_qrexec.mock_calls, [])
  3482. self.assertEqual(mock_dllist.mock_calls, [])
  3483. self.assertTrue(os.path.exists(
  3484. dir + '/qubes-template-fedora-31-1:2-3.rpm'))
  3485. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3486. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3487. @mock.patch('qubesadmin.tools.qvm_template.qrexec_download')
  3488. def test_187_download_success_retry(self, mock_qrexec, mock_dllist,
  3489. mock_verify_rpm):
  3490. counter = 0
  3491. def f(*args, **kwargs):
  3492. nonlocal counter
  3493. counter += 1
  3494. if counter == 1:
  3495. raise ConnectionError
  3496. self._mock_qrexec_download(*args, **kwargs)
  3497. mock_qrexec.side_effect = f
  3498. with tempfile.TemporaryDirectory() as dir:
  3499. args = argparse.Namespace(
  3500. retries=2,
  3501. repo_files=[],
  3502. keyring='/tmp/keyring.gpg',
  3503. releasever='4.1',
  3504. nogpgcheck=False,
  3505. downloaddir=dir
  3506. )
  3507. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err, \
  3508. mock.patch('os.remove') as mock_rm:
  3509. qubesadmin.tools.qvm_template.download(args, self.app, None, {
  3510. 'fedora-31': qubesadmin.tools.qvm_template.DlEntry(
  3511. ('1', '2', '3'), 'qubes-templates-itl', 1048576)
  3512. })
  3513. self.assertTrue('retrying...' in mock_err.getvalue())
  3514. self.assertEqual(mock_rm.mock_calls, [
  3515. mock.call(re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'))
  3516. ])
  3517. self.assertEqual(mock_qrexec.mock_calls, [
  3518. mock.call(args, self.app, 'qubes-template-fedora-31-1:2-3',
  3519. re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3520. 1048576),
  3521. mock.call(args, self.app, 'qubes-template-fedora-31-1:2-3',
  3522. re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3523. 1048576)
  3524. ])
  3525. self.assertEqual(mock_dllist.mock_calls, [])
  3526. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3527. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3528. @mock.patch('qubesadmin.tools.qvm_template.qrexec_download')
  3529. def test_188_download_fail_retry(self, mock_qrexec, mock_dllist,
  3530. mock_verify_rpm):
  3531. mock_qrexec.side_effect = self._mock_qrexec_download
  3532. counter = 0
  3533. def f(*args, **kwargs):
  3534. nonlocal counter
  3535. counter += 1
  3536. if counter <= 3:
  3537. raise ConnectionError
  3538. self._mock_qrexec_download(*args, **kwargs)
  3539. mock_qrexec.side_effect = f
  3540. with tempfile.TemporaryDirectory() as dir:
  3541. args = argparse.Namespace(
  3542. retries=3,
  3543. repo_files=[],
  3544. keyring='/tmp/keyring.gpg',
  3545. releasever='4.1',
  3546. nogpgcheck=False,
  3547. downloaddir=dir
  3548. )
  3549. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err, \
  3550. mock.patch('os.remove') as mock_rm:
  3551. with self.assertRaises(SystemExit):
  3552. qubesadmin.tools.qvm_template.download(
  3553. args, self.app, None, {
  3554. 'fedora-31': qubesadmin.tools.qvm_template.DlEntry(
  3555. ('1', '2', '3'), 'qubes-templates-itl', 1048576)
  3556. })
  3557. self.assertEqual(mock_err.getvalue().count('retrying...'), 2)
  3558. self.assertTrue('download failed' in mock_err.getvalue())
  3559. self.assertEqual(mock_rm.mock_calls, [
  3560. mock.call(re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED')),
  3561. mock.call(re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED')),
  3562. mock.call(re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'))
  3563. ])
  3564. self.assertEqual(mock_qrexec.mock_calls, [
  3565. mock.call(args, self.app, 'qubes-template-fedora-31-1:2-3',
  3566. re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3567. 1048576),
  3568. mock.call(args, self.app, 'qubes-template-fedora-31-1:2-3',
  3569. re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3570. 1048576),
  3571. mock.call(args, self.app, 'qubes-template-fedora-31-1:2-3',
  3572. re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3573. 1048576)
  3574. ])
  3575. self.assertEqual(mock_dllist.mock_calls, [])
  3576. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3577. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3578. @mock.patch('qubesadmin.tools.qvm_template.qrexec_download')
  3579. def test_189_download_fail_interrupt(self, mock_qrexec, mock_dllist,
  3580. mock_verify_rpm):
  3581. def f(*args):
  3582. raise RuntimeError
  3583. mock_qrexec.side_effect = f
  3584. with tempfile.TemporaryDirectory() as dir:
  3585. args = argparse.Namespace(
  3586. retries=3,
  3587. repo_files=[],
  3588. keyring='/tmp/keyring.gpg',
  3589. releasever='4.1',
  3590. nogpgcheck=False,
  3591. downloaddir=dir
  3592. )
  3593. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err, \
  3594. mock.patch('os.remove') as mock_rm:
  3595. with self.assertRaises(RuntimeError):
  3596. qubesadmin.tools.qvm_template.download(
  3597. args, self.app, None, {
  3598. 'fedora-31': qubesadmin.tools.qvm_template.DlEntry(
  3599. ('1', '2', '3'), 'qubes-templates-itl', 1048576)
  3600. })
  3601. self.assertEqual(mock_qrexec.mock_calls, [
  3602. mock.call(args, self.app, 'qubes-template-fedora-31-1:2-3',
  3603. re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm'),
  3604. 1048576)
  3605. ])
  3606. self.assertEqual(mock_dllist.mock_calls, [])
  3607. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3608. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3609. @mock.patch('qubesadmin.tools.qvm_template.qrexec_download')
  3610. def test_190_download_fail_verify(self, mock_qrexec, mock_dllist,
  3611. mock_verify_rpm):
  3612. mock_qrexec.side_effect = self._mock_qrexec_download
  3613. mock_verify_rpm.side_effect = \
  3614. qubesadmin.tools.qvm_template.SignatureVerificationError
  3615. with tempfile.TemporaryDirectory() as dir:
  3616. args = argparse.Namespace(
  3617. retries=3,
  3618. repo_files=[],
  3619. keyring='/tmp/keyring.gpg',
  3620. releasever='4.1',
  3621. nogpgcheck=True, # make sure it gets ignored
  3622. downloaddir=dir
  3623. )
  3624. with self.assertRaises(qubesadmin.tools.qvm_template.SignatureVerificationError):
  3625. qubesadmin.tools.qvm_template.download(
  3626. args, self.app, None, {
  3627. 'fedora-31': qubesadmin.tools.qvm_template.DlEntry(
  3628. ('1', '2', '3'), 'qubes-templates-itl', 1048576)
  3629. })
  3630. self.assertEqual(mock_qrexec.mock_calls, [
  3631. mock.call(args, self.app, 'qubes-template-fedora-31-1:2-3',
  3632. re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm'),
  3633. 1048576)
  3634. ])
  3635. self.assertEqual(mock_dllist.mock_calls, [])
  3636. self.assertEqual(os.listdir(dir), [])
  3637. self.assertEqual(mock_verify_rpm.mock_calls, [
  3638. mock.call(re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3639. '/tmp/keyring.gpg', template_name='fedora-31'),
  3640. ])
  3641. def _mock_qrexec_download_short(self, args, app, spec, path,
  3642. dlsize=None, refresh=False):
  3643. self.assertFalse(os.path.exists(path),
  3644. '{} should not exist before'.format(path))
  3645. # just create an empty file
  3646. with open(path, 'wb') as f:
  3647. if f is not None:
  3648. f.truncate(dlsize // 2)
  3649. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3650. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3651. @mock.patch('qubesadmin.tools.qvm_template.qrexec_download')
  3652. def test_191_download_fail_short(self, mock_qrexec, mock_dllist,
  3653. mock_verify_rpm):
  3654. mock_qrexec.side_effect = self._mock_qrexec_download_short
  3655. with tempfile.TemporaryDirectory() as dir, \
  3656. self.assertRaises(SystemExit):
  3657. args = argparse.Namespace(
  3658. repo_files=[],
  3659. keyring='/tmp/keyring.gpg',
  3660. releasever='4.1',
  3661. nogpgcheck=False,
  3662. retries=1
  3663. )
  3664. qubesadmin.tools.qvm_template.download(args, self.app, dir, {
  3665. 'fedora-31': qubesadmin.tools.qvm_template.DlEntry(
  3666. ('1', '2', '3'), 'qubes-templates-itl', 1048576),
  3667. })
  3668. self.assertEqual(mock_qrexec.mock_calls, [
  3669. mock.call(args, self.app, 'qubes-template-fedora-31-1:2-3',
  3670. re_str(dir + '/.*/qubes-template-fedora-31-1:2-3.rpm.UNTRUSTED'),
  3671. 1048576),
  3672. ])
  3673. self.assertEqual(mock_dllist.mock_calls, [])
  3674. self.assertEqual(mock_verify_rpm.mock_calls, [])
  3675. @mock.patch('os.rename')
  3676. @mock.patch('os.makedirs')
  3677. @mock.patch('subprocess.check_call')
  3678. @mock.patch('qubesadmin.tools.qvm_template.confirm_action')
  3679. @mock.patch('qubesadmin.tools.qvm_template.extract_rpm')
  3680. @mock.patch('qubesadmin.tools.qvm_template.download')
  3681. @mock.patch('qubesadmin.tools.qvm_template.get_dl_list')
  3682. @mock.patch('qubesadmin.tools.qvm_template.verify_rpm')
  3683. def test_200_reinstall_success(
  3684. self,
  3685. mock_verify,
  3686. mock_dl_list,
  3687. mock_dl,
  3688. mock_extract,
  3689. mock_confirm,
  3690. mock_call,
  3691. mock_mkdirs,
  3692. mock_rename):
  3693. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  3694. b'0\x00test-vm class=TemplateVM state=Halted\n'
  3695. build_time = '2020-09-01 14:30:00' # 1598970600
  3696. install_time = '2020-09-01 15:30:00'
  3697. for key, val in [
  3698. ('name', 'test-vm'),
  3699. ('epoch', '2'),
  3700. ('version', '4.1'),
  3701. ('release', '2020')]:
  3702. self.app.expected_calls[(
  3703. 'test-vm',
  3704. 'admin.vm.feature.Get',
  3705. f'template-{key}',
  3706. None)] = b'0\0' + val.encode()
  3707. for key, val in [
  3708. ('name', 'test-vm'),
  3709. ('epoch', '2'),
  3710. ('version', '4.1'),
  3711. ('release', '2020'),
  3712. ('reponame', 'qubes-templates-itl'),
  3713. ('buildtime', build_time),
  3714. ('installtime', install_time),
  3715. ('license', 'GPL'),
  3716. ('url', 'https://qubes-os.org'),
  3717. ('summary', 'Summary'),
  3718. ('description', 'Desc|desc')]:
  3719. self.app.expected_calls[(
  3720. 'test-vm',
  3721. 'admin.vm.feature.Set',
  3722. f'template-{key}',
  3723. val.encode())] = b'0\0'
  3724. rpm_hdr = {
  3725. rpm.RPMTAG_NAME : 'qubes-template-test-vm',
  3726. rpm.RPMTAG_BUILDTIME : 1598970600,
  3727. rpm.RPMTAG_DESCRIPTION : 'Desc\ndesc',
  3728. rpm.RPMTAG_EPOCHNUM : 2,
  3729. rpm.RPMTAG_LICENSE : 'GPL',
  3730. rpm.RPMTAG_RELEASE : '2020',
  3731. rpm.RPMTAG_SUMMARY : 'Summary',
  3732. rpm.RPMTAG_URL : 'https://qubes-os.org',
  3733. rpm.RPMTAG_VERSION : '4.1'
  3734. }
  3735. mock_dl.return_value = {'test-vm': rpm_hdr}
  3736. dl_list = {
  3737. 'test-vm': qubesadmin.tools.qvm_template.DlEntry(
  3738. ('1', '4.1', '20200101'), 'qubes-templates-itl', 1048576)
  3739. }
  3740. mock_dl_list.return_value = dl_list
  3741. mock_call.side_effect = self.add_new_vm_side_effect
  3742. mock_time = mock.Mock(wraps=datetime.datetime)
  3743. mock_time.now.return_value = \
  3744. datetime.datetime(2020, 9, 1, 15, 30, tzinfo=datetime.timezone.utc)
  3745. selector = qubesadmin.tools.qvm_template.VersionSelector.REINSTALL
  3746. with mock.patch('qubesadmin.tools.qvm_template.LOCK_FILE', '/tmp/test.lock'), \
  3747. mock.patch('datetime.datetime', new=mock_time), \
  3748. mock.patch('tempfile.TemporaryDirectory') as mock_tmpdir:
  3749. args = argparse.Namespace(
  3750. templates=['test-vm'],
  3751. keyring='/tmp/keyring.gpg',
  3752. nogpgcheck=False,
  3753. cachedir='/var/cache/qvm-template',
  3754. repo_files=[],
  3755. releasever='4.1',
  3756. yes=False,
  3757. keep_cache=True,
  3758. allow_pv=False,
  3759. pool=None
  3760. )
  3761. mock_tmpdir.return_value.__enter__.return_value = \
  3762. '/var/tmp/qvm-template-tmpdir'
  3763. qubesadmin.tools.qvm_template.install(args, self.app,
  3764. version_selector=selector,
  3765. override_existing=True)
  3766. # Attempt to get download list
  3767. self.assertEqual(mock_dl_list.mock_calls, [
  3768. mock.call(args, self.app, version_selector=selector)
  3769. ])
  3770. mock_dl.assert_called_with(args, self.app,
  3771. dl_list=dl_list, version_selector=selector)
  3772. # already verified by download()
  3773. self.assertEqual(mock_verify.mock_calls, [])
  3774. # Package is extracted
  3775. mock_extract.assert_called_with('test-vm',
  3776. '/var/cache/qvm-template/qubes-template-test-vm-1:4.1-20200101.rpm',
  3777. '/var/tmp/qvm-template-tmpdir')
  3778. # Expect override confirmation
  3779. self.assertEqual(mock_confirm.mock_calls,
  3780. [mock.call(re_str(r'.*override changes.*:'), ['test-vm'])])
  3781. # qvm-template-postprocess is called
  3782. self.assertEqual(mock_call.mock_calls, [
  3783. mock.call([
  3784. 'qvm-template-postprocess',
  3785. '--really',
  3786. '--no-installed-by-rpm',
  3787. 'post-install',
  3788. 'test-vm',
  3789. '/var/tmp/qvm-template-tmpdir'
  3790. '/var/lib/qubes/vm-templates/test-vm'
  3791. ])
  3792. ])
  3793. # Cache directory created
  3794. self.assertEqual(mock_mkdirs.mock_calls, [
  3795. mock.call(args.cachedir, exist_ok=True)
  3796. ])
  3797. self.assertAllCalled()
  3798. @mock.patch('qubesadmin.tools.qvm_remove.main')
  3799. @mock.patch('qubesadmin.tools.qvm_template.confirm_action')
  3800. def test_210_remove_success(self, mock_confirm, mock_remove):
  3801. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = (
  3802. b'0\x00vm1 class=TemplateVM state=Halted\n'
  3803. b'vm2 class=TemplateVM state=Halted\n'
  3804. )
  3805. args = argparse.Namespace(
  3806. templates=['vm1', 'vm2'],
  3807. yes=False
  3808. )
  3809. qubesadmin.tools.qvm_template.remove(args, self.app)
  3810. self.assertEqual(mock_confirm.mock_calls,
  3811. [mock.call(re_str(r'.*completely remove.*'), ['vm1', 'vm2'])])
  3812. self.assertEqual(mock_remove.mock_calls, [
  3813. mock.call(['--force', '--', 'vm1', 'vm2'], self.app)
  3814. ])
  3815. self.assertAllCalled()
  3816. @mock.patch('qubesadmin.tools.qvm_kill.main')
  3817. @mock.patch('qubesadmin.tools.qvm_remove.main')
  3818. @mock.patch('qubesadmin.tools.qvm_template.confirm_action')
  3819. def test_211_remove_purge_disassoc_success(
  3820. self,
  3821. mock_confirm,
  3822. mock_remove,
  3823. mock_kill):
  3824. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = (
  3825. b'0\x00vm1 class=TemplateVM state=Halted\n'
  3826. b'vm2 class=TemplateVM state=Halted\n'
  3827. b'vm3 class=TemplateVM state=Halted\n'
  3828. b'vm4 class=TemplateVM state=Halted\n'
  3829. b'dummy class=TemplateVM state=Halted\n'
  3830. b'dummy-1 class=TemplateVM state=Halted\n'
  3831. )
  3832. self.app.expected_calls[
  3833. ('dummy', 'admin.vm.feature.Get', 'template-dummy', None)] = \
  3834. b'0\x000'
  3835. self.app.expected_calls[
  3836. ('dummy-1', 'admin.vm.feature.Get',
  3837. 'template-dummy', None)] = \
  3838. b'0\x001'
  3839. self.app.expected_calls[
  3840. ('vm2', 'admin.vm.property.Set',
  3841. 'default_template', b'dummy-1')] = \
  3842. b'0\x00'
  3843. self.app.expected_calls[
  3844. ('vm2', 'admin.vm.property.Set', 'template', b'dummy-1')] = \
  3845. b'0\x00'
  3846. self.app.expected_calls[
  3847. ('vm3', 'admin.vm.property.Set', 'netvm', b'dummy-1')] = \
  3848. b'0\x00'
  3849. self.app.expected_calls[
  3850. ('vm3', 'admin.vm.property.Set', 'template', b'dummy-1')] = \
  3851. b'0\x00'
  3852. self.app.expected_calls[
  3853. ('vm4', 'admin.vm.property.Set', 'netvm', b'dummy-1')] = \
  3854. b'0\x00'
  3855. self.app.expected_calls[
  3856. ('vm4', 'admin.vm.property.Set', 'template', b'dummy-1')] = \
  3857. b'0\x00'
  3858. self.app.expected_calls[
  3859. ('dom0', 'admin.property.Set', 'updatevm', b'')] = \
  3860. b'0\x00'
  3861. args = argparse.Namespace(
  3862. templates=['vm1'],
  3863. yes=False
  3864. )
  3865. def deps(app, vm):
  3866. if vm == 'vm1':
  3867. return [(self.app.domains['vm2'], 'default_template'),
  3868. (self.app.domains['vm3'], 'netvm')]
  3869. if vm == 'vm2' or vm == 'vm3':
  3870. return [(self.app.domains['vm4'], 'netvm')]
  3871. if vm == 'vm4':
  3872. return [(None, 'updatevm')]
  3873. return []
  3874. with mock.patch('qubesadmin.utils.vm_dependencies') as mock_deps:
  3875. mock_deps.side_effect = deps
  3876. qubesadmin.tools.qvm_template.remove(args, self.app, purge=True)
  3877. # Once for purge (dependency detection) and
  3878. # one for disassoc (actually disassociating the dependencies
  3879. self.assertEqual(mock_deps.mock_calls, [
  3880. mock.call(self.app, self.app.domains['vm1']),
  3881. mock.call(self.app, self.app.domains['vm2']),
  3882. mock.call(self.app, self.app.domains['vm3']),
  3883. mock.call(self.app, self.app.domains['vm4']),
  3884. mock.call(self.app, self.app.domains['vm1']),
  3885. mock.call(self.app, self.app.domains['vm2']),
  3886. mock.call(self.app, self.app.domains['vm3']),
  3887. mock.call(self.app, self.app.domains['vm4'])
  3888. ])
  3889. self.assertEqual(mock_confirm.mock_calls, [
  3890. mock.call(re_str(r'.*completely remove.*'),
  3891. ['vm1', 'vm2', 'vm3', 'vm4']),
  3892. mock.call(re_str(r'.*completely remove.*'),
  3893. ['vm1', 'vm2', 'vm3', 'vm4']),
  3894. mock.call(re_str(r'.*completely remove.*'),
  3895. ['vm1', 'vm2', 'vm3', 'vm4'])
  3896. ])
  3897. self.assertEqual(mock_remove.mock_calls, [
  3898. mock.call(['--force', '--', 'vm1', 'vm2', 'vm3', 'vm4', 'dummy-1'],
  3899. self.app)
  3900. ])
  3901. self.assertEqual(mock_kill.mock_calls, [
  3902. mock.call(['--', 'vm1', 'vm2', 'vm3', 'vm4', 'dummy-1'], self.app)
  3903. ])
  3904. self.assertAllCalled()
  3905. @mock.patch('qubesadmin.tools.qvm_kill.main')
  3906. @mock.patch('qubesadmin.tools.qvm_remove.main')
  3907. @mock.patch('qubesadmin.tools.qvm_template.confirm_action')
  3908. def test_212_remove_disassoc_success(
  3909. self,
  3910. mock_confirm,
  3911. mock_remove,
  3912. mock_kill):
  3913. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = (
  3914. b'0\x00vm1 class=TemplateVM state=Halted\n'
  3915. b'vm2 class=TemplateVM state=Halted\n'
  3916. b'vm3 class=TemplateVM state=Halted\n'
  3917. b'vm4 class=TemplateVM state=Halted\n'
  3918. b'dummy class=TemplateVM state=Halted\n'
  3919. b'dummy-1 class=TemplateVM state=Halted\n'
  3920. )
  3921. self.app.expected_calls[
  3922. ('dummy', 'admin.vm.feature.Get', 'template-dummy', None)] = \
  3923. b'0\x000'
  3924. self.app.expected_calls[
  3925. ('dummy-1', 'admin.vm.feature.Get',
  3926. 'template-dummy', None)] = \
  3927. b'0\x001'
  3928. self.app.expected_calls[
  3929. ('vm2', 'admin.vm.property.Set',
  3930. 'default_template', b'dummy-1')] = \
  3931. b'0\x00'
  3932. self.app.expected_calls[
  3933. ('vm2', 'admin.vm.property.Set', 'template', b'dummy-1')] = \
  3934. b'0\x00'
  3935. self.app.expected_calls[
  3936. ('vm3', 'admin.vm.property.Set', 'netvm', b'dummy-1')] = \
  3937. b'0\x00'
  3938. self.app.expected_calls[
  3939. ('vm3', 'admin.vm.property.Set', 'template', b'dummy-1')] = \
  3940. b'0\x00'
  3941. self.app.expected_calls[
  3942. ('vm4', 'admin.vm.property.Set', 'netvm', b'dummy-1')] = \
  3943. b'0\x00'
  3944. self.app.expected_calls[
  3945. ('vm4', 'admin.vm.property.Set', 'template', b'dummy-1')] = \
  3946. b'0\x00'
  3947. self.app.expected_calls[
  3948. ('dom0', 'admin.property.Set', 'updatevm', b'')] = \
  3949. b'0\x00'
  3950. args = argparse.Namespace(
  3951. templates=['vm1', 'vm2', 'vm3', 'vm4'],
  3952. yes=False
  3953. )
  3954. def deps(app, vm):
  3955. if vm == 'vm1':
  3956. return [(self.app.domains['vm2'], 'default_template'),
  3957. (self.app.domains['vm3'], 'netvm')]
  3958. if vm == 'vm2' or vm == 'vm3':
  3959. return [(self.app.domains['vm4'], 'netvm')]
  3960. if vm == 'vm4':
  3961. return [(None, 'updatevm')]
  3962. return []
  3963. with mock.patch('qubesadmin.utils.vm_dependencies') as mock_deps:
  3964. mock_deps.side_effect = deps
  3965. qubesadmin.tools.qvm_template.remove(args, self.app, disassoc=True)
  3966. self.assertEqual(mock_deps.mock_calls, [
  3967. mock.call(self.app, self.app.domains['vm1']),
  3968. mock.call(self.app, self.app.domains['vm2']),
  3969. mock.call(self.app, self.app.domains['vm3']),
  3970. mock.call(self.app, self.app.domains['vm4'])
  3971. ])
  3972. self.assertEqual(mock_confirm.mock_calls, [
  3973. mock.call(re_str(r'.*completely remove.*'),
  3974. ['vm1', 'vm2', 'vm3', 'vm4'])
  3975. ])
  3976. self.assertEqual(mock_remove.mock_calls, [
  3977. mock.call(['--force', '--', 'vm1', 'vm2', 'vm3', 'vm4'],
  3978. self.app)
  3979. ])
  3980. self.assertEqual(mock_kill.mock_calls, [
  3981. mock.call(['--', 'vm1', 'vm2', 'vm3', 'vm4'], self.app)
  3982. ])
  3983. self.assertAllCalled()
  3984. def test_213_remove_fail_nodomain(self):
  3985. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  3986. b'0\x00vm1 class=TemplateVM state=Halted\n'
  3987. args = argparse.Namespace(
  3988. templates=['vm0'],
  3989. yes=False
  3990. )
  3991. with mock.patch('sys.stderr', new=io.StringIO()) as mock_err:
  3992. with self.assertRaises(SystemExit):
  3993. qubesadmin.tools.qvm_template.remove(args, self.app)
  3994. self.assertTrue('no such domain:' in mock_err.getvalue())
  3995. self.assertAllCalled()