1
0

test_memusage.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. from collections import namedtuple
  5. # Salt testing libs
  6. from tests.support.unit import skipIf, TestCase
  7. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. # Salt libs
  10. import salt.beacons.memusage as memusage
  11. STUB_MEMORY_USAGE = namedtuple('vmem', 'total available percent used free active inactive buffers cached shared')(
  12. 15722012672, 9329594368, 40.7, 5137018880,
  13. 4678086656, 6991405056, 2078953472,
  14. 1156378624, 4750528512, 898908160)
  15. @skipIf(NO_MOCK, NO_MOCK_REASON)
  16. class MemUsageBeaconTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test case for salt.beacons.memusage
  19. '''
  20. def setup_loader_modules(self):
  21. return {}
  22. def test_non_list_config(self):
  23. config = {}
  24. ret = memusage.validate(config)
  25. self.assertEqual(ret, (False, 'Configuration for memusage beacon must'
  26. ' be a list.'))
  27. def test_empty_config(self):
  28. config = [{}]
  29. ret = memusage.validate(config)
  30. self.assertEqual(ret, (False, 'Configuration for memusage beacon '
  31. 'requires percent.'))
  32. def test_memusage_match(self):
  33. with patch('psutil.virtual_memory',
  34. MagicMock(return_value=STUB_MEMORY_USAGE)):
  35. config = [{'percent': '40%'}, {'interval': 30}]
  36. ret = memusage.validate(config)
  37. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  38. ret = memusage.beacon(config)
  39. self.assertEqual(ret, [{'memusage': 40.7}])
  40. def test_memusage_nomatch(self):
  41. with patch('psutil.virtual_memory',
  42. MagicMock(return_value=STUB_MEMORY_USAGE)):
  43. config = [{'percent': '70%'}]
  44. ret = memusage.validate(config)
  45. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  46. ret = memusage.beacon(config)
  47. self.assertNotEqual(ret, [{'memusage': 50}])