libvirt events: fix domain defined/updated event details

Libvirt events algorigthm converting the libvirt enums into string has a
flaw that unit tests couldn't see. Libvirt python binding defines the
following constants:

VIR_DOMAIN_EVENT_CRASHED_PANICKED = 0
VIR_DOMAIN_EVENT_DEFINED = 0
VIR_DOMAIN_EVENT_CRASHED = 8

However VIR_DOMAIN_EVENT_CRASHED_PANICKED is the only value in this enum
and thus wasn't not considered a sub-enum element. So the value 0 in
enum 'VIR_DOMAIN_EVENT_' was wrongly mapped to "crashed panicked"
instead of "defined".

In order to safely rule this case out, check if we have an item that
just ends with the subprefix without the '_'.
This commit is contained in:
Cédric Bosdonnat 2018-12-18 15:44:28 +01:00
parent 80197bc49e
commit 0d32cb9228
2 changed files with 5 additions and 4 deletions

View file

@ -165,7 +165,7 @@ def _get_libvirt_enum_string(prefix, value):
# Filter out the values starting with a common base as they match another enum
prefixes = [_compute_subprefix(p) for p in attributes]
counts = {p: prefixes.count(p) for p in prefixes}
sub_prefixes = [p for p, count in counts.items() if count > 1]
sub_prefixes = [p for p, count in counts.items() if count > 1 or (p.endswith('_') and p[:-1] in prefixes)]
filtered = [attr for attr in attributes if _compute_subprefix(attr) not in sub_prefixes]
for candidate in filtered:

View file

@ -56,18 +56,19 @@ class EngineLibvirtEventTestCase(TestCase, LoaderModuleMockMixin):
@patch('salt.engines.libvirt_events.libvirt',
VIR_PREFIX_FOO=0,
VIR_PREFIX_FOO_BAR=1,
VIR_PREFIX_BAR_FOO=2)
VIR_PREFIX_BAR_FOO=1)
def test_get_libvirt_enum_string_underscores(self, libvirt_mock):
'''
Make sure the libvirt enum value to string works reliably and items
with an underscore aren't confused with sub prefixes.
'''
assert libvirt_events._get_libvirt_enum_string('VIR_PREFIX_', 1) == 'foo bar'
assert libvirt_events._get_libvirt_enum_string('VIR_PREFIX_', 1) == 'bar foo'
@patch('salt.engines.libvirt_events.libvirt',
VIR_DOMAIN_EVENT_CRASHED_PANICKED=0,
VIR_DOMAIN_EVENT_DEFINED=0,
VIR_DOMAIN_EVENT_UNDEFINED=1,
VIR_DOMAIN_EVENT_CRASHED=2,
VIR_DOMAIN_EVENT_DEFINED_ADDED=0,
VIR_DOMAIN_EVENT_DEFINED_UPDATED=1)
def test_get_domain_event_detail(self, mock_libvirt):