test_service.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. from collections import namedtuple
  5. # Salt testing libs
  6. from tests.support.unit import skipIf, TestCase
  7. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. # Salt libs
  10. import salt.beacons.service as service_beacon
  11. PATCH_OPTS = dict(autospec=True, spec_set=True)
  12. FakeProcess = namedtuple('Process', 'cmdline pid')
  13. @skipIf(NO_MOCK, NO_MOCK_REASON)
  14. class ServiceBeaconTestCase(TestCase, LoaderModuleMockMixin):
  15. '''
  16. Test case for salt.beacons.service
  17. '''
  18. def setup_loader_modules(self):
  19. return {
  20. service_beacon: {
  21. '__context__': {},
  22. '__salt__': {},
  23. }
  24. }
  25. def test_non_list_config(self):
  26. config = {}
  27. ret = service_beacon.validate(config)
  28. self.assertEqual(ret, (False, 'Configuration for service beacon must'
  29. ' be a list.'))
  30. def test_empty_config(self):
  31. config = [{}]
  32. ret = service_beacon.validate(config)
  33. self.assertEqual(ret, (False, 'Configuration for service '
  34. 'beacon requires services.'))
  35. def test_service_running(self):
  36. with patch.dict(service_beacon.__salt__,
  37. {'service.status': MagicMock(return_value=True)}):
  38. config = [{'services': {'salt-master': {}}}]
  39. ret = service_beacon.validate(config)
  40. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  41. ret = service_beacon.beacon(config)
  42. self.assertEqual(ret, [{'service_name': 'salt-master',
  43. 'tag': 'salt-master',
  44. 'salt-master': {'running': True}}])
  45. def test_service_not_running(self):
  46. with patch.dict(service_beacon.__salt__,
  47. {'service.status': MagicMock(return_value=False)}):
  48. config = [{'services': {'salt-master': {}}}]
  49. ret = service_beacon.validate(config)
  50. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  51. ret = service_beacon.beacon(config)
  52. self.assertEqual(ret, [{'service_name': 'salt-master',
  53. 'tag': 'salt-master',
  54. 'salt-master': {'running': False}}])