Merge remote-tracking branch 'qubesos/pr/75'

* qubesos/pr/75:
  qvm-ls: add filtering by tags
This commit is contained in:
Marek Marczykowski-Górecki 2018-09-03 00:33:47 +02:00
commit 835336d640
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
3 changed files with 68 additions and 1 deletions

View File

@ -6,7 +6,7 @@
Synopsis Synopsis
-------- --------
:command:`qvm-ls` [-h] [--verbose] [--quiet] [--help-columns] [--help-formats] [--format *FORMAT* | --fields *FIELD*,...] :command:`qvm-ls` [-h] [--verbose] [--quiet] [--help-columns] [--help-formats] [--format *FORMAT* | --fields *FIELD*,...] [--tags *TAG* [*TAG* ...]]
Options Options
------- -------
@ -43,6 +43,10 @@ Options
:option:`--format`. All columns along with short descriptions can be listed :option:`--format`. All columns along with short descriptions can be listed
with :option:`--help-columns`. with :option:`--help-columns`.
.. option:: --tags TAG ...
Shows only VMs having specific tag(s).
.. option:: --raw-data .. option:: --raw-data
Output data in easy to parse format. Table header is skipped and columns are Output data in easy to parse format. Table header is skipped and columns are

View File

@ -121,6 +121,60 @@ class TC_50_List(qubesadmin.tests.QubesTestCase):
'test-vm\n') 'test-vm\n')
class TC_70_Tags(qubesadmin.tests.QubesTestCase):
def setUp(self):
self.app = TestApp()
self.app.domains = TestVMCollection(
[
(
'dom0',
TestVM(
'dom0',
tags=['my'],
label='black'
)
),
(
'test-vm',
TestVM(
'test-vm',
tags=['not-my', 'other'],
label='red',
netvm=TestVM('sys-firewall'),
template=TestVM('template')
)
),
]
)
def test_100_tag(self):
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
qubesadmin.tools.qvm_ls.main(['--tags', 'my'], app=self.app)
self.assertEqual(stdout.getvalue(),
'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
'dom0 Running TestVM black - -\n')
def test_100_tag_nomatch(self):
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
qubesadmin.tools.qvm_ls.main(['--tags', 'nx'], app=self.app)
self.assertEqual(stdout.getvalue(),
'NAME STATE CLASS LABEL TEMPLATE NETVM\n')
def test_100_tags(self):
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
qubesadmin.tools.qvm_ls.main(['--tags', 'my', 'other'], app=self.app)
self.assertEqual(stdout.getvalue(),
'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
'dom0 Running TestVM black - -\n'
'test-vm Running TestVM red template sys-firewall\n')
def test_100_tags_nomatch(self):
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
qubesadmin.tools.qvm_ls.main(['--tags', 'nx1', 'nx2'], app=self.app)
self.assertEqual(stdout.getvalue(),
'NAME STATE CLASS LABEL TEMPLATE NETVM\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[

View File

@ -528,6 +528,9 @@ def get_parser():
help='user specified format (see available columns below)') help='user specified format (see available columns below)')
parser.add_argument('--tags', nargs='+', metavar='TAG',
help='show only VMs having specific tag(s)')
parser.add_argument('--raw-data', action='store_true', parser.add_argument('--raw-data', action='store_true',
help='Display specify data of specified VMs. Intended for ' help='Display specify data of specified VMs. Intended for '
'bash-parsing.') 'bash-parsing.')
@ -605,6 +608,12 @@ def main(args=None, app=None):
domains = args.domains domains = args.domains
else: else:
domains = args.app.domains domains = args.app.domains
if args.tags:
# filter only VMs having at least one of the specified tags
domains = [dom for dom in domains
if set(dom.tags).intersection(set(args.tags))]
table = Table(domains, columns, spinner, args.raw_data) table = Table(domains, columns, spinner, args.raw_data)
table.write_table(sys.stdout) table.write_table(sys.stdout)