Beispiel #1
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.schema = MessageSchema(
         exclude_fields=app.config['LABELS_EXCLUDED'],
         exclude_annotations=app.config['ANNOTATIONS_EXCLUDED'])
     self.sender = AlertSender(
         template_path=app.config.get('TEMPLATE_PATH'),
         group_alerts_by=app.config['GROUP_ALERTS_BY'],
         teams_client_config=app.config.get('TEAMS_CLIENT_CONFIG'))
    def test_with_too_long_payload(self):
        with open(
                os.path.join(self.TEST_CONFIG_FILES_PATH,
                             'all_ok_multiple.json')) as json_data:
            with open(
                    os.path.join(
                        self.TEST_CONFIG_FILES_PATH,
                        'teams_alert_all_ok_splitted.json')) as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema().load(json_received)
                rendered_data = '[' + ','.join([
                    a.replace("\n\n\n", " ")
                    for a in AlertSender(group_alerts_by='name',
                                         teams_client_config={
                                             'MAX_PAYLOAD': 800
                                         })._create_alerts(alerts)
                ]) + ']'
                json_rendered = json.loads(rendered_data)

                diff = DeepDiff(json_rendered,
                                json_expected,
                                ignore_order=True)
                self.assertTrue(not diff)
Beispiel #3
0
class AlertReceiver(Resource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.schema = MessageSchema(
            exclude_fields=app.config['LABELS_EXCLUDED'],
            exclude_annotations=app.config['ANNOTATIONS_EXCLUDED'])
        self.sender = AlertSender(
            template_path=app.config.get('TEMPLATE_PATH'),
            group_alerts_by=app.config['GROUP_ALERTS_BY'],
            teams_client_config=app.config.get('TEAMS_CLIENT_CONFIG'))

    @api_v2.expect(message)
    def post(self, connector):
        alerts = self.schema.load(request.get_json())
        self.sender.send_alerts(alerts,
                                app.config['MICROSOFT_TEAMS'][connector])
        return 'OK', 201
Beispiel #4
0
class AlertReceiver(Resource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.schema = MessageSchema()
        self.sender = AlertSender(
            template_path=app.config.get('TEMPLATE_PATH'),
            teams_client_config=app.config.get('TEAMS_CLIENT_CONFIG'))

    @api_v1.expect(message)
    def post(self):
        _show_deprecated_warning(
            "Call to deprecated function. It will be removed in future versions. "
            "Please view the README file.")
        alerts = self.schema.load(request.get_json())
        self.sender.send_alerts(alerts,
                                app.config['MICROSOFT_TEAMS']['Connector'])
        return 'OK', 201
    def test_without_fingerprint(self):
        with open(
                os.path.join(self.TEST_CONFIG_FILES_PATH,
                             'without_fingerprint.json')) as json_data:
            with open(
                    os.path.join(
                        self.TEST_CONFIG_FILES_PATH,
                        'teams_without_fingerprint.json')) as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema().load(json_received)
                rendered_data = AlertSender()._create_alerts(alerts)[0]
                json_rendered = json.loads(rendered_data)

                self.assertEqual(json_rendered.keys(), json_expected.keys())
    def test_with_common_items(self):
        self.maxDiff = None
        with open(
                os.path.join(self.TEST_CONFIG_FILES_PATH,
                             'with_common_items.json')) as json_data:
            with open(
                    os.path.join(self.TEST_CONFIG_FILES_PATH,
                                 'teams_alert_with_common_items.json')
            ) as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema().load(json_received)
                rendered_data = AlertSender()._create_alerts(alerts)[0]
                json_rendered = json.loads(rendered_data)

                self.assertEqual(json_rendered.keys(), json_expected.keys())
    def test_compose_all(self):
        with open(os.path.join(self.TEST_CONFIG_FILES_PATH,
                               'all_ok.json')) as json_data:
            with open(
                    os.path.join(self.TEST_CONFIG_FILES_PATH,
                                 'teams_alert_all_ok.json')) as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema().load(json_received)
                rendered_data = AlertSender()._create_alerts(alerts)[0]
                json_rendered = json.loads(rendered_data)

                diff = DeepDiff(json_rendered,
                                json_expected,
                                ignore_order=True)
                self.assertTrue(not diff)
    def test_with_extra_annotations(self):
        excluded_annotations = ('message', )
        with open(
                os.path.join(self.TEST_CONFIG_FILES_PATH,
                             'all_ok_extra_annotations.json')) as json_data:
            with open(
                    os.path.join(self.TEST_CONFIG_FILES_PATH,
                                 'teams_alert_all_ok_extra_annotations.json')
            ) as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema(exclude_annotations=excluded_annotations
                                       ).load(json_received)
                rendered_data = AlertSender()._create_alerts(alerts)[0]
                json_rendered = json.loads(rendered_data)

                diff = DeepDiff(json_rendered,
                                json_expected,
                                ignore_order=True)
                self.assertTrue(not diff)
    def test_grouping_multiple_alerts(self):
        with open(
                os.path.join(self.TEST_CONFIG_FILES_PATH,
                             'all_ok_multiple.json')) as json_data:
            with open(
                    os.path.join(
                        self.TEST_CONFIG_FILES_PATH,
                        'teams_alert_all_ok_multiple.json')) as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema().load(json_received)
                rendered_data = AlertSender(
                    group_alerts_by='name')._create_alerts(alerts)[0].replace(
                        "\n\n\n", " ")
                json_rendered = json.loads(rendered_data)

                diff = DeepDiff(json_rendered,
                                json_expected,
                                ignore_order=True)
                self.assertTrue(not diff)
Beispiel #10
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.schema = MessageSchema()
     self.sender = AlertSender(
         template_path=app.config.get('TEMPLATE_PATH'),
         teams_client_config=app.config.get('TEAMS_CLIENT_CONFIG'))