From ac7c6469badebbf53fcadfae6582a128eeb4005a Mon Sep 17 00:00:00 2001 From: Filippo Ledda Date: Wed, 23 Aug 2023 12:19:42 +0200 Subject: [PATCH] #700 #703 Improve local testing + alt schema url --- .../cloudharness_utils/testing/util.py | 4 +- .../cloudharness_test/apitest_init.py | 54 ++++++++++++------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/libraries/cloudharness-utils/cloudharness_utils/testing/util.py b/libraries/cloudharness-utils/cloudharness_utils/testing/util.py index b0e98624..58e20fa0 100644 --- a/libraries/cloudharness-utils/cloudharness_utils/testing/util.py +++ b/libraries/cloudharness-utils/cloudharness_utils/testing/util.py @@ -11,7 +11,9 @@ def get_user_password(main_user: ApplicationUser): def get_app_environment(app_config: ApplicationHarnessConfig, app_domain, use_local_env=True): my_env = os.environ.copy() if use_local_env else {} my_env["APP_URL"] = app_domain - + schema_file = f"applications/{app_config.name}/api/openapi.yaml" + if os.path.exists(schema_file): + my_env["APP_SCHEMA_FILE"] = schema_file if app_config.accounts and app_config.accounts.users: main_user: ApplicationUser = app_config.accounts.users[0] diff --git a/tools/cloudharness-test/cloudharness_test/apitest_init.py b/tools/cloudharness-test/cloudharness_test/apitest_init.py index 4ba65f7e..52d20f53 100644 --- a/tools/cloudharness-test/cloudharness_test/apitest_init.py +++ b/tools/cloudharness-test/cloudharness_test/apitest_init.py @@ -1,44 +1,62 @@ import os import logging +import requests import schemathesis as st from cloudharness.auth import get_token -if "APP_URL" in os.environ: - - app_url = os.environ["APP_URL"] - - try: - openapi_uri = app_url + "/openapi.json" - logging.info("Using openapi spec at %s", openapi_uri) - schema = st.from_uri(openapi_uri) - except Exception as e: - raise Exception(f"Cannot setup api tests: {openapi_uri} not reachable. Check your deployment is up and configuration") from e +if "APP_URL" or "APP_SCHEMA_FILE" in os.environ: + app_schema = os.environ.get("APP_SCHEMA_FILE", None) + app_url = os.environ.get("APP_URL", "http://samples.ch.local/api") + logging.info("Start schemathesis tests on %s", app_url) + if app_schema: + openapi_uri = app_schema + schema = st.from_file(openapi_uri) + else: + try: + openapi_uri = openapi_uri = app_url + "/openapi.json" + schema = st.from_file(openapi_uri) + except st.exceptions.SchemaLoadingError as e: + # Use alternative configuration + try: + openapi_uri = app_url.replace("/api", "") + "/openapi.json" + print(requests.get(openapi_uri)) + schema = st.from_uri(openapi_uri) + except st.exceptions.SchemaLoadingError as e: + raise Exception( + f"Cannot setup api tests: {openapi_uri} not valid. Check your deployment is up and configuration") from e + + except Exception as e: + raise Exception( + f"Cannot setup api tests: {openapi_uri}: {e}") from e + + logging.info("Using openapi spec at %s", openapi_uri) if "USERNAME" in os.environ and "PASSWORD" in os.environ: logging.info("Setting token from username and password") + @st.auth.register() class TokenAuth: def get(self, context): - + username = os.environ["USERNAME"] - password = os.environ["PASSWORD"] - + password = os.environ["PASSWORD"] + return get_token(username, password) def set(self, case, data, context): case.headers = case.headers or {} - case.headers["Authorization"] = f"Bearer {data}" - case.headers["Cookie"] = f"kc-access={data}" + case.headers["Authorization"] = f"Bearer {data}" + case.headers["Cookie"] = f"kc-access={data}" else: @st.auth.register() class TokenAuth: def get(self, context): - + return "" def set(self, case, data, context): case.headers = case.headers or {} - case.headers["Authorization"] = f"Bearer {data}" - case.headers["Cookie"] = f"kc-access={data}" \ No newline at end of file + case.headers["Authorization"] = f"Bearer {data}" + case.headers["Cookie"] = f"kc-access={data}"