qvm_template.py 143 KB

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