diff --git a/Makefile b/Makefile index 9dd1113b..e1628357 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,17 @@ PYTHON ?= python BASE_URL ?= http://localhost:8080 CA_FILE ?= ACTION_SCRIPT ?= +SESSION_TTL_SECONDS ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence test-failover +.PHONY: help check validate test load test-persistence test-failover test-expiry test-dependency collect-evidence up down help: @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" + @echo "test-persistence/test-failover: ACTION_SCRIPT=scripts/restart.sh|scripts/stop-one.sh" + @echo "test-expiry: SESSION_TTL_SECONDS=2 (стенд должен быть запущен с коротким TTL)" + @echo "test-dependency: остановка Redis/PostgreSQL через scripts/stop-*.sh" + @echo "collect-evidence: собрать evidence/*.txt (Windows: sh scripts/collect-evidence.sh)" check: $(PYTHON) scripts/check_contract.py @@ -22,9 +27,24 @@ load: $(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS) test-persistence: - @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) + $(if $(ACTION_SCRIPT),,$(error Set ACTION_SCRIPT to your implemented executable restart hook)) $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) test-failover: - @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) + $(if $(ACTION_SCRIPT),,$(error Set ACTION_SCRIPT to your implemented executable stop-one hook)) $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +test-expiry: + $(PYTHON) tests/test_expiry.py --base-url "$(BASE_URL)" $(if $(SESSION_TTL_SECONDS),--ttl $(SESSION_TTL_SECONDS),) + +test-dependency: + $(PYTHON) tests/test_dependency_failure.py --base-url "$(BASE_URL)" + +collect-evidence: + sh scripts/collect-evidence.sh + +up: + docker compose --env-file .env up -d --build + +down: + docker compose --env-file .env down diff --git a/tests/resilience.py b/tests/resilience.py index f3fa700a..b7ca5969 100644 --- a/tests/resilience.py +++ b/tests/resilience.py @@ -2,6 +2,7 @@ """Explicit operator-authored hook; tokens remain in memory, volumes are never removed by this test.""" import json import os +import shutil from pathlib import Path import subprocess import time @@ -10,6 +11,25 @@ from urllib.parse import quote from client import Client, Failure, LAB, arguments, check, run +def _script_command(full: Path) -> list[str]: + """Windows не умеет запускать .sh напрямую — вызываем через sh (Git Bash/WSL).""" + if os.name == "nt": + sh = shutil.which("sh") or shutil.which("bash") + if not sh: + raise Failure( + "sh/bash not found in PATH; install Git for Windows or run under WSL" + ) + return [sh, str(full)] + return [str(full)] + + +def _is_executable(full: Path) -> bool: + """На Windows .sh не имеет executable bit — проверяем через sh.""" + if os.name == "nt": + return full.is_file() and (shutil.which("sh") or shutil.which("bash")) is not None + return full.is_file() and os.access(full, os.X_OK) + + def history(c, actor, chat): path = '/api/v1/chats/'+chat['id']+'/messages?limit=100' result = [] @@ -34,7 +54,7 @@ def main(): check(LAB>=2,'persistence requires lab2+') check(args.mode!='failover' or LAB>=3,'failover requires lab3+') script = Path(args.action_script).expanduser().resolve() - check(script.is_file() and os.access(script,os.X_OK),'action script must exist and be executable (chmod +x)') + check(_is_executable(script),'action script must exist and be executable (chmod +x)') check('.example.' not in script.name,'copy and implement the example hook first') c = Client(args.base_url,args.ca_file,timeout=2) alice,bob,eve,chat = c.bootstrap() @@ -55,7 +75,7 @@ def main(): check(len(observed)>=2,'less than two API instances observed before failure; verify LB/session affinity') print('Executing explicit '+args.mode+' hook; hook output suppressed to avoid leaking secrets.') # No shell interpolation and no Docker commands in this test. - proc = subprocess.Popen([str(script)],env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) + proc = subprocess.Popen(_script_command(script),env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) started = time.monotonic() successes,failures,after_instances = [],0,[] try: