qvm-template: by default list only latest available template

But add --all-versions option to get all the available versions.
This commit is contained in:
Marek Marczykowski-Górecki 2021-02-06 03:36:06 +01:00
parent 86326b53c4
commit 4f9757ca88
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 84 additions and 1 deletions

View File

@ -2088,6 +2088,7 @@ qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 0
available=False, available=False,
extras=False, extras=False,
upgrades=False, upgrades=False,
all_versions=True,
machine_readable=False, machine_readable=False,
machine_readable_json=False, machine_readable_json=False,
templates=['test-vm*'] templates=['test-vm*']
@ -2150,6 +2151,7 @@ qubes-template-fedora-32|0|4.1|20200101|qubes-templates-itl|1048576|2020-01-23 0
available=True, available=True,
extras=False, extras=False,
upgrades=False, upgrades=False,
all_versions=True,
machine_readable=False, machine_readable=False,
machine_readable_json=False, machine_readable_json=False,
templates=['fedora-32', 'fedora-31'] templates=['fedora-32', 'fedora-31']
@ -2179,6 +2181,19 @@ f'''Available Templates
@mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery') @mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
def test_151_list_templates_available_all_success(self, mock_query): def test_151_list_templates_available_all_success(self, mock_query):
mock_query.return_value = [ mock_query.return_value = [
qubesadmin.tools.qvm_template.Template(
'fedora-31',
'1',
'4.1',
'20190101',
'qubes-templates-itl',
1048576,
datetime.datetime(2019, 1, 23, 4, 56),
'GPL',
'https://qubes-os.org',
'Qubes template for fedora-31',
'Qubes template\n for fedora-31\n'
),
qubesadmin.tools.qvm_template.Template( qubesadmin.tools.qvm_template.Template(
'fedora-31', 'fedora-31',
'1', '1',
@ -2191,7 +2206,7 @@ f'''Available Templates
'https://qubes-os.org', 'https://qubes-os.org',
'Qubes template for fedora-31', 'Qubes template for fedora-31',
'Qubes template\n for fedora-31\n' 'Qubes template\n for fedora-31\n'
) ),
] ]
args = argparse.Namespace( args = argparse.Namespace(
all=False, all=False,
@ -2199,6 +2214,60 @@ f'''Available Templates
available=True, available=True,
extras=False, extras=False,
upgrades=False, upgrades=False,
all_versions=True,
machine_readable=False,
machine_readable_json=False,
templates=[]
)
with mock.patch('sys.stdout', new=io.StringIO()) as mock_out:
qubesadmin.tools.qvm_template.list_templates(
args, self.app, 'list')
self.assertEqual(mock_out.getvalue(),
'''Available Templates
[('fedora-31', '1:4.1-20190101', 'qubes-templates-itl'), ('fedora-31', '1:4.1-20200101', 'qubes-templates-itl')]
''')
self.assertEqual(mock_query.mock_calls, [
mock.call(args, self.app)
])
self.assertAllCalled()
@mock.patch('qubesadmin.tools.qvm_template.qrexec_repoquery')
def test_151_list_templates_available_only_latest_success(self, mock_query):
mock_query.return_value = [
qubesadmin.tools.qvm_template.Template(
'fedora-31',
'1',
'4.1',
'20190101',
'qubes-templates-itl',
1048576,
datetime.datetime(2019, 1, 23, 4, 56),
'GPL',
'https://qubes-os.org',
'Qubes template for fedora-31',
'Qubes template\n for fedora-31\n'
),
qubesadmin.tools.qvm_template.Template(
'fedora-31',
'1',
'4.1',
'20200101',
'qubes-templates-itl',
1048576,
datetime.datetime(2020, 1, 23, 4, 56),
'GPL',
'https://qubes-os.org',
'Qubes template for fedora-31',
'Qubes template\n for fedora-31\n'
),
]
args = argparse.Namespace(
all=False,
installed=False,
available=True,
extras=False,
upgrades=False,
all_versions=False,
machine_readable=False, machine_readable=False,
machine_readable_json=False, machine_readable_json=False,
templates=[] templates=[]
@ -2283,6 +2352,7 @@ f'''Available Templates
available=False, available=False,
extras=True, extras=True,
upgrades=False, upgrades=False,
all_versions=True,
machine_readable=False, machine_readable=False,
machine_readable_json=False, machine_readable_json=False,
templates=['test-vm*'] templates=['test-vm*']
@ -2385,6 +2455,7 @@ f'''Available Templates
available=False, available=False,
extras=False, extras=False,
upgrades=True, upgrades=True,
all_versions=True,
machine_readable=False, machine_readable=False,
machine_readable_json=False, machine_readable_json=False,
templates=['test-vm*'] templates=['test-vm*']
@ -2463,6 +2534,7 @@ f'''Available Templates
available=False, available=False,
extras=False, extras=False,
upgrades=False, upgrades=False,
all_versions=True,
machine_readable=False, machine_readable=False,
machine_readable_json=False, machine_readable_json=False,
templates=['test-vm*'] templates=['test-vm*']
@ -2482,6 +2554,7 @@ Available Templates
available=False, available=False,
extras=False, extras=False,
upgrades=False, upgrades=False,
all_versions=True,
machine_readable=False, machine_readable=False,
machine_readable_json=False, machine_readable_json=False,
templates=['test-vm*'] templates=['test-vm*']
@ -2501,6 +2574,7 @@ Available Templates
available=False, available=False,
extras=False, extras=False,
upgrades=False, upgrades=False,
all_versions=True,
machine_readable=False, machine_readable=False,
machine_readable_json=False, machine_readable_json=False,
templates=['test-vm*'] templates=['test-vm*']
@ -2520,6 +2594,7 @@ Available Templates
available=False, available=False,
extras=False, extras=False,
upgrades=False, upgrades=False,
all_versions=True,
machine_readable=True, machine_readable=True,
machine_readable_json=False, machine_readable_json=False,
templates=['test-vm*'] templates=['test-vm*']
@ -2537,6 +2612,7 @@ available|test-vm|2:4.1-2020|qubes-templates-itl
available=False, available=False,
extras=False, extras=False,
upgrades=False, upgrades=False,
all_versions=True,
machine_readable=True, machine_readable=True,
machine_readable_json=False, machine_readable_json=False,
templates=['test-vm*'] templates=['test-vm*']
@ -2554,6 +2630,7 @@ available|test-vm|2|4.1|2020|qubes-templates-itl|1048576|2020-09-01 14:30:00||GP
available=False, available=False,
extras=False, extras=False,
upgrades=False, upgrades=False,
all_versions=True,
machine_readable=False, machine_readable=False,
machine_readable_json=True, machine_readable_json=True,
templates=['test-vm*'] templates=['test-vm*']
@ -2570,6 +2647,7 @@ available|test-vm|2|4.1|2020|qubes-templates-itl|1048576|2020-09-01 14:30:00||GP
available=False, available=False,
extras=False, extras=False,
upgrades=False, upgrades=False,
all_versions=True,
machine_readable=False, machine_readable=False,
machine_readable_json=True, machine_readable_json=True,
templates=['test-vm*'] templates=['test-vm*']
@ -2588,6 +2666,7 @@ r'''{"installed": [{"name": "test-vm-2", "epoch": "1", "version": "4.0", "releas
available=True, available=True,
extras=False, extras=False,
upgrades=False, upgrades=False,
all_versions=True,
machine_readable=False, machine_readable=False,
machine_readable_json=False, machine_readable_json=False,
templates=[] templates=[]

View File

@ -176,6 +176,8 @@ def get_parser() -> argparse.ArgumentParser:
' locally but not in repos) templates.')) ' locally but not in repos) templates.'))
parser_x.add_argument('--upgrades', action='store_true', parser_x.add_argument('--upgrades', action='store_true',
help='Show available upgrades.') help='Show available upgrades.')
parser_x.add_argument('--all-versions', action='store_true',
help='Show all available versions, not only the latest.')
readable = parser_x.add_mutually_exclusive_group() readable = parser_x.add_mutually_exclusive_group()
readable.add_argument('--machine-readable', action='store_true', readable.add_argument('--machine-readable', action='store_true',
help='Enable machine-readable output.') help='Enable machine-readable output.')
@ -1128,6 +1130,8 @@ def list_templates(args: argparse.Namespace,
query_res = list(query_res_set) query_res = list(query_res_set)
else: else:
query_res = qrexec_repoquery(args, app) query_res = qrexec_repoquery(args, app)
if not args.all_versions:
query_res = filter_version(query_res, app)
if args.installed or args.all: if args.installed or args.all:
for vm in app.domains: for vm in app.domains: