source: main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/utils/tests/test_utils.py @ 12278

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

Merge changes from uli-payments back into trunk.

  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1# -*- coding: utf-8 -*-
2
3## $Id: test_utils.py 12060 2014-11-25 18:44:01Z uli $
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 sys
22import unittest
23from waeup.ikoba.interfaces import IIkobaUtils
24from waeup.ikoba.utils.utils import IkobaUtils
25from zope.interface import verify
26
27
28class IkobaUtilsTestCase(unittest.TestCase):
29
30    def setUp(self):
31        self.max_vmem = psutil.virtual_memory().total
32        self.max_smem = psutil.swap_memory().total
33
34    def get_cleared_util(self):
35        # Helper: get a `KofUtil` instance with all values of
36        # SYSTEM_MAX_LOAD dict set to ``None``
37        util = IkobaUtils()
38        for key, val in util.SYSTEM_MAX_LOAD.items():
39            util.SYSTEM_MAX_LOAD[key] = None
40        return util
41
42    def test_iface(self):
43        # IkobaUtils fullfill IIkobaUtils expectations
44        utils = IkobaUtils()
45        verify.verifyClass(IIkobaUtils, IkobaUtils)
46        verify.verifyObject(IIkobaUtils, utils)
47
48    def test_expensive_actions_allowed_swap_none(self):
49        # unset swap maximum values make KofUtils ignore swap values
50        utils = self.get_cleared_util()
51        utils.SYSTEM_MAX_LOAD['swap-mem'] = None
52        assert utils.expensive_actions_allowed() == True
53        # even not-set values won't block us
54        del utils.SYSTEM_MAX_LOAD['swap-mem']
55        assert utils.expensive_actions_allowed() == True
56
57    @unittest.skipIf(
58        psutil.swap_memory().percent >= 99.0,
59        reason="System swap use over 99%. Cannot set higher allowed value.")
60    def test_expensive_actions_allowed_swap_ok(self):
61        # We can react to high swap values
62        utils = self.get_cleared_util()
63        utils.SYSTEM_MAX_LOAD['swap-mem'] = 99.0           # positive float
64        assert utils.expensive_actions_allowed() == True
65        utils.SYSTEM_MAX_LOAD['swap-mem'] = -1.0           # negative float
66        assert utils.expensive_actions_allowed() == True
67        utils.SYSTEM_MAX_LOAD['swap-mem'] = sys.maxint     # positive int
68        assert utils.expensive_actions_allowed() == True
69        utils.SYSTEM_MAX_LOAD['swap-mem'] = -1             # negative int
70        assert utils.expensive_actions_allowed() == True
71
72    @unittest.skipIf(
73        not psutil.swap_memory().percent,
74        reason="Can test swapping behavior only if actually swapping")
75    def test_expensive_actions_allowed_swap_too_much(self):
76        # We can react if too much swap is used
77        utils = self.get_cleared_util()
78        utils.SYSTEM_MAX_LOAD['swap-mem'] = 0.0            # positive float
79        assert utils.expensive_actions_allowed() == False
80        utils.SYSTEM_MAX_LOAD['swap-mem'] = -100.0         # negative float
81        assert utils.expensive_actions_allowed() == False
82        utils.SYSTEM_MAX_LOAD['swap-mem'] = 0              # positive int
83        assert utils.expensive_actions_allowed() == False
84        utils.SYSTEM_MAX_LOAD['swap-mem'] = -(sys.maxint)  # negative int
85        assert utils.expensive_actions_allowed() == False
86
87    def test_expensive_actions_allowed_virtmem_none(self):
88        # unset virtmem maximum values make KofUtils ignore virtmem values
89        utils = self.get_cleared_util()
90        utils.SYSTEM_MAX_LOAD['virt-mem'] = None
91        assert utils.expensive_actions_allowed() == True
92        # even not-set values won't block us
93        del utils.SYSTEM_MAX_LOAD['virt-mem']
94        assert utils.expensive_actions_allowed() == True
95
96    @unittest.skipIf(
97        psutil.virtual_memory().percent >= 99.0,
98        reason="System virtmem use over 99%. Cannot set higher allowed value.")
99    def test_expensive_actions_allowed_virtmem_ok(self):
100        # We can react to high virtmem values
101        utils = self.get_cleared_util()
102        utils.SYSTEM_MAX_LOAD['virt-mem'] = 99.0           # positive float
103        assert utils.expensive_actions_allowed() == True
104        utils.SYSTEM_MAX_LOAD['virt-mem'] = -1.0           # negative float
105        assert utils.expensive_actions_allowed() == True
106        utils.SYSTEM_MAX_LOAD['virt-mem'] = sys.maxint     # positive int
107        assert utils.expensive_actions_allowed() == True
108        utils.SYSTEM_MAX_LOAD['virt-mem'] = -1             # negative int
109        assert utils.expensive_actions_allowed() == True
110
111    @unittest.skipIf(
112        not psutil.virtual_memory().percent,
113        reason="Can test virtmem behavior only if actually using some")
114    def test_expensive_actions_allowed_virtmem_too_much(self):
115        # We can react if too much virtmem is used
116        utils = self.get_cleared_util()
117        utils.SYSTEM_MAX_LOAD['virt-mem'] = 0.0            # positive float
118        assert utils.expensive_actions_allowed() == False
119        utils.SYSTEM_MAX_LOAD['virt-mem'] = -100.0         # negative float
120        assert utils.expensive_actions_allowed() == False
121        utils.SYSTEM_MAX_LOAD['virt-mem'] = 0              # positive int
122        assert utils.expensive_actions_allowed() == False
123        utils.SYSTEM_MAX_LOAD['virt-mem'] = -(sys.maxint)  # negative int
124        assert utils.expensive_actions_allowed() == False
Note: See TracBrowser for help on using the repository browser.