source: main/waeup.kofa/branches/uli-stud-utils-cleanup/src/waeup/kofa/utils/tests/test_utils.py @ 12698

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

Test integer values as expensive action blockers.

File size: 7.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 sys
22import unittest
23from waeup.kofa.interfaces import IKofaUtils
24from waeup.kofa.utils.utils import KofaUtils
25from zope.interface import verify
26
27
28class KofaUtilsTestCase(unittest.TestCase):
29
30    def setUp(self):
31        self.max_pmem = psutil.phymem_usage().total
32        self.max_vmem = psutil.virtual_memory().total
33        self.max_smem = psutil.swap_memory().total
34
35    def get_cleared_util(self):
36        # Helper: get a `KofUtil` instance with all values of
37        # SYSTEM_MAX_LOAD dict set to ``None``
38        util = KofaUtils()
39        for key, val in util.SYSTEM_MAX_LOAD.items():
40            util.SYSTEM_MAX_LOAD[key] = None
41        return util
42
43    def test_iface(self):
44        # KofaUtils fullfill IKofaUtils expectations
45        utils = KofaUtils()
46        verify.verifyClass(IKofaUtils, KofaUtils)
47        verify.verifyObject(IKofaUtils, utils)
48
49    def test_expensive_actions_allowed_swap_none(self):
50        # unset swap maximum values make KofUtils ignore swap values
51        utils = self.get_cleared_util()
52        utils.SYSTEM_MAX_LOAD['swap-mem'] = None
53        assert utils.expensive_actions_allowed() == True
54        # even not-set values won't block us
55        del utils.SYSTEM_MAX_LOAD['swap-mem']
56        assert utils.expensive_actions_allowed() == True
57
58    @unittest.skipIf(
59        psutil.swap_memory().percent >= 99.0,
60        reason="System swap use over 99%. Cannot set higher allowed value.")
61    def test_expensive_actions_allowed_swap_ok(self):
62        # We can react to high swap values
63        utils = self.get_cleared_util()
64        utils.SYSTEM_MAX_LOAD['swap-mem'] = 99.0           # positive float
65        assert utils.expensive_actions_allowed() == True
66        utils.SYSTEM_MAX_LOAD['swap-mem'] = -1.0           # negative float
67        assert utils.expensive_actions_allowed() == True
68        utils.SYSTEM_MAX_LOAD['swap-mem'] = sys.maxint     # positive int
69        assert utils.expensive_actions_allowed() == True
70        utils.SYSTEM_MAX_LOAD['swap-mem'] = -1             # negative int
71        assert utils.expensive_actions_allowed() == True
72
73    @unittest.skipIf(
74        not psutil.swap_memory().percent,
75        reason="Can test swapping behavior only if actually swapping")
76    def test_expensive_actions_allowed_swap_too_much(self):
77        # We can react if too much swap is used
78        utils = self.get_cleared_util()
79        utils.SYSTEM_MAX_LOAD['swap-mem'] = 0.0            # positive float
80        assert utils.expensive_actions_allowed() == False
81        utils.SYSTEM_MAX_LOAD['swap-mem'] = -100.0         # negative float
82        assert utils.expensive_actions_allowed() == False
83        utils.SYSTEM_MAX_LOAD['swap-mem'] = 0              # positive int
84        assert utils.expensive_actions_allowed() == False
85        utils.SYSTEM_MAX_LOAD['swap-mem'] = -(sys.maxint)  # negative int
86        assert utils.expensive_actions_allowed() == False
87
88    def test_expensive_actions_allowed_virtmem_none(self):
89        # unset virtmem maximum values make KofUtils ignore virtmem values
90        utils = self.get_cleared_util()
91        utils.SYSTEM_MAX_LOAD['virt-mem'] = None
92        assert utils.expensive_actions_allowed() == True
93        # even not-set values won't block us
94        del utils.SYSTEM_MAX_LOAD['virt-mem']
95        assert utils.expensive_actions_allowed() == True
96
97    @unittest.skipIf(
98        psutil.virtual_memory().percent >= 99.0,
99        reason="System virtmem use over 99%. Cannot set higher allowed value.")
100    def test_expensive_actions_allowed_virtmem_ok(self):
101        # We can react to high virtmem values
102        utils = self.get_cleared_util()
103        utils.SYSTEM_MAX_LOAD['virt-mem'] = 99.0           # positive float
104        assert utils.expensive_actions_allowed() == True
105        utils.SYSTEM_MAX_LOAD['virt-mem'] = -1.0           # negative float
106        assert utils.expensive_actions_allowed() == True
107        utils.SYSTEM_MAX_LOAD['virt-mem'] = sys.maxint     # positive int
108        assert utils.expensive_actions_allowed() == True
109        utils.SYSTEM_MAX_LOAD['virt-mem'] = -1             # negative int
110        assert utils.expensive_actions_allowed() == True
111
112    @unittest.skipIf(
113        not psutil.virtual_memory().percent,
114        reason="Can test virtmem behavior only if actually using some")
115    def test_expensive_actions_allowed_virtmem_too_much(self):
116        # We can react if too much virtmem is used
117        utils = self.get_cleared_util()
118        utils.SYSTEM_MAX_LOAD['virt-mem'] = 0.0            # positive float
119        assert utils.expensive_actions_allowed() == False
120        utils.SYSTEM_MAX_LOAD['virt-mem'] = -100.0         # negative float
121        assert utils.expensive_actions_allowed() == False
122        utils.SYSTEM_MAX_LOAD['virt-mem'] = 0              # positive int
123        assert utils.expensive_actions_allowed() == False
124        utils.SYSTEM_MAX_LOAD['virt-mem'] = -(sys.maxint)  # negative int
125        assert utils.expensive_actions_allowed() == False
126
127    def test_expensive_actions_allowed_physmem_none(self):
128        # unset physmem maximum values make KofUtils ignore physmem values
129        utils = self.get_cleared_util()
130        utils.SYSTEM_MAX_LOAD['phys-mem'] = None
131        assert utils.expensive_actions_allowed() == True
132        # even not-set values won't block us
133        del utils.SYSTEM_MAX_LOAD['phys-mem']
134        assert utils.expensive_actions_allowed() == True
135
136    @unittest.skipIf(
137        psutil.phymem_usage().percent >= 99.99,
138        reason="System physmem use over 99%. Cannot set higher allowed value.")
139    def test_expensive_actions_allowed_physmem_ok(self):
140        # We can react to high physmem values
141        max_mem = psutil.phymem_usage().total
142        utils = self.get_cleared_util()
143        utils.SYSTEM_MAX_LOAD['phys-mem'] = 99.99          # positive float
144        assert utils.expensive_actions_allowed() == True
145        utils.SYSTEM_MAX_LOAD['phys-mem'] = -0.01          # negative float
146        assert utils.expensive_actions_allowed() == True
147        utils.SYSTEM_MAX_LOAD['phys-mem'] = max_mem        # positive int
148        assert utils.expensive_actions_allowed() == True
149        utils.SYSTEM_MAX_LOAD['phys-mem'] = -1             # negative int
150        assert utils.expensive_actions_allowed() == True
151
152    @unittest.skipIf(
153        not psutil.phymem_usage().percent,
154        reason="Can test physmem behavior only if actually using some")
155    def test_expensive_actions_allowed_physmem_too_much(self):
156        # We can react if too much physmem is used
157        max_mem = psutil.phymem_usage().total
158        utils = self.get_cleared_util()
159        utils.SYSTEM_MAX_LOAD['phys-mem'] = 0.0            # positive float
160        assert utils.expensive_actions_allowed() == False
161        utils.SYSTEM_MAX_LOAD['phys-mem'] = -100.0         # negative float
162        assert utils.expensive_actions_allowed() == False
163        utils.SYSTEM_MAX_LOAD['phys-mem'] = 0              # positive int
164        assert utils.expensive_actions_allowed() == False
165        utils.SYSTEM_MAX_LOAD['phys-mem'] = -(max_mem)     # negative int
166        assert utils.expensive_actions_allowed() == False
Note: See TracBrowser for help on using the repository browser.