test_memusage.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 TestCase
  7. from tests.support.mock import 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. class MemUsageBeaconTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test case for salt.beacons.memusage
  18. '''
  19. def setup_loader_modules(self):
  20. return {}
  21. def test_non_list_config(self):
  22. config = {}
  23. ret = memusage.validate(config)
  24. self.assertEqual(ret, (False, 'Configuration for memusage beacon must'
  25. ' be a list.'))
  26. def test_empty_config(self):
  27. config = [{}]
  28. ret = memusage.validate(config)
  29. self.assertEqual(ret, (False, 'Configuration for memusage beacon '
  30. 'requires percent.'))
  31. def test_memusage_match(self):
  32. with patch('psutil.virtual_memory',
  33. MagicMock(return_value=STUB_MEMORY_USAGE)):
  34. config = [{'percent': '40%'}, {'interval': 30}]
  35. ret = memusage.validate(config)
  36. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  37. ret = memusage.beacon(config)
  38. self.assertEqual(ret, [{'memusage': 40.7}])
  39. def test_memusage_nomatch(self):
  40. with patch('psutil.virtual_memory',
  41. MagicMock(return_value=STUB_MEMORY_USAGE)):
  42. config = [{'percent': '70%'}]
  43. ret = memusage.validate(config)
  44. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  45. ret = memusage.beacon(config)
  46. self.assertNotEqual(ret, [{'memusage': 50}])