1
0

test_ps.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt libs
  5. import salt.beacons.ps as ps
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.mock import patch
  8. # Salt testing libs
  9. from tests.support.unit import TestCase
  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 be a list."))
  27. def test_empty_config(self):
  28. config = [{}]
  29. ret = ps.validate(config)
  30. self.assertEqual(
  31. ret, (False, "Configuration for ps beacon requires processes.")
  32. )
  33. def test_ps_running(self):
  34. with patch(
  35. "salt.utils.psutil_compat.process_iter", **PATCH_OPTS
  36. ) as mock_process_iter:
  37. mock_process_iter.return_value = [
  38. FakeProcess(_name="salt-master", pid=3),
  39. FakeProcess(_name="salt-minion", pid=4),
  40. ]
  41. config = [{"processes": {"salt-master": "running"}}]
  42. ret = ps.validate(config)
  43. self.assertEqual(ret, (True, "Valid beacon configuration"))
  44. ret = ps.beacon(config)
  45. self.assertEqual(ret, [{"salt-master": "Running"}])
  46. def test_ps_not_running(self):
  47. with patch(
  48. "salt.utils.psutil_compat.process_iter", **PATCH_OPTS
  49. ) as mock_process_iter:
  50. mock_process_iter.return_value = [
  51. FakeProcess(_name="salt-master", pid=3),
  52. FakeProcess(_name="salt-minion", pid=4),
  53. ]
  54. config = [{"processes": {"mysql": "stopped"}}]
  55. ret = ps.validate(config)
  56. self.assertEqual(ret, (True, "Valid beacon configuration"))
  57. ret = ps.beacon(config)
  58. self.assertEqual(ret, [{"mysql": "Stopped"}])