tools: qvm-features

The tool and tests.
This commit is contained in:
Marek Marczykowski-Górecki 2017-04-21 03:51:20 +02:00
parent ed93b40e72
commit 77a96b8b21
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 342 additions and 0 deletions

View File

@ -0,0 +1,252 @@
# -*- 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.tools.qvm_features
class TC_00_qvm_features(qubesmgmt.tests.QubesTestCase):
def test_000_list(self):
self.app.expected_calls[
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n'
self.app.expected_calls[
('some-vm', 'mgmt.vm.feature.List', None, None)] = \
b'0\x00feature1\nfeature2\n'
self.app.expected_calls[
('some-vm', 'mgmt.vm.feature.Get', 'feature1', None)] = \
b'0\x00value1'
self.app.expected_calls[
('some-vm', 'mgmt.vm.feature.Get', 'feature2', None)] = \
b'0\x00value2 with spaces'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(
qubesmgmt.tools.qvm_features.main(['some-vm'], app=self.app),
0)
self.assertEqual(stdout.getvalue(),
'feature1 value1\n'
'feature2 value2 with spaces\n')
self.assertAllCalled()
def test_001_set(self):
self.app.expected_calls[
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n'
self.app.expected_calls[
('some-vm', 'mgmt.vm.feature.Set',
'feature3', 'value of feature')] = b'0\x00'
self.assertEqual(
qubesmgmt.tools.qvm_features.main(['some-vm', 'feature3',
'value of feature'],
app=self.app),
0)
self.assertAllCalled()
def test_002_get(self):
self.app.expected_calls[
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n'
self.app.expected_calls[
('some-vm', 'mgmt.vm.feature.Get', 'feature3', None)] = \
b'0\x00value2 with spaces'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(
qubesmgmt.tools.qvm_features.main(['some-vm', 'feature3'],
app=self.app),
0)
self.assertEqual(stdout.getvalue(),
'value2 with spaces\n')
self.assertAllCalled()
def test_003_del(self):
self.app.expected_calls[
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n'
self.app.expected_calls[
('some-vm', 'mgmt.vm.feature.Remove', 'feature4', None)] = \
b'0\x00'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(
qubesmgmt.tools.qvm_features.main(['--unset', 'some-vm',
'feature4'],
app=self.app),
0)
self.assertEqual(stdout.getvalue(),
'')
self.assertAllCalled()
def test_004_running_verbose(self):
self.app.expected_calls[
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n' \
b'some-vm2 class=AppVM state=Running\n' \
b'some-vm3 class=AppVM state=Halted\n'
self.app.expected_calls[
('some-vm', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(
qubesmgmt.tools.qvm_check.main(['--running',
'some-vm'], app=self.app),
0)
self.assertEqual(stdout.getvalue(),
'VM some-vm is running\n')
self.assertAllCalled()
def test_005_running_multi_verbose(self):
self.app.expected_calls[
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n' \
b'some-vm2 class=AppVM state=Running\n' \
b'some-vm3 class=AppVM state=Halted\n'
self.app.expected_calls[
('some-vm', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n'
self.app.expected_calls[
('some-vm2', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm2 class=AppVM state=Running\n'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(
qubesmgmt.tools.qvm_check.main(['--running',
'some-vm', 'some-vm2'],
app=self.app),
0)
self.assertEqual(stdout.getvalue(),
'VMs some-vm, some-vm2 are running\n')
self.assertAllCalled()
def test_006_running_multi_verbose2(self):
self.app.expected_calls[
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n' \
b'some-vm2 class=AppVM state=Running\n' \
b'some-vm3 class=AppVM state=Halted\n'
self.app.expected_calls[
('some-vm', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n'
self.app.expected_calls[
('some-vm2', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm2 class=AppVM state=Running\n'
self.app.expected_calls[
('some-vm3', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm3 class=AppVM state=Halted\n'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(
qubesmgmt.tools.qvm_check.main(['--running',
'--all'],
app=self.app),
0)
self.assertEqual(stdout.getvalue(),
'VMs some-vm, some-vm2 are running\n')
self.assertAllCalled()
def test_007_not_running_verbose(self):
self.app.expected_calls[
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n' \
b'some-vm2 class=AppVM state=Running\n' \
b'some-vm3 class=AppVM state=Halted\n'
self.app.expected_calls[
('some-vm3', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm3 class=AppVM state=Halted\n'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(
qubesmgmt.tools.qvm_check.main(['--running',
'some-vm3'],
app=self.app),
1)
self.assertEqual(stdout.getvalue(),
'None of given VM is running\n')
self.assertAllCalled()
def test_008_paused(self):
self.app.expected_calls[
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n' \
b'some-vm2 class=AppVM state=Paused\n' \
b'some-vm3 class=AppVM state=Halted\n'
self.app.expected_calls[
('some-vm2', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm2 class=AppVM state=Paused\n'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(
qubesmgmt.tools.qvm_check.main(['--paused',
'some-vm2'],
app=self.app),
0)
self.assertEqual(stdout.getvalue(),
'VM some-vm2 is paused\n')
self.assertAllCalled()
def test_009_paused_multi(self):
self.app.expected_calls[
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n' \
b'some-vm2 class=AppVM state=Paused\n' \
b'some-vm3 class=AppVM state=Halted\n'
self.app.expected_calls[
('some-vm2', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm2 class=AppVM state=Paused\n'
self.app.expected_calls[
('some-vm', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(
qubesmgmt.tools.qvm_check.main(['--paused',
'some-vm2', 'some-vm'],
app=self.app),
0)
self.assertEqual(stdout.getvalue(),
'VM some-vm2 is paused\n')
self.assertAllCalled()
def test_010_template(self):
self.app.expected_calls[
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n' \
b'some-vm2 class=AppVM state=Paused\n' \
b'some-vm3 class=TemplateVM state=Halted\n'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(
qubesmgmt.tools.qvm_check.main(['--template',
'some-vm3'],
app=self.app),
0)
self.assertEqual(stdout.getvalue(),
'VM some-vm3 is a template\n')
self.assertAllCalled()
def test_011_template_multi(self):
self.app.expected_calls[
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n' \
b'some-vm2 class=AppVM state=Paused\n' \
b'some-vm3 class=TemplateVM state=Halted\n'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(
qubesmgmt.tools.qvm_check.main(['--template',
'some-vm2', 'some-vm3'],
app=self.app),
0)
self.assertEqual(stdout.getvalue(),
'VM some-vm3 is a template\n')
self.assertAllCalled()

View File

@ -0,0 +1,90 @@
# coding=utf-8
#
# The Qubes OS Project, https://www.qubes-os.org/
#
# Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
# Copyright (C) 2016 Wojtek Porczyk <woju@invisiblethingslab.com>
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
'''qvm-features - Manage domain's features'''
from __future__ import print_function
import sys
import qubesmgmt
import qubesmgmt.tools
parser = qubesmgmt.tools.QubesArgumentParser(
vmname_nargs=1,
description='manage domain\'s features')
parser.add_argument('feature', metavar='FEATURE',
action='store', nargs='?',
help='name of the feature')
parser.add_argument('value', metavar='VALUE',
action='store', nargs='?',
help='new value of the feature')
parser.add_argument('--unset', '--default', '--delete', '-D',
dest='delete',
action='store_true',
help='unset the feature')
def main(args=None, app=None):
'''Main routine of :program:`qvm-features`.
:param list args: Optional arguments to override those delivered from \
command line.
'''
args = parser.parse_args(args, app=app)
vm = args.domains[0]
if args.feature is None:
if args.delete:
parser.error('--unset requires a feature')
features = [(feat, vm.features[feat]) for feat in vm.features]
qubesmgmt.tools.print_table(features)
elif args.delete:
if args.value is not None:
parser.error('cannot both set and unset a value')
try:
del vm.features[args.feature]
except KeyError:
pass
elif args.value is None:
try:
print(vm.features[args.feature])
return 0
except KeyError:
return 1
else:
vm.features[args.feature] = args.value
return 0
if __name__ == '__main__':
sys.exit(main())