1
0

test_service.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. from collections import namedtuple
  5. # Salt libs
  6. import salt.beacons.service as service_beacon
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, patch
  9. # Salt testing libs
  10. from tests.support.unit import TestCase
  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 {service_beacon: {"__context__": {}, "__salt__": {}}}
  19. def test_non_list_config(self):
  20. config = {}
  21. ret = service_beacon.validate(config)
  22. self.assertEqual(
  23. ret, (False, "Configuration for service beacon must be a list.")
  24. )
  25. def test_empty_config(self):
  26. config = [{}]
  27. ret = service_beacon.validate(config)
  28. self.assertEqual(
  29. ret, (False, "Configuration for service beacon requires services.")
  30. )
  31. def test_service_running(self):
  32. with patch.dict(
  33. service_beacon.__salt__, {"service.status": MagicMock(return_value=True)}
  34. ):
  35. config = [{"services": {"salt-master": {}}}]
  36. ret = service_beacon.validate(config)
  37. self.assertEqual(ret, (True, "Valid beacon configuration"))
  38. ret = service_beacon.beacon(config)
  39. self.assertEqual(
  40. ret,
  41. [
  42. {
  43. "service_name": "salt-master",
  44. "tag": "salt-master",
  45. "salt-master": {"running": True},
  46. }
  47. ],
  48. )
  49. def test_service_not_running(self):
  50. with patch.dict(
  51. service_beacon.__salt__, {"service.status": MagicMock(return_value=False)}
  52. ):
  53. config = [{"services": {"salt-master": {}}}]
  54. ret = service_beacon.validate(config)
  55. self.assertEqual(ret, (True, "Valid beacon configuration"))
  56. ret = service_beacon.beacon(config)
  57. self.assertEqual(
  58. ret,
  59. [
  60. {
  61. "service_name": "salt-master",
  62. "tag": "salt-master",
  63. "salt-master": {"running": False},
  64. }
  65. ],
  66. )