From 75394a1348fa82586d2cb1d7499674274b826685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Tue, 25 Jul 2017 05:36:00 +0200 Subject: [PATCH] tests: fix asyncio usage in some tests --- qubes/tests/__init__.py | 2 +- qubes/tests/integ/basic.py | 46 +++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/qubes/tests/__init__.py b/qubes/tests/__init__.py index 3e8f54e7..3ba1b0b5 100644 --- a/qubes/tests/__init__.py +++ b/qubes/tests/__init__.py @@ -867,7 +867,7 @@ class SystemTestCase(QubesTestCase): while timeout > 0: if not vm.is_running(): return - time.sleep(1) + self.loop.run_until_complete(asyncio.sleep(1)) timeout -= 1 self.fail("Timeout while waiting for VM {} shutdown".format(vm.name)) diff --git a/qubes/tests/integ/basic.py b/qubes/tests/integ/basic.py index c38c2bfb..901a4f64 100644 --- a/qubes/tests/integ/basic.py +++ b/qubes/tests/integ/basic.py @@ -60,10 +60,10 @@ class TC_00_Basic(qubes.tests.SystemTestCase): self.assertIsNotNone(vm) self.assertEqual(vm.name, vmname) self.assertEqual(vm.template, self.app.default_template) - vm.create_on_disk() + self.loop.run_until_complete(vm.create_on_disk()) with self.assertNotRaises(qubes.exc.QubesException): - vm.storage.verify() + self.loop.run_until_complete(vm.storage.verify()) class TC_01_Properties(qubes.tests.SystemTestCase): @@ -75,6 +75,7 @@ class TC_01_Properties(qubes.tests.SystemTestCase): self.vm = self.app.add_new_vm(qubes.vm.appvm.AppVM, name=self.vmname, template=self.app.default_template, label='red') + self.loop.run_until_complete(self.vm.create_on_disk()) @unittest.expectedFailure def test_030_clone(self): @@ -90,7 +91,7 @@ class TC_01_Properties(qubes.tests.SystemTestCase): label='red') testvm2.clone_properties(testvm1) self.loop.run_until_complete(testvm2.clone_disk_files(testvm1)) - self.assertTrue(testvm1.storage.verify()) + self.assertTrue(self.loop.run_until_complete(testvm1.storage.verify())) self.assertIn('source', testvm1.volumes['root'].config) self.assertNotEquals(testvm2, None) self.assertNotEquals(testvm2.volumes, {}) @@ -215,13 +216,18 @@ class TC_02_QvmPrefs(qubes.tests.SystemTestCase): self.app.save() def pref_set(self, name, value, valid=True): - p = subprocess.Popen( - ['qvm-prefs'] + self.sharedopts + - (['--'] if value != '-D' else []) + [self.testvm.name, name, value], + self.loop.run_until_complete(self._pref_set(name, value, valid)) + + @asyncio.coroutine + def _pref_set(self, name, value, valid=True): + cmd = ['qvm-prefs'] + if value != '-D': + cmd.append('--') + cmd.extend((self.testvm.name, name, value)) + p = yield from asyncio.create_subprocess_exec(*cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - (stdout, stderr) = p.communicate() + stderr=subprocess.PIPE) + (stdout, stderr) = yield from p.communicate() if valid: self.assertEqual(p.returncode, 0, "qvm-prefs .. '{}' '{}' failed: {}{}".format( @@ -233,9 +239,14 @@ class TC_02_QvmPrefs(qubes.tests.SystemTestCase): "property '{}'".format(value, name)) def pref_get(self, name): - p = subprocess.Popen(['qvm-prefs'] + self.sharedopts + - ['--', self.testvm.name, name], stdout=subprocess.PIPE) - (stdout, _) = p.communicate() + self.loop.run_until_complete(self._pref_get(name)) + + @asyncio.coroutine + def _pref_get(self, name): + p = yield from asyncio.create_subprocess_exec( + 'qvm-prefs', *self.sharedopts, '--', self.testvm.name, name, + stdout=subprocess.PIPE) + (stdout, _) = yield from p.communicate() self.assertEqual(p.returncode, 0) return stdout.strip() @@ -343,10 +354,13 @@ class TC_03_QvmRevertTemplateChanges(qubes.tests.SystemTestCase): self.log.warning("template not modified, test result will be " "unreliable") self.assertNotEqual(self.test_template.volumes['root'].revisions, {}) - with self.assertNotRaises(subprocess.CalledProcessError): - pool_vid = repr(self.test_template.volumes['root']).strip("'") - revert_cmd = ['qvm-block', 'revert', pool_vid] - subprocess.check_call(revert_cmd) + pool_vid = repr(self.test_template.volumes['root']).strip("'") + revert_cmd = ['qvm-block', 'revert', pool_vid] + p = self.loop.run_until_complete(asyncio.create_subprocess_exec( + *revert_cmd)) + self.loop.run_until_complete(p.wait()) + self.assertEqual(p.returncode, 0) + checksum_after = self.get_rootimg_checksum() self.assertEqual(checksum_before, checksum_after)