22
22
import time
23
23
from datetime import datetime
24
24
from typing import Any , AsyncGenerator , Dict , Generator , Tuple , cast
25
- from unittest import SkipTest , TestCase
25
+ from unittest import SkipTest
26
26
from unittest .mock import AsyncMock , Mock
27
27
28
28
import pytest_asyncio
37
37
from elasticsearch .exceptions import ConnectionError
38
38
from elasticsearch .helpers import bulk
39
39
40
+ from ..utils import CA_CERTS
40
41
from .test_integration ._async import test_document as async_document
41
42
from .test_integration ._sync import test_document as sync_document
42
43
from .test_integration .test_data import (
47
48
create_git_index ,
48
49
)
49
50
50
- if "ELASTICSEARCH_URL" in os .environ :
51
- ELASTICSEARCH_URL = os .environ ["ELASTICSEARCH_URL" ]
52
- else :
53
- ELASTICSEARCH_URL = "http://localhost:9200"
54
51
55
-
56
- def get_test_client (wait : bool = True , ** kwargs : Any ) -> Elasticsearch :
52
+ def get_test_client (
53
+ elasticsearch_url , wait : bool = True , ** kwargs : Any
54
+ ) -> Elasticsearch :
57
55
# construct kwargs from the environment
58
56
kw : Dict [str , Any ] = {"request_timeout" : 30 }
59
57
58
+ if elasticsearch_url .startswith ("https://" ):
59
+ kw ["ca_certs" ] = CA_CERTS
60
+
60
61
if "PYTHON_CONNECTION_CLASS" in os .environ :
61
62
kw ["node_class" ] = os .environ ["PYTHON_CONNECTION_CLASS" ]
62
63
63
64
kw .update (kwargs )
64
- client = Elasticsearch (ELASTICSEARCH_URL , ** kw )
65
+ client = Elasticsearch (elasticsearch_url , ** kw )
65
66
66
67
# wait for yellow status
67
68
for tries_left in range (100 if wait else 1 , 0 , - 1 ):
@@ -76,15 +77,17 @@ def get_test_client(wait: bool = True, **kwargs: Any) -> Elasticsearch:
76
77
raise SkipTest ("Elasticsearch failed to start." )
77
78
78
79
79
- async def get_async_test_client (wait : bool = True , ** kwargs : Any ) -> AsyncElasticsearch :
80
+ async def get_async_test_client (
81
+ elasticsearch_url , wait : bool = True , ** kwargs : Any
82
+ ) -> AsyncElasticsearch :
80
83
# construct kwargs from the environment
81
84
kw : Dict [str , Any ] = {"request_timeout" : 30 }
82
85
83
- if "PYTHON_CONNECTION_CLASS" in os . environ :
84
- kw ["node_class " ] = os . environ [ "PYTHON_CONNECTION_CLASS" ]
86
+ if elasticsearch_url . startswith ( "https://" ) :
87
+ kw ["ca_certs " ] = CA_CERTS
85
88
86
89
kw .update (kwargs )
87
- client = AsyncElasticsearch (ELASTICSEARCH_URL , ** kw )
90
+ client = AsyncElasticsearch (elasticsearch_url , ** kw )
88
91
89
92
# wait for yellow status
90
93
for tries_left in range (100 if wait else 1 , 0 , - 1 ):
@@ -100,36 +103,6 @@ async def get_async_test_client(wait: bool = True, **kwargs: Any) -> AsyncElasti
100
103
raise SkipTest ("Elasticsearch failed to start." )
101
104
102
105
103
- class ElasticsearchTestCase (TestCase ):
104
- client : Elasticsearch
105
-
106
- @staticmethod
107
- def _get_client () -> Elasticsearch :
108
- return get_test_client ()
109
-
110
- @classmethod
111
- def setup_class (cls ) -> None :
112
- cls .client = cls ._get_client ()
113
-
114
- def teardown_method (self , _ : Any ) -> None :
115
- # Hidden indices expanded in wildcards in ES 7.7
116
- expand_wildcards = ["open" , "closed" ]
117
- if self .es_version () >= (7 , 7 ):
118
- expand_wildcards .append ("hidden" )
119
-
120
- self .client .indices .delete_data_stream (
121
- name = "*" , expand_wildcards = expand_wildcards
122
- )
123
- self .client .indices .delete (index = "*" , expand_wildcards = expand_wildcards )
124
- self .client .indices .delete_template (name = "*" )
125
- self .client .indices .delete_index_template (name = "*" )
126
-
127
- def es_version (self ) -> Tuple [int , ...]:
128
- if not hasattr (self , "_es_version" ):
129
- self ._es_version = _get_version (self .client .info ()["version" ]["number" ])
130
- return self ._es_version
131
-
132
-
133
106
def _get_version (version_string : str ) -> Tuple [int , ...]:
134
107
if "." not in version_string :
135
108
return ()
@@ -138,19 +111,23 @@ def _get_version(version_string: str) -> Tuple[int, ...]:
138
111
139
112
140
113
@fixture (scope = "session" )
141
- def client () -> Elasticsearch :
114
+ def client (elasticsearch_url ) -> Elasticsearch :
142
115
try :
143
- connection = get_test_client (wait = "WAIT_FOR_ES" in os .environ )
116
+ connection = get_test_client (
117
+ elasticsearch_url , wait = "WAIT_FOR_ES" in os .environ
118
+ )
144
119
add_connection ("default" , connection )
145
120
return connection
146
121
except SkipTest :
147
122
skip ()
148
123
149
124
150
125
@pytest_asyncio .fixture
151
- async def async_client () -> AsyncGenerator [AsyncElasticsearch , None ]:
126
+ async def async_client (elasticsearch_url ) -> AsyncGenerator [AsyncElasticsearch , None ]:
152
127
try :
153
- connection = await get_async_test_client (wait = "WAIT_FOR_ES" in os .environ )
128
+ connection = await get_async_test_client (
129
+ elasticsearch_url , wait = "WAIT_FOR_ES" in os .environ
130
+ )
154
131
add_async_connection ("default" , connection )
155
132
yield connection
156
133
await connection .close ()
@@ -224,8 +201,8 @@ def data_client(client: Elasticsearch) -> Generator[Elasticsearch, None, None]:
224
201
bulk (client , DATA , raise_on_error = True , refresh = True )
225
202
bulk (client , FLAT_DATA , raise_on_error = True , refresh = True )
226
203
yield client
227
- client .indices .delete (index = "git" )
228
- client .indices .delete (index = "flat-git" )
204
+ client .options ( ignore_status = 404 ). indices .delete (index = "git" )
205
+ client .options ( ignore_status = 404 ). indices .delete (index = "flat-git" )
229
206
230
207
231
208
@pytest_asyncio .fixture
0 commit comments