diff --git a/README.md b/README.md index 9bb96d5..52b240e 100644 --- a/README.md +++ b/README.md @@ -44,15 +44,51 @@ for the `lightspeed-app-server` service. For example: oc port-forward -n openstack-lightspeed svc/lightspeed-app-server 8443:8443 ``` +Telemetry tests are excluded by default. To run them, deploy Lightspeed and +enable its metrics collection through telemetry-operator. Configure the +Lightspeed URL and token above, then set a reachable Prometheus URL. TLS +verification is enabled by default. For an internal CA, set +`RHOS_LIGHTSPEED_PROMETHEUS_CA_CERT_PATH` (`prometheus.ca_cert_path`) to its +PEM CA bundle: + +```bash +export RHOS_LIGHTSPEED_PROMETHEUS_URL="https://metric-storage-prometheus.openstack.svc:9090" +export RHOS_LIGHTSPEED_PROMETHEUS_CA_CERT_PATH="/path/to/prometheus-ca.pem" +lightspeed-tests -m telemetry +``` + +If you run the telemetry test outside of OpenShift, you need to have a running port forward +for the `metric-storage-prometheus` service. For example: + +``` +oc port-forward -n openstack svc/metric-storage-prometheus 9090:9090 +``` + +Map `metric-storage-prometheus.openstack.svc` to `127.0.0.1` in your local +hosts file and keep the service hostname in the URL above so TLS hostname +verification succeeds. Export the OpenStack control plane CA bundle locally: + +```bash +oc -n openstack get secret combined-ca-bundle \ + -o jsonpath='{.data.tls-ca-bundle\.pem}' | base64 --decode > prometheus-ca.pem +export RHOS_LIGHTSPEED_PROMETHEUS_CA_CERT_PATH="$PWD/prometheus-ca.pem" +``` + ## Running tests The command accepts pytest options and returns pytest's exit status. In a uv -checkout, prefix the commands below with `uv run`. +checkout, prefix the commands below with `uv run`. An explicit `-m` expression +replaces the default `not telemetry` filter. For example, `-m "not llm_dependent"` +also includes telemetry; use `-m "not llm_dependent and not telemetry"` to exclude +both groups. ```bash -# Run all tests +# Run the default suite (excludes telemetry) lightspeed-tests +# Run all tests, including telemetry (requires Prometheus configuration) +lightspeed-tests -m "" + # Run integration tests only lightspeed-tests -k integration diff --git a/playbooks/README.md b/playbooks/README.md index e04ba94..112a0ad 100644 --- a/playbooks/README.md +++ b/playbooks/README.md @@ -26,6 +26,10 @@ All parameters can be overridden via the `AnsibleTest` custom resource when runn * `pytest_args`: (String) Additional arguments to pass to pytest. Default value: `-v --tb=short --color=yes` * `junit_xml_path`: (String) Path where JUnit XML test results will be written. The test-operator provides writable storage at this location. Default value: `/var/lib/AnsibleTests/external_files/test-results.xml` * `test_suite_path`: (String) Path to the test suite directory. The test-operator clones git repositories to this location. Default value: `{{ ansible_env.HOME }}/ansible` +* `prometheus_url`: (String) Telemetry Prometheus URL. Empty by default; required when selecting telemetry tests. +* `prometheus_ca_cert_path`: (String) Path inside the test pod to a PEM CA bundle for Prometheus. Empty by default, which uses Requests' default trust store. TLS verification is always enabled. +* `prometheus_api_timeout`: (Number) Timeout in seconds for each Prometheus API request. Default: `10`. +* `prometheus_scrape_timeout`: (Number) Time in seconds to poll for a positive collected LLM call counter. Default: `120`. ### Usage diff --git a/playbooks/run_lightspeed_tests.yaml b/playbooks/run_lightspeed_tests.yaml index fdf2bc1..659b69d 100644 --- a/playbooks/run_lightspeed_tests.yaml +++ b/playbooks/run_lightspeed_tests.yaml @@ -8,6 +8,11 @@ lightspeed_timeout: 30 # Match the Goose container's LIGHTSPEED_MODEL for the Goose API tests. lightspeed_model: "" + # Required when pytest_args selects telemetry tests. + prometheus_url: "" + prometheus_ca_cert_path: "" + prometheus_api_timeout: 10 + prometheus_scrape_timeout: 120 test_question: "What is OpenStack?" pytest_args: "-v --tb=short --color=yes" # Test-operator provides writable storage at this location @@ -94,6 +99,10 @@ RHOS_LIGHTSPEED_URL: "{{ lightspeed_url }}" RHOS_LIGHTSPEED_TOKEN: "{{ lightspeed_token }}" RHOS_LIGHTSPEED_MODEL: "{{ lightspeed_model }}" + RHOS_LIGHTSPEED_PROMETHEUS_URL: "{{ prometheus_url }}" + RHOS_LIGHTSPEED_PROMETHEUS_CA_CERT_PATH: "{{ prometheus_ca_cert_path }}" + RHOS_LIGHTSPEED_PROMETHEUS_API_TIMEOUT: "{{ prometheus_api_timeout | string }}" + RHOS_LIGHTSPEED_PROMETHEUS_SCRAPE_TIMEOUT: "{{ prometheus_scrape_timeout | string }}" PYTHONUNBUFFERED: "1" PATH: "{{ ansible_env.HOME }}/.local/bin:{{ ansible_env.PATH }}" diff --git a/src/rhos_lightspeed_tests/config.py b/src/rhos_lightspeed_tests/config.py index 13b4eea..9cbd5ae 100644 --- a/src/rhos_lightspeed_tests/config.py +++ b/src/rhos_lightspeed_tests/config.py @@ -26,4 +26,25 @@ def load_config(config_path: str | None = None) -> dict: goose = config.setdefault("goose", {}) goose["model"] = os.environ.get("RHOS_LIGHTSPEED_MODEL", goose.get("model", "")) + prometheus = config.setdefault("prometheus", {}) + prometheus["url"] = os.environ.get( + "RHOS_LIGHTSPEED_PROMETHEUS_URL", prometheus.get("url", "") + ) + prometheus["ca_cert_path"] = os.environ.get( + "RHOS_LIGHTSPEED_PROMETHEUS_CA_CERT_PATH", + prometheus.get("ca_cert_path", ""), + ) + prometheus["api_timeout"] = float( + os.environ.get( + "RHOS_LIGHTSPEED_PROMETHEUS_API_TIMEOUT", + prometheus.get("api_timeout", 10), + ) + ) + prometheus["scrape_timeout"] = float( + os.environ.get( + "RHOS_LIGHTSPEED_PROMETHEUS_SCRAPE_TIMEOUT", + prometheus.get("scrape_timeout", 120), + ) + ) + return config diff --git a/src/rhos_lightspeed_tests/default.yaml b/src/rhos_lightspeed_tests/default.yaml index 97cf356..ec14f2c 100644 --- a/src/rhos_lightspeed_tests/default.yaml +++ b/src/rhos_lightspeed_tests/default.yaml @@ -10,3 +10,12 @@ query: goose: # Set RHOS_LIGHTSPEED_MODEL to the Goose container's LIGHTSPEED_MODEL value. model: "" + +prometheus: + # Required when running tests with -m telemetry. + url: "" + # Optional PEM CA bundle; empty uses Requests' default trust store. + ca_cert_path: "" + # Timeout in seconds for each Prometheus API request. + api_timeout: 10 + scrape_timeout: 120 diff --git a/src/rhos_lightspeed_tests/pytest.ini b/src/rhos_lightspeed_tests/pytest.ini index 76197f1..2c106af 100644 --- a/src/rhos_lightspeed_tests/pytest.ini +++ b/src/rhos_lightspeed_tests/pytest.ini @@ -1,8 +1,9 @@ [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 +addopts = --verbose --junitxml=test-results.xml -m "not telemetry" python_files = test_*.py python_functions = test_* markers = + telemetry: requires telemetry-operator metrics collection and a configured Prometheus URL llm_dependent: requires generated answer content from a real LLM diff --git a/src/rhos_lightspeed_tests/tests/e2e/test_telemetry_metrics.py b/src/rhos_lightspeed_tests/tests/e2e/test_telemetry_metrics.py new file mode 100644 index 0000000..30c55cd --- /dev/null +++ b/src/rhos_lightspeed_tests/tests/e2e/test_telemetry_metrics.py @@ -0,0 +1,48 @@ +"""Verify that telemetry's Prometheus collects metrics from a Lightspeed query.""" + +import time + +import pytest +import requests + + +def _get_llm_calls_total(prometheus: dict) -> float: + """Read the total collected LLM calls from telemetry's Prometheus.""" + query_url = prometheus["url"].rstrip("/") + "/api/v1/query" + + with requests.get( + query_url, + params={"query": "sum(ls_llm_calls_total)"}, + verify=prometheus["ca_cert_path"] or True, + timeout=prometheus["api_timeout"], + ) as response: + response.raise_for_status() + samples = response.json()["data"]["result"] + + if not samples: + return 0 + + _timestamp, value = samples[0]["value"] + return float(value) + + +@pytest.mark.telemetry +def test_query_metrics_collected_by_telemetry(client, config): + """Send a query and check that telemetry has collected a positive LLM counter.""" + prometheus = config["prometheus"] + + question = config["query"]["default_question"] + with client.query(question) as response: + response.raise_for_status() + + deadline = time.monotonic() + prometheus["scrape_timeout"] + llm_calls_total = 0 + while time.monotonic() < deadline: + llm_calls_total = _get_llm_calls_total(prometheus) + if llm_calls_total > 0: + break + time.sleep(5) + + assert llm_calls_total > 0, ( + "Prometheus did not collect a positive ls_llm_calls_total" + )