Skip to content

Commit 92f27cb

Browse files
committed
Expose API key as a test fixture
1 parent 780b8c5 commit 92f27cb

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

test_elasticsearch_serverless/conftest.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from elasticsearch_serverless import Elasticsearch
2323

24-
from .utils import es_url, es_version
24+
from .utils import es_api_key, es_url, es_version
2525

2626

2727
@pytest.fixture(scope="session")
@@ -32,6 +32,14 @@ def elasticsearch_url():
3232
pytest.skip(str(e))
3333

3434

35+
@pytest.fixture(scope="session")
36+
def elasticsearch_api_key():
37+
try:
38+
return es_api_key()
39+
except RuntimeError as e:
40+
pytest.skip(str(e))
41+
42+
3543
@pytest.fixture(scope="session")
3644
def elasticsearch_version(elasticsearch_url) -> Tuple[int, ...]:
3745
"""Returns the version of the current Elasticsearch cluster"""

test_elasticsearch_serverless/test_async/test_server/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
@pytest_asyncio.fixture(scope="function")
2929
@pytest.mark.usefixtures("sync_client")
30-
async def async_client(elasticsearch_url):
30+
async def async_client(elasticsearch_url, elasticsearch_api_key):
3131
# 'sync_client' fixture is used for the guaranteed wipe_cluster() call.
3232

3333
if not hasattr(elasticsearch_serverless, "AsyncElasticsearch"):
@@ -39,7 +39,7 @@ async def async_client(elasticsearch_url):
3939
client = None
4040
try:
4141
client = elasticsearch_serverless.AsyncElasticsearch(
42-
elasticsearch_url, request_timeout=3
42+
elasticsearch_url, api_key=elasticsearch_api_key, request_timeout=3
4343
)
4444
yield client
4545
finally:

test_elasticsearch_serverless/test_server/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import elasticsearch_serverless
2323

24-
from ..utils import es_api_key, wipe_cluster
24+
from ..utils import wipe_cluster
2525

2626
# Information about the Elasticsearch instance running, if any
2727
# Used for
@@ -31,19 +31,19 @@
3131

3232

3333
@pytest.fixture(scope="session")
34-
def sync_client_factory(elasticsearch_url):
34+
def sync_client_factory(elasticsearch_url, elasticsearch_api_key):
3535
client = None
3636
try:
3737
# Configure the client with API key and optionally
3838
# an HTTP conn class depending on 'PYTHON_CONNECTION_CLASS' envvar
39-
kw = {}
39+
kw = {'api_key': elasticsearch_api_key}
4040
if "PYTHON_CONNECTION_CLASS" in os.environ:
4141
kw["node_class"] = os.environ["PYTHON_CONNECTION_CLASS"]
4242

4343
# We do this little dance with the URL to force
4444
# Requests to respect 'headers: None' within rest API spec tests.
4545
client = elasticsearch_serverless.Elasticsearch(
46-
elasticsearch_url, api_key=es_api_key(), **kw
46+
elasticsearch_url, **kw
4747
)
4848

4949
# Wipe the cluster before we start testing just in case it wasn't wiped

0 commit comments

Comments
 (0)