tests: fix asyncio usage cont...
- Add missing loop.run_until_complete() calls. - Convert subprocess.Popen to asyncio.create_subprocess_exec where needed (when called process needs to communicate with qubesd). - Cleanup processes (call .wait()).
This commit is contained in:
parent
f0da5b21df
commit
ed3001da99
@ -27,6 +27,8 @@ import unittest
|
|||||||
|
|
||||||
from distutils import spawn
|
from distutils import spawn
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
import qubes.tests
|
import qubes.tests
|
||||||
|
|
||||||
class TC_04_DispVM(qubes.tests.SystemTestCase):
|
class TC_04_DispVM(qubes.tests.SystemTestCase):
|
||||||
@ -38,23 +40,22 @@ class TC_04_DispVM(qubes.tests.SystemTestCase):
|
|||||||
name=self.make_vm_name('dvm'),
|
name=self.make_vm_name('dvm'),
|
||||||
label='red',
|
label='red',
|
||||||
)
|
)
|
||||||
self.disp_base.create_on_disk()
|
self.loop.run_until_complete(self.disp_base.create_on_disk())
|
||||||
self.app.default_dispvm = self.disp_base
|
self.app.default_dispvm = self.disp_base
|
||||||
self.testvm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
self.testvm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
||||||
name=self.make_vm_name('vm'),
|
name=self.make_vm_name('vm'),
|
||||||
label='red',
|
label='red',
|
||||||
)
|
)
|
||||||
self.testvm.create_on_disk()
|
self.loop.run_until_complete(self.testvm.create_on_disk())
|
||||||
self.app.save()
|
self.app.save()
|
||||||
|
|
||||||
@unittest.expectedFailure
|
@unittest.expectedFailure
|
||||||
def test_002_cleanup(self):
|
def test_002_cleanup(self):
|
||||||
self.testvm.start()
|
self.loop.run_until_complete(self.testvm.start())
|
||||||
|
|
||||||
p = self.testvm.run("qvm-run --dispvm bash", passio_popen=True)
|
(stdout, _) = self.loop.run_until_complete(
|
||||||
(stdout, _) = p.communicate(input=b"echo test; qubesdb-read /name; "
|
self.testvm.run_for_stdio("qvm-run --dispvm bash",
|
||||||
b"echo ERROR\n")
|
input=b"echo test; qubesdb-read /name; echo ERROR\n"))
|
||||||
self.assertEqual(p.returncode, 0)
|
|
||||||
lines = stdout.decode('ascii').splitlines()
|
lines = stdout.decode('ascii').splitlines()
|
||||||
self.assertEqual(lines[0], "test")
|
self.assertEqual(lines[0], "test")
|
||||||
dispvm_name = lines[1]
|
dispvm_name = lines[1]
|
||||||
@ -69,23 +70,21 @@ class TC_04_DispVM(qubes.tests.SystemTestCase):
|
|||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.testvm.start()
|
self.loop.run_until_complete(self.testvm.start())
|
||||||
|
|
||||||
p = self.testvm.run("qvm-run --dispvm bash; true", passio_popen=True)
|
p = self.loop.run_until_complete(
|
||||||
|
self.testvm.run("qvm-run --dispvm bash; true",
|
||||||
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE))
|
||||||
p.stdin.write(b"qubesdb-read /name\n")
|
p.stdin.write(b"qubesdb-read /name\n")
|
||||||
p.stdin.write(b"echo ERROR\n")
|
p.stdin.write(b"echo ERROR\n")
|
||||||
p.stdin.write(b"sudo poweroff\n")
|
p.stdin.write(b"sudo poweroff\n")
|
||||||
# do not close p.stdin on purpose - wait to automatic disconnect when
|
# do not close p.stdin on purpose - wait to automatic disconnect when
|
||||||
# domain is destroyed
|
# domain is destroyed
|
||||||
timeout = 30
|
timeout = 30
|
||||||
while timeout > 0:
|
lines_task = asyncio.ensure_future(p.stdout.read())
|
||||||
if p.poll():
|
self.loop.run_until_complete(asyncio.wait_for(p.wait(), timeout))
|
||||||
break
|
self.loop.run_until_complete(lines_task)
|
||||||
time.sleep(1)
|
lines = lines_task.result().splitlines()
|
||||||
timeout -= 1
|
|
||||||
# includes check for None - timeout
|
|
||||||
self.assertEqual(p.returncode, 0)
|
|
||||||
lines = p.stdout.read().splitlines()
|
|
||||||
self.assertTrue(lines, 'No output received from DispVM')
|
self.assertTrue(lines, 'No output received from DispVM')
|
||||||
dispvm_name = lines[0]
|
dispvm_name = lines[0]
|
||||||
self.assertNotEquals(dispvm_name, b"ERROR")
|
self.assertNotEquals(dispvm_name, b"ERROR")
|
||||||
@ -110,9 +109,9 @@ class TC_20_DispVMMixin(object):
|
|||||||
qubes.vm.dispvm.DispVM.from_appvm(self.disp_base))
|
qubes.vm.dispvm.DispVM.from_appvm(self.disp_base))
|
||||||
try:
|
try:
|
||||||
self.loop.run_until_complete(dispvm.start())
|
self.loop.run_until_complete(dispvm.start())
|
||||||
p = self.loop.run_until_complete(
|
(stdout, _) = self.loop.run_until_complete(
|
||||||
dispvm.run_service('qubes.VMShell', passio_popen=True))
|
dispvm.run_service_for_stdio('qubes.VMShell',
|
||||||
(stdout, _) = p.communicate(input=b"echo test")
|
input=b"echo test"))
|
||||||
self.assertEqual(stdout, b"test\n")
|
self.assertEqual(stdout, b"test\n")
|
||||||
finally:
|
finally:
|
||||||
self.loop.run_until_complete(dispvm.cleanup())
|
self.loop.run_until_complete(dispvm.cleanup())
|
||||||
@ -125,10 +124,12 @@ class TC_20_DispVMMixin(object):
|
|||||||
try:
|
try:
|
||||||
self.loop.run_until_complete(dispvm.start())
|
self.loop.run_until_complete(dispvm.start())
|
||||||
p = self.loop.run_until_complete(
|
p = self.loop.run_until_complete(
|
||||||
dispvm.run_service('qubes.VMShell', passio_popen=True))
|
dispvm.run_service('qubes.VMShell',
|
||||||
|
stdin=subprocess.PIPE,
|
||||||
|
stdout=subprocess.PIPE))
|
||||||
# wait for DispVM startup:
|
# wait for DispVM startup:
|
||||||
p.stdin.write(b"echo test\n")
|
p.stdin.write(b"echo test\n")
|
||||||
p.stdin.flush()
|
self.loop.run_until_complete(p.stdin.drain())
|
||||||
l = self.loop.run_until_complete(p.stdout.readline())
|
l = self.loop.run_until_complete(p.stdout.readline())
|
||||||
self.assertEqual(l, b"test\n")
|
self.assertEqual(l, b"test\n")
|
||||||
|
|
||||||
@ -227,13 +228,14 @@ class TC_20_DispVMMixin(object):
|
|||||||
wait_count = 0
|
wait_count = 0
|
||||||
winid = None
|
winid = None
|
||||||
while True:
|
while True:
|
||||||
search = subprocess.Popen(['xdotool', 'search',
|
search = self.loop.run_until_complete(
|
||||||
'--onlyvisible', '--class', 'disp*'],
|
asyncio.create_subprocess_exec(
|
||||||
stdout=subprocess.PIPE,
|
'xdotool', 'search', '--onlyvisible', '--class', 'disp*',
|
||||||
stderr=open(os.path.devnull, 'w'))
|
stdout=subprocess.PIPE,
|
||||||
retcode = search.wait()
|
stderr=subprocess.DEVNULL))
|
||||||
if retcode == 0:
|
stdout, _ = self.loop.run_until_complete(search.communicate())
|
||||||
winid = search.stdout.read().strip()
|
if p.returncode == 0:
|
||||||
|
winid = stdout.strip()
|
||||||
# get window title
|
# get window title
|
||||||
(window_title, _) = subprocess.Popen(
|
(window_title, _) = subprocess.Popen(
|
||||||
['xdotool', 'getwindowname', winid], stdout=subprocess.PIPE). \
|
['xdotool', 'getwindowname', winid], stdout=subprocess.PIPE). \
|
||||||
@ -247,7 +249,7 @@ class TC_20_DispVMMixin(object):
|
|||||||
wait_count += 1
|
wait_count += 1
|
||||||
if wait_count > 100:
|
if wait_count > 100:
|
||||||
self.fail("Timeout while waiting for editor window")
|
self.fail("Timeout while waiting for editor window")
|
||||||
time.sleep(0.3)
|
self.loop.run_until_complete(asyncio.sleep(0.3))
|
||||||
|
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
self._handle_editor(winid)
|
self._handle_editor(winid)
|
||||||
|
@ -25,6 +25,8 @@ import subprocess
|
|||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
import qubes
|
import qubes
|
||||||
import qubes.tests
|
import qubes.tests
|
||||||
|
|
||||||
@ -118,8 +120,13 @@ enabled = 1
|
|||||||
os.path.join(self.tmpdir, 'pubkey.asc')])
|
os.path.join(self.tmpdir, 'pubkey.asc')])
|
||||||
self.loop.run_until_complete(self.updatevm.start())
|
self.loop.run_until_complete(self.updatevm.start())
|
||||||
self.repo_running = False
|
self.repo_running = False
|
||||||
|
self.repo_proc = None
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
if self.repo_proc:
|
||||||
|
self.repo_proc.terminate()
|
||||||
|
self.loop.run_until_complete(self.repo_proc.wait())
|
||||||
|
del self.repo_proc
|
||||||
super(TC_00_Dom0UpgradeMixin, self).tearDown()
|
super(TC_00_Dom0UpgradeMixin, self).tearDown()
|
||||||
|
|
||||||
subprocess.call(['sudo', 'rpm', '-e', self.pkg_name], stderr=open(
|
subprocess.call(['sudo', 'rpm', '-e', self.pkg_name], stderr=open(
|
||||||
@ -190,7 +197,7 @@ Test package
|
|||||||
def start_repo(self):
|
def start_repo(self):
|
||||||
if self.repo_running:
|
if self.repo_running:
|
||||||
return
|
return
|
||||||
self.loop.run_until_complete(self.updatevm.run(
|
self.repo_proc = self.loop.run_until_complete(self.updatevm.run(
|
||||||
'cd /tmp/repo && python -m SimpleHTTPServer 8080'))
|
'cd /tmp/repo && python -m SimpleHTTPServer 8080'))
|
||||||
self.repo_running = True
|
self.repo_running = True
|
||||||
|
|
||||||
@ -209,14 +216,17 @@ Test package
|
|||||||
open(self.update_flag_path, 'a').close()
|
open(self.update_flag_path, 'a').close()
|
||||||
|
|
||||||
logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
|
logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
|
||||||
try:
|
with open(logpath, 'w') as f_log:
|
||||||
subprocess.check_call(['sudo', '-E', 'qubes-dom0-update', '-y'] +
|
proc = self.loop.run_until_complete(asyncio.create_subprocess_exec(
|
||||||
self.dom0_update_common_opts,
|
'qubes-dom0-update', '-y', *self.dom0_update_common_opts,
|
||||||
stdout=open(logpath, 'w'),
|
stdout=f_log,
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT))
|
||||||
except subprocess.CalledProcessError:
|
self.loop.run_until_complete(proc.wait())
|
||||||
self.fail("qubes-dom0-update failed: " + open(
|
if proc.returncode:
|
||||||
logpath).read())
|
del proc
|
||||||
|
with open(logpath) as f_log:
|
||||||
|
self.fail("qubes-dom0-update failed: " + f_log.read())
|
||||||
|
del proc
|
||||||
|
|
||||||
retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
|
retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
|
||||||
self.pkg_name)], stdout=open(os.devnull, 'w'))
|
self.pkg_name)], stdout=open(os.devnull, 'w'))
|
||||||
@ -238,14 +248,17 @@ Test package
|
|||||||
open(self.update_flag_path, 'a').close()
|
open(self.update_flag_path, 'a').close()
|
||||||
|
|
||||||
logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
|
logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
|
||||||
try:
|
with open(logpath, 'w') as f_log:
|
||||||
subprocess.check_call(['sudo', '-E', 'qubes-dom0-update', '-y'] +
|
proc = self.loop.run_until_complete(asyncio.create_subprocess_exec(
|
||||||
self.dom0_update_common_opts,
|
'qubes-dom0-update', '-y', *self.dom0_update_common_opts,
|
||||||
stdout=open(logpath, 'w'),
|
stdout=f_log,
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT))
|
||||||
except subprocess.CalledProcessError:
|
self.loop.run_until_complete(proc.wait())
|
||||||
self.fail("qubes-dom0-update failed: " + open(
|
if proc.returncode:
|
||||||
logpath).read())
|
del proc
|
||||||
|
with open(logpath) as f_log:
|
||||||
|
self.fail("qubes-dom0-update failed: " + f_log.read())
|
||||||
|
del proc
|
||||||
|
|
||||||
with open(logpath) as f:
|
with open(logpath) as f:
|
||||||
dom0_update_output = f.read()
|
dom0_update_output = f.read()
|
||||||
@ -269,15 +282,18 @@ Test package
|
|||||||
if os.path.exists('/var/lib/qubes/updates/repodata'):
|
if os.path.exists('/var/lib/qubes/updates/repodata'):
|
||||||
shutil.rmtree('/var/lib/qubes/updates/repodata')
|
shutil.rmtree('/var/lib/qubes/updates/repodata')
|
||||||
logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
|
logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
|
||||||
try:
|
with open(logpath, 'w') as f_log:
|
||||||
subprocess.check_call(['sudo', '-E', 'qubes-dom0-update', '-y',
|
proc = self.loop.run_until_complete(asyncio.create_subprocess_exec(
|
||||||
'--clean'] +
|
'qubes-dom0-update', '-y', '--clean',
|
||||||
self.dom0_update_common_opts,
|
*self.dom0_update_common_opts,
|
||||||
stdout=open(logpath, 'w'),
|
stdout=f_log,
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT))
|
||||||
except subprocess.CalledProcessError:
|
self.loop.run_until_complete(proc.wait())
|
||||||
self.fail("qubes-dom0-update failed: " + open(
|
if proc.returncode:
|
||||||
logpath).read())
|
del proc
|
||||||
|
with open(logpath) as f_log:
|
||||||
|
self.fail("qubes-dom0-update failed: " + f_log.read())
|
||||||
|
del proc
|
||||||
|
|
||||||
with open(logpath) as f:
|
with open(logpath) as f:
|
||||||
dom0_update_output = f.read()
|
dom0_update_output = f.read()
|
||||||
@ -294,15 +310,18 @@ Test package
|
|||||||
self.send_pkg(filename)
|
self.send_pkg(filename)
|
||||||
|
|
||||||
logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
|
logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
|
||||||
try:
|
with open(logpath, 'w') as f_log:
|
||||||
subprocess.check_call(['sudo', '-E', 'qubes-dom0-update', '-y'] +
|
proc = self.loop.run_until_complete(asyncio.create_subprocess_exec(
|
||||||
self.dom0_update_common_opts + [
|
'qubes-dom0-update', '-y', *self.dom0_update_common_opts,
|
||||||
self.pkg_name],
|
self.pkg_name,
|
||||||
stdout=open(logpath, 'w'),
|
stdout=f_log,
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT))
|
||||||
except subprocess.CalledProcessError:
|
self.loop.run_until_complete(proc.wait())
|
||||||
self.fail("qubes-dom0-update failed: " + open(
|
if proc.returncode:
|
||||||
logpath).read())
|
del proc
|
||||||
|
with open(logpath) as f_log:
|
||||||
|
self.fail("qubes-dom0-update failed: " + f_log.read())
|
||||||
|
del proc
|
||||||
|
|
||||||
retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
|
retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
|
||||||
self.pkg_name)], stdout=open('/dev/null', 'w'))
|
self.pkg_name)], stdout=open('/dev/null', 'w'))
|
||||||
@ -316,16 +335,19 @@ Test package
|
|||||||
self.send_pkg(filename)
|
self.send_pkg(filename)
|
||||||
|
|
||||||
logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
|
logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
|
||||||
try:
|
with open(logpath, 'w') as f_log:
|
||||||
subprocess.check_call(['sudo', '-E', 'qubes-dom0-update', '-y'] +
|
proc = self.loop.run_until_complete(asyncio.create_subprocess_exec(
|
||||||
self.dom0_update_common_opts + [
|
'qubes-dom0-update', '-y', *self.dom0_update_common_opts,
|
||||||
self.pkg_name],
|
self.pkg_name,
|
||||||
stdout=open(logpath, 'w'),
|
stdout=f_log,
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT))
|
||||||
self.fail("qubes-dom0-update unexpectedly succeeded: " + open(
|
self.loop.run_until_complete(proc.wait())
|
||||||
logpath).read())
|
if not proc.returncode:
|
||||||
except subprocess.CalledProcessError:
|
del proc
|
||||||
pass
|
with open(logpath) as f_log:
|
||||||
|
self.fail("qubes-dom0-update unexpectedly succeeded: " +
|
||||||
|
f_log.read())
|
||||||
|
del proc
|
||||||
|
|
||||||
retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
|
retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
|
||||||
self.pkg_name)], stdout=open('/dev/null', 'w'))
|
self.pkg_name)], stdout=open('/dev/null', 'w'))
|
||||||
@ -341,17 +363,19 @@ Test package
|
|||||||
self.send_pkg(filename)
|
self.send_pkg(filename)
|
||||||
|
|
||||||
logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
|
logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
|
||||||
try:
|
with open(logpath, 'w') as f_log:
|
||||||
subprocess.check_call(['sudo', '-E', 'qubes-dom0-update', '-y'] +
|
proc = self.loop.run_until_complete(asyncio.create_subprocess_exec(
|
||||||
self.dom0_update_common_opts +
|
'qubes-dom0-update', '-y', *self.dom0_update_common_opts,
|
||||||
[self.pkg_name],
|
self.pkg_name,
|
||||||
stdout=open(logpath, 'w'),
|
stdout=f_log,
|
||||||
stderr=subprocess.STDOUT
|
stderr=subprocess.STDOUT))
|
||||||
)
|
self.loop.run_until_complete(proc.wait())
|
||||||
self.fail("qubes-dom0-update unexpectedly succeeded: " + open(
|
if not proc.returncode:
|
||||||
logpath).read())
|
del proc
|
||||||
except subprocess.CalledProcessError:
|
with open(logpath) as f_log:
|
||||||
pass
|
self.fail("qubes-dom0-update unexpectedly succeeded: " +
|
||||||
|
f_log.read())
|
||||||
|
del proc
|
||||||
|
|
||||||
retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
|
retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
|
||||||
self.pkg_name)], stdout=open('/dev/null', 'w'))
|
self.pkg_name)], stdout=open('/dev/null', 'w'))
|
||||||
|
Loading…
Reference in New Issue
Block a user