test_watchdog.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. from __future__ import print_function
  5. from __future__ import unicode_literals
  6. import os
  7. import shutil
  8. import tempfile
  9. import time
  10. # Salt libs
  11. import salt.utils.files
  12. from salt.beacons import watchdog
  13. from salt.ext.six.moves import range
  14. # Salt testing libs
  15. from tests.support.unit import skipIf, TestCase
  16. from tests.support.mixins import LoaderModuleMockMixin
  17. def check_events(config):
  18. total_delay = 1
  19. delay_per_loop = 20e-3
  20. for _ in range(int(total_delay / delay_per_loop)):
  21. events = watchdog.beacon(config)
  22. if events:
  23. return events
  24. time.sleep(delay_per_loop)
  25. return []
  26. def create(path, content=None):
  27. with salt.utils.files.fopen(path, 'w') as f:
  28. if content:
  29. f.write(content)
  30. os.fsync(f)
  31. @skipIf(not watchdog.HAS_WATCHDOG, 'watchdog is not available')
  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. config = [{'directories': {self.tmpdir: {'mask': ['modify']}}}]
  63. self.assertValid(config)
  64. self.assertEqual(watchdog.beacon(config), [])
  65. create(path, 'some content')
  66. ret = check_events(config)
  67. self.assertEqual(len(ret), 2)
  68. self.assertEqual(ret[0]['path'], os.path.dirname(path))
  69. self.assertEqual(ret[0]['change'], 'modified')
  70. self.assertEqual(ret[1]['path'], path)
  71. self.assertEqual(ret[1]['change'], 'modified')
  72. def test_file_deleted(self):
  73. path = os.path.join(self.tmpdir, 'tmpfile')
  74. create(path)
  75. config = [{'directories': {self.tmpdir: {'mask': ['delete']}}}]
  76. self.assertValid(config)
  77. self.assertEqual(watchdog.beacon(config), [])
  78. os.remove(path)
  79. ret = check_events(config)
  80. self.assertEqual(len(ret), 1)
  81. self.assertEqual(ret[0]['path'], path)
  82. self.assertEqual(ret[0]['change'], 'deleted')
  83. def test_file_moved(self):
  84. path = os.path.join(self.tmpdir, 'tmpfile')
  85. create(path)
  86. config = [{'directories': {self.tmpdir: {'mask': ['move']}}}]
  87. self.assertValid(config)
  88. self.assertEqual(watchdog.beacon(config), [])
  89. os.rename(path, path + '_moved')
  90. ret = check_events(config)
  91. self.assertEqual(len(ret), 1)
  92. self.assertEqual(ret[0]['path'], path)
  93. self.assertEqual(ret[0]['change'], 'moved')
  94. def test_file_create_in_directory(self):
  95. config = [{'directories': {self.tmpdir: {'mask': ['create', 'modify']}}}]
  96. self.assertValid(config)
  97. self.assertEqual(watchdog.beacon(config), [])
  98. path = os.path.join(self.tmpdir, 'tmpfile')
  99. create(path)
  100. ret = check_events(config)
  101. self.assertEqual(len(ret), 2)
  102. self.assertEqual(ret[0]['path'], path)
  103. self.assertEqual(ret[0]['change'], 'created')
  104. self.assertEqual(ret[1]['path'], self.tmpdir)
  105. self.assertEqual(ret[1]['change'], 'modified')
  106. def test_trigger_all_possible_events(self):
  107. path = os.path.join(self.tmpdir, 'tmpfile')
  108. moved = path + '_moved'
  109. config = [{'directories': {
  110. self.tmpdir: {},
  111. }}]
  112. self.assertValid(config)
  113. self.assertEqual(watchdog.beacon(config), [])
  114. # create
  115. create(path)
  116. # modify
  117. create(path, 'modified content')
  118. # move
  119. os.rename(path, moved)
  120. # delete
  121. os.remove(moved)
  122. ret = check_events(config)
  123. self.assertEqual(len(ret), 8)
  124. # create
  125. self.assertEqual(ret[0]['path'], path)
  126. self.assertEqual(ret[0]['change'], 'created')
  127. self.assertEqual(ret[1]['path'], self.tmpdir)
  128. self.assertEqual(ret[1]['change'], 'modified')
  129. # modify
  130. self.assertEqual(ret[2]['path'], path)
  131. self.assertEqual(ret[2]['change'], 'modified')
  132. self.assertEqual(ret[3]['path'], path)
  133. self.assertEqual(ret[3]['change'], 'modified')
  134. # move
  135. self.assertEqual(ret[4]['path'], path)
  136. self.assertEqual(ret[4]['change'], 'moved')
  137. self.assertEqual(ret[5]['path'], self.tmpdir)
  138. self.assertEqual(ret[5]['change'], 'modified')
  139. # delete
  140. self.assertEqual(ret[6]['path'], moved)
  141. self.assertEqual(ret[6]['change'], 'deleted')
  142. self.assertEqual(ret[7]['path'], self.tmpdir)
  143. self.assertEqual(ret[7]['change'], 'modified')