Skip to content

Commit

Permalink
Merge pull request #178 from idealista/bugs/177
Browse files Browse the repository at this point in the history
#177 Fixing unit tests when using Python <= 3.6
  • Loading branch information
dortegau committed Apr 13, 2020
2 parents 4270038 + d10a7db commit 0df2919
Show file tree
Hide file tree
Showing 19 changed files with 38 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions prom2teams/teams/alarm_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 9 additions & 9 deletions tests/test_app_configuration.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
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')
self.assertEqual(config.get('HTTP Server', 'Port'), '9089')
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')
self.assertEqual(config['Microsoft Teams']['connector2'], 'another_teams_webhook_url')
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')
Expand All @@ -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()
45 changes: 25 additions & 20 deletions tests/test_json_fields.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,61 @@
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)

alerts = MessageSchema().load(json_received)
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)

Expand All @@ -63,43 +66,45 @@ 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)

alerts = MessageSchema().load(json_received)
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)

alerts = MessageSchema(exclude_fields=excluded_labels).load(json_received)
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)

alerts = MessageSchema(exclude_annotations=excluded_annotations).load(json_received)
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()

0 comments on commit 0df2919

Please sign in to comment.