Merge branch 'devices-api'

* devices-api:
  devices: include devclass when comparing devices
  events: deserialize DeviceInfo class in device-* events
  devices: drop DeviceInfo.options
This commit is contained in:
Marek Marczykowski-Górecki 2018-10-07 02:46:14 +02:00
commit 0dfdab32e1
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
4 changed files with 22 additions and 8 deletions

View File

@ -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):
@ -95,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
)
@ -233,7 +233,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.

View File

@ -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)]

View File

@ -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()

View File

@ -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='{}')