2017-04-16 03:17:53 +02:00
|
|
|
# -*- encoding: utf8 -*-
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import io
|
2017-06-25 13:34:06 +02:00
|
|
|
import os
|
2017-04-16 03:17:53 +02:00
|
|
|
import unittest.mock
|
|
|
|
|
|
|
|
import subprocess
|
2017-04-30 22:54:20 +02:00
|
|
|
import sys
|
2017-04-16 03:17:53 +02:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin.tests
|
|
|
|
import qubesadmin.tools.qvm_run
|
2017-04-16 03:17:53 +02:00
|
|
|
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
class TC_00_qvm_run(qubesadmin.tests.QubesTestCase):
|
2017-04-30 22:54:20 +02:00
|
|
|
def setUp(self):
|
|
|
|
if sys.stdout is not sys.__stdout__ or \
|
|
|
|
sys.stderr is not sys.__stderr__:
|
|
|
|
self.skipTest('qvm-run change behavior on redirected stdout/stderr')
|
|
|
|
super(TC_00_qvm_run, self).setUp()
|
2017-06-25 13:34:06 +02:00
|
|
|
|
|
|
|
def default_filter_esc(self):
|
|
|
|
return os.isatty(sys.stdout.fileno())
|
|
|
|
|
2017-04-16 03:17:53 +02:00
|
|
|
def test_000_run_single(self):
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
b'0\x00test-vm class=AppVM state=Running\n'
|
2018-07-30 18:21:06 +02:00
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
|
|
|
|
b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
|
2017-04-16 03:17:53 +02:00
|
|
|
# self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
# ('test-vm', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
# b'0\x00test-vm class=AppVM state=Running\n'
|
2017-05-17 11:07:48 +02:00
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
|
|
|
['--no-gui', 'test-vm', 'command'],
|
|
|
|
app=self.app)
|
2017-04-16 03:17:53 +02:00
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('test-vm', 'qubes.VMShell', {
|
2017-05-17 11:06:56 +02:00
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
2017-04-16 03:17:53 +02:00
|
|
|
'user': None,
|
|
|
|
}),
|
2017-05-18 09:54:26 +02:00
|
|
|
('test-vm', 'qubes.VMShell', b'command; exit\n')
|
2017-04-16 03:17:53 +02:00
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_001_run_multiple(self):
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
b'0\x00test-vm class=AppVM state=Running\n' \
|
2017-08-06 20:44:55 +02:00
|
|
|
b'test-vm2 class=AppVM state=Running\n' \
|
|
|
|
b'test-vm3 class=AppVM state=Halted\n'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm2', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00test-vm2 class=AppVM state=Running\n'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm3', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00test-vm3 class=AppVM state=Halted\n'
|
2018-07-30 18:21:06 +02:00
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
|
|
|
|
b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm2', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
|
|
|
|
b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
|
2017-05-17 11:07:48 +02:00
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
2017-08-06 20:44:55 +02:00
|
|
|
['--no-gui', '--all', 'command'],
|
2017-04-16 03:17:53 +02:00
|
|
|
app=self.app)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('test-vm', 'qubes.VMShell', {
|
2017-05-17 11:06:56 +02:00
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
2017-04-16 03:17:53 +02:00
|
|
|
'user': None,
|
|
|
|
}),
|
2017-05-18 09:54:26 +02:00
|
|
|
('test-vm', 'qubes.VMShell', b'command; exit\n'),
|
2017-04-16 03:17:53 +02:00
|
|
|
('test-vm2', 'qubes.VMShell', {
|
2017-05-17 11:06:56 +02:00
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
2017-04-16 03:17:53 +02:00
|
|
|
'user': None,
|
|
|
|
}),
|
2017-05-18 09:54:26 +02:00
|
|
|
('test-vm2', 'qubes.VMShell', b'command; exit\n')
|
2017-04-16 03:17:53 +02:00
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_002_passio(self):
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
b'0\x00test-vm class=AppVM state=Running\n'
|
2018-07-30 18:21:06 +02:00
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
|
|
|
|
b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
|
2017-04-16 03:17:53 +02:00
|
|
|
# self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
# ('test-vm', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
# b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
echo = subprocess.Popen(['echo', 'some-data'], stdout=subprocess.PIPE)
|
|
|
|
with unittest.mock.patch('sys.stdin', echo.stdout):
|
2017-05-11 23:21:04 +02:00
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
2018-12-07 04:14:50 +01:00
|
|
|
['--no-gui', '--pass-io', '--filter-escape-chars',
|
|
|
|
'test-vm', 'command'],
|
2017-04-16 03:17:53 +02:00
|
|
|
app=self.app)
|
2018-12-07 04:14:50 +01:00
|
|
|
echo.stdout.close()
|
|
|
|
echo.wait()
|
2017-04-16 03:17:53 +02:00
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('test-vm', 'qubes.VMShell', {
|
2018-12-07 04:14:50 +01:00
|
|
|
'filter_esc': True,
|
2017-04-16 03:17:53 +02:00
|
|
|
'stdout': None,
|
|
|
|
'stderr': None,
|
|
|
|
'user': None,
|
|
|
|
}),
|
2018-12-07 04:14:50 +01:00
|
|
|
# TODO: find a way to compare b'some-data\n' sent from another
|
|
|
|
# proces
|
|
|
|
('test-vm', 'qubes.VMShell', b'command; exit\n')
|
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_002_passio_service(self):
|
|
|
|
self.app.expected_calls[
|
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
# self.app.expected_calls[
|
|
|
|
# ('test-vm', 'admin.vm.List', None, None)] = \
|
|
|
|
# b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
echo = subprocess.Popen(['echo', 'some-data'], stdout=subprocess.PIPE)
|
|
|
|
with unittest.mock.patch('sys.stdin', echo.stdout):
|
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
|
|
|
['--no-gui', '--service', '--pass-io', '--filter-escape-chars',
|
|
|
|
'test-vm', 'test.service'],
|
|
|
|
app=self.app)
|
|
|
|
echo.stdout.close()
|
|
|
|
echo.wait()
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('test-vm', 'test.service', {
|
|
|
|
'filter_esc': True,
|
|
|
|
'stdout': None,
|
|
|
|
'stderr': None,
|
|
|
|
'user': None,
|
|
|
|
}),
|
|
|
|
# TODO: find a way to compare b'some-data\n' sent from another
|
|
|
|
# proces
|
|
|
|
('test-vm', 'test.service', b'')
|
2017-04-16 03:17:53 +02:00
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
2017-07-18 01:03:57 +02:00
|
|
|
@unittest.expectedFailure
|
2017-04-16 03:17:53 +02:00
|
|
|
def test_002_color_output(self):
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
b'0\x00test-vm class=AppVM state=Running\n'
|
2018-07-30 18:21:06 +02:00
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
|
|
|
|
b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
|
2017-04-16 03:17:53 +02:00
|
|
|
# self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
# ('test-vm', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
# b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
stdout = io.StringIO()
|
|
|
|
echo = subprocess.Popen(['echo', 'some-data'], stdout=subprocess.PIPE)
|
|
|
|
with unittest.mock.patch('sys.stdin', echo.stdout):
|
|
|
|
with unittest.mock.patch('sys.stdout', stdout):
|
2017-05-11 23:21:04 +02:00
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
2017-06-25 13:34:06 +02:00
|
|
|
['--no-gui', '--filter-esc', '--pass-io', 'test-vm',
|
|
|
|
'command'],
|
2017-04-16 03:17:53 +02:00
|
|
|
app=self.app)
|
2018-12-07 04:14:50 +01:00
|
|
|
echo.stdout.close()
|
|
|
|
echo.wait()
|
2017-04-16 03:17:53 +02:00
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('test-vm', 'qubes.VMShell', {
|
|
|
|
'filter_esc': True,
|
|
|
|
'stdout': None,
|
|
|
|
'stderr': None,
|
|
|
|
'user': None,
|
|
|
|
}),
|
2017-05-18 09:54:26 +02:00
|
|
|
('test-vm', 'qubes.VMShell', b'command; exit\nsome-data\n')
|
2017-04-16 03:17:53 +02:00
|
|
|
])
|
|
|
|
self.assertEqual(stdout.getvalue(), '\033[0;31m\033[0m')
|
|
|
|
stdout.close()
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
2017-07-18 01:03:57 +02:00
|
|
|
@unittest.expectedFailure
|
2017-04-16 03:17:53 +02:00
|
|
|
def test_003_no_color_output(self):
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
# self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
# ('test-vm', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
# b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
stdout = io.StringIO()
|
|
|
|
echo = subprocess.Popen(['echo', 'some-data'], stdout=subprocess.PIPE)
|
|
|
|
with unittest.mock.patch('sys.stdin', echo.stdout):
|
|
|
|
with unittest.mock.patch('sys.stdout', stdout):
|
2017-05-11 23:21:04 +02:00
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
2017-05-17 11:07:48 +02:00
|
|
|
['--no-gui', '--pass-io', '--no-color-output',
|
|
|
|
'test-vm', 'command'],
|
2017-04-16 03:17:53 +02:00
|
|
|
app=self.app)
|
|
|
|
|
2018-12-07 04:14:50 +01:00
|
|
|
echo.stdout.close()
|
|
|
|
echo.wait()
|
2017-04-16 03:17:53 +02:00
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('test-vm', 'qubes.VMShell', {
|
2017-06-25 13:34:06 +02:00
|
|
|
'filter_esc': self.default_filter_esc(),
|
2017-04-16 03:17:53 +02:00
|
|
|
'stdout': None,
|
|
|
|
'stderr': None,
|
|
|
|
'user': None,
|
|
|
|
}),
|
2017-05-18 09:54:26 +02:00
|
|
|
('test-vm', 'qubes.VMShell', b'command; exit\nsome-data\n')
|
2017-04-16 03:17:53 +02:00
|
|
|
])
|
|
|
|
self.assertEqual(stdout.getvalue(), '')
|
|
|
|
stdout.close()
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
2017-07-18 01:03:57 +02:00
|
|
|
@unittest.expectedFailure
|
2017-04-16 03:17:53 +02:00
|
|
|
def test_004_no_filter_esc(self):
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
b'0\x00test-vm class=AppVM state=Running\n'
|
2018-07-30 18:21:06 +02:00
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
|
|
|
|
b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
|
2017-04-16 03:17:53 +02:00
|
|
|
# self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
# ('test-vm', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
# b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
stdout = io.StringIO()
|
|
|
|
echo = subprocess.Popen(['echo', 'some-data'], stdout=subprocess.PIPE)
|
|
|
|
with unittest.mock.patch('sys.stdin', echo.stdout):
|
|
|
|
with unittest.mock.patch('sys.stdout', stdout):
|
2017-05-11 23:21:04 +02:00
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
2017-05-17 11:07:48 +02:00
|
|
|
['--no-gui', '--pass-io', '--no-filter-esc',
|
|
|
|
'test-vm', 'command'],
|
2017-04-16 03:17:53 +02:00
|
|
|
app=self.app)
|
|
|
|
|
2018-12-07 04:14:50 +01:00
|
|
|
echo.stdout.close()
|
|
|
|
echo.wait()
|
2017-04-16 03:17:53 +02:00
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('test-vm', 'qubes.VMShell', {
|
|
|
|
'filter_esc': False,
|
|
|
|
'stdout': None,
|
|
|
|
'stderr': None,
|
|
|
|
'user': None,
|
|
|
|
}),
|
2017-05-18 09:54:26 +02:00
|
|
|
('test-vm', 'qubes.VMShell', b'command; exit\nsome-data\n')
|
2017-04-16 03:17:53 +02:00
|
|
|
])
|
|
|
|
self.assertEqual(stdout.getvalue(), '')
|
|
|
|
stdout.close()
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
tools: fix qvm-run --pass-io --localcmd=... vmname command
qubes.VMShell service, used by qvm-run, expects the command on the first
input line. Previously, when --localcmd was used, the command wasn't
written anywhere and the local command was connected directly to
qubes.VMShell service. And the first line of its output was interpreted
as a command.
Fix this by starting the local command separately, after sending the
command to qubes.VMShell service.
While at it, unify handling shell command and service calls in the process.
vm.run_service(..., localcmd= ) isn't that useful in general case,
because for qubes.VMShell the caller first need to send the command
before starting local process. Since the qvm-run tool needs to implement
manual starting localcmd anyway, don't use localcmd= run_service's
argument at all to unify calling methods.
There is slight behavior change: previously localcmd was started only
after establishing service connection (for example only if qrexec policy
allows), now it is started in all the cases.
Fixes QubesOS/qubes-issues#4040
2018-12-06 23:46:20 +01:00
|
|
|
@unittest.mock.patch('subprocess.Popen')
|
|
|
|
def test_005_localcmd(self, mock_popen):
|
2017-04-16 03:17:53 +02:00
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
b'0\x00test-vm class=AppVM state=Running\n'
|
2018-07-30 18:21:06 +02:00
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
|
|
|
|
b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
|
2017-04-16 03:17:53 +02:00
|
|
|
# self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
# ('test-vm', 'admin.vm.List', None, None)] = \
|
2017-04-16 03:17:53 +02:00
|
|
|
# b'0\x00test-vm class=AppVM state=Running\n'
|
tools: fix qvm-run --pass-io --localcmd=... vmname command
qubes.VMShell service, used by qvm-run, expects the command on the first
input line. Previously, when --localcmd was used, the command wasn't
written anywhere and the local command was connected directly to
qubes.VMShell service. And the first line of its output was interpreted
as a command.
Fix this by starting the local command separately, after sending the
command to qubes.VMShell service.
While at it, unify handling shell command and service calls in the process.
vm.run_service(..., localcmd= ) isn't that useful in general case,
because for qubes.VMShell the caller first need to send the command
before starting local process. Since the qvm-run tool needs to implement
manual starting localcmd anyway, don't use localcmd= run_service's
argument at all to unify calling methods.
There is slight behavior change: previously localcmd was started only
after establishing service connection (for example only if qrexec policy
allows), now it is started in all the cases.
Fixes QubesOS/qubes-issues#4040
2018-12-06 23:46:20 +01:00
|
|
|
mock_popen.return_value.wait.return_value = 0
|
2017-05-11 23:21:04 +02:00
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
2017-05-17 11:07:48 +02:00
|
|
|
['--no-gui', '--pass-io', '--localcmd', 'local-command',
|
2017-04-16 03:17:53 +02:00
|
|
|
'test-vm', 'command'],
|
|
|
|
app=self.app)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('test-vm', 'qubes.VMShell', {
|
tools: fix qvm-run --pass-io --localcmd=... vmname command
qubes.VMShell service, used by qvm-run, expects the command on the first
input line. Previously, when --localcmd was used, the command wasn't
written anywhere and the local command was connected directly to
qubes.VMShell service. And the first line of its output was interpreted
as a command.
Fix this by starting the local command separately, after sending the
command to qubes.VMShell service.
While at it, unify handling shell command and service calls in the process.
vm.run_service(..., localcmd= ) isn't that useful in general case,
because for qubes.VMShell the caller first need to send the command
before starting local process. Since the qvm-run tool needs to implement
manual starting localcmd anyway, don't use localcmd= run_service's
argument at all to unify calling methods.
There is slight behavior change: previously localcmd was started only
after establishing service connection (for example only if qrexec policy
allows), now it is started in all the cases.
Fixes QubesOS/qubes-issues#4040
2018-12-06 23:46:20 +01:00
|
|
|
'stdout': subprocess.PIPE,
|
|
|
|
'stdin': subprocess.PIPE,
|
2017-04-16 03:17:53 +02:00
|
|
|
'stderr': None,
|
|
|
|
'user': None,
|
|
|
|
}),
|
2017-05-18 09:54:26 +02:00
|
|
|
('test-vm', 'qubes.VMShell', b'command; exit\n')
|
2017-04-16 03:17:53 +02:00
|
|
|
])
|
tools: fix qvm-run --pass-io --localcmd=... vmname command
qubes.VMShell service, used by qvm-run, expects the command on the first
input line. Previously, when --localcmd was used, the command wasn't
written anywhere and the local command was connected directly to
qubes.VMShell service. And the first line of its output was interpreted
as a command.
Fix this by starting the local command separately, after sending the
command to qubes.VMShell service.
While at it, unify handling shell command and service calls in the process.
vm.run_service(..., localcmd= ) isn't that useful in general case,
because for qubes.VMShell the caller first need to send the command
before starting local process. Since the qvm-run tool needs to implement
manual starting localcmd anyway, don't use localcmd= run_service's
argument at all to unify calling methods.
There is slight behavior change: previously localcmd was started only
after establishing service connection (for example only if qrexec policy
allows), now it is started in all the cases.
Fixes QubesOS/qubes-issues#4040
2018-12-06 23:46:20 +01:00
|
|
|
mock_popen.assert_called_once_with('local-command',
|
|
|
|
# TODO: check if the right stdin/stdout objects are used
|
|
|
|
stdout=unittest.mock.ANY, stdin=unittest.mock.ANY, shell=True)
|
2017-04-16 03:17:53 +02:00
|
|
|
self.assertAllCalled()
|
2017-05-17 11:07:48 +02:00
|
|
|
|
|
|
|
def test_006_run_single_with_gui(self):
|
|
|
|
self.app.expected_calls[
|
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.property.Get', 'default_user', None)] = \
|
|
|
|
b'0\x00default=yes type=str user'
|
2018-07-30 18:21:06 +02:00
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
|
|
|
|
b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
|
2017-05-17 11:07:48 +02:00
|
|
|
# self.app.expected_calls[
|
|
|
|
# ('test-vm', 'admin.vm.List', None, None)] = \
|
|
|
|
# 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
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('test-vm', 'qubes.WaitForSession', {
|
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
|
|
|
}),
|
|
|
|
('test-vm', 'qubes.WaitForSession', b'user'),
|
|
|
|
('test-vm', 'qubes.VMShell', {
|
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
|
|
|
'user': None,
|
|
|
|
}),
|
2017-05-18 09:54:26 +02:00
|
|
|
('test-vm', 'qubes.VMShell', b'command; exit\n')
|
2017-05-17 11:07:48 +02:00
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
2017-05-19 19:41:39 +02:00
|
|
|
|
|
|
|
def test_007_run_service_with_gui(self):
|
|
|
|
self.app.expected_calls[
|
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.property.Get', 'default_user', None)] = \
|
|
|
|
b'0\x00default=yes type=str user'
|
|
|
|
# self.app.expected_calls[
|
|
|
|
# ('test-vm', 'admin.vm.List', None, None)] = \
|
|
|
|
# b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
|
|
|
['--service', 'test-vm', 'service.name'],
|
|
|
|
app=self.app)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
# make sure we have the same instance below
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('test-vm', 'qubes.WaitForSession', {
|
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
|
|
|
}),
|
|
|
|
('test-vm', 'qubes.WaitForSession', b'user'),
|
|
|
|
('test-vm', 'service.name', {
|
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
|
|
|
'user': None,
|
|
|
|
}),
|
|
|
|
('test-vm', 'service.name', b''),
|
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
2017-08-06 20:44:55 +02:00
|
|
|
|
|
|
|
def test_008_dispvm_remote(self):
|
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
|
|
|
['--dispvm', '--service', 'test.service'], app=self.app)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('$dispvm', 'test.service', {
|
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
|
|
|
'user': None,
|
|
|
|
}),
|
|
|
|
('$dispvm', 'test.service', b''),
|
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_009_dispvm_remote_specific(self):
|
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
|
|
|
['--dispvm=test-vm', '--service', 'test.service'], app=self.app)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('$dispvm:test-vm', 'test.service', {
|
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
|
|
|
'user': None,
|
|
|
|
}),
|
|
|
|
('$dispvm:test-vm', 'test.service', b''),
|
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_010_dispvm_local(self):
|
|
|
|
self.app.qubesd_connection_type = 'socket'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('dom0', 'admin.vm.CreateDisposable', None, None)] = \
|
|
|
|
b'0\0disp123'
|
|
|
|
self.app.expected_calls[('disp123', 'admin.vm.Kill', None, None)] = \
|
|
|
|
b'0\0'
|
2018-01-12 19:57:45 +01:00
|
|
|
self.app.expected_calls[
|
|
|
|
('disp123', 'admin.vm.property.Get', 'qrexec_timeout', None)] = \
|
|
|
|
b'0\0default=yes type=int 30'
|
2017-08-06 20:44:55 +02:00
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
|
|
|
['--dispvm', '--service', 'test.service'], app=self.app)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('disp123', 'test.service', {
|
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
|
|
|
'user': None,
|
2018-01-12 19:57:45 +01:00
|
|
|
'connect_timeout': 30,
|
2017-08-06 20:44:55 +02:00
|
|
|
}),
|
|
|
|
('disp123', 'test.service', b''),
|
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_011_dispvm_local_specific(self):
|
|
|
|
self.app.qubesd_connection_type = 'socket'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.CreateDisposable', None, None)] = \
|
|
|
|
b'0\0disp123'
|
|
|
|
self.app.expected_calls[('disp123', 'admin.vm.Kill', None, None)] = \
|
|
|
|
b'0\0'
|
2018-01-12 19:57:45 +01:00
|
|
|
self.app.expected_calls[
|
|
|
|
('disp123', 'admin.vm.property.Get', 'qrexec_timeout', None)] = \
|
|
|
|
b'0\0default=yes type=int 30'
|
2017-08-06 20:44:55 +02:00
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
|
|
|
['--dispvm=test-vm', '--service', 'test.service'], app=self.app)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('disp123', 'test.service', {
|
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
|
|
|
'user': None,
|
2018-01-12 19:57:45 +01:00
|
|
|
'connect_timeout': 30,
|
2017-08-06 20:44:55 +02:00
|
|
|
}),
|
|
|
|
('disp123', 'test.service', b''),
|
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_012_exclude(self):
|
|
|
|
self.app.expected_calls[
|
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00test-vm class=AppVM state=Running\n' \
|
|
|
|
b'test-vm2 class=AppVM state=Running\n' \
|
|
|
|
b'test-vm3 class=AppVM state=Halted\n'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm3', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00test-vm3 class=AppVM state=Halted\n'
|
2018-07-30 18:21:06 +02:00
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
|
|
|
|
b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
|
2017-08-06 20:44:55 +02:00
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
|
|
|
['--no-gui', '--all', '--exclude', 'test-vm2', 'command'],
|
|
|
|
app=self.app)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('test-vm', 'qubes.VMShell', {
|
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
|
|
|
'user': None,
|
|
|
|
}),
|
|
|
|
('test-vm', 'qubes.VMShell', b'command; exit\n'),
|
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
2017-10-19 14:55:33 +02:00
|
|
|
|
|
|
|
def test_013_no_autostart(self):
|
|
|
|
self.app.expected_calls[
|
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00test-vm class=AppVM state=Running\n' \
|
|
|
|
b'test-vm2 class=AppVM state=Running\n' \
|
|
|
|
b'test-vm3 class=AppVM state=Halted\n'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm3', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00test-vm3 class=AppVM state=Halted\n'
|
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
|
|
|
['--no-gui', '--no-autostart', 'test-vm3', 'command'],
|
|
|
|
app=self.app)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [])
|
|
|
|
self.assertAllCalled()
|
2018-01-14 19:36:46 +01:00
|
|
|
|
|
|
|
def test_014_dispvm_local_gui(self):
|
|
|
|
self.app.qubesd_connection_type = 'socket'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('dom0', 'admin.vm.CreateDisposable', None, None)] = \
|
|
|
|
b'0\0disp123'
|
|
|
|
self.app.expected_calls[('disp123', 'admin.vm.Kill', None, None)] = \
|
|
|
|
b'0\0'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('disp123', 'admin.vm.property.Get', 'qrexec_timeout', None)] = \
|
|
|
|
b'0\0default=yes type=int 30'
|
2018-07-30 18:21:06 +02:00
|
|
|
self.app.expected_calls[
|
|
|
|
('disp123', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
|
|
|
|
b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
|
2018-01-14 19:36:46 +01:00
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
|
|
|
['--dispvm', '--', 'test.command'], app=self.app)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('disp123', 'qubes.VMShell+WaitForSession', {
|
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
|
|
|
'user': None,
|
|
|
|
'connect_timeout': 30,
|
|
|
|
}),
|
|
|
|
('disp123', 'qubes.VMShell+WaitForSession',
|
|
|
|
b'test.command; exit\n'),
|
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_015_dispvm_local_no_gui(self):
|
|
|
|
self.app.qubesd_connection_type = 'socket'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('dom0', 'admin.vm.CreateDisposable', None, None)] = \
|
|
|
|
b'0\0disp123'
|
|
|
|
self.app.expected_calls[('disp123', 'admin.vm.Kill', None, None)] = \
|
|
|
|
b'0\0'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('disp123', 'admin.vm.property.Get', 'qrexec_timeout', None)] = \
|
|
|
|
b'0\0default=yes type=int 30'
|
2018-07-30 18:21:06 +02:00
|
|
|
self.app.expected_calls[
|
|
|
|
('disp123', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
|
|
|
|
b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
|
2018-01-14 19:36:46 +01:00
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
|
|
|
['--dispvm', '--no-gui', 'test.command'], app=self.app)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('disp123', 'qubes.VMShell', {
|
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
|
|
|
'user': None,
|
|
|
|
'connect_timeout': 30,
|
|
|
|
}),
|
|
|
|
('disp123', 'qubes.VMShell', b'test.command; exit\n'),
|
|
|
|
])
|
|
|
|
self.assertAllCalled()
|
2018-07-30 18:21:06 +02:00
|
|
|
|
|
|
|
def test_016_run_single_windows(self):
|
|
|
|
self.app.expected_calls[
|
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
|
|
|
b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
self.app.expected_calls[
|
|
|
|
('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
|
|
|
|
b'0\x00Windows'
|
|
|
|
# self.app.expected_calls[
|
|
|
|
# ('test-vm', 'admin.vm.List', None, None)] = \
|
|
|
|
# b'0\x00test-vm class=AppVM state=Running\n'
|
|
|
|
ret = qubesadmin.tools.qvm_run.main(
|
|
|
|
['--no-gui', 'test-vm', 'command'],
|
|
|
|
app=self.app)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
self.assertEqual(self.app.service_calls, [
|
|
|
|
('test-vm', 'qubes.VMShell', {
|
|
|
|
'stdout': subprocess.DEVNULL,
|
|
|
|
'stderr': subprocess.DEVNULL,
|
|
|
|
'user': None,
|
|
|
|
}),
|
|
|
|
('test-vm', 'qubes.VMShell', b'command& exit\n')
|
|
|
|
])
|
|
|
|
self.assertAllCalled()
|