Isolate database credentials for agent tests

This commit is contained in:
2026-08-27 18:17:39 +02:00
parent 2df37b671b
commit efce42b967
8 changed files with 110 additions and 3 deletions

View File

@@ -33,6 +33,13 @@ class Settings:
agent_command: str
agent_model: str
test_commands: tuple[str, ...]
test_database_host: str
test_database_port: int
test_database_name: str
test_database_user: str
test_database_password: str
test_customer_database: str
test_config_database: str
poll_seconds: int
max_changed_files: int
max_changed_lines: int
@@ -71,6 +78,13 @@ class Settings:
"composer install --no-interaction --prefer-dist;;php artisan test",
)
),
test_database_host=os.getenv("TEST_DB_HOST", "host.docker.internal"),
test_database_port=int(os.getenv("TEST_DB_PORT", "33060")),
test_database_name=os.getenv("TEST_DB_NAME", "laravel_ai_chat_agent_test"),
test_database_user=os.getenv("TEST_DB_USER", "lai_test_runner"),
test_database_password=os.getenv("TEST_DB_PASSWORD", ""),
test_customer_database=os.getenv("TEST_CUSTOMER_DB", "customer_agent_test"),
test_config_database=os.getenv("TEST_CONFIG_DB", "cockpit_agent_test"),
poll_seconds=int(os.getenv("POLL_SECONDS", "60")),
max_changed_files=int(os.getenv("MAX_CHANGED_FILES", "12")),
max_changed_lines=int(os.getenv("MAX_CHANGED_LINES", "800")),
@@ -87,3 +101,5 @@ class Settings:
raise ValueError(f"{self.mode} requires GITEA_TOKEN")
if not self.database_password:
raise ValueError("FAILURE_DB_PASSWORD is required")
if not self.test_database_password:
raise ValueError("TEST_DB_PASSWORD is required")

View File

@@ -5,6 +5,7 @@ import pwd
import shlex
import shutil
import subprocess
from collections.abc import Mapping
from dataclasses import dataclass
from pathlib import Path
from urllib.parse import urlparse
@@ -46,8 +47,9 @@ class GitWorkspace:
command: str,
check: bool = True,
username: str | None = None,
extra_environment: Mapping[str, str] | None = None,
) -> subprocess.CompletedProcess[str]:
environment = None
environment = dict(os.environ)
drop_privileges = None
if username:
account = pwd.getpwnam(username)
@@ -61,6 +63,8 @@ class GitWorkspace:
os.setgid(account.pw_gid)
os.setuid(account.pw_uid)
environment.update(extra_environment or {})
return subprocess.run(
shlex.split(command),
cwd=self.path,

View File

@@ -99,8 +99,35 @@ class AutomationWorker:
def _run_tests(self, workspace: GitWorkspace) -> list[dict]:
results = []
test_environment = {
"AGENT_TEST_DB_ENABLED": "true",
"TEST_DB_HOST": self.settings.test_database_host,
"TEST_DB_PORT": str(self.settings.test_database_port),
"TEST_DB_NAME": self.settings.test_database_name,
"TEST_DB_USER": self.settings.test_database_user,
"TEST_DB_PASSWORD": self.settings.test_database_password,
"TEST_CUSTOMER_DB": self.settings.test_customer_database,
"TEST_CONFIG_DB": self.settings.test_config_database,
"DB_CONNECTION": "mysql",
"DB_HOST": self.settings.test_database_host,
"DB_PORT": str(self.settings.test_database_port),
"DB_DATABASE": self.settings.test_database_name,
"DB_USERNAME": self.settings.test_database_user,
"DB_PASSWORD": self.settings.test_database_password,
"ERP_DB_HOST": self.settings.test_database_host,
"ERP_DB_PORT": str(self.settings.test_database_port),
"ERP_DATABASE": self.settings.test_customer_database,
"ERP_DB_USERNAME": self.settings.test_database_user,
"ERP_DB_PASSWORD": self.settings.test_database_password,
"COCKPIT_CONFIG_DATABASE": self.settings.test_config_database,
}
for command in self.settings.test_commands:
result = workspace.run(command, check=False, username="coder")
result = workspace.run(
command,
check=False,
username="coder",
extra_environment=test_environment,
)
results.append({
"command": command,
"returncode": result.returncode,