test_load.py 1.8 KB

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