]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/selftest: interface for testing health checks
authorNoah Watkins <nwatkins@redhat.com>
Tue, 14 Aug 2018 23:53:55 +0000 (16:53 -0700)
committerNoah Watkins <nwatkins@redhat.com>
Thu, 16 Aug 2018 22:29:46 +0000 (15:29 -0700)
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 <nwatkins@redhat.com>
src/pybind/mgr/selftest/module.py

index 794c0cc61acfab8e7b5e57eef37e685684988b16..2c0577ef2dc31b100a506ae90682d23ba8cd0ccc 100644 (file)
@@ -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...")