source: main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_utils.py @ 11815

Last change on this file since 11815 was 11815, checked in by uli, 10 years ago

Add helper to check swap space.

File size: 2.8 KB
Line 
1# -*- coding: utf-8 -*-
2
3## $Id$
4##
5## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann
6## This program is free software; you can redistribute it and/or modify
7## it under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 2 of the License, or
9## (at your option) any later version.
10##
11## This program is distributed in the hope that it will be useful,
12## but WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14## GNU General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with this program; if not, write to the Free Software
18## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19##
20import psutil
21import unittest
22from waeup.kofa.interfaces import IKofaUtils
23from waeup.kofa.utils.utils import KofaUtils
24from zope.interface import verify
25
26
27class KofaUtilsTestCase(unittest.TestCase):
28
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
37    def test_iface(self):
38        # KofaUtils fullfill IKofaUtils expectations
39        utils = KofaUtils()
40        verify.verifyClass(IKofaUtils, KofaUtils)
41        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 TracBrowser for help on using the repository browser.