tools: allow supplying app object to main function
This is mostly for tests, which use dummy app instance.
This commit is contained in:
parent
61731a9490
commit
a575bc3422
86
qubesmgmt/tests/tools/qvm_prefs.py
Normal file
86
qubesmgmt/tests/tools/qvm_prefs.py
Normal file
@ -0,0 +1,86 @@
|
||||
# -*- 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 qubesmgmt.tests
|
||||
import qubesmgmt.tests.tools
|
||||
import qubesmgmt.tools.qvm_prefs
|
||||
|
||||
|
||||
class TC_00_qvm_prefs(qubesmgmt.tests.QubesTestCase):
|
||||
def test_000_list(self):
|
||||
self.app.expected_calls[
|
||||
('dom0', 'mgmt.vm.List', None, None)] = \
|
||||
b'0\x00dom0 class=AdminVM state=Running\n'
|
||||
self.app.expected_calls[
|
||||
('dom0', 'mgmt.vm.property.List', None, None)] = \
|
||||
b'0\x00prop1\nprop2\n'
|
||||
self.app.expected_calls[
|
||||
('dom0', 'mgmt.vm.property.Get', 'prop1', None)] = \
|
||||
b'0\x00default=True \'value1\''
|
||||
self.app.expected_calls[
|
||||
('dom0', 'mgmt.vm.property.Get', 'prop2', None)] = \
|
||||
b'0\x00default=False \'value2\''
|
||||
with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
|
||||
self.assertEqual(0, qubesmgmt.tools.qvm_prefs.main([
|
||||
'dom0'], app=self.app))
|
||||
self.assertEqual(stdout.getvalue(),
|
||||
'prop1 D value1\n'
|
||||
'prop2 - value2\n')
|
||||
self.assertAllCalled()
|
||||
|
||||
def test_001_no_vm(self):
|
||||
with self.assertRaises(SystemExit):
|
||||
qubesmgmt.tools.qvm_prefs.main([], app=self.app)
|
||||
self.assertAllCalled()
|
||||
|
||||
def test_002_set_property(self):
|
||||
self.app.expected_calls[
|
||||
('dom0', 'mgmt.vm.List', None, None)] = \
|
||||
b'0\x00dom0 class=AdminVM state=Running\n'
|
||||
self.app.expected_calls[
|
||||
('dom0', 'mgmt.vm.property.Set', 'default_user', b'testuser')] = \
|
||||
b'0\x00'
|
||||
self.assertEqual(0, qubesmgmt.tools.qvm_prefs.main([
|
||||
'dom0', 'default_user', 'testuser'], app=self.app))
|
||||
self.assertAllCalled()
|
||||
|
||||
def test_003_invalid_property(self):
|
||||
self.app.expected_calls[
|
||||
('dom0', 'mgmt.vm.List', None, None)] = \
|
||||
b'0\x00dom0 class=AdminVM state=Running\n'
|
||||
self.app.expected_calls[
|
||||
('dom0', 'mgmt.vm.property.Get', 'no_such_property', None)] = \
|
||||
b'2\x00AttributeError\x00\x00no_such_property\x00'
|
||||
with self.assertRaises(SystemExit):
|
||||
qubesmgmt.tools.qvm_prefs.main([
|
||||
'dom0', 'no_such_property'], app=self.app)
|
||||
self.assertAllCalled()
|
||||
|
||||
def test_004_set_invalid_property(self):
|
||||
self.app.expected_calls[
|
||||
('dom0', 'mgmt.vm.List', None, None)] = \
|
||||
b'0\x00dom0 class=AdminVM state=Running\n'
|
||||
self.app.expected_calls[
|
||||
('dom0', 'mgmt.vm.property.Set', 'no_such_property', b'value')] = \
|
||||
b'2\x00AttributeError\x00\x00no_such_property\x00'
|
||||
with self.assertRaises(SystemExit):
|
||||
qubesmgmt.tools.qvm_prefs.main([
|
||||
'dom0', 'no_such_property', 'value'], app=self.app)
|
||||
self.assertAllCalled()
|
@ -350,14 +350,16 @@ class QubesArgumentParser(argparse.ArgumentParser):
|
||||
self.set_defaults(verbose=1, quiet=0)
|
||||
|
||||
def parse_args(self, *args, **kwargs):
|
||||
# hack for tests
|
||||
app = kwargs.pop('app', None)
|
||||
namespace = super(QubesArgumentParser, self).parse_args(*args, **kwargs)
|
||||
|
||||
if self._want_app and not self._want_app_no_instance:
|
||||
self.set_qubes_verbosity(namespace)
|
||||
namespace.app = qubesmgmt.Qubes()
|
||||
|
||||
if self._want_force_root:
|
||||
self.dont_run_as_root(namespace)
|
||||
if app is not None:
|
||||
namespace.app = app
|
||||
else:
|
||||
namespace.app = qubesmgmt.Qubes()
|
||||
|
||||
for action in self._actions:
|
||||
# pylint: disable=protected-access
|
||||
|
@ -31,9 +31,9 @@ import qubesmgmt
|
||||
import qubesmgmt.tools.qvm_prefs
|
||||
|
||||
|
||||
def main(args=None): # pylint: disable=missing-docstring
|
||||
parser = qubesmgmt.tools.qvm_prefs.get_parser(0)
|
||||
args = parser.parse_args(args)
|
||||
def main(args=None, app=None): # pylint: disable=missing-docstring
|
||||
parser = qubesmgmt.tools.qvm_prefs.get_parser(None)
|
||||
args = parser.parse_args(args, app=app)
|
||||
target = args.app
|
||||
return qubesmgmt.tools.qvm_prefs.process_actions(parser, args, target)
|
||||
|
||||
|
@ -29,14 +29,14 @@ parser = qubesmgmt.tools.QubesArgumentParser(
|
||||
description='forceful shutdown of a domain', vmname_nargs='+')
|
||||
|
||||
|
||||
def main(args=None):
|
||||
def main(args=None, app=None):
|
||||
'''Main routine of :program:`qvm-kill`.
|
||||
|
||||
:param list args: Optional arguments to override those delivered from \
|
||||
command line.
|
||||
'''
|
||||
|
||||
args = parser.parse_args(args)
|
||||
args = parser.parse_args(args, app=app)
|
||||
|
||||
exit_code = 0
|
||||
for domain in args.domains:
|
||||
|
@ -28,14 +28,14 @@ parser = qubesmgmt.tools.QubesArgumentParser(vmname_nargs='+',
|
||||
description='pause a domain')
|
||||
|
||||
|
||||
def main(args=None):
|
||||
def main(args=None, app=None):
|
||||
'''Main routine of :program:`qvm-pause`.
|
||||
|
||||
:param list args: Optional arguments to override those delivered from \
|
||||
command line.
|
||||
'''
|
||||
|
||||
args = parser.parse_args(args)
|
||||
args = parser.parse_args(args, app=app)
|
||||
for domain in args.domains:
|
||||
domain.pause()
|
||||
|
||||
|
@ -117,9 +117,9 @@ def process_actions(parser, args, target):
|
||||
return 0
|
||||
|
||||
|
||||
def main(args=None): # pylint: disable=missing-docstring
|
||||
def main(args=None, app=None): # pylint: disable=missing-docstring
|
||||
parser = get_parser(1)
|
||||
args = parser.parse_args(args)
|
||||
args = parser.parse_args(args, app=app)
|
||||
target = args.domains.pop()
|
||||
return process_actions(parser, args, target)
|
||||
|
||||
|
@ -48,8 +48,8 @@ parser.add_argument('--timeout',
|
||||
' (default: %d)')
|
||||
|
||||
|
||||
def main(args=None): # pylint: disable=missing-docstring
|
||||
args = parser.parse_args(args)
|
||||
def main(args=None, app=None): # pylint: disable=missing-docstring
|
||||
args = parser.parse_args(args, app=app)
|
||||
|
||||
for vm in args.domains:
|
||||
if not vm.is_halted():
|
||||
|
@ -29,14 +29,14 @@ parser = qubesmgmt.tools.QubesArgumentParser(
|
||||
description='unpause a domain')
|
||||
|
||||
|
||||
def main(args=None):
|
||||
def main(args=None, app=None):
|
||||
'''Main routine of :program:`qvm-unpause`.
|
||||
|
||||
:param list args: Optional arguments to override those delivered from \
|
||||
command line.
|
||||
'''
|
||||
|
||||
args = parser.parse_args(args)
|
||||
args = parser.parse_args(args, app=app)
|
||||
for domain in args.domains:
|
||||
domain.unpause()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user