vm_update.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2015-2020
  5. # Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  6. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. import subprocess
  22. import sys
  23. import qubes.vm
  24. # noinspection PyAttributeOutsideInit,PyPep8Naming
  25. class VmUpdatesMixin(object):
  26. """
  27. Tests for VM updates
  28. """
  29. # filled by load_tests
  30. template = None
  31. # made this way to work also when no package build tools are installed
  32. """
  33. $ cat test-pkg.spec:
  34. Name: test-pkg
  35. Version: 1.0
  36. Release: 1%{?dist}
  37. Summary: Test package
  38. Group: System
  39. License: GPL
  40. URL: http://example.com/
  41. %description
  42. Test package
  43. %files
  44. %changelog
  45. $ rpmbuild -bb test-pkg.spec
  46. $ cat test-pkg-1.0-1.fc21.x86_64.rpm | gzip | base64
  47. $ cat test-pkg-1.1-1.fc21.x86_64.rpm | gzip | base64
  48. """
  49. RPM_PACKAGE_GZIP_BASE64 = [(
  50. b"H4sIAPzRLlYAA+2Y728URRjHn7ueUCkERKJVJDnTxLSxs7293o8WOER6ljYYrtKCLUSa3"
  51. b"bnZ64bd22VmTq8nr4wJbwxvjNHIG0x8oTHGGCHB8AcYE1/0lS80GgmQFCJU3wgB4ZjdfZ"
  52. b"q2xDe8NNlvMjfzmeeZH7tPbl98b35169cOUEpIJiTxT9SIrmVUs2hWh8dUAp54dOrM14s"
  53. b"JHK4D2DKl+j2qrVfjsuq3qEWbohjuAB2Lqk+p1o/8Z5QPmSi/YwnjezH+F8bLQZjqllW0"
  54. b"hvODRmFIL5hFk9JMXi/mi5ZuDleNwSEzP5wtmLnouNQnm3/6fndz7FLt9M/Hruj37gav4"
  55. b"tTjPnasWLFixYoVK1asWLFixYoV63+p0KNot9vnIPQc1vgYOwCSgXfxCoS+QzKHOVXVOj"
  56. b"Fn2ccIfI0k8nXkLuQbyJthxed4UrVnkG8i9yDfgsj3yCAv4foc8t+w1hf5B+Nl5Du43xj"
  57. b"yvxivIN9HpsgPkO2IU9uQfeRn8Xk/iJ4x1Y3nfxH1qecwfhH5+YgT25F7o/0SRdxvOppP"
  58. b"7MX9ZjB/DNnE/OOYX404uRGZIT+FbCFvQ3aQ8f0+/WF0XjJ8nyOw7H+BrmUA/a8pNZf2D"
  59. b"XrCqLG1cERbWHI8ajhznpBY9P0Tr8PkvJDMhTkp/Z0DA6xpuL7DNOq5A+DY9UYTmkOF2U"
  60. b"IO/sNt0wSnGvfdlZssD3rVIlLI9UUX37C6qXzHNntHPNfnTAhWHbUddtBwmegDjAUz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  91. )]
  92. """
  93. Minimal package generated by running dh_make on empty directory
  94. Then cat test-pkg_1.0-1_amd64.deb | gzip | base64
  95. Then cat test-pkg_1.1-1_amd64.deb | gzip | base64
  96. """
  97. DEB_PACKAGE_GZIP_BASE64 = [(
  98. b"H4sIACTXLlYAA1O0SSxKzrDjSklNykzM003KzEssqlRQUDA0MTG1NDQwNDVTUDBQAAEIa"
  99. b"WhgYGZioqBgogADCVxGegZcyfl5JUX5OXoliUV66VVE6DcwheuX7+ZgAAEW5rdXHb0PG4"
  100. b"iwf5j3WfMT6zWzzMuZgoE3jjYraNzbbFKWGms0SaRw/r2SV23WZ4IdP8preM4yqf0jt95"
  101. b"3c8qnacfNxJUkf9/w+/3X9ph2GEdgQdixrz/niHKKTnYXizf4oSC7tHOz2Zzq+/6vn8/7"
  102. b"ezQ7c1tmi7xZ3SGJ4yzhT2dcr7V+W3zM5ZPu/56PSv4Zdok+7Yv/V/6buWaKVlFkkV58S"
  103. b"N3GmLgnqzRmeZ3V3ymmurS5fGa85/LNx1bpZMin3S6dvXKqydp3ubP1vmyarJZb/qSh62"
  104. b"C8oIdxqm/BtvkGDza+On/Vfv2py7/0LV7VH+qR6a+bkKUbHXt5/SG187d+nps1a5PJfMO"
  105. b"i11dWcUe1HjwaW3Q5RHXn9LmcHy+tW9YcKf0768XVB1t3R0bKrzs5t9P+6r7rZ99svH10"
  106. b"+Q6F/o8tf1fO/32y+fWa14eifd+WxUy0jcxYH7N9/tUvmnUZL74pW32qLeuRU+ZwYGASa"
  107. b"GBgUWBgxM90ayy3VdmykkGDgYErJbEkERydFVWQmCMQo8aWZvAY/WteFRHFwMCYqXTPjI"
  108. b"lBkVEMGLsl+k8XP1D/z+gXyyDOvUemlnHqAVkvu0rRQ2fUFodkN3mtU9uwhqk8V+TqPEE"
  109. b"Nc7fzoQ4n71lqRs/7kbbT0+qOZuKH4r8mjzsc1k/YkCHN8Pjg48fbpE+teHa96LNcfu0V"
  110. b"5n2/Z2xa2KDvaCOx8cqBFxc514uZ3TmadXS+6cpzU7wSzq5SWfapJOD9n6wLXSwtlgxZh"
  111. b"xITzWW7buhx/bb291RcVlEfeC9K5hlrqunSzIMSZT7/Nqgc/qMvMNW227WI8ezB8mVuZh"
  112. b"0hERJSvysfburr4Dx0I9BW57UwR4+e1gxu49PcEt8sbK18Xpvt//Hj5UYm+Zc25q+T4xl"
  113. b"rJvxfVnh80oadq57OZxPaU1bbztv1yF365W4t45Yr+XrFzov237GVY1Zgf7NvE4+W2SuR"
  114. b"lQtLauR1TQ/mbOiIONYya6tU1jPGpWfk/i1+ttiXe3ZO14n0YOWggndznjGlGLyfVbBC6"
  115. b"MRP5aMM7aCco/s7sZqB8RlTQwADw8rnuT/sDHi7mUASjJFRAAbWwNLiAwAA"
  116. ), (
  117. b"H4sIAL05B1wCA1O0SSxKzrDjSklNykzM003KzEssqlRQUDA0NTG2NDc3NjdTUDBQAAEIa"
  118. b"WhgYGZioqBgogADCVxGegZcyfl5JUX5OXoliUV66VVE6De3gOuX7+ZgAAEW5rdXzmbdMR"
  119. b"BgSJj/VeQzQ+ztT/W+EVEnFraKOTlXh6+JXB8RbTRpzgWb2qdLX0+RmTRZcYlyxJutJsk"
  120. b"/pfsfq9yqWZJ4JVVS97jBPPnz1yviluw51b0q4tnrWemCU2a/17mTUBYX0XBC6nH8rvvZ"
  121. b"n/WP7nu40+Jlz7drPNLvCjULQkXOv677OV9s4bPsv5+tvCzPG8s57no479qV/5V/813Kh"
  122. b"Wy3Pbj4827Jq5v6W/wk7zL1/+zbfH6btVb/3Pm5EapukaJvdgfcape/JZZWe+mZ4+Grby"
  123. b"7UTaroPzyv9urC1W2MT9+F2bZtWJOyXfGo5dv7DGXJUzee+p930Od0j8QNceNHJffOTr2"
  124. b"kOJe93mWG+nPdLsG6fz++MV5h1OGr0N9yf3N2ydzQ5x/E9Aw/s9xzmOpULnKtsSZqc/rr"
  125. b"RQdf/Lu/ckKE9xU5VRuNehbzTr6789a+P2lt2zk5cFqe3N2289+j/hfH2X39/+nvc5vTW"
  126. b"a/+83pvWqY3e93JWYsmup693HzCOPBk0LI9O7PtiqawN9y8eaTV75DLLL2dNWqTLsTsOn"
  127. b"7wy0fTe5oLH//7eNf89Co3dRUHJmLRh20s/xhYJkoeYdBgYEhJLEkEJ4uKKkgKIJQyjI3"
  128. b"gKeOveVVEFAMDY6bSPTMmBkVGMWAqKdF/uviB+n/GwlgGce49MrWMUw/IetlVih46o7Y4"
  129. b"0uZe/t9lt85aMUrdWhjueTHRd1nr1uK830feH74vcPKU2pkbP4SZnta5PhC9dfPTqvv7f"
  130. b"n068XRDRDzLuv8Oa5p1L+02ZN127vp6mzSzzFqpLkmbwyl131J1xW58YlcxXSWs0PTbpT"
  131. b"z28ZUnE/e+NN93weAd40a/zzJ7+Re/v+R7+f3VBVFJCyZsv523ySJ12t7Nt5b8uBu8zuJ"
  132. b"2Laer//nZCkbXlxtYXvvA8+VSVsCRpo8BawtftKWyZBjkWa6/0X7qXfbF9reH/ro6S63Y"
  133. b"rCj8t8cltPIOj9H/8LyIxj6bMsZVVtu+ngj6MCNV5JXhOs07RXWxrb3xsqJMDRksx/5bO"
  134. b"bNtevXz2cdpzzI19Roede4NXxAyK9Dlrtp8JtELLNPWbBe9HfJlj1Hiv69erIFBnX/Pe1"
  135. b"4QnzLD+p2AiTc383/P+7sW3WoxnXra49iJKJeZy7gc9Z02S57qrvWW3day501VhsbPtfK"
  136. b"C5nyBG9qjr08E59KY1vUTGRg7mRsCGBimFa+3sTPg7WYCSTBGRgEAzEOeH04EAAA="
  137. )]
  138. def run_cmd(self, vm, cmd, user="root"):
  139. '''Run a command *cmd* in a *vm* as *user*. Return its exit code.
  140. :type self: qubes.tests.SystemTestCase | VmUpdatesMixin
  141. :param qubes.vm.qubesvm.QubesVM vm: VM object to run command in
  142. :param str cmd: command to execute
  143. :param std user: user to execute command as
  144. :return int: command exit code
  145. '''
  146. try:
  147. self.loop.run_until_complete(vm.run_for_stdio(cmd))
  148. except subprocess.CalledProcessError as e:
  149. return e.returncode
  150. return 0
  151. def assertRunCommandReturnCode(self, vm, cmd, expected_returncode):
  152. p = self.loop.run_until_complete(
  153. vm.run(cmd, user='root',
  154. stdout=subprocess.PIPE, stderr=subprocess.PIPE))
  155. (stdout, stderr) = self.loop.run_until_complete(p.communicate())
  156. self.assertIn(
  157. self.loop.run_until_complete(p.wait()), expected_returncode,
  158. '{}: {}\n{}'.format(cmd, stdout, stderr))
  159. def setUp(self):
  160. '''
  161. :type self: qubes.tests.SystemTestCase | VmUpdatesMixin
  162. '''
  163. if not self.template.count('debian') and \
  164. not self.template.count('fedora'):
  165. self.skipTest("Template {} not supported by this test".format(
  166. self.template))
  167. super(VmUpdatesMixin, self).setUp()
  168. self.update_cmd = None
  169. if self.template.count("debian"):
  170. self.update_cmd = "set -o pipefail; apt-get update 2>&1 | " \
  171. "{ ! grep '^W:\|^E:'; }"
  172. self.upgrade_cmd = "apt-get -V dist-upgrade -y"
  173. self.install_cmd = "apt-get install -y {}"
  174. self.install_test_cmd = "dpkg -l {}"
  175. self.exit_code_ok = [0]
  176. elif self.template.count("fedora"):
  177. cmd = "yum"
  178. try:
  179. # assume template name in form "fedora-XX-suffix"
  180. if int(self.template.split("-")[1]) > 21:
  181. cmd = "dnf"
  182. except ValueError:
  183. pass
  184. self.update_cmd = "{cmd} clean all; {cmd} check-update".format(
  185. cmd=cmd)
  186. self.upgrade_cmd = "{cmd} upgrade -y".format(cmd=cmd)
  187. self.install_cmd = cmd + " install -y {}"
  188. self.install_test_cmd = "rpm -q {}"
  189. self.exit_code_ok = [0, 100]
  190. self.init_default_template(self.template)
  191. self.init_networking()
  192. self.testvm1 = self.app.add_new_vm(
  193. qubes.vm.appvm.AppVM,
  194. name=self.make_vm_name('vm1'),
  195. label='red')
  196. self.loop.run_until_complete(self.testvm1.create_on_disk())
  197. def test_000_simple_update(self):
  198. '''
  199. :type self: qubes.tests.SystemTestCase | VmUpdatesMixin
  200. '''
  201. self.app.save()
  202. self.testvm1 = self.app.domains[self.testvm1.qid]
  203. self.loop.run_until_complete(self.testvm1.start())
  204. self.assertRunCommandReturnCode(self.testvm1,
  205. self.update_cmd, self.exit_code_ok)
  206. def create_repo_apt(self, version=0):
  207. '''
  208. :type self: qubes.tests.SystemTestCase | VmUpdatesMixin
  209. '''
  210. pkg_file_name = "test-pkg_1.{}-1_amd64.deb".format(version)
  211. self.loop.run_until_complete(self.netvm_repo.run_for_stdio('''
  212. mkdir -p /tmp/apt-repo \
  213. && cd /tmp/apt-repo \
  214. && base64 -d | zcat > {}
  215. '''.format(pkg_file_name),
  216. input=self.DEB_PACKAGE_GZIP_BASE64[version]))
  217. # do not assume dpkg-scanpackage installed
  218. packages_path = "dists/test/main/binary-amd64/Packages"
  219. self.loop.run_until_complete(self.netvm_repo.run_for_stdio('''
  220. mkdir -p /tmp/apt-repo/dists/test/main/binary-amd64 \
  221. && cd /tmp/apt-repo \
  222. && cat > {packages} \
  223. && echo MD5sum: $(openssl md5 -r {pkg} | cut -f 1 -d ' ') \
  224. >> {packages} \
  225. && echo SHA1: $(openssl sha1 -r {pkg} | cut -f 1 -d ' ') \
  226. >> {packages} \
  227. && echo SHA256: $(openssl sha256 -r {pkg} | cut -f 1 -d ' ') \
  228. >> {packages} \
  229. && sed -i -e "s,@SIZE@,$(stat -c %s {pkg})," {packages} \
  230. && gzip < {packages} > {packages}.gz
  231. '''.format(pkg=pkg_file_name, packages=packages_path),
  232. input='''\
  233. Package: test-pkg
  234. Version: 1.{version}-1
  235. Architecture: amd64
  236. Maintainer: unknown <user@host>
  237. Installed-Size: 25
  238. Filename: {pkg}
  239. Size: @SIZE@
  240. Section: unknown
  241. Priority: optional
  242. Description: Test package'''.format(pkg=pkg_file_name, version=version).encode(
  243. 'utf-8')))
  244. self.loop.run_until_complete(self.netvm_repo.run_for_stdio('''
  245. mkdir -p /tmp/apt-repo/dists/test \
  246. && cd /tmp/apt-repo/dists/test \
  247. && cat > Release \
  248. && echo '' $(sha256sum {p} | cut -f 1 -d ' ') $(stat -c %s {p}) {p}\
  249. >> Release \
  250. && echo '' $(sha256sum {z} | cut -f 1 -d ' ') $(stat -c %s {z}) {z}\
  251. >> Release
  252. '''.format(p='main/binary-amd64/Packages',
  253. z='main/binary-amd64/Packages.gz'),
  254. input=b'''\
  255. Label: Test repo
  256. Suite: test
  257. Codename: test
  258. Date: Tue, 27 Oct 2015 03:22:09 UTC
  259. Architectures: amd64
  260. Components: main
  261. SHA256:
  262. '''))
  263. def create_repo_yum(self, version=0):
  264. '''
  265. :type self: qubes.tests.SystemTestCase | VmUpdatesMixin
  266. '''
  267. pkg_file_name = "test-pkg-1.{}-1.fc21.x86_64.rpm".format(version)
  268. self.loop.run_until_complete(self.netvm_repo.run_for_stdio('''
  269. mkdir -p /tmp/yum-repo \
  270. && cd /tmp/yum-repo \
  271. && base64 -d | zcat > {}
  272. '''.format(pkg_file_name), input=self.RPM_PACKAGE_GZIP_BASE64[
  273. version]))
  274. # createrepo is installed by default in Fedora template
  275. self.loop.run_until_complete(self.netvm_repo.run_for_stdio(
  276. 'createrepo /tmp/yum-repo'))
  277. def create_repo_and_serve(self):
  278. '''
  279. :type self: qubes.tests.SystemTestCase | VmUpdatesMixin
  280. '''
  281. if self.template.count("debian") or self.template.count("whonix"):
  282. self.create_repo_apt()
  283. self.loop.run_until_complete(self.netvm_repo.run(
  284. 'cd /tmp/apt-repo && python3 -m http.server 8080',
  285. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL))
  286. elif self.template.count("fedora"):
  287. self.create_repo_yum()
  288. self.loop.run_until_complete(self.netvm_repo.run(
  289. 'cd /tmp/yum-repo && python3 -m http.server 8080',
  290. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL))
  291. else:
  292. # not reachable...
  293. self.skipTest("Template {} not supported by this test".format(
  294. self.template))
  295. def add_update_to_repo(self):
  296. if self.template.count("debian") or self.template.count("whonix"):
  297. self.create_repo_apt(1)
  298. elif self.template.count("fedora"):
  299. self.create_repo_yum(1)
  300. def configure_test_repo(self):
  301. """
  302. Configure test repository in test-vm and disable rest of them.
  303. The critical part is to use "localhost" - this will work only when
  304. accessed through update proxy and this is exactly what we want to
  305. test here.
  306. :type self: qubes.tests.SystemTestCase | VmUpdatesMixin
  307. """
  308. if self.template.count("debian") or self.template.count("whonix"):
  309. self.loop.run_until_complete(self.testvm1.run_for_stdio(
  310. "rm -f /etc/apt/sources.list.d/* &&"
  311. "echo 'deb [trusted=yes] http://localhost:8080 test main' "
  312. "> /etc/apt/sources.list",
  313. user="root"))
  314. elif self.template.count("fedora"):
  315. self.loop.run_until_complete(self.testvm1.run_for_stdio(
  316. "rm -f /etc/yum.repos.d/*.repo &&"
  317. "echo '[test]' > /etc/yum.repos.d/test.repo &&"
  318. "echo 'name=Test repo' >> /etc/yum.repos.d/test.repo &&"
  319. "echo 'gpgcheck=0' >> /etc/yum.repos.d/test.repo &&"
  320. "echo 'baseurl=http://localhost:8080/'"
  321. " >> /etc/yum.repos.d/test.repo",
  322. user="root"
  323. ))
  324. else:
  325. # not reachable...
  326. self.skipTest("Template {} not supported by this test".format(
  327. self.template))
  328. def test_010_update_via_proxy(self):
  329. '''
  330. Test both whether updates proxy works and whether is actually used
  331. by the VM
  332. :type self: qubes.tests.SystemTestCase | VmUpdatesMixin
  333. '''
  334. if self.template.count("minimal"):
  335. self.skipTest("Template {} not supported by this test".format(
  336. self.template))
  337. self.netvm_repo = self.app.add_new_vm(
  338. qubes.vm.appvm.AppVM,
  339. name=self.make_vm_name('net'),
  340. label='red')
  341. self.netvm_repo.provides_network = True
  342. self.loop.run_until_complete(self.netvm_repo.create_on_disk())
  343. self.testvm1.netvm = self.netvm_repo
  344. self.netvm_repo.features['service.qubes-updates-proxy'] = True
  345. # TODO: consider also adding a test for the template itself
  346. self.testvm1.features['service.updates-proxy-setup'] = True
  347. self.app.save()
  348. # Setup test repo
  349. self.loop.run_until_complete(self.netvm_repo.start())
  350. self.create_repo_and_serve()
  351. # Configure local repo
  352. self.loop.run_until_complete(self.testvm1.start())
  353. self.configure_test_repo()
  354. with self.qrexec_policy('qubes.UpdatesProxy', self.testvm1,
  355. '$default', action='allow,target=' + self.netvm_repo.name):
  356. # update repository metadata
  357. self.assertRunCommandReturnCode(self.testvm1,
  358. self.update_cmd, self.exit_code_ok)
  359. # install test package
  360. self.assertRunCommandReturnCode(self.testvm1,
  361. self.install_cmd.format('test-pkg'), self.exit_code_ok)
  362. # verify if it was really installed
  363. self.assertRunCommandReturnCode(self.testvm1,
  364. self.install_test_cmd.format('test-pkg'), self.exit_code_ok)
  365. def test_020_updates_available_notification(self):
  366. # override with StandaloneVM
  367. self.testvm1 = self.app.add_new_vm(
  368. qubes.vm.standalonevm.StandaloneVM,
  369. name=self.make_vm_name('vm2'),
  370. label='red')
  371. tpl = self.app.domains[self.template]
  372. self.testvm1.clone_properties(tpl)
  373. self.testvm1.features.update(tpl.features)
  374. self.loop.run_until_complete(
  375. self.testvm1.clone_disk_files(tpl))
  376. self.loop.run_until_complete(self.testvm1.start())
  377. self.netvm_repo = self.testvm1
  378. self.create_repo_and_serve()
  379. self.configure_test_repo()
  380. self.loop.run_until_complete(
  381. self.testvm1.run_for_stdio(
  382. '/usr/lib/qubes/upgrades-status-notify',
  383. user='root',
  384. ))
  385. self.assertFalse(self.testvm1.features.get('updates-available', False))
  386. # update repository metadata
  387. self.assertRunCommandReturnCode(
  388. self.testvm1, self.update_cmd, self.exit_code_ok)
  389. # install test package
  390. self.assertRunCommandReturnCode(
  391. self.testvm1, self.install_cmd.format('test-pkg'), self.exit_code_ok)
  392. self.assertFalse(self.testvm1.features.get('updates-available', False))
  393. self.add_update_to_repo()
  394. # update repository metadata
  395. self.assertRunCommandReturnCode(
  396. self.testvm1, self.update_cmd, self.exit_code_ok)
  397. self.loop.run_until_complete(
  398. self.testvm1.run_for_stdio(
  399. '/usr/lib/qubes/upgrades-status-notify',
  400. user='root',
  401. ))
  402. self.assertTrue(self.testvm1.features.get('updates-available', False))
  403. # install updates
  404. self.assertRunCommandReturnCode(
  405. self.testvm1, self.upgrade_cmd, self.exit_code_ok)
  406. self.assertFalse(self.testvm1.features.get('updates-available', False))
  407. def create_testcases_for_templates():
  408. yield from qubes.tests.create_testcases_for_templates('VmUpdates',
  409. VmUpdatesMixin, qubes.tests.SystemTestCase,
  410. module=sys.modules[__name__])
  411. def load_tests(loader, tests, pattern):
  412. tests.addTests(loader.loadTestsFromNames(
  413. create_testcases_for_templates()))
  414. return tests
  415. qubes.tests.maybe_create_testcases_on_import(create_testcases_for_templates)