Tests - Invalid JSON replies in hooks no longer fail the test framework

This commit is contained in:
Renato Alves
2015-02-15 17:25:58 +00:00
parent e6031183dd
commit 1b218d68f5
2 changed files with 47 additions and 2 deletions

View File

@@ -16,6 +16,26 @@ from .utils import DEFAULT_HOOK_PATH
from .exceptions import HookError 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): class Hooks(object):
"""Abstraction to help interact with hooks (add, remove) during tests and """Abstraction to help interact with hooks (add, remove) during tests and
keep track of which are active. keep track of which are active.
@@ -436,8 +456,6 @@ class LoggedHook(Hook):
log = self._parse_log() log = self._parse_log()
newlog = {} newlog = {}
json_decoder = json.JSONDecoder().decode
for k1 in log: for k1 in log:
# Timestamps # Timestamps
if k1 == "calls": if k1 == "calls":
@@ -492,4 +510,30 @@ class LoggedHook(Hook):
log["exitcode"] 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 # vim: ai sts=4 et sw=4

View File

@@ -125,6 +125,7 @@ class TestHooksOnAdd(TestCase):
self.t.hooks[hookname].assertTriggered() self.t.hooks[hookname].assertTriggered()
self.t.hooks[hookname].assertTriggeredCount(1) self.t.hooks[hookname].assertTriggeredCount(1)
self.t.hooks[hookname].assertExitcode(0) self.t.hooks[hookname].assertExitcode(0)
self.t.hooks[hookname].assertInvalidJSONOutput()
logs = self.t.hooks[hookname].get_logs() logs = self.t.hooks[hookname].get_logs()
self.assertEqual(self.t.hooks[hookname].get_logs()["output"]["msgs"][0], "FEEDBACK") self.assertEqual(self.t.hooks[hookname].get_logs()["output"]["msgs"][0], "FEEDBACK")