diff --git a/README.md b/README.md index 02cbf40..9bb96d5 100644 --- a/README.md +++ b/README.md @@ -2,39 +2,40 @@ Test suite for RHOS Lightspeed in RHOSO environments. -## Setup +## Installation + +Python 3.12 is required. Install from a cloned repo: ```bash -uv venv uv sync +source .venv/bin/activate ``` -## Configuration - -Configure the service in `config/default.yaml`. For Goose API tests, set -`goose.model` to the value of the Goose container's `LIGHTSPEED_MODEL`: +Or install the standalone command directly from GitHub: -```yaml -rhos_lightspeed: - base_url: "http://localhost:8080" - timeout: 30 +```bash +uv tool install --python 3.12 git+https://github.com/openstack-k8s-operators/lightspeed-tests.git +``` -query: - default_question: "What is OpenStack?" +The package includes the tests and their default configuration. Once installed, +`lightspeed-tests` works from any directory. `lightspeed-test` is also available +as an alias. -goose: - model: "" -``` +## Configuration -Supply the bearer token through the environment: +The tests load the default config from `src/rhos_lightspeed_tests/default.yaml`. +Configure the service through environment variables: ```bash +export RHOS_LIGHTSPEED_URL="https://localhost:8443" export RHOS_LIGHTSPEED_TOKEN="" +export RHOS_LIGHTSPEED_MODEL="" ``` `RHOS_LIGHTSPEED_URL` and `RHOS_LIGHTSPEED_MODEL` can override the URL and model -from the config file. Use a service origin without `/v1`. With the test-operator -playbook, supply `lightspeed_model`. +from the default config. Use a service origin without `/v1`. With the test-operator +playbook, supply `lightspeed_model`. For Goose API tests, set the model to the Goose +container's `LIGHTSPEED_MODEL` value. If you run the tests outside of OpenShift, you need to have a running port forward for the `lightspeed-app-server` service. For example: @@ -45,29 +46,44 @@ oc port-forward -n openstack-lightspeed svc/lightspeed-app-server 8443:8443 ## Running tests +The command accepts pytest options and returns pytest's exit status. In a uv +checkout, prefix the commands below with `uv run`. + ```bash # Run all tests -uv run pytest +lightspeed-tests # Run integration tests only -uv run pytest tests/integration/ +lightspeed-tests -k integration + +# Run one installed test module +lightspeed-tests rhos_lightspeed_tests.tests.integration.test_query_endpoint + +# Run one test file from a checkout +lightspeed-tests src/rhos_lightspeed_tests/tests/integration/test_query_endpoint.py # Run Goose API tests -uv run pytest tests/integration/test_goose_responses.py +lightspeed-tests -k goose # Skip Goose tests that require a real LLM's answer content -uv run pytest tests/integration/test_goose_responses.py -m "not llm_dependent" +lightspeed-tests -k goose -m "not llm_dependent" -# Generate JUnit XML (automatic via pyproject.toml config) -uv run pytest # outputs test-results.xml +# Verify test discovery without contacting a service +lightspeed-tests --collect-only + +# Choose a different JUnit XML report path +lightspeed-tests --junitxml=results.xml ``` +Integration tests require a deployed Lightspeed service. Goose tests also +require a configured model. + ## Adding tests 1. Create test file in appropriate directory: - - `tests/unit/` - Fast, isolated tests - - `tests/integration/` - Tests requiring deployed service - - `tests/e2e/` - Full end-to-end scenarios + - `src/rhos_lightspeed_tests/tests/unit/` - Fast, isolated suite tests + - `src/rhos_lightspeed_tests/tests/integration/` - Tests requiring deployed service + - `src/rhos_lightspeed_tests/tests/e2e/` - Full end-to-end scenarios 2. Use the shared fixtures from `conftest.py`: ```python @@ -77,24 +93,25 @@ uv run pytest # outputs test-results.xml assert response.status_code == 200 ``` -3. Run: `uv run pytest tests/integration/test_my_feature.py` +3. Run: `uv run lightspeed-tests -k test_my_feature` + +Build a source distribution and wheel with `uv build`. ## Project structure ``` rhos-lightspeed-tests/ -├── config/ -│ └── default.yaml # Default test configuration -├── src/rhos_lightspeed_tests/ # Shared utilities -│ ├── __init__.py +├── src/rhos_lightspeed_tests/ # Installed package +│ ├── __init__.py # lightspeed-tests entry point │ ├── client.py # API client (query, health) -│ └── config.py # Reads default.yaml, env var overrides -├── tests/ -│ ├── conftest.py # Shared fixtures (config, client) -│ ├── unit/ -│ ├── integration/ -│ │ └── test_query_endpoint.py # First test: /query endpoint -│ └── e2e/ +│ ├── config.py # Loads defaults and environment overrides +│ ├── default.yaml # Bundled default configuration +│ ├── pytest.ini # Bundled pytest settings and default suite +│ └── tests/ +│ ├── conftest.py # Shared fixtures (config, client) +│ ├── unit/ +│ ├── integration/ +│ └── e2e/ ├── pyproject.toml └── README.md ``` diff --git a/playbooks/README.md b/playbooks/README.md index abc0bb5..e04ba94 100644 --- a/playbooks/README.md +++ b/playbooks/README.md @@ -4,14 +4,14 @@ Ansible playbooks to run the test suite. ## `run_lightspeed_tests.yaml` -Executes the RHOS Lightspeed test suite using pytest. The playbook: +Executes the RHOS Lightspeed test suite using `lightspeed-tests`. The playbook: 1. Installs the [uv](https://astral.sh/uv) package manager if not already available 2. Installs Python 3.12 using uv 3. Verifies the test suite directory exists (cloned by test-operator) 4. Reads the Kubernetes service account token for authentication 5. Installs Python dependencies using uv -6. Runs the pytest test suite with configurable arguments +6. Runs `lightspeed-tests` with configurable pytest arguments 7. Generates JUnit XML test results for reporting Test results are stored in the location specified by `junit_xml_path`, which the test-operator can consume for reporting. diff --git a/playbooks/run_lightspeed_tests.yaml b/playbooks/run_lightspeed_tests.yaml index 09c6c06..fdf2bc1 100644 --- a/playbooks/run_lightspeed_tests.yaml +++ b/playbooks/run_lightspeed_tests.yaml @@ -97,10 +97,10 @@ PYTHONUNBUFFERED: "1" PATH: "{{ ansible_env.HOME }}/.local/bin:{{ ansible_env.PATH }}" - - name: Run pytest tests + - name: Run Lightspeed tests ansible.builtin.command: cmd: > - {{ ansible_env.HOME }}/.local/bin/uv run pytest + {{ ansible_env.HOME }}/.local/bin/uv run lightspeed-tests {{ pytest_args }} --junitxml={{ junit_xml_path }} chdir: "{{ test_suite_path }}" diff --git a/pyproject.toml b/pyproject.toml index e666c28..61adce8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,10 @@ dependencies = [ "pyyaml>=6.0.0", ] +[project.scripts] +lightspeed-tests = "rhos_lightspeed_tests:main" +lightspeed-test = "rhos_lightspeed_tests:main" + [dependency-groups] dev = [ "ruff>=0.16.1", @@ -24,15 +28,6 @@ dev = [ requires = ["uv_build>=0.12.3,<0.13"] build-backend = "uv_build" -[tool.pytest.ini_options] -testpaths = ["tests"] -addopts = "--verbose --junitxml=test-results.xml" -python_files = "test_*.py" -python_functions = "test_*" -markers = [ - "llm_dependent: requires generated answer content from a real LLM", -] - [tool.ruff] line-length = 88 target-version = "py312" @@ -41,4 +36,4 @@ target-version = "py312" extend-select = ["S"] [tool.ruff.lint.per-file-ignores] -"tests/**/*.py" = ["S101"] +"src/rhos_lightspeed_tests/tests/**/*.py" = ["S101"] diff --git a/src/rhos_lightspeed_tests/__init__.py b/src/rhos_lightspeed_tests/__init__.py index d25a5c7..5b95cf7 100644 --- a/src/rhos_lightspeed_tests/__init__.py +++ b/src/rhos_lightspeed_tests/__init__.py @@ -1 +1,21 @@ -"""RHOS Lightspeed test utilities.""" +"""RHOS Lightspeed test suite and shared utilities.""" + +import sys +from pathlib import Path + +import pytest + + +def main() -> int: + """Run selected tests or the bundled suite, returning pytest's exit status.""" + return pytest.main( + [ + "-c", + str(Path(__file__).with_name("pytest.ini")), + # Apply testpaths from any working directory and keep the cache there. + "--rootdir", + str(Path.cwd()), + "--pyargs", + *sys.argv[1:], + ] + ) diff --git a/src/rhos_lightspeed_tests/config.py b/src/rhos_lightspeed_tests/config.py index db23795..13b4eea 100644 --- a/src/rhos_lightspeed_tests/config.py +++ b/src/rhos_lightspeed_tests/config.py @@ -5,9 +5,7 @@ import yaml -DEFAULT_CONFIG_PATH = ( - Path(__file__).resolve().parent.parent.parent / "config" / "default.yaml" -) +DEFAULT_CONFIG_PATH = Path(__file__).with_name("default.yaml") def load_config(config_path: str | None = None) -> dict: diff --git a/config/default.yaml b/src/rhos_lightspeed_tests/default.yaml similarity index 100% rename from config/default.yaml rename to src/rhos_lightspeed_tests/default.yaml diff --git a/src/rhos_lightspeed_tests/pytest.ini b/src/rhos_lightspeed_tests/pytest.ini new file mode 100644 index 0000000..76197f1 --- /dev/null +++ b/src/rhos_lightspeed_tests/pytest.ini @@ -0,0 +1,8 @@ +[pytest] +# Used only when no test path, module or node ID is supplied on the command line. +testpaths = rhos_lightspeed_tests.tests +addopts = --verbose --junitxml=test-results.xml +python_files = test_*.py +python_functions = test_* +markers = + llm_dependent: requires generated answer content from a real LLM diff --git a/tests/__init__.py b/src/rhos_lightspeed_tests/tests/__init__.py similarity index 100% rename from tests/__init__.py rename to src/rhos_lightspeed_tests/tests/__init__.py diff --git a/tests/conftest.py b/src/rhos_lightspeed_tests/tests/conftest.py similarity index 100% rename from tests/conftest.py rename to src/rhos_lightspeed_tests/tests/conftest.py diff --git a/tests/e2e/__init__.py b/src/rhos_lightspeed_tests/tests/e2e/__init__.py similarity index 100% rename from tests/e2e/__init__.py rename to src/rhos_lightspeed_tests/tests/e2e/__init__.py diff --git a/tests/integration/__init__.py b/src/rhos_lightspeed_tests/tests/integration/__init__.py similarity index 100% rename from tests/integration/__init__.py rename to src/rhos_lightspeed_tests/tests/integration/__init__.py diff --git a/tests/integration/test_goose_responses.py b/src/rhos_lightspeed_tests/tests/integration/test_goose_responses.py similarity index 99% rename from tests/integration/test_goose_responses.py rename to src/rhos_lightspeed_tests/tests/integration/test_goose_responses.py index a98f5be..b32dbd5 100644 --- a/tests/integration/test_goose_responses.py +++ b/src/rhos_lightspeed_tests/tests/integration/test_goose_responses.py @@ -18,7 +18,7 @@ repeating it in the follow-up. This requires generated answer content and is marked llm_dependent. Select the other two scenarios with: - pytest tests/integration/test_goose_responses.py -m "not llm_dependent" + lightspeed-tests -k goose -m "not llm_dependent" This selection still requires a deployed Lightspeed service and a configured LLM backend, which can be real or mocked. It does not configure the mock. diff --git a/tests/integration/test_metrics_endpoint.py b/src/rhos_lightspeed_tests/tests/integration/test_metrics_endpoint.py similarity index 100% rename from tests/integration/test_metrics_endpoint.py rename to src/rhos_lightspeed_tests/tests/integration/test_metrics_endpoint.py diff --git a/tests/integration/test_okp_virtualization_filter.py b/src/rhos_lightspeed_tests/tests/integration/test_okp_virtualization_filter.py similarity index 100% rename from tests/integration/test_okp_virtualization_filter.py rename to src/rhos_lightspeed_tests/tests/integration/test_okp_virtualization_filter.py diff --git a/tests/integration/test_query_endpoint.py b/src/rhos_lightspeed_tests/tests/integration/test_query_endpoint.py similarity index 100% rename from tests/integration/test_query_endpoint.py rename to src/rhos_lightspeed_tests/tests/integration/test_query_endpoint.py diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py deleted file mode 100644 index e69de29..0000000