From 96ac12a17aed193f933254e447dfb1cc50292857 Mon Sep 17 00:00:00 2001 From: ncgl-syngenta Date: Tue, 8 Nov 2022 11:12:07 -0600 Subject: [PATCH] add elasticsearch timeout (#131) * add elasticsearch timeout * linter * Update es_connector.py * Update es_connector.py * pr review * pr review Co-authored-by: Paul Cruse III --- .../elasticsearch/es_connector.py | 6 ++- .../elasticsearch/test_es_connector.py | 38 +++++++++++++++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/syngenta_digital_dta/elasticsearch/es_connector.py b/syngenta_digital_dta/elasticsearch/es_connector.py index fbb41dc..a9dea0a 100644 --- a/syngenta_digital_dta/elasticsearch/es_connector.py +++ b/syngenta_digital_dta/elasticsearch/es_connector.py @@ -26,7 +26,7 @@ def __configure(self): elif not self.port and not self.local: self.port = 443 config = { - 'hosts':[ + 'hosts': [ { 'host': self.host, 'port': self.port @@ -34,7 +34,9 @@ def __configure(self): ], 'use_ssl': not self.local, 'verify_certs': not self.local, - 'connection_class': RequestsHttpConnection + 'connection_class': RequestsHttpConnection, + 'timeout': 30, # Amount of time to wait to collect info on all nodes + 'request_timeout': 30 # Amount of time to wait for an HTTP response to start } if self.authentication == 'lambda': config['http_auth'] = self.__authenticate_lambda() diff --git a/tests/syngenta_digital_dta/elasticsearch/test_es_connector.py b/tests/syngenta_digital_dta/elasticsearch/test_es_connector.py index a515c5b..10790c1 100644 --- a/tests/syngenta_digital_dta/elasticsearch/test_es_connector.py +++ b/tests/syngenta_digital_dta/elasticsearch/test_es_connector.py @@ -1,11 +1,12 @@ -import uuid import unittest import warnings -import json +from collections import namedtuple +from unittest import mock from syngenta_digital_dta.elasticsearch.es_connector import ESConnector from tests.syngenta_digital_dta.elasticsearch.mocks import MockESAdapter + class ESConnectorTest(unittest.TestCase): def setUp(self, *args, **kwargs): @@ -23,7 +24,38 @@ def test_class_port_nonlocalhost(self): self.assertEqual(connector.port, mock_adapter.port) def test_class_port_user_pass(self): - mock_adapter = MockESAdapter(endpoint='dev.aws.com', user='root', password='root', authentication='user-password') + mock_adapter = MockESAdapter(endpoint='dev.aws.com', user='root', password='root', + authentication='user-password') connector = ESConnector(mock_adapter) self.assertEqual(connector.user, mock_adapter.user) self.assertEqual(connector.password, mock_adapter.password) + + @mock.patch('syngenta_digital_dta.elasticsearch.es_connector.Elasticsearch') + def test_elasticsearch_constructor(self, mock_elasticsearch): + cls = namedtuple('cls', ['endpoint', 'port', 'authentication', 'user', 'password']) + + kwargs = cls( + endpoint='endpoint', + port=1234, + authentication='user-password', + user='user', + password='password' + ) + + esc = ESConnector(kwargs) + esc.connect() + + mock_elasticsearch.assert_called_with( + hosts=[ + { + 'host': 'endpoint', + 'port': 1234 + } + ], + use_ssl=True, + verify_certs=True, + connection_class=mock.ANY, + timeout=30, + request_timeout=30, + http_auth=('user', 'password') + )