test_watchdog.py 6.4 KB

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