diff --git a/CHANGELOG.md b/CHANGELOG.md index fc4e76d..1bd058b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a ch ## [Unreleased](https://github.com/idealista/prom2teams/tree/develop) +## Fixed +- *[#177](https://github.com/idealista/prom2teams/issues/177) Fixing unit tests when using Python <= 3.6* @dortegau + ## [2.5.4](https://github.com/idealista/prom2teams/tree/2.5.4) [Full Changelog](https://github.com/idealista/prom2teams/compare/2.5.3...2.5.4) ## Fixed diff --git a/prom2teams/teams/alarm_mapper.py b/prom2teams/teams/alarm_mapper.py index 39d1158..3269266 100644 --- a/prom2teams/teams/alarm_mapper.py +++ b/prom2teams/teams/alarm_mapper.py @@ -2,12 +2,6 @@ from collections import defaultdict -def map_alarm_to_json(alarm): - schema = TeamsAlarmSchema() - result = schema.dump(alarm) - return result - - def map_prom_alerts_to_teams_alarms(alerts): alerts = group_alerts(alerts, 'status') teams_alarms = [] diff --git a/requirements.txt b/requirements.txt index f94babe..afabfb1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ pyyaml==5.1 uwsgi==2.0.16 prometheus_flask_exporter==0.9.0 werkzeug==0.16.1 +DeepDiff==4.3.0 \ No newline at end of file diff --git a/tests/data/jsons/all_ok.json b/tests/data/json_files/all_ok.json similarity index 100% rename from tests/data/jsons/all_ok.json rename to tests/data/json_files/all_ok.json diff --git a/tests/data/jsons/all_ok_extra_annotations.json b/tests/data/json_files/all_ok_extra_annotations.json similarity index 100% rename from tests/data/jsons/all_ok_extra_annotations.json rename to tests/data/json_files/all_ok_extra_annotations.json diff --git a/tests/data/jsons/all_ok_extra_labels.json b/tests/data/json_files/all_ok_extra_labels.json similarity index 100% rename from tests/data/jsons/all_ok_extra_labels.json rename to tests/data/json_files/all_ok_extra_labels.json diff --git a/tests/data/jsons/all_ok_multiple.json b/tests/data/json_files/all_ok_multiple.json similarity index 100% rename from tests/data/jsons/all_ok_multiple.json rename to tests/data/json_files/all_ok_multiple.json diff --git a/tests/data/jsons/teams_alarm_all_ok.json b/tests/data/json_files/teams_alarm_all_ok.json similarity index 100% rename from tests/data/jsons/teams_alarm_all_ok.json rename to tests/data/json_files/teams_alarm_all_ok.json diff --git a/tests/data/jsons/teams_alarm_all_ok_extra_annotations.json b/tests/data/json_files/teams_alarm_all_ok_extra_annotations.json similarity index 100% rename from tests/data/jsons/teams_alarm_all_ok_extra_annotations.json rename to tests/data/json_files/teams_alarm_all_ok_extra_annotations.json diff --git a/tests/data/jsons/teams_alarm_all_ok_extra_labels.json b/tests/data/json_files/teams_alarm_all_ok_extra_labels.json similarity index 100% rename from tests/data/jsons/teams_alarm_all_ok_extra_labels.json rename to tests/data/json_files/teams_alarm_all_ok_extra_labels.json diff --git a/tests/data/jsons/teams_alarm_all_ok_multiple.json b/tests/data/json_files/teams_alarm_all_ok_multiple.json similarity index 100% rename from tests/data/jsons/teams_alarm_all_ok_multiple.json rename to tests/data/json_files/teams_alarm_all_ok_multiple.json diff --git a/tests/data/jsons/teams_alarm_with_common_items.json b/tests/data/json_files/teams_alarm_with_common_items.json similarity index 100% rename from tests/data/jsons/teams_alarm_with_common_items.json rename to tests/data/json_files/teams_alarm_with_common_items.json diff --git a/tests/data/jsons/with_common_items.json b/tests/data/json_files/with_common_items.json similarity index 100% rename from tests/data/jsons/with_common_items.json rename to tests/data/json_files/with_common_items.json diff --git a/tests/data/jsons/without_instance_field.json b/tests/data/json_files/without_instance_field.json similarity index 100% rename from tests/data/jsons/without_instance_field.json rename to tests/data/json_files/without_instance_field.json diff --git a/tests/data/jsons/without_mandatory_field.json b/tests/data/json_files/without_mandatory_field.json similarity index 100% rename from tests/data/jsons/without_mandatory_field.json rename to tests/data/json_files/without_mandatory_field.json diff --git a/tests/data/jsons/without_optional_field.json b/tests/data/json_files/without_optional_field.json similarity index 100% rename from tests/data/jsons/without_optional_field.json rename to tests/data/json_files/without_optional_field.json diff --git a/tests/data/without_overriding_defaults.ini b/tests/data/not_overriding_defaults.ini similarity index 100% rename from tests/data/without_overriding_defaults.ini rename to tests/data/not_overriding_defaults.ini diff --git a/tests/test_app_configuration.py b/tests/test_app_configuration.py index e6c795b..65de2db 100644 --- a/tests/test_app_configuration.py +++ b/tests/test_app_configuration.py @@ -1,30 +1,32 @@ import unittest +import os + from prom2teams.app import configuration from prom2teams.app import exceptions class TestServer(unittest.TestCase): - TEST_CONFIG_FILES_PATH = 'tests/data/' + TEST_CONFIG_FILES_PATH = './tests/data/' DEFAULT_CONFIG_RELATIVE_PATH = './prom2teams/config.ini' def test_get_config_with_invalid_path(self): - invalid_relative_path = self.TEST_CONFIG_FILES_PATH + 'invalid_path' + invalid_relative_path = os.path.join(self.TEST_CONFIG_FILES_PATH, 'invalid_path') self.assertRaises(FileNotFoundError, configuration._config_provided, invalid_relative_path) def test_get_config_without_required_keys_should_raise_exception(self): - empty_config_relative_path = self.TEST_CONFIG_FILES_PATH + 'empty_config.ini' + empty_config_relative_path = os.path.join(self.TEST_CONFIG_FILES_PATH, 'empty_config.ini') self.assertRaises(exceptions.MissingConnectorConfigKeyException, configuration._config_provided, empty_config_relative_path) def test_get_config_without_override(self): - provided_config_relative_path = self.TEST_CONFIG_FILES_PATH + 'without_overriding_defaults.ini' + provided_config_relative_path = os.path.join(self.TEST_CONFIG_FILES_PATH, 'not_overriding_defaults.ini') config = configuration._config_provided(provided_config_relative_path) self.assertTrue(config.get('Microsoft Teams', 'Connector')) def test_get_config_overriding_defaults(self): - provided_config_relative_path = self.TEST_CONFIG_FILES_PATH + 'overriding_defaults.ini' + provided_config_relative_path = os.path.join(self.TEST_CONFIG_FILES_PATH, 'overriding_defaults.ini') config = configuration._config_provided(provided_config_relative_path) self.assertEqual(config.get('HTTP Server', 'Host'), '1.1.1.1') @@ -32,7 +34,7 @@ def test_get_config_overriding_defaults(self): self.assertTrue(config.get('Microsoft Teams', 'Connector')) def test_connectors_configured(self): - provided_config_relative_path = self.TEST_CONFIG_FILES_PATH + 'multiple_connectors_config.ini' + provided_config_relative_path = os.path.join(self.TEST_CONFIG_FILES_PATH, 'multiple_connectors_config.ini') config = configuration._config_provided(provided_config_relative_path) self.assertEqual(config['Microsoft Teams']['connector1'], 'teams_webhook_url') @@ -40,7 +42,7 @@ def test_connectors_configured(self): self.assertEqual(config['Microsoft Teams']['connector3'], 'definitely_another_teams_webhook_url') def test_get_config_for_all_fields(self): - provided_config_relative_path = self.TEST_CONFIG_FILES_PATH + 'all_fields.ini' + provided_config_relative_path = os.path.join(self.TEST_CONFIG_FILES_PATH, 'all_fields.ini') config = configuration._config_provided(provided_config_relative_path) self.assertEqual(config.get('HTTP Server', 'Host'), '1.1.1.1') @@ -51,7 +53,5 @@ def test_get_config_for_all_fields(self): self.assertEqual(config.get('Template', 'Path'), 'jinja2/template/path') self.assertEqual(config.get('Group Alerts', 'Field'), 'name') - - if __name__ == '__main__': unittest.main() diff --git a/tests/test_json_fields.py b/tests/test_json_fields.py index 8c9ac99..1f008d6 100644 --- a/tests/test_json_fields.py +++ b/tests/test_json_fields.py @@ -1,45 +1,47 @@ import unittest +import os import json from prom2teams.teams.alarm_mapper import map_prom_alerts_to_teams_alarms from prom2teams.prometheus.message_schema import MessageSchema from prom2teams.app.sender import AlarmSender +from deepdiff import DeepDiff class TestJSONFields(unittest.TestCase): - TEST_CONFIG_FILES_PATH = 'tests/data/jsons/' + TEST_CONFIG_FILES_PATH = './tests/data/json_files/' def test_json_with_all_fields(self): - with open(self.TEST_CONFIG_FILES_PATH + 'all_ok.json') as json_data: + with open(os.path.join(self.TEST_CONFIG_FILES_PATH, 'all_ok.json')) as json_data: json_received = json.load(json_data) alerts = MessageSchema().load(json_received) alarm = map_prom_alerts_to_teams_alarms(alerts)[0] self.assertNotIn('unknown', str(alarm)) def test_json_without_mandatory_field(self): - with open(self.TEST_CONFIG_FILES_PATH + 'without_mandatory_field.json') as json_data: + with open(os.path.join(self.TEST_CONFIG_FILES_PATH, 'without_mandatory_field.json')) as json_data: json_received = json.load(json_data) alerts = MessageSchema().load(json_received) alarm = map_prom_alerts_to_teams_alarms(alerts)[0] self.assertIn('unknown', str(alarm)) def test_json_without_optional_field(self): - with open(self.TEST_CONFIG_FILES_PATH + 'without_optional_field.json') as json_data: + with open(os.path.join(self.TEST_CONFIG_FILES_PATH, 'without_optional_field.json')) as json_data: json_received = json.load(json_data) alerts = MessageSchema().load(json_received) alarm = map_prom_alerts_to_teams_alarms(alerts)[0] self.assertIn("'description': 'unknown'", str(alarm)) def test_json_without_instance_field(self): - with open(self.TEST_CONFIG_FILES_PATH + 'without_instance_field.json') as json_data: + with open(os.path.join(self.TEST_CONFIG_FILES_PATH, 'without_instance_field.json')) as json_data: json_received = json.load(json_data) alerts = MessageSchema().load(json_received) alarm = map_prom_alerts_to_teams_alarms(alerts)[0] self.assertEqual('unknown', str(alarm['instance'])) def test_compose_all(self): - with open(self.TEST_CONFIG_FILES_PATH + 'all_ok.json') as json_data: - with open(self.TEST_CONFIG_FILES_PATH + 'teams_alarm_all_ok.json') as expected_data: + 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_alarm_all_ok.json')) as expected_data: json_received = json.load(json_data) json_expected = json.load(expected_data) @@ -47,12 +49,13 @@ def test_compose_all(self): rendered_data = AlarmSender()._create_alarms(alerts)[0] json_rendered = json.loads(rendered_data) - self.assertDictEqual(json_rendered, json_expected) + diff = DeepDiff(json_rendered, json_expected, ignore_order=True) + self.assertTrue(not diff) def test_with_common_items(self): self.maxDiff = None - with open(self.TEST_CONFIG_FILES_PATH + 'with_common_items.json') as json_data: - with open(self.TEST_CONFIG_FILES_PATH + 'teams_alarm_with_common_items.json') as expected_data: + 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_alarm_with_common_items.json')) as expected_data: json_received = json.load(json_data) json_expected = json.load(expected_data) @@ -63,8 +66,8 @@ def test_with_common_items(self): self.assertEqual(json_rendered.keys(), json_expected.keys()) def test_grouping_multiple_alerts(self): - with open(self.TEST_CONFIG_FILES_PATH + 'all_ok_multiple.json') as json_data: - with open(self.TEST_CONFIG_FILES_PATH + 'teams_alarm_all_ok_multiple.json') as expected_data: + 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_alarm_all_ok_multiple.json')) as expected_data: json_received = json.load(json_data) json_expected = json.load(expected_data) @@ -72,12 +75,13 @@ def test_grouping_multiple_alerts(self): rendered_data = AlarmSender(group_alerts_by='name')._create_alarms(alerts)[0].replace("\n\n\n", " ") json_rendered = json.loads(rendered_data) - self.assertDictEqual(json_rendered, json_expected) + diff = DeepDiff(json_rendered, json_expected, ignore_order=True) + self.assertTrue(not diff) def test_with_extra_labels(self): excluded_labels = ('pod_name', ) - with open(self.TEST_CONFIG_FILES_PATH + 'all_ok_extra_labels.json') as json_data: - with open(self.TEST_CONFIG_FILES_PATH + 'teams_alarm_all_ok_extra_labels.json') as expected_data: + with open(os.path.join(self.TEST_CONFIG_FILES_PATH, 'all_ok_extra_labels.json')) as json_data: + with open(os.path.join(self.TEST_CONFIG_FILES_PATH, 'teams_alarm_all_ok_extra_labels.json')) as expected_data: json_received = json.load(json_data) json_expected = json.load(expected_data) @@ -85,12 +89,13 @@ def test_with_extra_labels(self): rendered_data = AlarmSender()._create_alarms(alerts)[0] json_rendered = json.loads(rendered_data) - self.assertDictEqual(json_rendered, json_expected) + diff = DeepDiff(json_rendered, json_expected, ignore_order=True) + self.assertTrue(not diff) def test_with_extra_annotations(self): excluded_annotations = ('message', ) - with open(self.TEST_CONFIG_FILES_PATH + 'all_ok_extra_annotations.json') as json_data: - with open(self.TEST_CONFIG_FILES_PATH + 'teams_alarm_all_ok_extra_annotations.json') as expected_data: + 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_alarm_all_ok_extra_annotations.json')) as expected_data: json_received = json.load(json_data) json_expected = json.load(expected_data) @@ -98,8 +103,8 @@ def test_with_extra_annotations(self): rendered_data = AlarmSender()._create_alarms(alerts)[0] json_rendered = json.loads(rendered_data) - self.assertDictEqual(json_rendered, json_expected) - + diff = DeepDiff(json_rendered, json_expected, ignore_order=True) + self.assertTrue(not diff) if __name__ == '__main__': unittest.main()