test_beacons.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """
  2. unit tests for the beacon_module parameter
  3. """
  4. import logging
  5. import salt.beacons as beacons
  6. import salt.config
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, call, patch
  9. from tests.support.unit import TestCase
  10. log = logging.getLogger(__name__)
  11. class BeaconsTestCase(TestCase, LoaderModuleMockMixin):
  12. """
  13. Test cases for salt beacon_module parameter
  14. """
  15. def setup_loader_modules(self):
  16. return {beacons: {}}
  17. def test_beacon_module(self):
  18. """
  19. Test that beacon_module parameter for beacon configuration
  20. """
  21. mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
  22. mock_opts["id"] = "minion"
  23. mock_opts["__role"] = "minion"
  24. mock_opts["beacons"] = {
  25. "watch_apache": [
  26. {"processes": {"apache2": "stopped"}},
  27. {"beacon_module": "ps"},
  28. ]
  29. }
  30. with patch.dict(beacons.__opts__, mock_opts):
  31. beacon = salt.beacons.Beacon(mock_opts, [])
  32. ret = beacon.process(mock_opts["beacons"], mock_opts["grains"])
  33. _expected = [
  34. {
  35. "tag": "salt/beacon/minion/watch_apache/",
  36. "data": {"id": "minion", "apache2": "Stopped"},
  37. "beacon_name": "ps",
  38. }
  39. ]
  40. self.assertEqual(ret, _expected)
  41. # Ensure that "beacon_name" is available in the call to the beacon function
  42. name = "ps.beacon"
  43. mocked = {name: MagicMock(return_value=_expected)}
  44. mocked[name].__globals__ = {}
  45. calls = [
  46. call(
  47. [
  48. {"processes": {"apache2": "stopped"}},
  49. {"beacon_module": "ps"},
  50. {"_beacon_name": "watch_apache"},
  51. ]
  52. )
  53. ]
  54. with patch.object(beacon, "beacons", mocked) as patched:
  55. beacon.process(mock_opts["beacons"], mock_opts["grains"])
  56. patched[name].assert_has_calls(calls)