Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/299'
Browse files Browse the repository at this point in the history
* origin/pr/299:
  add tests
  fix QubesOS/qubes-issues/issues/9354
  • Loading branch information
marmarek committed Jul 18, 2024
2 parents 55ebbbe + 3e5edbf commit 39133f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
24 changes: 19 additions & 5 deletions qubesadmin/tests/tools/qvm_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,40 @@ def test_001_list_assigned_required(self):
# This shouldn't be listed
self.expected_device_call(
'test-vm2', 'Available',
b"0\0dev2 ident='dev1' devclass='testclass' backend_domain='test-vm2'\n")
self.expected_device_call('test-vm3', 'Available')
b"0\0dev2 ident='dev2' devclass='testclass' backend_domain='test-vm2'\n")
self.expected_device_call(
'test-vm3', 'Available',
b"0\0dev3 ident='dev3' devclass='testclass' backend_domain='test-vm3' vendor='evil inc.' product='test-device-3'\n"
)
self.expected_device_call('test-vm1', 'Attached')
self.expected_device_call('test-vm2', 'Attached')
self.expected_device_call('test-vm3', 'Attached')
self.expected_device_call('test-vm1', 'Assigned')
self.expected_device_call('test-vm2', 'Assigned')
self.expected_device_call(
'test-vm3', 'Assigned',
'test-vm2', 'Assigned',
b"0\0test-vm1+dev1 ident='dev1' devclass='testclass' "
b"backend_domain='test-vm1' attach_automatically='yes' "
b"required='yes' _option='other option' _extra_opt='yes'\n"
b"test-vm3+dev3 ident='dev3' devclass='testclass' "
b"backend_domain='test-vm3' attach_automatically='yes' "
b"required='yes'\n"
)
self.expected_device_call(
'test-vm3', 'Assigned',
b"0\0test-vm1+dev1 ident='dev1' devclass='testclass' "
b"backend_domain='test-vm1' attach_automatically='yes' "
b"required='yes' _option='test option'\n"
)

with qubesadmin.tests.tools.StdoutBuffer() as buf:
qubesadmin.tools.qvm_device.main(
['testclass', 'list', 'test-vm3'], app=self.app)
self.assertEqual(
buf.getvalue(),
'test-vm1:dev1 ?******: itl test-device test-vm3\n'
'test-vm1:dev1 ?******: itl test-device '
'test-vm2 (option=other option, extra_opt=yes), '
'test-vm3 (option=test option)\n'
'test-vm3:dev3 ?******: evil inc. test-device-3 test-vm2\n'
)

def test_002_list_attach(self):
Expand Down
14 changes: 7 additions & 7 deletions qubesadmin/tools/qvm_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ def list_devices(args):
for dev in result:
for vm in app.domains:
frontends = _load_frontends_info(vm, dev, args.devclass)
if frontends is not None:
result[dev].frontends.append(frontends)
result[dev].frontends.extend(frontends)

qubesadmin.tools.print_table(prepare_table(result.values()))

Expand Down Expand Up @@ -132,19 +131,20 @@ def _load_frontends_info(vm, dev, devclass):
Returns string of vms to which a device is connected or `None`.
"""
if vm == dev.backend_domain:
return None
return

try:
for assignment in vm.devices[devclass].get_dedicated_devices():
if dev != assignment:
return None
continue
if assignment.options:
return '{!s} ({})'.format(
yield '{!s} ({})'.format(
vm, ', '.join('{}={}'.format(key, value)
for key, value in assignment.options.items()))
return str(vm)
else:
yield str(vm)
except qubesadmin.exc.QubesVMNotFoundError:
return None
pass


def attach_device(args):
Expand Down

0 comments on commit 39133f4

Please sign in to comment.