test_libvirt_events.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -*- coding: utf-8 -*-
  2. '''
  3. unit tests for the libvirt_events engine
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch)
  13. # Import Salt Libs
  14. import salt.engines.libvirt_events as libvirt_events
  15. # pylint: disable=protected-access,attribute-defined-outside-init,invalid-name,unused-argument,no-self-use
  16. class EngineLibvirtEventTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.engine.libvirt_events
  19. '''
  20. def setup_loader_modules(self):
  21. patcher = patch('salt.engines.libvirt_events.libvirt')
  22. self.mock_libvirt = patcher.start()
  23. self.mock_libvirt.getVersion.return_value = 2000000
  24. self.mock_libvirt.virEventRunDefaultImpl.return_value = -1 # Don't loop for ever
  25. self.mock_libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE = 0
  26. self.mock_libvirt.VIR_DOMAIN_EVENT_ID_REBOOT = 1
  27. self.addCleanup(patcher.stop)
  28. self.addCleanup(delattr, self, 'mock_libvirt')
  29. return {libvirt_events: {}}
  30. @patch('salt.engines.libvirt_events.libvirt',
  31. VIR_PREFIX_NONE=0,
  32. VIR_PREFIX_ONE=1,
  33. VIR_PREFIX_TWO=2,
  34. VIR_PREFIX_SUB_FOO=0,
  35. VIR_PREFIX_SUB_BAR=1,
  36. VIR_PREFIX_SUB_FOOBAR=2)
  37. def test_get_libvirt_enum_string_subprefix(self, libvirt_mock):
  38. '''
  39. Make sure the libvirt enum value to string works reliably with
  40. elements with a sub prefix, eg VIR_PREFIX_SUB_* in this case.
  41. '''
  42. # Test case with a sub prefix
  43. assert libvirt_events._get_libvirt_enum_string('VIR_PREFIX_', 2) == 'two'
  44. @patch('salt.engines.libvirt_events.libvirt',
  45. VIR_PREFIX_FOO=0,
  46. VIR_PREFIX_BAR_FOO=1)
  47. def test_get_libvirt_enum_string_underscores(self, libvirt_mock):
  48. '''
  49. Make sure the libvirt enum value to string works reliably and items
  50. with an underscore aren't confused with sub prefixes.
  51. '''
  52. assert libvirt_events._get_libvirt_enum_string('VIR_PREFIX_', 1) == 'bar foo'
  53. @patch('salt.engines.libvirt_events.libvirt',
  54. VIR_DOMAIN_EVENT_CRASHED_PANICKED=0,
  55. VIR_DOMAIN_EVENT_DEFINED=0,
  56. VIR_DOMAIN_EVENT_UNDEFINED=1,
  57. VIR_DOMAIN_EVENT_CRASHED=2,
  58. VIR_DOMAIN_EVENT_DEFINED_ADDED=0,
  59. VIR_DOMAIN_EVENT_DEFINED_UPDATED=1)
  60. def test_get_domain_event_detail(self, mock_libvirt):
  61. '''
  62. Test get_domain_event_detail function
  63. '''
  64. assert libvirt_events._get_domain_event_detail(1, 2) == ('undefined', 'unknown')
  65. assert libvirt_events._get_domain_event_detail(0, 1) == ('defined', 'updated')
  66. assert libvirt_events._get_domain_event_detail(4, 2) == ('unknown', 'unknown')
  67. @patch('salt.engines.libvirt_events.libvirt', VIR_NETWORK_EVENT_ID_LIFECYCLE=1000)
  68. def test_event_register(self, mock_libvirt):
  69. '''
  70. Test that the libvirt_events engine actually registers events catch them and cleans
  71. before leaving the place.
  72. '''
  73. mock_cnx = MagicMock()
  74. mock_libvirt.openReadOnly.return_value = mock_cnx
  75. # Don't loop for ever
  76. mock_libvirt.virEventRunDefaultImpl.return_value = -1
  77. mock_cnx.networkEventRegisterAny.return_value = 10000
  78. libvirt_events.start('test:///', 'test/prefix')
  79. # Check that the connection has been opened
  80. mock_libvirt.openReadOnly.assert_called_once_with('test:///')
  81. # Check that the connection has been closed
  82. mock_cnx.close.assert_called_once()
  83. # Check events registration and deregistration
  84. mock_cnx.domainEventRegisterAny.assert_any_call(
  85. None, mock_libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
  86. libvirt_events._domain_event_lifecycle_cb,
  87. {'prefix': 'test/prefix', 'object': 'domain', 'event': 'lifecycle'})
  88. mock_cnx.networkEventRegisterAny.assert_any_call(
  89. None, mock_libvirt.VIR_NETWORK_EVENT_ID_LIFECYCLE,
  90. libvirt_events._network_event_lifecycle_cb,
  91. {'prefix': 'test/prefix', 'object': 'network', 'event': 'lifecycle'})
  92. # Check that the deregister events are called with the result of register
  93. mock_cnx.networkEventDeregisterAny.assert_called_with(
  94. mock_cnx.networkEventRegisterAny.return_value)
  95. # Check that the default 'all' filter actually worked
  96. counts = {obj: len(callback_def) for obj, callback_def in libvirt_events.CALLBACK_DEFS.items()}
  97. for obj, count in counts.items():
  98. register = libvirt_events.REGISTER_FUNCTIONS[obj]
  99. assert getattr(mock_cnx, register).call_count == count
  100. def test_event_skipped(self):
  101. '''
  102. Test that events are skipped if their ID isn't defined in the libvirt
  103. module (older libvirt)
  104. '''
  105. self.mock_libvirt.mock_add_spec([
  106. 'openReadOnly',
  107. 'virEventRegisterDefaultImpl',
  108. 'virEventRunDefaultImpl',
  109. 'VIR_DOMAIN_EVENT_ID_LIFECYCLE'], spec_set=True)
  110. libvirt_events.start('test:///', 'test/prefix')
  111. # Check events registration and deregistration
  112. mock_cnx = self.mock_libvirt.openReadOnly.return_value
  113. mock_cnx.domainEventRegisterAny.assert_any_call(
  114. None, self.mock_libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
  115. libvirt_events._domain_event_lifecycle_cb,
  116. {'prefix': 'test/prefix', 'object': 'domain', 'event': 'lifecycle'})
  117. # Network events should have been skipped
  118. mock_cnx.networkEventRegisterAny.assert_not_called()
  119. def test_event_filtered(self):
  120. '''
  121. Test that events are skipped if their ID isn't defined in the libvirt
  122. module (older libvirt)
  123. '''
  124. libvirt_events.start('test', 'test/prefix', 'domain/lifecycle')
  125. # Check events registration and deregistration
  126. mock_cnx = self.mock_libvirt.openReadOnly.return_value
  127. mock_cnx.domainEventRegisterAny.assert_any_call(
  128. None, 0, libvirt_events._domain_event_lifecycle_cb,
  129. {'prefix': 'test/prefix', 'object': 'domain', 'event': 'lifecycle'})
  130. # Network events should have been filtered out
  131. mock_cnx.networkEventRegisterAny.assert_not_called()