tools/qvm-ls: allow list only selected VMs

Restore possibility to list only some VMs, by naming them on command
line.
This commit is contained in:
Marek Marczykowski-Górecki 2017-12-22 23:04:42 +01:00
parent 4af13d5445
commit 0533703ddf
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 64 additions and 6 deletions

View File

@ -98,6 +98,21 @@ class TC_50_List(qubesadmin.tests.QubesTestCase):
'dom0 - TestVM\n' 'dom0 - TestVM\n'
'test-vm pv TestVM\n') 'test-vm pv TestVM\n')
def test_102_list_selected(self):
app = TestApp()
app.domains['test-vm'].internal = False
app.domains['test-vm'].updateable = False
app.domains['test-vm'].template = TestVM('template')
app.domains['test-vm'].netvm = TestVM('sys-net')
app.domains['test-vm'].label = 'green'
app.domains['dom0'].label = 'black'
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
qubesadmin.tools.qvm_ls.main(['test-vm'], app=app)
self.assertEqual(stdout.getvalue(),
'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
'test-vm Running TestVM green template sys-net\n')
class TC_90_List_with_qubesd_calls(qubesadmin.tests.QubesTestCase): class TC_90_List_with_qubesd_calls(qubesadmin.tests.QubesTestCase):
def test_100_list_with_status(self): def test_100_list_with_status(self):
self.app.expected_calls[ self.app.expected_calls[
@ -150,3 +165,41 @@ class TC_90_List_with_qubesd_calls(qubesadmin.tests.QubesTestCase):
'template1 Halted TemplateVM black - sys-net\n' 'template1 Halted TemplateVM black - sys-net\n'
'vm1 Running AppVM green template1 sys-net\n') 'vm1 Running AppVM green template1 sys-net\n')
self.assertAllCalled() self.assertAllCalled()
def test_101_list_selected(self):
self.app.expected_calls[
('dom0', 'admin.vm.List', None, None)] = \
b'0\x00vm1 class=AppVM state=Running\n' \
b'template1 class=TemplateVM state=Halted\n' \
b'sys-net class=AppVM state=Running\n'
self.app.expected_calls[
('vm1', 'admin.vm.List', None, None)] = \
b'0\x00vm1 class=AppVM state=Running\n'
self.app.expected_calls[
('sys-net', 'admin.vm.List', None, None)] = \
b'0\x00sys-net class=AppVM state=Running\n'
props = {
'label': b'type=label green',
'template': b'type=vm template1',
'netvm': b'type=vm sys-net',
# 'virt_mode': b'type=str pv',
}
for key, value in props.items():
self.app.expected_calls[
('vm1', 'admin.vm.property.Get', key, None)] = \
b'0\x00default=True ' + value
# setup sys-net
props['label'] = b'type=label red'
for key, value in props.items():
self.app.expected_calls[
('sys-net', 'admin.vm.property.Get', key, None)] = \
b'0\x00default=True ' + value
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
qubesadmin.tools.qvm_ls.main(['vm1', 'sys-net'], app=self.app)
self.assertEqual(stdout.getvalue(),
'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
'sys-net Running AppVM red template1 sys-net\n'
'vm1 Running AppVM green template1 sys-net\n')
self.assertAllCalled()

View File

@ -384,11 +384,11 @@ FlagsColumn()
class Table(object): class Table(object):
'''Table that is displayed to the user. '''Table that is displayed to the user.
:param qubes.Qubes app: Qubes application object. :param domains: Domains to include in the table.
:param list colnames: Names of the columns (need not to be uppercase). :param list colnames: Names of the columns (need not to be uppercase).
''' '''
def __init__(self, app, colnames, spinner, raw_data=False): def __init__(self, domains, colnames, spinner, raw_data=False):
self.app = app self.domains = domains
self.columns = tuple(Column.columns[col.upper().replace('_', '-')] self.columns = tuple(Column.columns[col.upper().replace('_', '-')]
for col in colnames) for col in colnames)
self.spinner = spinner self.spinner = spinner
@ -417,12 +417,12 @@ class Table(object):
self.spinner.show('please wait...') self.spinner.show('please wait...')
table_data.append(self.get_head()) table_data.append(self.get_head())
self.spinner.update() self.spinner.update()
for vm in sorted(self.app.domains): for vm in sorted(self.domains):
table_data.append(self.get_row(vm)) table_data.append(self.get_row(vm))
self.spinner.hide() self.spinner.hide()
qubesadmin.tools.print_table(table_data, stream=stream) qubesadmin.tools.print_table(table_data, stream=stream)
else: else:
for vm in sorted(self.app.domains): for vm in sorted(self.domains):
stream.write('|'.join(self.get_row(vm)) + '\n') stream.write('|'.join(self.get_row(vm)) + '\n')
@ -504,6 +504,7 @@ def get_parser():
initial_indent=' ', subsequent_indent=' ') initial_indent=' ', subsequent_indent=' ')
parser = qubesadmin.tools.QubesArgumentParser( parser = qubesadmin.tools.QubesArgumentParser(
vmname_nargs=argparse.ZERO_OR_MORE,
formatter_class=argparse.RawTextHelpFormatter, formatter_class=argparse.RawTextHelpFormatter,
description='List Qubes domains and their parametres.', description='List Qubes domains and their parametres.',
epilog='available formats (see --help-formats):\n{}\n\n' epilog='available formats (see --help-formats):\n{}\n\n'
@ -579,7 +580,11 @@ def main(args=None, app=None):
else: else:
spinner = qubesadmin.spinner.DummySpinner(sys.stderr) spinner = qubesadmin.spinner.DummySpinner(sys.stderr)
table = Table(args.app, columns, spinner, args.raw_data) if args.domains:
domains = args.domains
else:
domains = args.app.domains
table = Table(domains, columns, spinner, args.raw_data)
table.write_table(sys.stdout) table.write_table(sys.stdout)
return 0 return 0