From 05115aead56cfc5ced499ae38eb45c1aaca02ed4 Mon Sep 17 00:00:00 2001 From: Piotr Bartman-Szwarc Date: Mon, 14 Oct 2024 22:59:13 +0200 Subject: [PATCH] q-dev: add short way to create DeviceAssignment --- qubesadmin/backup/restore.py | 11 ++++---- qubesadmin/device_protocol.py | 20 +++++++++++++ qubesadmin/tests/devices.py | 53 +++++++++++------------------------ qubesadmin/tools/qvm_start.py | 10 ++----- 4 files changed, 44 insertions(+), 50 deletions(-) diff --git a/qubesadmin/backup/restore.py b/qubesadmin/backup/restore.py index 27ac9ef4..cf8d728b 100644 --- a/qubesadmin/backup/restore.py +++ b/qubesadmin/backup/restore.py @@ -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', ) diff --git a/qubesadmin/device_protocol.py b/qubesadmin/device_protocol.py index 91ab73c8..2ea5f77e 100644 --- a/qubesadmin/device_protocol.py +++ b/qubesadmin/device_protocol.py @@ -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. diff --git a/qubesadmin/tests/devices.py b/qubesadmin/tests/devices.py index e1f898d2..fc3fe231 100644 --- a/qubesadmin/tests/devices.py +++ b/qubesadmin/tests/devices.py @@ -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() @@ -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) @@ -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() @@ -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) @@ -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() @@ -293,13 +288,8 @@ 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() @@ -307,13 +297,8 @@ 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() @@ -321,14 +306,8 @@ 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() diff --git a/qubesadmin/tools/qvm_start.py b/qubesadmin/tools/qvm_start.py index 0c314248..69f6a929 100644 --- a/qubesadmin/tools/qvm_start.py +++ b/qubesadmin/tools/qvm_start.py @@ -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