mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
21 2 (#77)
* 21 - Improving exception when an invalid type is used in the config * Bumping the version
This commit is contained in:
@@ -3,6 +3,7 @@ import sys
|
||||
import unittest
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
from requests import ConnectionError, HTTPError, Timeout
|
||||
from yaml import load, SafeLoader
|
||||
|
||||
@@ -32,7 +33,8 @@ class ConfigurationTest(unittest.TestCase):
|
||||
|
||||
sys.modules['requests'].get = get
|
||||
|
||||
self.configuration = Configuration(load(open('config.yml', 'r'), SafeLoader), 0)
|
||||
self.configuration = Configuration(
|
||||
load(open(os.path.join(os.path.dirname(__file__), 'configs/config.yml'), 'rt'), SafeLoader), 0)
|
||||
sys.modules['requests'].Timeout = Timeout
|
||||
sys.modules['requests'].ConnectionError = ConnectionError
|
||||
sys.modules['requests'].HTTPError = HTTPError
|
||||
@@ -174,3 +176,57 @@ class ConfigurationTest(unittest.TestCase):
|
||||
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
|
||||
'Incorrect component update parameters')
|
||||
self.configuration.push_status()
|
||||
|
||||
|
||||
class ConfigurationMultipleUrlTest(unittest.TestCase):
|
||||
@mock.patch.dict(os.environ, {'CACHET_TOKEN': 'token2'})
|
||||
def setUp(self):
|
||||
def getLogger(name):
|
||||
self.mock_logger = mock.Mock()
|
||||
return self.mock_logger
|
||||
|
||||
sys.modules['logging'].getLogger = getLogger
|
||||
|
||||
def get(url, headers):
|
||||
get_return = mock.Mock()
|
||||
get_return.ok = True
|
||||
get_return.json = mock.Mock()
|
||||
get_return.json.return_value = {'data': {'status': 1, 'default_value': 0.5}}
|
||||
return get_return
|
||||
|
||||
sys.modules['requests'].get = get
|
||||
|
||||
config_yaml = load(open(os.path.join(os.path.dirname(__file__), 'configs/config_multiple_urls.yml'), 'rt'),
|
||||
SafeLoader)
|
||||
self.configuration = []
|
||||
|
||||
for index in range(len(config_yaml['endpoints'])):
|
||||
self.configuration.append(Configuration(config_yaml, index))
|
||||
|
||||
sys.modules['requests'].Timeout = Timeout
|
||||
sys.modules['requests'].ConnectionError = ConnectionError
|
||||
sys.modules['requests'].HTTPError = HTTPError
|
||||
|
||||
def test_init(self):
|
||||
expected_method = ['GET', 'POST']
|
||||
expected_url = ['http://localhost:8080/swagger', 'http://localhost:8080/bar']
|
||||
|
||||
for index in range(len(self.configuration)):
|
||||
config = self.configuration[index]
|
||||
self.assertEqual(len(config.data), 2, 'Number of root elements in config.yml is incorrect')
|
||||
self.assertEqual(len(config.expectations), 1, 'Number of expectations read from file is incorrect')
|
||||
self.assertDictEqual(config.headers, {'X-Cachet-Token': 'token2'}, 'Header was not set correctly')
|
||||
self.assertEqual(config.api_url, 'https://demo.cachethq.io/api/v1',
|
||||
'Cachet API URL was set incorrectly')
|
||||
|
||||
self.assertEqual(expected_method[index], config.endpoint_method)
|
||||
self.assertEqual(expected_url[index], config.endpoint_url)
|
||||
|
||||
|
||||
class ConfigurationNegativeTest(unittest.TestCase):
|
||||
@mock.patch.dict(os.environ, {'CACHET_TOKEN': 'token2'})
|
||||
def test_init(self):
|
||||
with pytest.raises(cachet_url_monitor.configuration.ConfigurationValidationError):
|
||||
self.configuration = Configuration(
|
||||
load(open(os.path.join(os.path.dirname(__file__), 'configs/config_invalid_type.yml'), 'rt'),
|
||||
SafeLoader), 0)
|
||||
|
||||
Reference in New Issue
Block a user