Fix memory module tests

This commit is contained in:
Thaynã Moretti 2020-08-30 12:11:48 -03:00
parent 780c5bf3d0
commit 820598b1b8
2 changed files with 23 additions and 13 deletions

View file

@ -41,18 +41,8 @@ class Module(core.module.Module):
return self._format.format(**self._mem) return self._format.format(**self._mem)
def update(self): def update(self):
data = {} data = self.__parse_meminfo()
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
if "MemAvailable" in data: if "MemAvailable" in data:
used = data["MemTotal"] - data["MemAvailable"] used = data["MemTotal"] - data["MemAvailable"]
else: else:
@ -78,5 +68,26 @@ class Module(core.module.Module):
return "warning" return "warning"
return None return None
def __parse_meminfo(self):
data = {}
with open("/proc/meminfo", "r") as f:
# https://bugs.python.org/issue32933
while True:
line = f.readline()
if line == '':
break
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
return data
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -30,7 +30,6 @@ def meminfo_mock(
('Slab', slab) ('Slab', slab)
] ]
for i, (key, value) in enumerate(states): for i, (key, value) in enumerate(states):
data.append('{}: {} kB'.format(key, value)) data.append('{}: {} kB'.format(key, value))