コード例 #1
0
    def _load_rules(self):
        """
        Load and pre-process the rules from the entry points.

        Rules are specified per-tool as a list of the form:

            template_path_rules = [
                    ['>', 'tool1'],  # this tool must be resolved before tool1
                    ['<', 'tool2'],  # this tool must be resolved after tool2
                    ['=', 'tool3'],  # this tool replaces all of tool3's templates
                ]

        Returns two lists of rules, order_rules and replacement_rules.

        order_rules represents all of the '>' and '<' rules and are returned
        as a list of pairs of the form ('a', 'b') indicating that path 'a' must
        come before path 'b'.

        replacement_rules represent all of the '=' rules and are returned as
        a dictionary mapping the paths to replace to the paths to replace with.
        """
        order_rules = []
        replacement_rules = {}
        for ep in iter_entry_points(self.override_entrypoint):
            for rule in getattr(ep.load(), 'template_path_rules', []):
                if rule[0] == '>':
                    order_rules.append((ep.name, rule[1]))
                elif rule[0] == '=':
                    replacement_rules[rule[1]] = ep.name
                elif rule[0] == '<':
                    order_rules.append((rule[1], ep.name))
                else:
                    raise jinja2.TemplateError(
                        'Unknown template path rule in %s: %s' %
                        (ep.name, ' '.join(rule)))
        return order_rules, replacement_rules
コード例 #2
0
ファイル: test_mail.py プロジェクト: tuxlife/zmon-worker
    def test_send(self, mock_jinja, mock_smtp):
        alert = {
            'id':
            'a1',
            'period':
            '',
            'name':
            'test_alert',
            'notifications': [
                'send_sms("42", repeat=5)',
                'send_mail("*****@*****.**", repeat=5)'
            ],
            'check_id':
            1,
            'entity': {
                'id': 'e1'
            },
            'worker':
            'worker-1',
        }
        s = Mock()
        mock_smtp.return_value = s
        mock_tmpl = Mock()
        mock_tmpl.render.return_value = ''
        mock_jinja.get_template.return_value = mock_tmpl

        # Regular send
        repeat = m.Mail.notify(
            {
                'captures': {},
                'changed': True,
                'value': {
                    'value': 1.0
                },
                'entity': {
                    'id': 'e1'
                },
                'is_alert': True,
                'worker': 'worker-1',
                'alert_def': alert,
                'duration': datetime.timedelta(seconds=0),
            },
            '*****@*****.**',
            include_value=False,
            include_definition=False,
            include_entity=False)
        self.assertEqual(0, repeat)
        mock_jinja.get_template.assert_called_with('alert.txt')
        mock_smtp.assert_called_with('test_host', 25)
        s.sendmail.assert_called_with('test_sender', ['*****@*****.**'], ANY)
        mock_tmpl.render.assert_called_with(
            alert_def=alert,
            captures={},
            changed=True,
            duration=datetime.timedelta(0),
            entity=alert['entity'],
            expanded_alert_name=alert['name'],
            include_captures=True,
            include_definition=False,
            include_entity=False,
            include_value=False,
            is_alert=True,
            alert_url='https://zmon.example.org/#/alert-details/a1',
            worker='worker-1',
            value={'value': 1.0})

        # Send with repeat in HTML
        repeat = m.Mail.notify(
            {
                'captures': {},
                'changed': True,
                'value': {
                    'value': 1.0
                },
                'entity': {
                    'id': 'e1'
                },
                'is_alert': True,
                'alert_def': alert,
                'duration': datetime.timedelta(seconds=0),
            },
            '*****@*****.**',
            repeat=100,
            html=True)
        self.assertEqual(100, repeat)
        mock_jinja.get_template.assert_called_with('alert.html')
        mock_smtp.assert_called_with('test_host', 25)
        s.sendmail.assert_called_with('test_sender', ['*****@*****.**'], ANY)

        # Exception handling 1: Jinja Error
        mock_smtp.reset_mock()
        s.reset_mock()
        mock_smtp.return_value = s
        mock_jinja.reset_mock()
        t = Mock()
        t.render.side_effect = jinja2.TemplateError('Jinja Error')
        mock_jinja.get_template.return_value = t

        repeat = m.Mail.notify(
            {
                'captures': {},
                'changed': True,
                'value': {
                    'value': 1.0
                },
                'entity': {
                    'id': 'e1'
                },
                'is_alert': True,
                'alert_def': alert,
                'duration': datetime.timedelta(seconds=0),
            },
            '*****@*****.**',
            repeat=101)
        self.assertEqual(101, repeat)
        mock_jinja.get_template.assert_called_with('alert.txt')
        self.assertFalse(mock_smtp.called)

        # Exception handling 2: SMTP Error
        mock_jinja.reset_mock()
        t = Mock()
        t.render.return_value = 'test'
        mock_jinja.get_template.return_value = t
        mock_smtp.reset_mock()
        s = Mock()
        s.sendmail.side_effect = Exception('Error connecting to host')
        mock_smtp.return_value = s

        repeat = m.Mail.notify(
            {
                'captures': {},
                'changed': True,
                'value': {
                    'value': 1.0
                },
                'entity': {
                    'id': 'e1'
                },
                'is_alert': True,
                'alert_def': alert,
                'duration': datetime.timedelta(seconds=0),
            },
            '*****@*****.**',
            repeat=102)
        self.assertEqual(102, repeat)
        mock_jinja.get_template.assert_called_with('alert.txt')
        self.assertTrue(mock_smtp.called)
コード例 #3
0
 def err(self, msg, caller):
     """Raise a template error."""
     raise jinja2.TemplateError(msg)
コード例 #4
0
 def err(self, msg, caller):
     raise jinja2.TemplateError(msg)