1
0

test_ps.py 2.2 KB

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