From 8f0b7f0bcf7ccb146115bcf9c394b26a37189128 Mon Sep 17 00:00:00 2001 From: mazhead Date: Tue, 19 Mar 2019 08:13:00 +0100 Subject: [PATCH 1/5] Add header option --- cachet_url_monitor/configuration.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cachet_url_monitor/configuration.py b/cachet_url_monitor/configuration.py index 3e3e695..8ff21a6 100644 --- a/cachet_url_monitor/configuration.py +++ b/cachet_url_monitor/configuration.py @@ -97,6 +97,7 @@ def __init__(self, config_file): self.endpoint_url = os.environ.get('ENDPOINT_URL') or self.data['endpoint']['url'] self.endpoint_url = normalize_url(self.endpoint_url) self.endpoint_timeout = os.environ.get('ENDPOINT_TIMEOUT') or self.data['endpoint'].get('timeout') or 1 + self.endpoint_header = os.environ.get('HEADER') or self.data['endpoint'].get('header') or None self.allowed_fails = os.environ.get('ALLOWED_FAILS') or self.data['endpoint'].get('allowed_fails') or 0 self.api_url = os.environ.get('CACHET_API_URL') or self.data['cachet']['api_url'] @@ -172,7 +173,10 @@ def evaluate(self): according to the expectation results. """ try: - self.request = requests.request(self.endpoint_method, self.endpoint_url, timeout=self.endpoint_timeout) + if self.endpoint_header is not None: + self.request = requests.request(self.endpoint_method, self.endpoint_url, timeout=self.endpoint_timeout, headers=self.endpoint_header) + else: + self.request = requests.request(self.endpoint_method, self.endpoint_url, timeout=self.endpoint_timeout) self.current_timestamp = int(time.time()) except requests.ConnectionError: self.message = 'The URL is unreachable: %s %s' % (self.endpoint_method, self.endpoint_url) From 5e44206ec35ad6949b188877a0639b16a01e7cd4 Mon Sep 17 00:00:00 2001 From: mazhead Date: Tue, 19 Mar 2019 08:15:39 +0100 Subject: [PATCH 2/5] Add header option --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 38f68a3..ba53696 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ This project is available at PyPI: [https://pypi.python.org/pypi/cachet-url-moni endpoint: url: http://www.google.com method: GET + header: + SOME-HEADER: SOME-VALUE timeout: 1 # seconds expectation: - type: HTTP_STATUS @@ -44,6 +46,7 @@ frequency: 30 - **endpoint**, the configuration about the URL that will be monitored. - **url**, the URL that is going to be monitored. - **method**, the HTTP method that will be used by the monitor. + - **header**, client header passed to the request. Remove if you do not want to pass a header. - **timeout**, how long we'll wait to consider the request failed. The unit of it is seconds. - **expectation**, the list of expectations set for the URL. - **HTTP_STATUS**, we will verify if the response status code falls into the expected range. Please keep in mind the range is inclusive on the first number and exclusive on the second number. If just one value is specified, it will default to only the given value, for example `200` will be converted to `200-201`. From 268f047d52e79ec8c79a1a7b0eaf6ab9c03222e5 Mon Sep 17 00:00:00 2001 From: mazhead Date: Thu, 21 Mar 2019 16:29:21 +0100 Subject: [PATCH 3/5] Add unit test for headers --- tests/test_configuration.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 533db2b..67c69f9 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -42,12 +42,31 @@ def test_init(self): self.assertDictEqual(self.configuration.headers, {'X-Cachet-Token': 'token2'}, 'Header was not set correctly') self.assertEquals(self.configuration.api_url, 'https://demo.cachethq.io/api/v1', 'Cachet API URL was set incorrectly') + self.assertDictEqual(self.configuration.endpoint_header, {'SOME-HEADER': 'SOME-VALUE'}, 'Header is incorrect') def test_evaluate(self): def total_seconds(): return 0.1 - def request(method, url, timeout=None): + def request(method, url, headers, timeout=None): + response = mock.Mock() + response.status_code = 200 + response.elapsed = mock.Mock() + response.elapsed.total_seconds = total_seconds + response.text = '' + return response + + sys.modules['requests'].request = request + self.configuration.evaluate() + + self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL, + 'Component status set incorrectly') + + def test_evaluate_without_header(self): + def total_seconds(): + return 0.1 + + def request(method, url, headers=None, timeout=None): response = mock.Mock() response.status_code = 200 response.elapsed = mock.Mock() @@ -65,7 +84,7 @@ def test_evaluate_with_failure(self): def total_seconds(): return 0.1 - def request(method, url, timeout=None): + def request(method, url, headers, timeout=None): response = mock.Mock() # We are expecting a 200 response, so this will fail the expectation. response.status_code = 400 @@ -81,7 +100,7 @@ def request(method, url, timeout=None): 'Component status set incorrectly') def test_evaluate_with_timeout(self): - def request(method, url, timeout=None): + def request(method, url, headers, timeout=None): self.assertEquals(method, 'GET', 'Incorrect HTTP method') self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect') self.assertEquals(timeout, 0.010) @@ -96,7 +115,7 @@ def request(method, url, timeout=None): self.mock_logger.warning.assert_called_with('Request timed out') def test_evaluate_with_connection_error(self): - def request(method, url, timeout=None): + def request(method, url, headers, timeout=None): self.assertEquals(method, 'GET', 'Incorrect HTTP method') self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect') self.assertEquals(timeout, 0.010) @@ -111,7 +130,7 @@ def request(method, url, timeout=None): self.mock_logger.warning.assert_called_with('The URL is unreachable: GET http://localhost:8080/swagger') def test_evaluate_with_http_error(self): - def request(method, url, timeout=None): + def request(method, url, headers, timeout=None): self.assertEquals(method, 'GET', 'Incorrect HTTP method') self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect') self.assertEquals(timeout, 0.010) From f6a9a2ceadbb502b31933eb8a8443c80d1f0cdd2 Mon Sep 17 00:00:00 2001 From: mazhead Date: Thu, 21 Mar 2019 16:30:11 +0100 Subject: [PATCH 4/5] Add client header --- config.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config.yml b/config.yml index 7b52bd6..241fb73 100644 --- a/config.yml +++ b/config.yml @@ -1,6 +1,8 @@ endpoint: url: http://localhost:8080/swagger method: GET + header: + SOME-HEADER: SOME-VALUE timeout: 0.01 expectation: - type: HTTP_STATUS From f0b60b44ff144077b73108c53311f6b4c40fe1c1 Mon Sep 17 00:00:00 2001 From: mazhead Date: Thu, 21 Mar 2019 16:31:13 +0100 Subject: [PATCH 5/5] Remove environ for HEADER environ does not support dictionaries, so HEADER can't be passed. --- cachet_url_monitor/configuration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachet_url_monitor/configuration.py b/cachet_url_monitor/configuration.py index 8ff21a6..21ebc34 100644 --- a/cachet_url_monitor/configuration.py +++ b/cachet_url_monitor/configuration.py @@ -97,7 +97,7 @@ def __init__(self, config_file): self.endpoint_url = os.environ.get('ENDPOINT_URL') or self.data['endpoint']['url'] self.endpoint_url = normalize_url(self.endpoint_url) self.endpoint_timeout = os.environ.get('ENDPOINT_TIMEOUT') or self.data['endpoint'].get('timeout') or 1 - self.endpoint_header = os.environ.get('HEADER') or self.data['endpoint'].get('header') or None + self.endpoint_header = self.data['endpoint'].get('header') or None self.allowed_fails = os.environ.get('ALLOWED_FAILS') or self.data['endpoint'].get('allowed_fails') or 0 self.api_url = os.environ.get('CACHET_API_URL') or self.data['cachet']['api_url']