1
0

test_libvirt_events.py 6.4 KB

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