Skip to content

Commit 8e642ab

Browse files
committed
Fetch YAML tests from elastic/serverless-clients-tests
Instead of using the entire set of YAML REST tests from the elasticsearch repo.
1 parent 1c90489 commit 8e642ab

File tree

4 files changed

+29
-66
lines changed

4 files changed

+29
-66
lines changed

test_elasticsearch_serverless/test_async/test_server/test_helpers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,12 @@ async def test_reindex_accepts_a_query(self, async_client, reindex_setup):
921921
)["_source"]
922922

923923
async def test_all_documents_get_moved(self, async_client, reindex_setup):
924-
await helpers.async_reindex(async_client, "test_index", "prod_index", bulk_kwargs={"refresh": True})
924+
await helpers.async_reindex(
925+
async_client,
926+
"test_index",
927+
"prod_index",
928+
bulk_kwargs={"refresh": "wait_for"},
929+
)
925930

926931
assert await async_client.indices.exists(index="prod_index")
927932
assert (

test_elasticsearch_serverless/test_server/conftest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ def sync_client_factory(elasticsearch_url, elasticsearch_api_key):
3636
try:
3737
# Configure the client with API key and optionally
3838
# an HTTP conn class depending on 'PYTHON_CONNECTION_CLASS' envvar
39-
kw = {'api_key': elasticsearch_api_key}
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.
45-
client = elasticsearch_serverless.Elasticsearch(
46-
elasticsearch_url, **kw
47-
)
45+
client = elasticsearch_serverless.Elasticsearch(elasticsearch_url, **kw)
4846

4947
# Wipe the cluster before we start testing just in case it wasn't wiped
5048
# cleanly from the previous run of pytest?

test_elasticsearch_serverless/test_server/test_helpers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ def test_bulk_actions_remain_unchanged(sync_client):
6666
def test_bulk_all_documents_get_inserted(sync_client):
6767
docs = [{"answer": x, "_id": x} for x in range(100)]
6868
for ok, item in helpers.streaming_bulk(
69-
sync_client, docs, index="test-index", refresh=True,
69+
sync_client,
70+
docs,
71+
index="test-index",
72+
refresh=True,
7073
):
7174
assert ok
7275

@@ -873,7 +876,9 @@ def test_reindex_accepts_a_query(sync_client):
873876

874877
@pytest.mark.usefixtures("reindex_setup")
875878
def test_all_documents_get_moved(sync_client):
876-
helpers.reindex(sync_client, "test_index", "prod_index", bulk_kwargs={"refresh": True})
879+
helpers.reindex(
880+
sync_client, "test_index", "prod_index", bulk_kwargs={"refresh": True}
881+
)
877882

878883
assert sync_client.indices.exists(index="prod_index")
879884
assert 50 == sync_client.count(index="prod_index", q="type:questions")["count"]

test_elasticsearch_serverless/test_server/test_rest_api_spec.py

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from elasticsearch_serverless._sync.client.utils import _base64_auth_header
4343
from elasticsearch_serverless.compat import string_types
4444

45-
from ..utils import es_api_key, es_url, parse_version
45+
from ..utils import es_api_key, es_url
4646

4747
# some params had to be changed in python, keep track of them so we can rename
4848
# those in the tests accordingly
@@ -135,7 +135,6 @@
135135

136136

137137
XPACK_FEATURES = None
138-
ES_VERSION = None
139138
RUN_ASYNC_REST_API_TESTS = (
140139
sys.version_info >= (3, 8)
141140
and os.environ.get("PYTHON_CONNECTION_CLASS") == "requests"
@@ -182,16 +181,6 @@ def teardown(self):
182181
self.section("teardown")
183182
self.run_code(self._teardown_code)
184183

185-
def es_version(self):
186-
global ES_VERSION
187-
if ES_VERSION is None:
188-
version_string = (self.client.info())["version"]["number"]
189-
if "." not in version_string:
190-
return ()
191-
version = version_string.strip().split(".")
192-
ES_VERSION = tuple(int(v) if v.isdigit() else 999 for v in version)
193-
return ES_VERSION
194-
195184
def section(self, name):
196185
print(("=" * 10) + " " + name + " " + ("=" * 10))
197186

@@ -340,16 +329,6 @@ def run_skip(self, skip):
340329
continue
341330
pytest.skip(f"feature '{feature}' is not supported")
342331

343-
if "version" in skip:
344-
version, reason = skip["version"], skip["reason"]
345-
if version == "all":
346-
pytest.skip(reason)
347-
min_version, _, max_version = version.partition("-")
348-
min_version = parse_version(min_version.strip()) or (0,)
349-
max_version = parse_version(max_version.strip()) or (999,)
350-
if min_version <= (self.es_version()) <= max_version:
351-
pytest.skip(reason)
352-
353332
def run_gt(self, action):
354333
for key, value in action.items():
355334
value = self._resolve(value)
@@ -553,55 +532,32 @@ def remove_implicit_resolver(cls, tag_to_remove):
553532

554533
# Try loading the REST API test specs from the Elastic Artifacts API
555534
try:
535+
github_token = os.environ.get("GITHUB_TOKEN")
536+
if github_token is None:
537+
raise RuntimeError("GITHUB_TOKEN environment variable is not set")
538+
556539
# Construct the HTTP and Elasticsearch client
557540
http = urllib3.PoolManager(retries=10)
558541
client = Elasticsearch(es_url(), api_key=es_api_key(), request_timeout=3)
559542

560-
# Make a request to Elasticsearch for the build hash, we'll be looking for
561-
# an artifact with this same hash to download test specs for.
562-
client_info = client.info()
563-
version_number = client_info["version"]["number"]
564-
build_hash = client_info["version"]["build_hash"]
565-
566-
# Now talk to the artifacts API with the 'STACK_VERSION' environment variable
567-
resp = http.request(
568-
"GET",
569-
f"https://artifacts-api.elastic.co/v1/versions/{version_number}",
570-
)
571-
resp = json.loads(resp.data.decode("utf-8"))
572-
573-
# Look through every build and see if one matches the commit hash
574-
# we're looking for. If not it's okay, we'll just use the latest and
575-
# hope for the best!
576-
builds = resp["version"]["builds"]
577-
for build in builds:
578-
if build["projects"]["elasticsearch"]["commit_hash"] == build_hash:
579-
break
580-
else:
581-
build = builds[0] # Use the latest
582-
583-
# Now we're looking for the 'rest-api-spec-<VERSION>-sources.jar' file
584-
# to download and extract in-memory.
585-
packages = build["projects"]["elasticsearch"]["packages"]
586-
for package in packages:
587-
if re.match(r"rest-resources-zip-.*\.zip", package):
588-
package_url = packages[package]["url"]
589-
break
590-
else:
591-
raise RuntimeError(
592-
f"Could not find the package 'rest-resources-zip-*.zip' in build {build!r}"
593-
)
543+
yaml_tests_url = "https://api.github.com/repos/elastic/serverless-clients-tests/zipball/572c72b5b32f145e62a966563688e46401d3d28a"
594544

595545
# Download the zip and start reading YAML from the files in memory
596546
package_zip = zipfile.ZipFile(
597547
io.BytesIO(
598548
http.request(
599-
"GET", package_url, headers={"content-type": "application/json"}
549+
"GET",
550+
yaml_tests_url,
551+
headers={
552+
"Authorization": f"Bearer {github_token}",
553+
"Accept": "application/vnd.github+json",
554+
},
600555
).data
601556
)
602557
)
558+
603559
for yaml_file in package_zip.namelist():
604-
if not re.match(r"^rest-api-spec/test/.*\.ya?ml$", yaml_file):
560+
if not re.match(r"^.*\/tests\/.*\.ya?ml$", yaml_file):
605561
continue
606562
yaml_tests = list(
607563
yaml.load_all(package_zip.read(yaml_file), Loader=NoDatesSafeLoader)
@@ -660,7 +616,6 @@ def _pytest_param_sort_key(param: pytest.param) -> Tuple[Union[str, int], ...]:
660616
# Sort the tests by ID so they're grouped together nicely.
661617
YAML_TEST_SPECS = sorted(YAML_TEST_SPECS, key=_pytest_param_sort_key)
662618

663-
664619
if not RUN_ASYNC_REST_API_TESTS:
665620

666621
@pytest.mark.parametrize("test_spec", YAML_TEST_SPECS)

0 commit comments

Comments
 (0)