test_service.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 TestCase
  7. from tests.support.mock import 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. class ServiceBeaconTestCase(TestCase, LoaderModuleMockMixin):
  14. '''
  15. Test case for salt.beacons.service
  16. '''
  17. def setup_loader_modules(self):
  18. return {
  19. service_beacon: {
  20. '__context__': {},
  21. '__salt__': {},
  22. }
  23. }
  24. def test_non_list_config(self):
  25. config = {}
  26. ret = service_beacon.validate(config)
  27. self.assertEqual(ret, (False, 'Configuration for service beacon must'
  28. ' be a list.'))
  29. def test_empty_config(self):
  30. config = [{}]
  31. ret = service_beacon.validate(config)
  32. self.assertEqual(ret, (False, 'Configuration for service '
  33. 'beacon requires services.'))
  34. def test_service_running(self):
  35. with patch.dict(service_beacon.__salt__,
  36. {'service.status': MagicMock(return_value=True)}):
  37. config = [{'services': {'salt-master': {}}}]
  38. ret = service_beacon.validate(config)
  39. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  40. ret = service_beacon.beacon(config)
  41. self.assertEqual(ret, [{'service_name': 'salt-master',
  42. 'tag': 'salt-master',
  43. 'salt-master': {'running': True}}])
  44. def test_service_not_running(self):
  45. with patch.dict(service_beacon.__salt__,
  46. {'service.status': MagicMock(return_value=False)}):
  47. config = [{'services': {'salt-master': {}}}]
  48. ret = service_beacon.validate(config)
  49. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  50. ret = service_beacon.beacon(config)
  51. self.assertEqual(ret, [{'service_name': 'salt-master',
  52. 'tag': 'salt-master',
  53. 'salt-master': {'running': False}}])