|
| 1 | +from collections import OrderedDict |
| 2 | + |
| 3 | +from django.utils import timezone |
| 4 | + |
| 5 | +from .constants import LOG_DEFAULT, LOG_FAILURE, LOG_INFO, LOG_LEVEL_CODES, LOG_SUCCESS, LOG_WARNING |
| 6 | + |
| 7 | + |
| 8 | +class Report(object): |
| 9 | + """ |
| 10 | + NetBox users can extend this object to write custom reports to be used for validating data within NetBox. Each |
| 11 | + report must have one or more test methods named `test_*`. |
| 12 | +
|
| 13 | + The `results` attribute of a completed report will take the following form: |
| 14 | +
|
| 15 | + { |
| 16 | + 'test_bar': { |
| 17 | + 'failures': 42, |
| 18 | + 'log': [ |
| 19 | + (<datetime>, <level>, <object>, <message>), |
| 20 | + ... |
| 21 | + ] |
| 22 | + }, |
| 23 | + 'test_foo': { |
| 24 | + 'failures': 0, |
| 25 | + 'log': [ |
| 26 | + (<datetime>, <level>, <object>, <message>), |
| 27 | + ... |
| 28 | + ] |
| 29 | + } |
| 30 | + } |
| 31 | + """ |
| 32 | + results = OrderedDict() |
| 33 | + active_test = None |
| 34 | + failed = False |
| 35 | + |
| 36 | + def __init__(self): |
| 37 | + |
| 38 | + # Compile test methods and initialize results skeleton |
| 39 | + test_methods = [] |
| 40 | + for method in dir(self): |
| 41 | + if method.startswith('test_') and callable(getattr(self, method)): |
| 42 | + test_methods.append(method) |
| 43 | + self.results[method] = OrderedDict([ |
| 44 | + ('success', 0), |
| 45 | + ('info', 0), |
| 46 | + ('warning', 0), |
| 47 | + ('failed', 0), |
| 48 | + ('log', []), |
| 49 | + ]) |
| 50 | + if not test_methods: |
| 51 | + raise Exception("A report must contain at least one test method.") |
| 52 | + self.test_methods = test_methods |
| 53 | + |
| 54 | + def _log(self, obj, message, level=LOG_DEFAULT): |
| 55 | + """ |
| 56 | + Log a message from a test method. Do not call this method directly; use one of the log_* wrappers below. |
| 57 | + """ |
| 58 | + if level not in LOG_LEVEL_CODES: |
| 59 | + raise Exception("Unknown logging level: {}".format(level)) |
| 60 | + logline = [timezone.now(), level, obj, message] |
| 61 | + self.results[self.active_test]['log'].append(logline) |
| 62 | + |
| 63 | + def log_success(self, obj, message=None): |
| 64 | + """ |
| 65 | + Record a successful test against an object. Logging a message is optional. |
| 66 | + """ |
| 67 | + if message: |
| 68 | + self._log(obj, message, level=LOG_SUCCESS) |
| 69 | + self.results[self.active_test]['success'] += 1 |
| 70 | + |
| 71 | + def log_info(self, obj, message): |
| 72 | + """ |
| 73 | + Log an informational message. |
| 74 | + """ |
| 75 | + self._log(obj, message, level=LOG_INFO) |
| 76 | + self.results[self.active_test]['info'] += 1 |
| 77 | + |
| 78 | + def log_warning(self, obj, message): |
| 79 | + """ |
| 80 | + Log a warning. |
| 81 | + """ |
| 82 | + self._log(obj, message, level=LOG_WARNING) |
| 83 | + self.results[self.active_test]['warning'] += 1 |
| 84 | + |
| 85 | + def log_failure(self, obj, message): |
| 86 | + """ |
| 87 | + Log a failure. Calling this method will automatically mark the report as failed. |
| 88 | + """ |
| 89 | + self._log(obj, message, level=LOG_FAILURE) |
| 90 | + self.results[self.active_test]['failed'] += 1 |
| 91 | + self.failed = True |
| 92 | + |
| 93 | + def run(self): |
| 94 | + """ |
| 95 | + Run the report. Each test method will be executed in order. |
| 96 | + """ |
| 97 | + for method_name in self.test_methods: |
| 98 | + self.active_test = method_name |
| 99 | + test_method = getattr(self, method_name) |
| 100 | + test_method() |
0 commit comments