def __init__(self, plugin, result, source=None, check=None, start=None, duration=None, when=None, **kw): self.result = result self.kw = kw self.when = when or generalized_time(datetime.utcnow()) self.duration = duration self.uuid = str(uuid.uuid4()) if None not in (check, source): self.check = check self.source = source else: if plugin is None: raise TypeError('source and check or plugin must be provided') self.check = plugin.__class__.__name__ self.source = plugin.__class__.__module__ if start is not None: dur = datetime.utcnow() - start self.duration = '%6.6f' % dur.total_seconds() assert getLevelName(result) is not None
def output(self): for result in self.results: yield dict(source=result.source, check=result.check, result=getLevelName(result.result), uuid=result.uuid, when=result.when, duration=result.duration, kw=result.kw)
def generate(self, data): output = '' for line in data: kw = line.get('kw') result = line.get('result') source = line.get('source') check = line.get('check') outline = '%s: %s.%s' % (getLevelName(result), source, check) if 'key' in kw: outline += '.%s' % kw.get('key') if 'msg' in kw: msg = kw.get('msg') err = msg.format(**kw) outline += ': %s' % err elif 'exception' in kw: outline += ': %s' % kw.get('exception') output += outline + '\n' return output
def test_Result(): """ Test the `ipahealthcheck.plugin.Result` class """ registry = Registry() p = Plugin(registry) # Standard case of passing plugin to Result r = Result(p, constants.SUCCESS) kw = dict(key='value') r = Result(p, constants.SUCCESS, **kw) e = raises(TypeError, Result) assert str(e) == "__init__() missing 2 required positional arguments: " \ "'plugin' and 'result'" # Test passing source and check to Result. This is used for loading # a previous output. try: r = Result(None, constants.SUCCESS) except TypeError as e: assert str(e) == "source and check or plugin must be provided" try: r = Result(None, constants.SUCCESS, source='test') except TypeError as e: assert str(e) == "source and check or plugin must be provided" try: r = Result(None, constants.SUCCESS, check='test') except TypeError as e: assert str(e) == "source and check or plugin must be provided" r = Result(None, constants.SUCCESS, source='test', check='test') # Test results r = Result(p, constants.SUCCESS) results = Results() results.add(r) assert len(results) == 1 r = Result(p, constants.CRITICAL) results2 = Results() results2.add(r) assert len(results2) == 1 results.extend(results2) assert len(results) == 2 output = [x for x in results.output()] assert len(output) == 2 for x in output: assert x['source'] == 'ipahealthcheck.core.plugin' assert x['check'] == 'Plugin' assert x['result'] in (constants.getLevelName(constants.SUCCESS), constants.getLevelName(constants.CRITICAL)) assert len(x['kw']) == 0