Merge pull request #696 from izn/fix-memory-module-tests
Fix memory module tests
This commit is contained in:
commit
ebeb084da0
2 changed files with 49 additions and 14 deletions
|
@ -41,18 +41,8 @@ class Module(core.module.Module):
|
|||
return self._format.format(**self._mem)
|
||||
|
||||
def update(self):
|
||||
data = {}
|
||||
with open("/proc/meminfo", "r") as f:
|
||||
for line in f:
|
||||
tmp = re.split(r"[:\s]+", line)
|
||||
value = int(tmp[1])
|
||||
if tmp[2] == "kB":
|
||||
value = value * 1024
|
||||
if tmp[2] == "mB":
|
||||
value = value * 1024 * 1024
|
||||
if tmp[2] == "gB":
|
||||
value = value * 1024 * 1024 * 1024
|
||||
data[tmp[0]] = value
|
||||
data = self.__parse_meminfo()
|
||||
|
||||
if "MemAvailable" in data:
|
||||
used = data["MemTotal"] - data["MemAvailable"]
|
||||
else:
|
||||
|
@ -78,5 +68,28 @@ class Module(core.module.Module):
|
|||
return "warning"
|
||||
return None
|
||||
|
||||
def __parse_meminfo(self):
|
||||
data = {}
|
||||
with open("/proc/meminfo", "r") as f:
|
||||
# https://bugs.python.org/issue32933
|
||||
for line in f.readlines():
|
||||
tmp = re.split(r"[:\s]+", line)
|
||||
value = self.__parse_value(tmp)
|
||||
|
||||
data[tmp[0]] = value
|
||||
|
||||
return data
|
||||
|
||||
def __parse_value(self, data):
|
||||
value = int(data[1])
|
||||
|
||||
if data[2] == "kB":
|
||||
value = value * 1024
|
||||
if data[2] == "mB":
|
||||
value = value * 1024 * 1024
|
||||
if data[2] == "gB":
|
||||
value = value * 1024 * 1024 * 1024
|
||||
|
||||
return value
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -15,6 +15,7 @@ def memory_widget(module):
|
|||
def meminfo_mock(
|
||||
total,
|
||||
available,
|
||||
unit = 'kB',
|
||||
free = 0,
|
||||
buffers = 0,
|
||||
cached = 0,
|
||||
|
@ -30,9 +31,8 @@ def meminfo_mock(
|
|||
('Slab', slab)
|
||||
]
|
||||
|
||||
|
||||
for i, (key, value) in enumerate(states):
|
||||
data.append('{}: {} kB'.format(key, value))
|
||||
data.append('{}: {} {}'.format(key, value, unit))
|
||||
|
||||
return '\n'.join(data)
|
||||
|
||||
|
@ -119,3 +119,25 @@ class TestMemory(TestCase):
|
|||
|
||||
assert widget.full_text() == '50.0%'
|
||||
assert module.state(widget) == None
|
||||
|
||||
|
||||
@mock.patch('builtins.open', mock.mock_open(read_data=meminfo_mock(8196, 4096, 'mB')))
|
||||
def test_mb_unit(self):
|
||||
module = build_module()
|
||||
module.update()
|
||||
|
||||
widget = memory_widget(module)
|
||||
|
||||
assert widget.full_text() == '4.00GiB/8.00GiB (50.02%)'
|
||||
assert module.state(widget) == None
|
||||
|
||||
@mock.patch('builtins.open', mock.mock_open(read_data=meminfo_mock(2, 1, 'gB')))
|
||||
def test_gb_unit(self):
|
||||
module = build_module()
|
||||
module.update()
|
||||
|
||||
widget = memory_widget(module)
|
||||
|
||||
assert widget.full_text() == '1.00GiB/2.00GiB (50.00%)'
|
||||
assert module.state(widget) == None
|
||||
|
||||
|
|
Loading…
Reference in a new issue