qvm-ls: add filtering by tags

This commit is contained in:
Patrik Hagara 2018-08-23 22:42:22 +02:00
parent 584bd052ed
commit cd4424235b
No known key found for this signature in database
GPG Key ID: 5C1E71DF031F9AE5
3 changed files with 68 additions and 1 deletions

View File

@ -6,7 +6,7 @@
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
-------
@ -43,6 +43,10 @@ Options
:option:`--format`. All columns along with short descriptions can be listed
with :option:`--help-columns`.
.. option:: --tags TAG ...
Shows only VMs having specific tag(s).
.. option:: --raw-data
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')
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):
def test_100_list_with_status(self):
self.app.expected_calls[

View File

@ -528,6 +528,9 @@ def get_parser():
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',
help='Display specify data of specified VMs. Intended for '
'bash-parsing.')
@ -605,6 +608,12 @@ def main(args=None, app=None):
domains = args.domains
else:
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.write_table(sys.stdout)