From a8c24bee0fc08f740a038f6fb26efff935c7915c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Tue, 18 Sep 2018 21:54:51 +0200 Subject: [PATCH 1/3] devices: drop DeviceInfo.options It's part of DeviceAssignment class, here it was leftover from draft API. --- qubesadmin/devices.py | 5 ++--- qubesadmin/tests/devices.py | 5 ----- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/qubesadmin/devices.py b/qubesadmin/devices.py index 2afb664..b6fe22d 100644 --- a/qubesadmin/devices.py +++ b/qubesadmin/devices.py @@ -77,7 +77,7 @@ class DeviceInfo(object): ''' Holds all information about a device ''' # pylint: disable=too-few-public-methods def __init__(self, backend_domain, devclass, ident, description=None, - options=None, **kwargs): + **kwargs): #: domain providing this device self.backend_domain = backend_domain #: device class @@ -86,7 +86,6 @@ class DeviceInfo(object): self.ident = ident #: human readable description/name of the device self.description = description - self.options = options or dict() self.data = kwargs def __hash__(self): @@ -233,7 +232,7 @@ class DeviceCollection(object): for info_single in info.split(' ') if info_single) yield DeviceInfo(self._vm, self._class, ident, description=description, - options=None, **info_dict) + **info_dict) def update_persistent(self, device, persistent): '''Update `persistent` flag of already attached device. diff --git a/qubesadmin/tests/devices.py b/qubesadmin/tests/devices.py index d4f134b..2ea55b0 100644 --- a/qubesadmin/tests/devices.py +++ b/qubesadmin/tests/devices.py @@ -42,7 +42,6 @@ class TC_00_DeviceCollection(qubesadmin.tests.QubesTestCase): self.assertEqual(dev.backend_domain, self.vm) self.assertEqual(dev.ident, 'dev1') self.assertEqual(dev.description, '') - self.assertEqual(dev.options, {}) self.assertEqual(dev.data, {}) self.assertEqual(str(dev), 'test-vm:dev1') self.assertAllCalled() @@ -58,7 +57,6 @@ class TC_00_DeviceCollection(qubesadmin.tests.QubesTestCase): self.assertEqual(dev.backend_domain, self.vm) self.assertEqual(dev.ident, 'dev1') self.assertEqual(dev.description, 'This is description') - self.assertEqual(dev.options, {}) self.assertEqual(dev.data, {}) self.assertEqual(str(dev), 'test-vm:dev1') @@ -73,7 +71,6 @@ class TC_00_DeviceCollection(qubesadmin.tests.QubesTestCase): self.assertEqual(dev.backend_domain, self.vm) self.assertEqual(dev.ident, 'dev1') self.assertEqual(dev.description, 'This is description') - self.assertEqual(dev.options, {}) self.assertEqual(dev.data, {'ro': 'True', 'other': '123'}) self.assertEqual(str(dev), 'test-vm:dev1') self.assertAllCalled() @@ -87,7 +84,6 @@ class TC_00_DeviceCollection(qubesadmin.tests.QubesTestCase): self.assertEqual(dev.backend_domain, self.vm) self.assertEqual(dev.ident, 'dev1') self.assertEqual(dev.description, 'This is description') - self.assertEqual(dev.options, {}) self.assertEqual(dev.data, {}) self.assertEqual(str(dev), 'test-vm:dev1') self.assertAllCalled() @@ -101,7 +97,6 @@ class TC_00_DeviceCollection(qubesadmin.tests.QubesTestCase): self.assertEqual(dev.backend_domain, self.vm) self.assertEqual(dev.ident, 'dev2') self.assertEqual(dev.description, 'Unknown device') - self.assertEqual(dev.options, {}) self.assertEqual(dev.data, {}) self.assertEqual(str(dev), 'test-vm:dev2') self.assertAllCalled() From 4a1e90392b7c7176a8dad8afdc2662fad9444e27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Tue, 18 Sep 2018 21:55:32 +0200 Subject: [PATCH 2/3] events: deserialize DeviceInfo class in device-* events --- qubesadmin/events/__init__.py | 9 +++++++++ qubesadmin/tests/events.py | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/qubesadmin/events/__init__.py b/qubesadmin/events/__init__.py index 2f0487f..a10f16f 100644 --- a/qubesadmin/events/__init__.py +++ b/qubesadmin/events/__init__.py @@ -202,6 +202,15 @@ class EventsDispatcher(object): if event in ['domain-add', 'domain-delete']: self.app.domains.clear_cache() subject = None + # deserialize known attributes + if event.startswith('device-') and 'device' in kwargs: + try: + devclass = event.split(':', 1)[1] + backend_domain, ident = kwargs['device'].split(':', 1) + kwargs['device'] = self.app.domains.get_blind(backend_domain)\ + .devices[devclass][ident] + except (KeyError, ValueError): + pass handlers = [h_func for h_name, h_func_set in self.handlers.items() for h_func in h_func_set if fnmatch.fnmatch(event, h_name)] diff --git a/qubesadmin/tests/events.py b/qubesadmin/tests/events.py index e2f43e7..54984bb 100644 --- a/qubesadmin/tests/events.py +++ b/qubesadmin/tests/events.py @@ -253,3 +253,13 @@ class TC_00_Events(qubesadmin.tests.QubesTestCase): unittest.mock.call().kill.assert_called_once_with() loop.close() + + def test_030_events_device(self): + handler = unittest.mock.Mock() + self.dispatcher.add_handler('device-attach:test', handler) + self.dispatcher.handle('test-vm', 'device-attach:test', + device='test-vm2:dev', options='{}') + vm = self.app.domains.get_blind('test-vm') + dev = self.app.domains.get_blind('test-vm2').devices['test']['dev'] + handler.assert_called_once_with(vm, 'device-attach:test', device=dev, + options='{}') From 541473927235a2b9bb9d24c82c434e0f4e98fa12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Sat, 29 Sep 2018 14:13:06 +0200 Subject: [PATCH 3/3] devices: include devclass when comparing devices --- qubesadmin/devices.py | 1 + 1 file changed, 1 insertion(+) diff --git a/qubesadmin/devices.py b/qubesadmin/devices.py index b6fe22d..037c8c8 100644 --- a/qubesadmin/devices.py +++ b/qubesadmin/devices.py @@ -94,6 +94,7 @@ class DeviceInfo(object): def __eq__(self, other): try: return ( + self.devclass == other.devclass and self.backend_domain == other.backend_domain and self.ident == other.ident )