Commit 7954028f authored by Jérome Perrin's avatar Jérome Perrin

cpu load plugin: fix test and minor cleanups

This test was failing randomly because it uses a timeout of 0.5 seconds, which
is sometimes not enough to run commands.

Increase the timeout to something large enough so that we don't have timeout
failures when test machine is busy.

Also simplify the whole thing by using python instead of parsing the output
of processes.
parent 41107678
...@@ -4,6 +4,7 @@ from slapos.grid.promise.generic import GenericPromise ...@@ -4,6 +4,7 @@ from slapos.grid.promise.generic import GenericPromise
import subprocess import subprocess
import os import os
import psutil
@implementer(interface.IPromise) @implementer(interface.IPromise)
class RunPromise(GenericPromise): class RunPromise(GenericPromise):
...@@ -14,13 +15,9 @@ class RunPromise(GenericPromise): ...@@ -14,13 +15,9 @@ class RunPromise(GenericPromise):
self.setPeriodicity(minute=3) self.setPeriodicity(minute=3)
def checkCPULoad(self, tolerance=2.2): def checkCPULoad(self, tolerance=2.2):
# tolerance=1.5 => accept CPU load up to 1.5 =150% # tolerance=1.5 => accept CPU load up to 1.5 =150%
uptime_result = subprocess.check_output('uptime', universal_newlines=True) load, load5, long_load = psutil.getloadavg()
line = uptime_result.strip().split(' ') core_count = psutil.cpu_count()
load, load5, long_load = line[-3:]
long_load = float(long_load.replace(',', '.'))
core_count = int(subprocess.check_output('nproc').strip())
max_load = core_count * tolerance max_load = core_count * tolerance
if long_load > max_load: if long_load > max_load:
# display top statistics # display top statistics
...@@ -51,9 +48,9 @@ class RunPromise(GenericPromise): ...@@ -51,9 +48,9 @@ class RunPromise(GenericPromise):
self.checkCPULoad(threshold or 2.2) self.checkCPULoad(threshold or 2.2)
def test(self): def test(self):
# fail if load is high than the threshold for more than 30 minutes # fail if load is higher than the threshold for more than 30 minutes
return self._test(result_count=10, failure_amount=10) return self._test(result_count=10, failure_amount=10)
def anomaly(self): def anomaly(self):
# fail if load is high than the threshold for more than 30 minutes # fail if load is higher than the threshold for more than 30 minutes
return self._test(result_count=10, failure_amount=10) return self._test(result_count=10, failure_amount=10)
...@@ -32,7 +32,7 @@ import os ...@@ -32,7 +32,7 @@ import os
class TestCheckServerCPULoad(TestPromisePluginMixin): class TestCheckServerCPULoad(TestPromisePluginMixin):
def setUp(self): def setUp(self):
TestPromisePluginMixin.setUp(self) super(TestCheckServerCPULoad, self).setUp()
self.promise_name = "server-cpu-load-promise.py" self.promise_name = "server-cpu-load-promise.py"
content = """from slapos.promise.plugin.check_server_cpu_load import RunPromise content = """from slapos.promise.plugin.check_server_cpu_load import RunPromise
...@@ -44,13 +44,10 @@ extra_config_dict = { ...@@ -44,13 +44,10 @@ extra_config_dict = {
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
def test_check_cpu_load_run(self): def test_check_cpu_load_run(self):
self.configureLauncher() self.configureLauncher(timeout=5)
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
if result['result']['failed']: if result['result']['failed']:
self.assertTrue("CPU load is high" in result['result']['message']) self.assertTrue("CPU load is high" in result['result']['message'])
else: else:
self.assertEqual("CPU load is OK", result['result']['message'].strip()) self.assertEqual("CPU load is OK", result['result']['message'].strip())
if __name__ == '__main__':
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment