Skip to content

Commit

Permalink
vm: improve sending events for tags and features
Browse files Browse the repository at this point in the history
1. Send the event after setting tag/feature
2. Provide old value for feature
3. Rename 'key' kwarg to 'feature'
  • Loading branch information
marmarek committed Jun 14, 2017
1 parent 8ba60cd commit 160ab96
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
24 changes: 13 additions & 11 deletions qubes/tests/vm/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,22 @@ def setUp(self):
def test_000_set(self):
self.features['testfeature'] = 'value'
self.assertEventFired(self.vm, 'domain-feature-set',
kwargs={'key': 'testfeature', 'value': 'value'})
kwargs={'feature': 'testfeature', 'value': 'value'})

def test_001_set_existing(self):
self.features['test'] = 'value'
self.features['test'] = 'oldvalue'
self.vm.fired_events.clear()
self.features['test'] = 'value'
self.assertEventFired(self.vm, 'domain-feature-set',
kwargs={'key': 'test', 'value': 'value'})
kwargs={'feature': 'test', 'value': 'value', 'oldvalue':
'oldvalue'})

def test_002_unset(self):
self.features['test'] = 'value'
self.vm.fired_events.clear()
del self.features['test']
self.assertEventFired(self.vm, 'domain-feature-delete',
kwargs={'key': 'test'})
kwargs={'feature': 'test'})

def test_003_unset_not_present(self):
with self.assertRaises(KeyError):
Expand All @@ -244,28 +245,28 @@ def test_004_set_bool_true(self):
self.features['test'] = True
self.assertTrue(self.features['test'])
self.assertEventFired(self.vm, 'domain-feature-set',
kwargs={'key': 'test', 'value': '1'})
kwargs={'feature': 'test', 'value': '1'})

def test_005_set_bool_false(self):
self.features['test'] = False
self.assertFalse(self.features['test'])
self.assertEventFired(self.vm, 'domain-feature-set',
kwargs={'key': 'test', 'value': ''})
kwargs={'feature': 'test', 'value': ''})

def test_006_set_int(self):
self.features['test'] = 123
self.assertEventFired(self.vm, 'domain-feature-set',
kwargs={'key': 'test', 'value': '123'})
kwargs={'feature': 'test', 'value': '123'})

def test_007_clear(self):
self.features['test'] = 'value1'
self.features['test2'] = 'value2'
self.vm.fired_events.clear()
self.features.clear()
self.assertEventFired(self.vm, 'domain-feature-delete',
kwargs={'key': 'test'})
kwargs={'feature': 'test'})
self.assertEventFired(self.vm, 'domain-feature-delete',
kwargs={'key': 'test2'})
kwargs={'feature': 'test2'})

def test_008_update(self):
self.features['test'] = 'value'
Expand All @@ -276,6 +277,7 @@ def test_008_update(self):
self.assertEqual(self.features['test3'], 'value4')
self.assertEqual(self.features['test'], 'value')
self.assertEventFired(self.vm, 'domain-feature-set',
kwargs={'key': 'test2', 'value': 'value3'})
kwargs={'feature': 'test2', 'value': 'value3',
'oldvalue': 'value2'})
self.assertEventFired(self.vm, 'domain-feature-set',
kwargs={'key': 'test3', 'value': 'value4'})
kwargs={'feature': 'test3', 'value': 'value4'})
17 changes: 12 additions & 5 deletions qubes/vm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,24 @@ def __init__(self, vm, other=None, **kwargs):

def __delitem__(self, key):
super(Features, self).__delitem__(key)
self.vm.fire_event('domain-feature-delete', key=key)
self.vm.fire_event('domain-feature-delete', feature=key)

def __setitem__(self, key, value):
if value is None or isinstance(value, bool):
value = '1' if value else ''
else:
value = str(value)
# TODO: perhaps this shouldn't be fired on unchanged value? or at
# least oldvalue should be provided?
self.vm.fire_event('domain-feature-set', key=key, value=value)
try:
oldvalue = self[key]
has_oldvalue = True
except KeyError:
has_oldvalue = False
super(Features, self).__setitem__(key, value)
if has_oldvalue:
self.vm.fire_event('domain-feature-set', feature=key, value=value,
oldvalue=oldvalue)
else:
self.vm.fire_event('domain-feature-set', feature=key, value=value)

def clear(self):
for key in tuple(self):
Expand Down Expand Up @@ -225,8 +232,8 @@ def add(self, elem):
raise ValueError('Invalid character in tag')
if elem in self:
return
self.vm.fire_event('domain-tag-add', tag=elem)
super(Tags, self).add(elem)
self.vm.fire_event('domain-tag-add', tag=elem)

def remove(self, elem):
'''Remove a tag'''
Expand Down
9 changes: 6 additions & 3 deletions qubes/vm/qubesvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,22 +276,25 @@ class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM):
:param subject: Event emitter (the qube object)
:param event: Event name (``'domain-restore'``)
.. event:: domain-feature-set (subject, event, key, value)
.. event:: domain-feature-set (subject, event, feature, value
[, oldvalue])
A feature was changed.
*oldvalue* is present only when there was any.
:param subject: Event emitter (the qube object)
:param event: Event name (``'domain-feature-set'``)
:param key: feature name
:param feature: feature name
:param value: new value
:param oldvalue: old value, if any
.. event:: domain-feature-delete (subject, event, key)
A feature was removed.
:param subject: Event emitter (the qube object)
:param event: Event name (``'domain-feature-delete'``)
:param key: feature name
:param feature: feature name
.. event:: domain-tag-add (subject, event, tag)
Expand Down

0 comments on commit 160ab96

Please sign in to comment.