Skip to content

Commit

Permalink
add elasticsearch timeout (#131)
Browse files Browse the repository at this point in the history
* add elasticsearch timeout

* linter

* Update es_connector.py

* Update es_connector.py

* pr review

* pr review

Co-authored-by: Paul Cruse III <[email protected]>
  • Loading branch information
ncgl-syngenta and paulcruse-syn authored Nov 8, 2022
1 parent 4c5db45 commit 96ac12a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
6 changes: 4 additions & 2 deletions syngenta_digital_dta/elasticsearch/es_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ def __configure(self):
elif not self.port and not self.local:
self.port = 443
config = {
'hosts':[
'hosts': [
{
'host': self.host,
'port': self.port
}
],
'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()
Expand Down
38 changes: 35 additions & 3 deletions tests/syngenta_digital_dta/elasticsearch/test_es_connector.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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')
)

0 comments on commit 96ac12a

Please sign in to comment.