test_memusage.py 1.9 KB

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