tools/qvm-run: use subproces.DEVNULL instead of manually opened /dev/null

This commit is contained in:
Marek Marczykowski-Górecki 2017-05-17 11:06:56 +02:00
parent f386d45695
commit 210876bd8f
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 9 additions and 22 deletions

View File

@ -43,15 +43,12 @@ class TC_00_qvm_run(qubesadmin.tests.QubesTestCase):
# b'0\x00test-vm class=AppVM state=Running\n'
ret = qubesadmin.tools.qvm_run.main(['test-vm', 'command'], app=self.app)
self.assertEqual(ret, 0)
# make sure we have the same instance below
null = self.app.service_calls[0][2]['stdout']
self.assertIsInstance(null, io.BufferedWriter)
self.assertEqual(self.app.service_calls, [
('test-vm', 'qubes.VMShell', {
'filter_esc': True,
'localcmd': None,
'stdout': null,
'stderr': null,
'stdout': subprocess.DEVNULL,
'stderr': subprocess.DEVNULL,
'user': None,
}),
('test-vm', 'qubes.VMShell', b'command\n')
@ -69,28 +66,20 @@ class TC_00_qvm_run(qubesadmin.tests.QubesTestCase):
ret = qubesadmin.tools.qvm_run.main(['test-vm', 'test-vm2', 'command'],
app=self.app)
self.assertEqual(ret, 0)
for i in range(0, len(self.app.service_calls), 2):
self.assertIsInstance(self.app.service_calls[i][2]['stdout'],
io.BufferedWriter)
self.assertIsInstance(self.app.service_calls[i][2]['stderr'],
io.BufferedWriter)
# make sure we have the same instance below
null = self.app.service_calls[0][2]['stdout']
null2 = self.app.service_calls[2][2]['stdout']
self.assertEqual(self.app.service_calls, [
('test-vm', 'qubes.VMShell', {
'filter_esc': True,
'localcmd': None,
'stdout': null,
'stderr': null,
'stdout': subprocess.DEVNULL,
'stderr': subprocess.DEVNULL,
'user': None,
}),
('test-vm', 'qubes.VMShell', b'command\n'),
('test-vm2', 'qubes.VMShell', {
'filter_esc': True,
'localcmd': None,
'stdout': null2,
'stderr': null2,
'stdout': subprocess.DEVNULL,
'stderr': subprocess.DEVNULL,
'user': None,
}),
('test-vm2', 'qubes.VMShell', b'command\n')

View File

@ -27,7 +27,7 @@ import sys
import asyncio
import functools
import subprocess
import logging
import qubesadmin.tools
@ -129,8 +129,8 @@ def main(args=None, app=None):
retcode = 0
run_kwargs = {}
if not args.passio:
run_kwargs['stdout'] = open(os.devnull, 'wb')
run_kwargs['stderr'] = run_kwargs['stdout']
run_kwargs['stdout'] = subprocess.DEVNULL
run_kwargs['stderr'] = subprocess.DEVNULL
else:
# connect process output to stdout/err directly if --pass-io is given
run_kwargs['stdout'] = None
@ -184,8 +184,6 @@ def main(args=None, app=None):
if args.color_stderr:
sys.stderr.write('\033[0m')
sys.stderr.flush()
if run_kwargs['stdout'] is not None:
run_kwargs['stdout'].close()
return retcode