diff --git a/test/basetest/hooks.py b/test/basetest/hooks.py index c53b7b760..627f2f711 100644 --- a/test/basetest/hooks.py +++ b/test/basetest/hooks.py @@ -16,6 +16,26 @@ from .utils import DEFAULT_HOOK_PATH from .exceptions import HookError +class InvalidJSON(object): + """Object representing the original unparsed JSON string and the JSON error + """ + def __init__(self, original, error): + self.original = original + self.error = error + + +def json_decoder(string): + """Attempt to decode a JSON string and in case of error return an + InvalidJSON object + """ + decoder = json.JSONDecoder().decode + + try: + return decoder(string) + except json.JSONDecodeError as e: + return InvalidJSON(string, str(e)) + + class Hooks(object): """Abstraction to help interact with hooks (add, remove) during tests and keep track of which are active. @@ -436,8 +456,6 @@ class LoggedHook(Hook): log = self._parse_log() newlog = {} - json_decoder = json.JSONDecoder().decode - for k1 in log: # Timestamps if k1 == "calls": @@ -492,4 +510,30 @@ class LoggedHook(Hook): log["exitcode"] )) + def assertValidJSONOutput(self): + """Check if current hook output is valid JSON in all expected replies + """ + log = self.get_logs() + + for i, out in enumerate(log["output"]["json"]): + assert not isinstance(out, InvalidJSON), ("Invalid JSON found at " + "reply number {0} with " + "content {1}".format( + i + 1, + out.original + )) + + def assertInvalidJSONOutput(self): + """Check if current hook output is invalid JSON in any expected reply + """ + log = self.get_logs() + + for i, out in enumerate(log["output"]["json"]): + assert isinstance(out, InvalidJSON), ("Valid JSON found at reply " + "number {0} with content " + "{1}".format( + i + 1, + out.original + )) + # vim: ai sts=4 et sw=4 diff --git a/test/hooks.on-add.t b/test/hooks.on-add.t index 469205d4d..c2a0f4760 100755 --- a/test/hooks.on-add.t +++ b/test/hooks.on-add.t @@ -125,6 +125,7 @@ class TestHooksOnAdd(TestCase): self.t.hooks[hookname].assertTriggered() self.t.hooks[hookname].assertTriggeredCount(1) self.t.hooks[hookname].assertExitcode(0) + self.t.hooks[hookname].assertInvalidJSONOutput() logs = self.t.hooks[hookname].get_logs() self.assertEqual(self.t.hooks[hookname].get_logs()["output"]["msgs"][0], "FEEDBACK")