2017-09-19 17:47:42 -04:00
|
|
|
from collections import OrderedDict
|
2017-09-21 16:32:05 -04:00
|
|
|
import importlib
|
|
|
|
import inspect
|
|
|
|
import pkgutil
|
2017-09-19 17:47:42 -04:00
|
|
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from .constants import LOG_DEFAULT, LOG_FAILURE, LOG_INFO, LOG_LEVEL_CODES, LOG_SUCCESS, LOG_WARNING
|
2017-09-26 16:36:43 -04:00
|
|
|
import reports as custom_reports
|
2017-09-21 16:32:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
def is_report(obj):
|
|
|
|
"""
|
|
|
|
Returns True if the given object is a Report.
|
|
|
|
"""
|
|
|
|
if obj in Report.__subclasses__():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2017-09-25 17:27:58 -04:00
|
|
|
def get_report(module_name, report_name):
|
|
|
|
"""
|
|
|
|
Return a specific report from within a module.
|
|
|
|
"""
|
|
|
|
module = importlib.import_module('reports.{}'.format(module_name))
|
2017-09-26 16:36:43 -04:00
|
|
|
report = getattr(module, report_name, None)
|
2017-09-27 17:39:22 -04:00
|
|
|
if report is None:
|
|
|
|
return None
|
2017-09-26 16:36:43 -04:00
|
|
|
return report()
|
2017-09-25 17:27:58 -04:00
|
|
|
|
|
|
|
|
2017-09-21 16:32:05 -04:00
|
|
|
def get_reports():
|
|
|
|
"""
|
2017-09-22 12:11:10 -04:00
|
|
|
Compile a list of all reports available across all modules in the reports path. Returns a list of tuples:
|
|
|
|
|
|
|
|
[
|
2017-09-26 16:36:43 -04:00
|
|
|
(module_name, (report_class, report_class, report_class, ...)),
|
|
|
|
(module_name, (report_class, report_class, report_class, ...)),
|
|
|
|
...
|
2017-09-22 12:11:10 -04:00
|
|
|
]
|
2017-09-21 16:32:05 -04:00
|
|
|
"""
|
|
|
|
module_list = []
|
|
|
|
|
2017-09-26 16:36:43 -04:00
|
|
|
# Iterate through all modules within the reports path. These are the user-defined files in which reports are
|
|
|
|
# defined.
|
|
|
|
for importer, module_name, is_pkg in pkgutil.walk_packages(custom_reports.__path__):
|
2017-09-21 16:32:05 -04:00
|
|
|
module = importlib.import_module('reports.{}'.format(module_name))
|
2017-09-26 16:36:43 -04:00
|
|
|
report_list = [cls() for _, cls in inspect.getmembers(module, is_report)]
|
2017-09-21 16:32:05 -04:00
|
|
|
module_list.append((module_name, report_list))
|
|
|
|
|
|
|
|
return module_list
|
2017-09-19 17:47:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Report(object):
|
|
|
|
"""
|
|
|
|
NetBox users can extend this object to write custom reports to be used for validating data within NetBox. Each
|
|
|
|
report must have one or more test methods named `test_*`.
|
|
|
|
|
|
|
|
The `results` attribute of a completed report will take the following form:
|
|
|
|
|
|
|
|
{
|
|
|
|
'test_bar': {
|
|
|
|
'failures': 42,
|
|
|
|
'log': [
|
|
|
|
(<datetime>, <level>, <object>, <message>),
|
|
|
|
...
|
|
|
|
]
|
|
|
|
},
|
|
|
|
'test_foo': {
|
|
|
|
'failures': 0,
|
|
|
|
'log': [
|
|
|
|
(<datetime>, <level>, <object>, <message>),
|
|
|
|
...
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2017-09-21 16:32:05 -04:00
|
|
|
description = None
|
2017-09-19 17:47:42 -04:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
2017-09-21 13:49:04 -04:00
|
|
|
self.results = OrderedDict()
|
|
|
|
self.active_test = None
|
|
|
|
self.failed = False
|
|
|
|
|
2017-09-19 17:47:42 -04:00
|
|
|
# Compile test methods and initialize results skeleton
|
|
|
|
test_methods = []
|
|
|
|
for method in dir(self):
|
|
|
|
if method.startswith('test_') and callable(getattr(self, method)):
|
|
|
|
test_methods.append(method)
|
|
|
|
self.results[method] = OrderedDict([
|
|
|
|
('success', 0),
|
|
|
|
('info', 0),
|
|
|
|
('warning', 0),
|
|
|
|
('failed', 0),
|
|
|
|
('log', []),
|
|
|
|
])
|
|
|
|
if not test_methods:
|
|
|
|
raise Exception("A report must contain at least one test method.")
|
|
|
|
self.test_methods = test_methods
|
|
|
|
|
2017-09-26 16:36:43 -04:00
|
|
|
@property
|
|
|
|
def module(self):
|
|
|
|
return self.__module__.rsplit('.', 1)[1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
|
|
|
@property
|
|
|
|
def full_name(self):
|
|
|
|
return '.'.join([self.module, self.name])
|
|
|
|
|
2017-09-19 17:47:42 -04:00
|
|
|
def _log(self, obj, message, level=LOG_DEFAULT):
|
|
|
|
"""
|
|
|
|
Log a message from a test method. Do not call this method directly; use one of the log_* wrappers below.
|
|
|
|
"""
|
|
|
|
if level not in LOG_LEVEL_CODES:
|
|
|
|
raise Exception("Unknown logging level: {}".format(level))
|
2017-09-22 12:11:10 -04:00
|
|
|
logline = [timezone.now().isoformat(), level, str(obj), message]
|
2017-09-19 17:47:42 -04:00
|
|
|
self.results[self.active_test]['log'].append(logline)
|
|
|
|
|
|
|
|
def log_success(self, obj, message=None):
|
|
|
|
"""
|
|
|
|
Record a successful test against an object. Logging a message is optional.
|
|
|
|
"""
|
|
|
|
if message:
|
|
|
|
self._log(obj, message, level=LOG_SUCCESS)
|
|
|
|
self.results[self.active_test]['success'] += 1
|
|
|
|
|
|
|
|
def log_info(self, obj, message):
|
|
|
|
"""
|
|
|
|
Log an informational message.
|
|
|
|
"""
|
|
|
|
self._log(obj, message, level=LOG_INFO)
|
|
|
|
self.results[self.active_test]['info'] += 1
|
|
|
|
|
|
|
|
def log_warning(self, obj, message):
|
|
|
|
"""
|
|
|
|
Log a warning.
|
|
|
|
"""
|
|
|
|
self._log(obj, message, level=LOG_WARNING)
|
|
|
|
self.results[self.active_test]['warning'] += 1
|
|
|
|
|
|
|
|
def log_failure(self, obj, message):
|
|
|
|
"""
|
|
|
|
Log a failure. Calling this method will automatically mark the report as failed.
|
|
|
|
"""
|
|
|
|
self._log(obj, message, level=LOG_FAILURE)
|
|
|
|
self.results[self.active_test]['failed'] += 1
|
|
|
|
self.failed = True
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""
|
2017-09-21 13:49:04 -04:00
|
|
|
Run the report and return its results. Each test method will be executed in order.
|
2017-09-19 17:47:42 -04:00
|
|
|
"""
|
|
|
|
for method_name in self.test_methods:
|
|
|
|
self.active_test = method_name
|
|
|
|
test_method = getattr(self, method_name)
|
|
|
|
test_method()
|
2017-09-21 13:49:04 -04:00
|
|
|
|
|
|
|
return self.results
|