Skip to content

Commit b6cfc71

Browse files
committed
WIP: fetch YAML tests from elastic/serverless-clients-tests
1 parent 7b2bd8f commit b6cfc71

File tree

1 file changed

+11
-59
lines changed

1 file changed

+11
-59
lines changed

test_elasticsearch_serverless/test_server/test_rest_api_spec.py

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -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,30 @@ 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", yaml_tests_url, headers={
550+
"Authorization": f"Bearer {github_token}",
551+
"Accept": "application/vnd.github+json",
552+
}
600553
).data
601554
)
602555
)
556+
603557
for yaml_file in package_zip.namelist():
604-
if not re.match(r"^rest-api-spec/test/.*\.ya?ml$", yaml_file):
558+
if not re.match(r"^tests/.*\.ya?ml$", yaml_file):
605559
continue
606560
yaml_tests = list(
607561
yaml.load_all(package_zip.read(yaml_file), Loader=NoDatesSafeLoader)
@@ -660,9 +614,7 @@ def _pytest_param_sort_key(param: pytest.param) -> Tuple[Union[str, int], ...]:
660614
# Sort the tests by ID so they're grouped together nicely.
661615
YAML_TEST_SPECS = sorted(YAML_TEST_SPECS, key=_pytest_param_sort_key)
662616

663-
664617
if not RUN_ASYNC_REST_API_TESTS:
665-
666618
@pytest.mark.parametrize("test_spec", YAML_TEST_SPECS)
667619
def test_rest_api_spec(test_spec, sync_runner):
668620
if test_spec.get("skip", False):

0 commit comments

Comments
 (0)