Merge remote-tracking branch 'origin/pr/111'
* origin/pr/111: Add tests for --tree option of qvm-ls Sort domains in network tree list network tree manpage entry added added feature to list domains as network tree
This commit is contained in:
commit
6e76d1c928
@ -62,6 +62,11 @@ Options
|
|||||||
Give plain list of VM names, without header or separator. Useful in scripts.
|
Give plain list of VM names, without header or separator. Useful in scripts.
|
||||||
Same as --raw-data --fields=name
|
Same as --raw-data --fields=name
|
||||||
|
|
||||||
|
.. option:: --tree, -t
|
||||||
|
|
||||||
|
List domains as a network tree. Domains are sorted as they are connected to
|
||||||
|
their netvms. Names are indented relative to the number of connected netvms.
|
||||||
|
|
||||||
.. option:: --disk, -d
|
.. option:: --disk, -d
|
||||||
|
|
||||||
Same as --format=disk, for compatibility with Qubes 3.x
|
Same as --format=disk, for compatibility with Qubes 3.x
|
||||||
|
@ -120,6 +120,57 @@ class TC_50_List(qubesadmin.tests.QubesTestCase):
|
|||||||
'dom0\n'
|
'dom0\n'
|
||||||
'test-vm\n')
|
'test-vm\n')
|
||||||
|
|
||||||
|
def test_103_network_tree(self):
|
||||||
|
app = TestApp()
|
||||||
|
app.domains = TestVMCollection(
|
||||||
|
[
|
||||||
|
('dom0', TestVM('dom0')),
|
||||||
|
('test-vm-temp', TestVM('test-vm-temp')),
|
||||||
|
('test-vm-proxy', TestVM('test-vm-proxy')),
|
||||||
|
('test-vm-1', TestVM('test-vm-1')),
|
||||||
|
('test-vm-2', TestVM('test-vm-2')),
|
||||||
|
('test-vm-3', TestVM('test-vm-3')),
|
||||||
|
('test-vm-4', TestVM('test-vm-4')),
|
||||||
|
('test-vm-net-1', TestVM('test-vm-net-1')),
|
||||||
|
('test-vm-net-2', TestVM('test-vm-net-2')),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
ad = app.domains # For the sake of a 80 character line
|
||||||
|
ad['dom0'].label = 'black'
|
||||||
|
ad['test-vm-temp'].template = TestVM('template')
|
||||||
|
ad['test-vm-net-1'].netvm = None
|
||||||
|
ad['test-vm-net-1'].provides_network = True
|
||||||
|
ad['test-vm-net-2'].netvm = None
|
||||||
|
ad['test-vm-net-2'].provides_network = True
|
||||||
|
ad['test-vm-proxy'].netvm = TestVM('test-vm-net-2')
|
||||||
|
ad['test-vm-proxy'].provides_network = True
|
||||||
|
ad['test-vm-1'].netvm = TestVM('test-vm-net-1')
|
||||||
|
ad['test-vm-1'].provides_network = False
|
||||||
|
ad['test-vm-2'].netvm = TestVM('test-vm-proxy')
|
||||||
|
ad['test-vm-2'].provides_network = False
|
||||||
|
ad['test-vm-3'].netvm = TestVM('test-vm-proxy')
|
||||||
|
ad['test-vm-3'].provides_network = False
|
||||||
|
ad['test-vm-4'].netvm = TestVM('test-vm-net-2')
|
||||||
|
ad['test-vm-4'].provides_network = False
|
||||||
|
ad['test-vm-net-1'].connected_vms = [ad['test-vm-1']]
|
||||||
|
ad['test-vm-proxy'].connected_vms = [ad['test-vm-2'],
|
||||||
|
ad['test-vm-3']]
|
||||||
|
ad['test-vm-net-2'].connected_vms = [ad['test-vm-proxy'],
|
||||||
|
ad['test-vm-4']]
|
||||||
|
|
||||||
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
||||||
|
qubesadmin.tools.qvm_ls.main(['--tree'], app=app)
|
||||||
|
self.assertEqual(stdout.getvalue(),
|
||||||
|
'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
|
||||||
|
'dom0 Running TestVM black - -\n'
|
||||||
|
'test-vm-temp Running TestVM - template -\n'
|
||||||
|
'test-vm-net-1 Running TestVM - - -\n'
|
||||||
|
'└─test-vm-1 Running TestVM - - test-vm-net-1\n'
|
||||||
|
'test-vm-net-2 Running TestVM - - -\n'
|
||||||
|
'└─test-vm-proxy Running TestVM - - test-vm-net-2\n'
|
||||||
|
' └─test-vm-2 Running TestVM - - test-vm-proxy\n'
|
||||||
|
' └─test-vm-3 Running TestVM - - test-vm-proxy\n'
|
||||||
|
'└─test-vm-4 Running TestVM - - test-vm-net-2\n')
|
||||||
|
|
||||||
class TC_70_Tags(qubesadmin.tests.QubesTestCase):
|
class TC_70_Tags(qubesadmin.tests.QubesTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -38,6 +38,7 @@ import qubesadmin.spinner
|
|||||||
import qubesadmin.tools
|
import qubesadmin.tools
|
||||||
import qubesadmin.utils
|
import qubesadmin.utils
|
||||||
import qubesadmin.vm
|
import qubesadmin.vm
|
||||||
|
import qubesadmin.exc
|
||||||
|
|
||||||
#
|
#
|
||||||
# columns
|
# columns
|
||||||
@ -68,7 +69,7 @@ class Column(object):
|
|||||||
self.__class__.columns[self.ls_head] = self
|
self.__class__.columns[self.ls_head] = self
|
||||||
|
|
||||||
|
|
||||||
def cell(self, vm):
|
def cell(self, vm, insertion=0):
|
||||||
'''Format one cell.
|
'''Format one cell.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
@ -78,11 +79,15 @@ class Column(object):
|
|||||||
:py:meth:`Column.format` method instead.
|
:py:meth:`Column.format` method instead.
|
||||||
|
|
||||||
:param qubes.vm.qubesvm.QubesVM: Domain to get a value from.
|
:param qubes.vm.qubesvm.QubesVM: Domain to get a value from.
|
||||||
|
:param int insertion: Intending to shift the value to the right.
|
||||||
:returns: string to display
|
:returns: string to display
|
||||||
:rtype: str
|
:rtype: str
|
||||||
'''
|
'''
|
||||||
|
|
||||||
value = self.format(vm) or '-'
|
value = self.format(vm) or '-'
|
||||||
|
if insertion > 0 and self.ls_head == 'NAME':
|
||||||
|
value = '└─' + value
|
||||||
|
value = ' ' * (insertion-1) + value
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
@ -387,25 +392,73 @@ class Table(object):
|
|||||||
:param domains: Domains to include in the table.
|
: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, domains, colnames, spinner, raw_data=False):
|
def __init__(self, domains, colnames, spinner, raw_data=False,
|
||||||
|
tree_sorted=False):
|
||||||
self.domains = domains
|
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
|
||||||
self.raw_data = raw_data
|
self.raw_data = raw_data
|
||||||
|
self.tree_sorted = tree_sorted
|
||||||
|
|
||||||
def get_head(self):
|
def get_head(self):
|
||||||
'''Get table head data (all column heads).'''
|
'''Get table head data (all column heads).'''
|
||||||
return [col.ls_head for col in self.columns]
|
return [col.ls_head for col in self.columns]
|
||||||
|
|
||||||
def get_row(self, vm):
|
def get_row(self, vm, insertion=0):
|
||||||
'''Get single table row data (all columns for one domain).'''
|
'''Get single table row data (all columns for one domain).'''
|
||||||
ret = []
|
ret = []
|
||||||
for col in self.columns:
|
for col in self.columns:
|
||||||
|
if self.tree_sorted and col.ls_head == 'NAME':
|
||||||
|
ret.append(col.cell(vm, insertion))
|
||||||
|
else:
|
||||||
ret.append(col.cell(vm))
|
ret.append(col.cell(vm))
|
||||||
self.spinner.update()
|
self.spinner.update()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
def tree_append_child(self, parent, level):
|
||||||
|
'''Concatenate the network children of the vm to a list.
|
||||||
|
|
||||||
|
:param qubes.vm.qubesvm.QubesVM: Parent vm of the children VMs
|
||||||
|
'''
|
||||||
|
childs = list()
|
||||||
|
for child in parent.connected_vms:
|
||||||
|
if child.provides_network and child in self.domains:
|
||||||
|
childs.append((level, child))
|
||||||
|
childs += self.tree_append_child(child, level+1)
|
||||||
|
elif child in self.domains:
|
||||||
|
childs.append((level, child))
|
||||||
|
return childs
|
||||||
|
|
||||||
|
def sort_to_tree(self, domains):
|
||||||
|
'''Sort the domains as a network tree. It returns a list of sets. Each
|
||||||
|
tuple stores the insertion of the cell name and the vm object.
|
||||||
|
|
||||||
|
:param list() domains: The domains which will be sorted
|
||||||
|
:return list(tuple()) tree: returns a list of tuple(insertion, vm)
|
||||||
|
'''
|
||||||
|
tree = list()
|
||||||
|
# We need a copy of domains, because domains.remove() is not allowed
|
||||||
|
# while iterating over it. Besides, the domains should be sorted anyway.
|
||||||
|
sorted_doms = sorted(domains)
|
||||||
|
for dom in sorted_doms:
|
||||||
|
try:
|
||||||
|
if dom.netvm is None and not dom.provides_network:
|
||||||
|
tree.append((0, dom))
|
||||||
|
domains.remove(dom)
|
||||||
|
# dom0 and eventually others have no netvm attribute
|
||||||
|
except (qubesadmin.exc.QubesNoSuchPropertyError, AttributeError):
|
||||||
|
tree.append((0, dom))
|
||||||
|
domains.remove(dom)
|
||||||
|
|
||||||
|
for dom in sorted(domains):
|
||||||
|
if dom.netvm is None and dom.provides_network:
|
||||||
|
tree.append((0, dom))
|
||||||
|
domains.remove(dom)
|
||||||
|
tree += self.tree_append_child(dom, 1)
|
||||||
|
|
||||||
|
return tree
|
||||||
|
|
||||||
def write_table(self, stream=sys.stdout):
|
def write_table(self, stream=sys.stdout):
|
||||||
'''Write whole table to file-like object.
|
'''Write whole table to file-like object.
|
||||||
|
|
||||||
@ -417,6 +470,11 @@ 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()
|
||||||
|
if self.tree_sorted:
|
||||||
|
insertion_vm_list = self.sort_to_tree(self.domains)
|
||||||
|
for insertion, vm in insertion_vm_list:
|
||||||
|
table_data.append(self.get_row(vm, insertion))
|
||||||
|
else:
|
||||||
for vm in sorted(self.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()
|
||||||
@ -554,6 +612,10 @@ def get_parser():
|
|||||||
help='Display specify data of specified VMs. Intended for '
|
help='Display specify data of specified VMs. Intended for '
|
||||||
'bash-parsing.')
|
'bash-parsing.')
|
||||||
|
|
||||||
|
parser.add_argument('--tree', '-t',
|
||||||
|
action='store_const', const='tree',
|
||||||
|
help='sort domain list as network tree')
|
||||||
|
|
||||||
parser.add_argument('--spinner',
|
parser.add_argument('--spinner',
|
||||||
action='store_true', dest='spinner',
|
action='store_true', dest='spinner',
|
||||||
help='reenable spinner')
|
help='reenable spinner')
|
||||||
@ -637,7 +699,7 @@ def main(args=None, app=None):
|
|||||||
domains = [d for d in domains
|
domains = [d for d in domains
|
||||||
if matches_power_states(d, **pwrstates)]
|
if matches_power_states(d, **pwrstates)]
|
||||||
|
|
||||||
table = Table(domains, columns, spinner, args.raw_data)
|
table = Table(domains, columns, spinner, args.raw_data, args.tree)
|
||||||
table.write_table(sys.stdout)
|
table.write_table(sys.stdout)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
Loading…
Reference in New Issue
Block a user