Ignore:
Timestamp:
24 Sep 2014, 15:06:49 (10 years ago)
Author:
uli
Message:

Add helper to check swap space.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_utils.py

    r11802 r11815  
    1818## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    1919##
     20import psutil
    2021import unittest
    2122from waeup.kofa.interfaces import IKofaUtils
     
    2627class KofaUtilsTestCase(unittest.TestCase):
    2728
     29    def get_cleared_util(self):
     30        # Helper: get a `KofUtil` instance with all values of
     31        # SYSTEM_MAX_LOAD dict set to ``None``
     32        util = KofaUtils()
     33        for key, val in util.SYSTEM_MAX_LOAD.items():
     34            util.SYSTEM_MAX_LOAD[key] = None
     35        return util
     36
    2837    def test_iface(self):
    2938        # KofaUtils fullfill IKofaUtils expectations
     
    3140        verify.verifyClass(IKofaUtils, KofaUtils)
    3241        verify.verifyObject(IKofaUtils, utils)
     42
     43    def test_expensive_actions_allowed_swap_none(self):
     44        # unset swap maximum values make KofUtils ignore swap values
     45        utils = self.get_cleared_util()
     46        utils.SYSTEM_MAX_LOAD['swap-mem'] = None
     47        assert utils.expensive_actions_allowed() == True
     48
     49    @unittest.skipIf(
     50        psutil.swap_memory().percent >= 99.0,
     51        reason="System swap use over 99%. Cannot set higher allowed value.")
     52    def test_expensive_actions_allowed_swap_ok(self):
     53        # We can react to high swap values
     54        utils = self.get_cleared_util()
     55        utils.SYSTEM_MAX_LOAD['swap-mem'] = 99.0    # positive number
     56        assert utils.expensive_actions_allowed() == True
     57        utils.SYSTEM_MAX_LOAD['swap-mem'] = -1.0    # negative number
     58        assert utils.expensive_actions_allowed() == True
     59
     60    @unittest.skipIf(
     61        not psutil.swap_memory().percent,
     62        reason="Can test swapping behavior only if actually swapping")
     63    def test_expensive_actions_allowed_swap_too_much(self):
     64        # We can react if too much swap is used
     65        utils = self.get_cleared_util()
     66        utils.SYSTEM_MAX_LOAD['swap-mem'] = 0.0     # positive number
     67        assert utils.expensive_actions_allowed() == False
     68        utils.SYSTEM_MAX_LOAD['swap-mem'] = -100.0  # negative number
     69        assert utils.expensive_actions_allowed() == False
Note: See TracChangeset for help on using the changeset viewer.