예제 #1
0
    def fix(self):
        """
        Perform all fix routines found in executed checks.

        Iterate through all compliance checks and looks for `fix_*` methods
        with corresponding `test_*` methods. These are executed, and results
        are recorded if fixes are made.

        Note: Instead of individual `fix_*` methods, you can instead define
        a single `fix_failures` method which handles all fixes for the class.
        """
        if not self._results:
            return

        for test_id, test_desc in self._results.items():
            if test_desc['status'] != 'fail':
                continue

            test_obj = test_desc['test'].test
            method_name = parse_test_id(test_id)['method']
            candidate = method_name.replace('test_', 'fix_')

            if len(test_obj.tests) > 1 and hasattr(test_obj, candidate):
                getattr(test_obj, candidate)(self)
            elif hasattr(test_obj, 'fix_failures'):
                test_obj.fix_failures(self)
예제 #2
0
    def messages(self):
        """
        Check test messages.

        A generator of list of tuples containing the following structure::

          ([str] test_id, [dict] test_descriptor, [dict] message)
        """
        for test_id, test_desc in self._results.items():
            test_obj = test_desc['test'].test
            method_name = parse_test_id(test_id)['method']

            msg_method = 'get_notification_message'
            if len(test_obj.tests) > 1:
                candidate = method_name.replace('test_', 'msg_', 1)
                if hasattr(test_obj, candidate):
                    msg_method = candidate

            # set body to None if the notification function hasn't been
            # defined or if it returns None.
            # use a predefined error message for error status.
            # otherwise get the results of the notification function.
            # note that passed tests get their notifications called in order to
            # deduce things like subtitle, but their notifications are not
            # displayed.
            if not hasattr(test_obj, msg_method):
                msg = None
                body = None
            elif test_desc['status'] == 'error':
                msg = None
                body = f'Check {test_id} failed to execute'
            elif len(test_obj.tests) > 1 and not msg_method.startswith('msg_'):
                msg = getattr(test_obj, msg_method)(method_name)
                body = msg and 'body' in msg and msg['body'] or None
            else:
                msg = getattr(test_obj, msg_method)()
                body = msg and 'body' in msg and msg['body'] or None

            title = test_obj.title
            if msg and 'subtitle' in msg and msg['subtitle']:
                title += f' - {msg["subtitle"]}'

            failure_count = 0
            if msg and test_obj.failures:
                failure_count = test_obj.failures_count()

            warning_count = 0
            if msg and test_obj.warnings:
                warning_count = test_obj.warnings_count()

            msg = {
                'title': title,
                'body': body,
                'failure_count': failure_count,
                'warning_count': warning_count
            }
            yield test_id, test_desc, msg
예제 #3
0
 def _messages_by_accreditations(self):
     retval = {}
     for test_id, test_desc, msg in self.messages:
         test_class = parse_test_id(test_id)['class_path']
         accreditations = self._controls.get_accreditations(test_class)
         for a in accreditations:
             messages = retval.get(a, [])
             messages.append((test_id, test_desc, msg))
             retval[a] = messages
     return retval