Skip to content

Commit

Permalink
q-dev: add short way to create DeviceAssignment
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed Oct 14, 2024
1 parent 2a6841d commit 05115ae
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 50 deletions.
11 changes: 5 additions & 6 deletions qubesadmin/backup/restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2093,12 +2093,11 @@ def _restore_vms_metadata(self, restore_info):
del options['required']
else:
required = False
assignment = DeviceAssignment(
VirtualDevice(Port(
backend_domain=self.app.domains[backend_domain],
port_id=port_id,
devclass=bus,
)),
assignment = DeviceAssignment.new(
backend_domain=self.app.domains[backend_domain],
port_id=port_id,
devclass=bus,
device_id=None,
options=options,
mode='required' if required else 'auto-attach',
)
Expand Down
20 changes: 20 additions & 0 deletions qubesadmin/device_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,26 @@ def __init__(
self.mode = AssignmentMode(mode)
self.frontend_domain = frontend_domain

@classmethod
def new(
cls,
backend_domain: QubesVM,
port_id: str,
devclass: str,
device_id: Optional[str] = None,
frontend_domain: Optional[QubesVM] = None,
options=None,
mode: Union[str, AssignmentMode] = "manual",
) -> 'DeviceAssignment':
return cls(
VirtualDevice(
Port(backend_domain, port_id, devclass),
device_id),
frontend_domain,
options,
mode
)

def clone(self, **kwargs):
"""
Clone object and substitute attributes with explicitly given.
Expand Down
53 changes: 16 additions & 37 deletions qubesadmin/tests/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ def test_020_attach(self):
b"backend_domain='test-vm2' mode='manual' "
b"frontend_domain='test-vm'")] = \
b'0\0'
assign = DeviceAssignment(
VirtualDevice(Port(
self.app.domains['test-vm2'], 'dev1', devclass='test',)))
assign = DeviceAssignment.new(
self.app.domains['test-vm2'], 'dev1', devclass='test')
self.vm.devices['test'].attach(assign)
self.assertAllCalled()

Expand All @@ -132,9 +131,8 @@ def test_021_attach_options(self):
b"backend_domain='test-vm2' mode='manual' "
b"frontend_domain='test-vm' _ro='True' "
b"_something='value'")] = b'0\0'
assign = DeviceAssignment(
VirtualDevice(Port(
self.app.domains['test-vm2'], 'dev1', devclass='test')))
assign = DeviceAssignment.new(
self.app.domains['test-vm2'], 'dev1', devclass='test')
assign.options['ro'] = True
assign.options['something'] = 'value'
self.vm.devices['test'].attach(assign)
Expand All @@ -146,9 +144,8 @@ def test_022_attach_required(self):
b"device_id='*' port_id='dev1' devclass='test' "
b"backend_domain='test-vm2' mode='required' "
b"frontend_domain='test-vm'")] = b'0\0'
assign = DeviceAssignment(
VirtualDevice(Port(
self.app.domains['test-vm2'], 'dev1', devclass='test')),
assign = DeviceAssignment.new(
self.app.domains['test-vm2'], 'dev1', devclass='test',
mode='required')
self.vm.devices['test'].attach(assign)
self.assertAllCalled()
Expand All @@ -159,9 +156,8 @@ def test_023_attach_required_options(self):
b"device_id='*' port_id='dev1' devclass='test' "
b"backend_domain='test-vm2' mode='required' "
b"frontend_domain='test-vm' _ro='True'")] = b'0\0'
assign = DeviceAssignment(
VirtualDevice(Port(
self.app.domains['test-vm2'], 'dev1', devclass='test')),
assign = DeviceAssignment.new(
self.app.domains['test-vm2'], 'dev1', devclass='test',
mode='required')
assign.options['ro'] = True
self.vm.devices['test'].attach(assign)
Expand All @@ -171,9 +167,8 @@ def test_030_detach(self):
self.app.expected_calls[
('test-vm', 'admin.vm.device.test.Detach', 'test-vm2+dev1:*',
None)] = b'0\0'
assign = DeviceAssignment(
VirtualDevice(Port(
self.app.domains['test-vm2'], 'dev1', devclass='test')))
assign = DeviceAssignment.new(
self.app.domains['test-vm2'], 'dev1', devclass='test')
self.vm.devices['test'].detach(assign)
self.assertAllCalled()

Expand Down Expand Up @@ -293,42 +288,26 @@ def test_070_update_assignment_required(self):
self.app.expected_calls[
('test-vm', 'admin.vm.device.test.Set.assignment',
'test-vm2+dev1:*', b'required')] = b'0\0'
dev = DeviceAssignment(
VirtualDevice(
Port(
self.app.domains['test-vm2'],
devclass='test',
port_id='dev1'),
))
dev = DeviceAssignment.new(
self.app.domains['test-vm2'], devclass='test', port_id='dev1')
self.vm.devices['test'].update_assignment(dev, AssignmentMode.REQUIRED)
self.assertAllCalled()

def test_071_update_assignment_ask(self):
self.app.expected_calls[
('test-vm', 'admin.vm.device.test.Set.assignment',
'test-vm2+dev1:*', b'ask-to-attach')] = b'0\0'
dev = DeviceAssignment(
VirtualDevice(
Port(
self.app.domains['test-vm2'],
devclass='test',
port_id='dev1'),
))
dev = DeviceAssignment.new(
self.app.domains['test-vm2'], devclass='test', port_id='dev1')
self.vm.devices['test'].update_assignment(dev, AssignmentMode.ASK)
self.assertAllCalled()

def test_072_update_assignment_auto(self):
self.app.expected_calls[
('test-vm', 'admin.vm.device.test.Set.assignment',
'test-vm2+dev1:*', b'auto-attach')] = b'0\0'
dev = DeviceAssignment(
VirtualDevice(
Port(
self.app.domains['test-vm2'],
devclass='test',
port_id='dev1',
)
))
dev = DeviceAssignment.new(
self.app.domains['test-vm2'], devclass='test', port_id='dev1')
self.vm.devices['test'].update_assignment(dev, AssignmentMode.AUTO)
self.assertAllCalled()

Expand Down
10 changes: 3 additions & 7 deletions qubesadmin/tools/qvm_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,9 @@ def get_drive_assignment(app, drive_str):
'devtype': devtype,
'read-only': devtype == 'cdrom'
}
assignment = DeviceAssignment(VirtualDevice(Port(
backend_domain=backend_domain,
port_id=port_id,
devclass='block',
)),
options=options,
mode="required")
assignment = DeviceAssignment.new(
backend_domain=backend_domain, port_id=port_id, devclass='block',
options=options, mode="required")

return assignment

Expand Down

0 comments on commit 05115ae

Please sign in to comment.