Files
ServerKit/backend/tests/test_bootstrap_boundaries.py
T
6e096fa286 Add real preview deployments and strengthen core boundaries (#184)
* fix(security): honour the failed-login time window (#180)

check_failed_logins computed a cutoff from since_hours and never used
it, so the Security -> Events "Failed login attempts (24h)" card and
its alert counted every failure still in /var/log/auth.log or
/var/log/secure: up to a week of SSH attempts under the usual weekly
log rotation.

Each matching line is now dated from its stamp, either RFC 3339
(rsyslog's high-precision format) or the traditional BSD syslog stamp
(no year, so a December line read in January is placed in the previous
year), and lines older than the cutoff are skipped. A line whose stamp
is not recognised is still counted rather than hidden. The log paths
move to SecurityService.AUTH_LOGS so the tests can point at a temp
file.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* refactor: separate backend startup phases and guard ordering

* refactor: route file operations through capability-checked adapters

* chore: bump version to 1.11.25 [skip ci]

* refactor: split File Manager state and views with regression guards

* test: enforce core boundaries and document extension rules

* test: cover File Manager tree races and listing failures

* fix: preserve File Manager deep links on initial mount

* refactor: keep File Manager download operations in the action hook

* fix: start agent maintenance only in the guarded runtime phase

* test: capture nginx rendering contract before extraction

* refactor: separate nginx configuration rendering from host operations

* test: enforce pure nginx configuration boundaries

* fix: require provisioned runtime before advertising previews

* feat: expose value-free effective environment provenance through app access boundary

* feat: add effective environment inspector with browser regression checks

* feat: provision isolated generic previews with retryable resource cleanup

* feat: queue preview automation and preserve cleanup ownership through app deletion

* feat: add pinned preview deployment controls and queued lifecycle feedback

* fix: preserve container port semantics for previews of slot deployments

* fix: preserve search focus and ignore stale list responses

* feat: run approved assistant deployments through persistent recipe operations

* test: run search and assistant deployment regressions in CI

* fix: refresh preview API and controller boundary inventories

* docs: refresh controller migration count after preview extraction

* fix: bind file editor previews to their storage target

* chore: bump version to 1.11.26 [skip ci]

---------

Co-authored-by: Max Freedom Pollard <272618364+MaxFreedomPollard@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-10-24 15:31:39 -04:00

141 lines
6.4 KiB
Python

"""Startup ordering and dependency boundaries, without launching workers."""
import ast
import sys
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
from app.bootstrap import runtime
@pytest.fixture
def runtime_calls(monkeypatch):
calls = []
def record(name, result=None):
def call(*args, **kwargs):
calls.append(name)
return result
return call
modules = {
'app.jobs.service': {'JobService': SimpleNamespace(recover_interrupted=record('recover'))},
'app.bootstrap.agents': {'start_agent_runtime': record('agents')},
'app.services.metrics_history_service': {'MetricsHistoryService': SimpleNamespace(
is_running=lambda: False, start_collection=record('metrics'))},
'app.queue_bus.consumers': {'start_webhook_consumer': record('webhooks')},
'app.notifications.consumer': {'start_notification_consumer': record('notifications')},
'app.middleware.api_analytics': {'start_analytics_flush_thread': record('analytics')},
'app.jobs': {'start_job_system': record('jobs')},
'app.bootstrap.jobs': {'register_job_handlers': record('handlers')},
'app.services.monitoring_service': {'MonitoringService': SimpleNamespace(resume_if_enabled=record('monitoring'))},
'app.services.uptime_service': {'UptimeService': SimpleNamespace(resume_if_enabled=record('uptime'))},
'app.services.linked_panel_service': {'LinkedPanelService': SimpleNamespace(start_client_if_linked=record('linked'))},
}
for name, attrs in modules.items():
monkeypatch.setitem(sys.modules, name, SimpleNamespace(**attrs))
from app import services
monkeypatch.setattr(services, 'connect_client', SimpleNamespace(
start_client_if_paired=record('relay')), raising=False)
monkeypatch.delenv('SERVERKIT_SKIP_BACKGROUND', raising=False)
monkeypatch.setattr(sys, 'argv', ['gunicorn'])
return calls
@pytest.mark.parametrize('argv,skip', [(['flask', 'db', 'upgrade'], None),
(['flask'], None), (['gunicorn'], '1')])
def test_one_shot_startup_never_recovers_or_starts_workers(monkeypatch, runtime_calls, argv, skip):
monkeypatch.setattr(sys, 'argv', argv)
if skip:
monkeypatch.setenv('SERVERKIT_SKIP_BACKGROUND', skip)
runtime.start_runtime(SimpleNamespace(config={}, logger=Mock()))
assert runtime_calls == []
@pytest.mark.parametrize('argv', [['gunicorn'], ['flask', 'run']])
def test_recovery_precedes_consumers_and_handlers_precede_jobs(monkeypatch, runtime_calls, argv):
monkeypatch.setattr(sys, 'argv', argv)
runtime.start_runtime(SimpleNamespace(config={}, logger=Mock()))
assert runtime_calls == ['recover', 'agents', 'metrics', 'webhooks', 'notifications',
'analytics', 'handlers', 'jobs', 'monitoring',
'uptime', 'linked', 'relay']
def test_monitor_failure_does_not_prevent_other_runtime_services(runtime_calls):
def fail(_app):
raise RuntimeError('monitor unavailable')
sys.modules['app.services.monitoring_service'].MonitoringService.resume_if_enabled = fail
logger = Mock()
runtime.start_runtime(SimpleNamespace(config={}, logger=logger))
logger.exception.assert_called_once()
assert runtime_calls[-3:] == ['uptime', 'linked', 'relay']
def test_agent_maintenance_runs_only_in_server_mode(monkeypatch):
from app.bootstrap.agents import start_agent_runtime
from app.services.agent_registry import agent_registry
from app.services.nonce_service import nonce_service
from app.services.anomaly_detection_service import anomaly_detection_service
heartbeat, nonces, anomalies = Mock(), Mock(), Mock()
monkeypatch.setattr(agent_registry, 'start', heartbeat)
monkeypatch.setattr(nonce_service, 'start_cleanup', nonces)
monkeypatch.setattr(anomaly_detection_service, 'start_cleanup', anomalies)
start_agent_runtime(SimpleNamespace(config={'TESTING': True}))
assert not any(call.called for call in (heartbeat, nonces, anomalies))
server = SimpleNamespace(config={})
start_agent_runtime(server)
heartbeat.assert_called_once_with(server)
nonces.assert_called_once_with()
anomalies.assert_called_once_with()
@pytest.mark.parametrize('module_name,instance_name', [
('nonce_service', 'nonce_service'),
('anomaly_detection_service', 'anomaly_detection_service'),
])
def test_agent_cleanup_start_is_idempotent(monkeypatch, module_name, instance_name):
import importlib
import threading
module = importlib.import_module(f'app.services.{module_name}')
service = getattr(module, instance_name)
thread = Mock()
thread.is_alive.return_value = True
constructor = Mock(return_value=thread)
monkeypatch.setattr(service, '_cleanup_thread', None)
monkeypatch.setattr(service, '_stop_cleanup', threading.Event())
monkeypatch.setattr(module.threading, 'Thread', constructor)
service.start_cleanup()
service.start_cleanup()
constructor.assert_called_once()
thread.start.assert_called_once()
def test_heartbeat_runtime_captures_the_serving_app(monkeypatch):
from app.services.agent_registry import agent_registry
start = Mock()
monkeypatch.setattr(agent_registry, '_start_heartbeat_checker', start)
monkeypatch.setattr(agent_registry, '_app', None)
server = object()
agent_registry.start(server)
assert agent_registry._app is server
start.assert_called_once_with()
def test_bootstrap_is_an_assembly_layer_not_a_service_dependency():
root = Path(__file__).parents[1] / 'app'
for folder in ('api', 'services', 'models', 'jobs'):
for path in (root / folder).rglob('*.py'):
for node in ast.walk(ast.parse(path.read_text(encoding='utf-8-sig'))):
if isinstance(node, ast.ImportFrom):
assert not (node.module or '').startswith('app.bootstrap'), path
elif isinstance(node, ast.Import):
assert not any(a.name.startswith('app.bootstrap') for a in node.names), path
factory = ast.parse((root / '__init__.py').read_text(encoding='utf-8'))
create = next(n for n in factory.body if isinstance(n, ast.FunctionDef) and n.name == 'create_app')
assert create.end_lineno - create.lineno < 70, 'Keep feature setup in an owned bootstrap phase'
for node in ast.walk(create):
assert not isinstance(node, (ast.Try, ast.For, ast.While)), 'Factory must compose phases'