From 88ecec10f8277b67edcb43dadcceb0a390e45bc2 Mon Sep 17 00:00:00 2001 From: Noah Watkins Date: Tue, 14 Aug 2018 16:53:55 -0700 Subject: [PATCH] mgr/selftest: interface for testing health checks Adds an interface used to set/clear health checks. This is being added for use in testing health history tracking in the insights module. Signed-off-by: Noah Watkins --- src/pybind/mgr/selftest/module.py | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/pybind/mgr/selftest/module.py b/src/pybind/mgr/selftest/module.py index 794c0cc61ac..2c0577ef2dc 100644 --- a/src/pybind/mgr/selftest/module.py +++ b/src/pybind/mgr/selftest/module.py @@ -4,6 +4,7 @@ import threading import random import json import errno +import six class Module(MgrModule): @@ -70,12 +71,23 @@ class Module(MgrModule): "desc": "Run another module's self_test() method", "perm": "rw" }, + { + "cmd": "mgr self-test health set name=checks,type=CephString", + "desc": "Set a health check from a JSON-formatted description.", + "perm": "rw" + }, + { + "cmd": "mgr self-test health clear name=checks,type=CephString,n=N,req=False", + "desc": "Clear health checks by name. If no names provided, clear all.", + "perm": "rw" + }, ] def __init__(self, *args, **kwargs): super(Module, self).__init__(*args, **kwargs) self._event = threading.Event() self._workload = None + self._health = {} def handle_command(self, inbuf, command): if command['prefix'] == 'mgr self-test run': @@ -113,10 +125,44 @@ class Module(MgrModule): return -1, '', "Test failed: {0}".format(e.message) else: return 0, str(r), "Self-test OK" + elif command['prefix'] == 'mgr self-test health set': + return self._health_set(inbuf, command) + elif command['prefix'] == 'mgr self-test health clear': + return self._health_clear(inbuf, command) else: return (-errno.EINVAL, '', "Command not found '{0}'".format(command['prefix'])) + def _health_set(self, inbuf, command): + try: + checks = json.loads(command["checks"]) + except Exception as e: + return -1, "", "Failed to decode JSON input: {}".format(e.message) + + try: + for check, info in six.iteritems(checks): + self._health[check] = { + "severity": str(info["severity"]), + "summary": str(info["summary"]), + "detail": [str(m) for m in info["detail"]] + } + except Exception as e: + return -1, "", "Invalid health check format: {}".format(e.message) + + self.set_health_checks(self._health) + return 0, "", "" + + def _health_clear(self, inbuf, command): + if "checks" in command: + for check in command["checks"]: + if check in self._health: + del self._health[check] + else: + self._health = dict() + + self.set_health_checks(self._health) + return 0, "", "" + def _self_test(self): self.log.info("Running self-test procedure...") -- 2.39.5