2023-10-26 13:34:34 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-03-17 12:03:46 -07:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
#
|
|
|
|
|
# The OpenSearch Contributors require contributions made to
|
|
|
|
|
# this file be licensed under the Apache-2.0 license or a
|
|
|
|
|
# compatible open source license.
|
|
|
|
|
#
|
|
|
|
|
# Modifications Copyright OpenSearch Contributors. See
|
|
|
|
|
# GitHub history for details.
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import time
|
2023-11-06 13:08:19 -05:00
|
|
|
from typing import Any
|
2023-03-17 12:03:46 -07:00
|
|
|
from unittest import SkipTest
|
|
|
|
|
|
|
|
|
|
from opensearchpy import AsyncOpenSearch
|
|
|
|
|
from opensearchpy.exceptions import ConnectionError
|
|
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
OPENSEARCH_URL = os.environ.get("OPENSEARCH_URL", "https://admin:admin@localhost:9200")
|
2023-03-17 12:03:46 -07:00
|
|
|
|
|
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
async def get_test_client(nowait: bool = False, **kwargs: Any) -> Any:
|
2023-03-17 12:03:46 -07:00
|
|
|
# construct kwargs from the environment
|
|
|
|
|
kw = {"timeout": 30}
|
|
|
|
|
|
|
|
|
|
from opensearchpy import AsyncConnection
|
|
|
|
|
|
|
|
|
|
async_connection = AsyncConnection()
|
|
|
|
|
if hasattr(async_connection, "AIOHttpConnection"):
|
|
|
|
|
kw["connection_class"] = getattr(async_connection, "AIOHttpConnection")
|
|
|
|
|
|
|
|
|
|
kw.update(kwargs)
|
2023-11-06 13:08:19 -05:00
|
|
|
client = AsyncOpenSearch(OPENSEARCH_URL, **kw) # type: ignore
|
2023-03-17 12:03:46 -07:00
|
|
|
|
|
|
|
|
# wait for yellow status
|
|
|
|
|
for _ in range(1 if nowait else 100):
|
|
|
|
|
try:
|
|
|
|
|
await client.cluster.health(wait_for_status="yellow")
|
|
|
|
|
return client
|
|
|
|
|
except ConnectionError:
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
else:
|
|
|
|
|
# timeout
|
|
|
|
|
raise SkipTest("OpenSearch failed to start.")
|