1
0

test_ps.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt testing libs
  5. from tests.support.unit import skipIf, TestCase
  6. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. # Salt libs
  9. import salt.beacons.ps as ps
  10. PATCH_OPTS = dict(autospec=True, spec_set=True)
  11. class FakeProcess(object):
  12. def __init__(self, _name, pid):
  13. self._name = _name
  14. self.pid = pid
  15. def name(self):
  16. return self._name
  17. @skipIf(NO_MOCK, NO_MOCK_REASON)
  18. class PSBeaconTestCase(TestCase, LoaderModuleMockMixin):
  19. '''
  20. Test case for salt.beacons.[s]
  21. '''
  22. def setup_loader_modules(self):
  23. return {}
  24. def test_non_list_config(self):
  25. config = {}
  26. ret = ps.validate(config)
  27. self.assertEqual(ret, (False, 'Configuration for ps beacon must'
  28. ' be a list.'))
  29. def test_empty_config(self):
  30. config = [{}]
  31. ret = ps.validate(config)
  32. self.assertEqual(ret, (False, 'Configuration for ps '
  33. 'beacon requires processes.'))
  34. def test_ps_running(self):
  35. with patch('salt.utils.psutil_compat.process_iter', **PATCH_OPTS) as mock_process_iter:
  36. mock_process_iter.return_value = [FakeProcess(_name='salt-master', pid=3),
  37. FakeProcess(_name='salt-minion', pid=4)]
  38. config = [{'processes': {'salt-master': 'running'}}]
  39. ret = ps.validate(config)
  40. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  41. ret = ps.beacon(config)
  42. self.assertEqual(ret, [{'salt-master': 'Running'}])
  43. def test_ps_not_running(self):
  44. with patch('salt.utils.psutil_compat.process_iter', **PATCH_OPTS) as mock_process_iter:
  45. mock_process_iter.return_value = [FakeProcess(_name='salt-master', pid=3),
  46. FakeProcess(_name='salt-minion', pid=4)]
  47. config = [{'processes': {'mysql': 'stopped'}}]
  48. ret = ps.validate(config)
  49. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  50. ret = ps.beacon(config)
  51. self.assertEqual(ret, [{'mysql': 'Stopped'}])