2024-08-12 11:21:40 -05:00
|
|
|
import concurrent.futures
|
|
|
|
import glob
|
|
|
|
import json
|
|
|
|
import logging
|
2024-12-12 09:41:20 -06:00
|
|
|
import time
|
2024-08-12 11:21:40 -05:00
|
|
|
|
|
|
|
from deepdiff import DeepDiff
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def load_tests(fixture_glob="tests/openapi/**/*.json"):
|
|
|
|
for test_fixture in glob.glob(fixture_glob):
|
|
|
|
with open(test_fixture) as f:
|
|
|
|
yield (test_fixture, json.load(f))
|
|
|
|
|
|
|
|
|
2024-09-27 11:31:25 -05:00
|
|
|
def execute_request(auth_session, request):
|
2024-08-12 11:21:40 -05:00
|
|
|
if "method" in request:
|
|
|
|
method = request.pop("method")
|
|
|
|
else:
|
|
|
|
method = "post"
|
|
|
|
|
2024-09-27 11:31:25 -05:00
|
|
|
url = auth_session.gms_url() + request.pop("url")
|
2024-08-12 11:21:40 -05:00
|
|
|
|
2024-09-27 11:31:25 -05:00
|
|
|
return getattr(auth_session, method)(url, **request)
|
2024-08-12 11:21:40 -05:00
|
|
|
|
|
|
|
|
2024-09-27 11:31:25 -05:00
|
|
|
def evaluate_test(auth_session, test_name, test_data):
|
2024-08-12 11:21:40 -05:00
|
|
|
try:
|
|
|
|
for idx, req_resp in enumerate(test_data):
|
|
|
|
if "description" in req_resp["request"]:
|
|
|
|
description = req_resp["request"].pop("description")
|
|
|
|
else:
|
|
|
|
description = None
|
2024-12-12 09:41:20 -06:00
|
|
|
if "wait" in req_resp["request"]:
|
|
|
|
time.sleep(int(req_resp["request"]["wait"]))
|
|
|
|
continue
|
2024-08-12 11:21:40 -05:00
|
|
|
url = req_resp["request"]["url"]
|
2024-09-27 11:31:25 -05:00
|
|
|
actual_resp = execute_request(auth_session, req_resp["request"])
|
2024-08-12 11:21:40 -05:00
|
|
|
try:
|
|
|
|
if "response" in req_resp and "status_codes" in req_resp["response"]:
|
|
|
|
assert (
|
|
|
|
actual_resp.status_code in req_resp["response"]["status_codes"]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
assert actual_resp.status_code in [200, 202, 204]
|
|
|
|
if "response" in req_resp:
|
|
|
|
if "json" in req_resp["response"]:
|
|
|
|
if "exclude_regex_paths" in req_resp["response"]:
|
|
|
|
exclude_regex_paths = req_resp["response"][
|
|
|
|
"exclude_regex_paths"
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
exclude_regex_paths = []
|
|
|
|
diff = DeepDiff(
|
|
|
|
actual_resp.json(),
|
|
|
|
req_resp["response"]["json"],
|
|
|
|
exclude_regex_paths=exclude_regex_paths,
|
2024-08-15 10:14:29 -05:00
|
|
|
ignore_order=True,
|
2024-08-12 11:21:40 -05:00
|
|
|
)
|
|
|
|
assert not diff
|
|
|
|
else:
|
|
|
|
logger.warning("No expected response json found")
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(
|
|
|
|
f"Error executing step: {idx}, url: {url}, test: {test_name}"
|
|
|
|
)
|
|
|
|
if description:
|
|
|
|
logger.error(f"Step {idx} Description: {description}")
|
|
|
|
logger.error(f"Response content: {actual_resp.content}")
|
|
|
|
raise e
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error executing test: {test_name}")
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
2024-09-27 11:31:25 -05:00
|
|
|
def run_tests(auth_session, fixture_globs, num_workers=3):
|
2024-08-12 11:21:40 -05:00
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
|
|
|
|
futures = []
|
2024-08-15 10:14:29 -05:00
|
|
|
for fixture_glob in fixture_globs:
|
|
|
|
for test_fixture, test_data in load_tests(fixture_glob=fixture_glob):
|
2024-09-27 11:31:25 -05:00
|
|
|
futures.append(
|
|
|
|
executor.submit(
|
|
|
|
evaluate_test, auth_session, test_fixture, test_data
|
|
|
|
)
|
|
|
|
)
|
2024-08-12 11:21:40 -05:00
|
|
|
|
|
|
|
for future in concurrent.futures.as_completed(futures):
|
|
|
|
logger.info(future.result())
|
|
|
|
|
|
|
|
|
2024-09-27 11:31:25 -05:00
|
|
|
def test_openapi_all(auth_session):
|
|
|
|
run_tests(auth_session, fixture_globs=["tests/openapi/*/*.json"], num_workers=10)
|
2024-08-12 11:21:40 -05:00
|
|
|
|
|
|
|
|
2024-09-27 11:31:25 -05:00
|
|
|
# def test_openapi_v1(auth_session):
|
|
|
|
# run_tests(auth_session, fixture_globs=["tests/openapi/v1/*.json"], num_workers=4)
|
2024-08-12 11:21:40 -05:00
|
|
|
#
|
|
|
|
#
|
2024-09-27 11:31:25 -05:00
|
|
|
# def test_openapi_v2(auth_session):
|
|
|
|
# run_tests(auth_session, fixture_globs=["tests/openapi/v2/*.json"], num_workers=4)
|
2024-08-12 11:21:40 -05:00
|
|
|
#
|
|
|
|
#
|
2024-09-27 11:31:25 -05:00
|
|
|
# def test_openapi_v3(auth_session):
|
|
|
|
# run_tests(auth_session, fixture_globs=["tests/openapi/v3/*.json"], num_workers=4)
|