test_load.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. import logging
  5. # Salt libs
  6. import salt.beacons.load as load
  7. import salt.utils.platform
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.mock import MagicMock, patch
  10. # Salt testing libs
  11. from tests.support.unit import TestCase, skipIf
  12. log = logging.getLogger(__name__)
  13. class LoadBeaconTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test case for salt.beacons.load
  16. """
  17. def setup_loader_modules(self):
  18. return {load: {"__context__": {}, "__salt__": {}}}
  19. def test_non_list_config(self):
  20. config = {}
  21. ret = load.validate(config)
  22. self.assertEqual(ret, (False, "Configuration for load beacon must be a list."))
  23. def test_empty_config(self):
  24. config = [{}]
  25. ret = load.validate(config)
  26. self.assertEqual(
  27. ret, (False, "Averages configuration is required for load beacon.")
  28. )
  29. @skipIf(salt.utils.platform.is_windows(), "os.getloadavg not available on Windows")
  30. def test_load_match(self):
  31. with patch("os.getloadavg", MagicMock(return_value=(1.82, 1.84, 1.56))):
  32. config = [
  33. {
  34. "averages": {"1m": [0.0, 2.0], "5m": [0.0, 1.5], "15m": [0.0, 1.0]},
  35. "emitatstartup": True,
  36. "onchangeonly": False,
  37. }
  38. ]
  39. ret = load.validate(config)
  40. self.assertEqual(ret, (True, "Valid beacon configuration"))
  41. _expected_return = [{"1m": 1.82, "5m": 1.84, "15m": 1.56}]
  42. ret = load.beacon(config)
  43. self.assertEqual(ret, _expected_return)