test_watchdog.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. import shutil
  6. import tempfile
  7. import time
  8. # Salt libs
  9. import salt.utils.files
  10. import salt.utils.platform
  11. from salt.beacons import watchdog
  12. from salt.ext.six.moves import range
  13. # Salt testing libs
  14. from tests.support.unit import skipIf, TestCase
  15. from tests.support.mixins import LoaderModuleMockMixin
  16. def check_events(config):
  17. total_delay = 1
  18. delay_per_loop = 20e-3
  19. for _ in range(int(total_delay / delay_per_loop)):
  20. events = watchdog.beacon(config)
  21. if events:
  22. return events
  23. time.sleep(delay_per_loop)
  24. return []
  25. def create(path, content=None):
  26. with salt.utils.files.fopen(path, 'w') as f:
  27. if content:
  28. f.write(content)
  29. os.fsync(f)
  30. @skipIf(not watchdog.HAS_WATCHDOG, 'watchdog is not available')
  31. @skipIf(salt.utils.platform.is_darwin(), 'Tests were being skipped pre macos under nox. Keep it like that for now.')
  32. class IWatchdogBeaconTestCase(TestCase, LoaderModuleMockMixin):
  33. '''
  34. Test case for salt.beacons.watchdog
  35. '''
  36. def setup_loader_modules(self):
  37. return {watchdog: {}}
  38. def setUp(self):
  39. self.tmpdir = tempfile.mkdtemp()
  40. def tearDown(self):
  41. watchdog.close({})
  42. shutil.rmtree(self.tmpdir, ignore_errors=True)
  43. def assertValid(self, config):
  44. ret = watchdog.validate(config)
  45. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  46. def test_empty_config(self):
  47. config = [{}]
  48. ret = watchdog.beacon(config)
  49. self.assertEqual(ret, [])
  50. def test_file_create(self):
  51. path = os.path.join(self.tmpdir, 'tmpfile')
  52. config = [{'directories': {self.tmpdir: {'mask': ['create']}}}]
  53. self.assertValid(config)
  54. self.assertEqual(watchdog.beacon(config), [])
  55. create(path)
  56. ret = check_events(config)
  57. self.assertEqual(len(ret), 1)
  58. self.assertEqual(ret[0]['path'], path)
  59. self.assertEqual(ret[0]['change'], 'created')
  60. def test_file_modified(self):
  61. path = os.path.join(self.tmpdir, 'tmpfile')
  62. # Create triggers a modify event along with the create event in Py3
  63. # So, let's do this before configuring the beacon
  64. create(path)
  65. config = [{'directories': {self.tmpdir: {'mask': ['modify']}}}]
  66. self.assertValid(config)
  67. self.assertEqual(watchdog.beacon(config), [])
  68. create(path, 'some content')
  69. ret = check_events(config)
  70. modified = False
  71. for event in ret:
  72. # "modified" requires special handling
  73. # A modification sometimes triggers 2 modified events depending on
  74. # the OS and the python version
  75. # When the "modified" event triggers on modify, it will have the
  76. # path to the temp file (path), other modified events will contain
  77. # the path minus "tmpfile" and will not match. That's how we'll
  78. # distinguish the two
  79. if event['change'] == 'modified':
  80. if event['path'] == path:
  81. modified = True
  82. # Check results of the for loop to validate modified
  83. self.assertTrue(modified)
  84. def test_file_deleted(self):
  85. path = os.path.join(self.tmpdir, 'tmpfile')
  86. create(path)
  87. config = [{'directories': {self.tmpdir: {'mask': ['delete']}}}]
  88. self.assertValid(config)
  89. self.assertEqual(watchdog.beacon(config), [])
  90. os.remove(path)
  91. ret = check_events(config)
  92. self.assertEqual(len(ret), 1)
  93. self.assertEqual(ret[0]['path'], path)
  94. self.assertEqual(ret[0]['change'], 'deleted')
  95. def test_file_moved(self):
  96. path = os.path.join(self.tmpdir, 'tmpfile')
  97. create(path)
  98. config = [{'directories': {self.tmpdir: {'mask': ['move']}}}]
  99. self.assertValid(config)
  100. self.assertEqual(watchdog.beacon(config), [])
  101. os.rename(path, path + '_moved')
  102. ret = check_events(config)
  103. self.assertEqual(len(ret), 1)
  104. self.assertEqual(ret[0]['path'], path)
  105. self.assertEqual(ret[0]['change'], 'moved')
  106. def test_file_create_in_directory(self):
  107. config = [{'directories': {self.tmpdir: {'mask': ['create']}}}]
  108. self.assertValid(config)
  109. self.assertEqual(watchdog.beacon(config), [])
  110. path = os.path.join(self.tmpdir, 'tmpfile')
  111. create(path)
  112. ret = check_events(config)
  113. self.assertEqual(len(ret), 1)
  114. self.assertEqual(ret[0]['path'], path)
  115. self.assertEqual(ret[0]['change'], 'created')
  116. def test_trigger_all_possible_events(self):
  117. path = os.path.join(self.tmpdir, 'tmpfile')
  118. moved = path + '_moved'
  119. config = [{'directories': {
  120. self.tmpdir: {},
  121. }}]
  122. self.assertValid(config)
  123. self.assertEqual(watchdog.beacon(config), [])
  124. # create
  125. create(path)
  126. # modify
  127. create(path, 'modified content')
  128. # move
  129. os.rename(path, moved)
  130. # delete
  131. os.remove(moved)
  132. # Give the events time to load into the queue
  133. time.sleep(1)
  134. ret = check_events(config)
  135. events = {'created': '',
  136. 'deleted': '',
  137. 'moved': ''}
  138. modified = False
  139. for event in ret:
  140. if event['change'] == 'created':
  141. self.assertEqual(event['path'], path)
  142. events.pop('created', '')
  143. if event['change'] == 'moved':
  144. self.assertEqual(event['path'], path)
  145. events.pop('moved', '')
  146. if event['change'] == 'deleted':
  147. self.assertEqual(event['path'], moved)
  148. events.pop('deleted', '')
  149. # "modified" requires special handling
  150. # All events [created, moved, deleted] also trigger a "modified"
  151. # event on Linux
  152. # Only the "created" event triggers a modified event on Py3 Windows
  153. # When the "modified" event triggers on modify, it will have the
  154. # path to the temp file (path), other modified events will contain
  155. # the path minus "tmpfile" and will not match. That's how we'll
  156. # distinguish the two
  157. if event['change'] == 'modified':
  158. if event['path'] == path:
  159. modified = True
  160. # Check results of the for loop to validate modified
  161. self.assertTrue(modified)
  162. # Make sure all events were checked
  163. self.assertDictEqual(events, {})