1
0

test_load.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 NO_MOCK, NO_MOCK_REASON, 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. @skipIf(NO_MOCK, NO_MOCK_REASON)
  14. class LoadBeaconTestCase(TestCase, LoaderModuleMockMixin):
  15. '''
  16. Test case for salt.beacons.load
  17. '''
  18. def setup_loader_modules(self):
  19. return {
  20. load: {
  21. '__context__': {},
  22. '__salt__': {},
  23. }
  24. }
  25. def test_non_list_config(self):
  26. config = {}
  27. ret = load.validate(config)
  28. self.assertEqual(ret, (False, 'Configuration for load beacon must'
  29. ' be a list.'))
  30. def test_empty_config(self):
  31. config = [{}]
  32. ret = load.validate(config)
  33. self.assertEqual(ret, (False, 'Averages configuration is required'
  34. ' for load beacon.'))
  35. @skipIf(salt.utils.platform.is_windows(),
  36. 'os.getloadavg not available on Windows')
  37. def test_load_match(self):
  38. with patch('os.getloadavg',
  39. MagicMock(return_value=(1.82, 1.84, 1.56))):
  40. config = [{'averages': {'1m': [0.0, 2.0],
  41. '5m': [0.0, 1.5],
  42. '15m': [0.0, 1.0]},
  43. 'emitatstartup': True,
  44. 'onchangeonly': False}]
  45. ret = load.validate(config)
  46. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  47. _expected_return = [{'1m': 1.82, '5m': 1.84, '15m': 1.56}]
  48. ret = load.beacon(config)
  49. self.assertEqual(ret, _expected_return)