From a3f892a975c800afbd188e9f0197f92856201d8d Mon Sep 17 00:00:00 2001 From: Israel Weinberg Date: Wed, 8 Jan 2025 17:25:59 +0200 Subject: [PATCH] Refactor test scripts and improve documentation comments --- tests/fixtures/policy_stores.py | 2 +- tests/genopalkeys.sh | 2 ++ tests/install_opal.sh | 7 ++++--- tests/run.sh | 26 -------------------------- tests/utils.py | 9 +++++++-- 5 files changed, 14 insertions(+), 32 deletions(-) diff --git a/tests/fixtures/policy_stores.py b/tests/fixtures/policy_stores.py index 8838fa833..c07980e75 100644 --- a/tests/fixtures/policy_stores.py +++ b/tests/fixtures/policy_stores.py @@ -1,10 +1,10 @@ import pytest -from images import cedar_image, opa_image from testcontainers.core.network import Network from tests.containers.cedar_container import CedarContainer from tests.containers.opa_container import OpaContainer, OpaSettings from tests.containers.settings.cedar_settings import CedarSettings +from tests.fixtures.images import cedar_image, opa_image @pytest.fixture(scope="session") diff --git a/tests/genopalkeys.sh b/tests/genopalkeys.sh index b57a2ce06..23256cdd5 100644 --- a/tests/genopalkeys.sh +++ b/tests/genopalkeys.sh @@ -1,3 +1,5 @@ +# This is utility script to generate OPAL keys - Use it for your needs + # This function generates a pair of RSA keys using ssh-keygen, extracts the public key into OPAL_AUTH_PUBLIC_KEY, # formats the private key by replacing newlines with underscores and stores it in OPAL_AUTH_PRIVATE_KEY, # and then removes the key files. It outputs messages indicating the start and completion of key generation. diff --git a/tests/install_opal.sh b/tests/install_opal.sh index e4740f146..c51db1c82 100644 --- a/tests/install_opal.sh +++ b/tests/install_opal.sh @@ -1,7 +1,8 @@ +# This is a helper script to install opal-server and opal-client - # Installs opal-server and opal-client using pip. - # If the installation fails or the commands are not available, - # it exits with an error message. +# Installs opal-server and opal-client using pip. +# If the installation fails or the commands are not available, +# it exits with an error message. function install_opal_server_and_client { echo "- Installing opal-server and opal-client from pip..." diff --git a/tests/run.sh b/tests/run.sh index 11a2b1b75..38922a912 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -6,31 +6,8 @@ if [[ -f ".env" ]]; then source .env fi - -# Deletes pytest-generated .env files so they don't interfere with other tests. -function cleanup { - - PATTERN="pytest_[a-f,0-9]*.env" - echo "Looking for auto-generated .env files matching pattern '$PATTERN'..." - - for file in $PATTERN; do - if [[ -f "$file" ]]; then - echo "Deleting file: $file" - rm "$file" - else - echo "No matching files found for pattern '$PATTERN'." - break - fi - done - - echo "Cleanup complete!\n" -} - function main { - # Cleanup before starting, maybe some leftovers from previous runs - cleanup - echo "Running tests..." # Check if a specific test is provided @@ -43,9 +20,6 @@ function main { fi echo "Done!" - - # Cleanup at the end - cleanup } main "$@" diff --git a/tests/utils.py b/tests/utils.py index d58d24c37..2cab0962d 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -57,8 +57,13 @@ def build_docker_image(docker_file: str, image_name: str, session_matrix: dict): image = docker_client.images.get(image_name) if not image: - # context_path=os.path.join(os.path.dirname(__file__), ".."), # Expands the context - context_path = ".." + if "tests" in os.path.abspath(__file__): + logger.info(f"Right now the file is {os.path.abspath(__file__)}") + context_path = os.path.abspath( + os.path.join(os.path.dirname(__file__), "..", "..", "opal") + ) + else: + context_path = ".." dockerfile_path = os.path.join(os.path.dirname(__file__), "docker", docker_file) logger.info(f"Context path: {context_path}, Dockerfile path: {dockerfile_path}")