ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
perf(tests): build the Flask app once per session, not once per test
Juan Denis9d570834aa32
diff --git a/backend/tests/conftest.py b/backend/tests/conftest.pyindex 765e26d0..ec69ea8d 100644--- a/backend/tests/conftest.py+++ b/backend/tests/conftest.py@@ -95,18 +95,100 @@ except ImportError:pass+def pytest_configure(config):+ config.addinivalue_line(+ 'markers',+ 'fresh_app: build a PRIVATE Flask app for this test instead of sharing '+ 'the session-wide one. Required for tests that mutate application '+ 'STRUCTURE — Flask cannot unregister a blueprint or a url rule, so such '+ 'a mutation would leak into every later test on a shared app.',+ )++[email protected](scope='session')+def _flask_app():+ """The Flask application and the schema, built ONCE for the test process.++ `create_app('testing')` registers 90+ blueprints and is expensive. Building+ it per test made fixture setup ~90% of this suite's total runtime (plan 64+ Phase 0), so it is built once and shared.++ Booted TWICE on purpose. `create_app()` is not a pure constructor: it seeds+ rows as a side effect of booting (the flagship extension rows that+ test_cloudflare_extraction asserts are "seeded on boot"). So the schema has+ to exist before the app that seeds into it:++ boot #1 -> create_all() tables now exist+ boot #2 -> seeds into them rows land in real tables++ A single boot would seed against a schema that does not exist yet, and the+ flagship rows would silently never appear. The cost is one extra create_app+ for the whole session.++ The import stays inside the function body on purpose: some CI jobs run only+ the stdlib-y system-utils tests with pytest and nothing else installed (see+ test-system-utils.yml), and conftest still has to import cleanly there.+ Never make a fixture that touches this one autouse.+ """+ from app import create_app+ from app import db as _db++ bootstrap = create_app('testing')+ with bootstrap.app_context():+ _db.create_all()++ return create_app('testing')++@pytest.fixture(scope='function')-def app():- """Create Flask app with testing config and in-memory DB."""+def app(request, _flask_app):+ """Per-test database, on the session-wide app unless opted out.++ Still create_all/drop_all per test — Phase 1 deliberately changes only the+ app's lifetime, so that "shared app object" breakage can be diagnosed+ separately from "shared database" breakage.++ Two consequences of sharing one app, both handled here rather than patched+ test by test:++ * Config mutation now leaks where it previously died with the app (e.g.+ test_demo_deploys sets DEMO_DEPLOYS_ENABLED=False and never restores it),+ so config is snapshotted and restored around every test.+ * STRUCTURE mutation cannot be undone at all — Flask has no+ unregister_blueprint. Tests doing that mark themselves `fresh_app` and get+ a private app, paying the old per-test create_app cost knowingly.+ """from app import create_appfrom app import db as _db- app = create_app('testing')- with app.app_context():+ # `wp_extension` mounts the WordPress extension's blueprints onto whatever+ # app it is handed, so every test using it is structure-mutating too. Detect+ # it from the fixture graph instead of requiring each such module to+ # remember the marker — miss one and the failure lands in an unrelated test+ # much later in the run.+ needs_private_app = (+ request.node.get_closest_marker('fresh_app') is not None+ or 'wp_extension' in request.fixturenames+ )+ target = create_app('testing') if needs_private_app else _flask_app++ saved_config = dict(target.config)+ with target.app_context():_db.create_all()- yield app- _db.session.remove()- _db.drop_all()+ # create_app() seeds the bundled flagship extension rows as a boot side+ # effect, and this drop_all/create_all wipes them. With a per-test app+ # that reseeding came free on every boot; with a shared app it has to be+ # re-run explicitly, or every test after the first sees no flagship and+ # the extension routes answer 503.+ from app.services.plugin_service import seed_flagship_extensions+ seed_flagship_extensions()+ try:+ yield target+ finally:+ _db.session.remove()+ _db.drop_all()+ target.config.clear()+ target.config.update(saved_config)@pytest.fixturediff --git a/backend/tests/test_agent_features_honest.py b/backend/tests/test_agent_features_honest.pyindex 38c42728..ea046c14 100644--- a/backend/tests/test_agent_features_honest.py+++ b/backend/tests/test_agent_features_honest.py@@ -12,6 +12,12 @@ from app.models.server_template import ServerTemplate, ServerTemplateAssignmentfrom app.services.agent_plugin_service import AgentPluginServicefrom app.services.server_template_service import ServerTemplateService+import pytest+# Installs plugins, and plugin_service hot-loads their blueprints onto the+# live app. Flask refuses register_blueprint once an app has served a+# request, so these need a private app (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+def test_plugin_install_reports_unimplemented_not_stuck_installing(app):with app.app_context():diff --git a/backend/tests/test_api_scopes.py b/backend/tests/test_api_scopes.pyindex 0ce764fc..a33ba5d2 100644--- a/backend/tests/test_api_scopes.py+++ b/backend/tests/test_api_scopes.py@@ -12,6 +12,11 @@ from app.utils.sensitive_data_filter import ()from app.models.api_key import ApiKey+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+# ---------------------------------------------------------------------------# Helpersdiff --git a/backend/tests/test_audit_log.py b/backend/tests/test_audit_log.pyindex 987c30f0..28c6fb96 100644--- a/backend/tests/test_audit_log.py+++ b/backend/tests/test_audit_log.py@@ -1,5 +1,12 @@+import pytest+from app.models.audit_log import AuditLog+# Adds routes to the app fixture with @app.route. Flask refuses setup methods+# once an app has served its first request, so these need a private app rather+# than the session-wide one (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+def test_generic_resource_action_constants_are_defined():assert AuditLog.ACTION_API_MUTATION == 'api.mutation'diff --git a/backend/tests/test_bandwidth.py b/backend/tests/test_bandwidth.pyindex ccaa33c6..dbbe7f7e 100644--- a/backend/tests/test_bandwidth.py+++ b/backend/tests/test_bandwidth.py@@ -19,6 +19,11 @@ from app.services.bandwidth_service import (_day_prefix,)+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+# ── helpers ──────────────────────────────────────────────────────────────────diff --git a/backend/tests/test_cloud_provision_extraction.py b/backend/tests/test_cloud_provision_extraction.pyindex c8704c7b..c8c02832 100644--- a/backend/tests/test_cloud_provision_extraction.py+++ b/backend/tests/test_cloud_provision_extraction.py@@ -14,6 +14,11 @@ from app import dbfrom app.models.plugin import InstalledPluginfrom app.services import plugin_service+# Installs plugins, and plugin_service hot-loads their blueprints onto the+# live app. Flask refuses register_blueprint once an app has served a+# request, so these need a private app (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+SLUG = 'serverkit-cloud-provision'_PKG = f'app.plugins.{SLUG}'diff --git a/backend/tests/test_db_admin_sso.py b/backend/tests/test_db_admin_sso.pyindex a922d37e..459250b8 100644--- a/backend/tests/test_db_admin_sso.py+++ b/backend/tests/test_db_admin_sso.py@@ -16,6 +16,11 @@ from app.services.db_admin_sso_service import (DbAdminSsoService, ADMINER_LABEL, ADMINER_CONTAINER, REAP_JOB_KIND,)+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+@pytest.fixturedef managed(app):diff --git a/backend/tests/test_drift_doctor.py b/backend/tests/test_drift_doctor.pyindex a8aef5a5..3a0d436d 100644--- a/backend/tests/test_drift_doctor.py+++ b/backend/tests/test_drift_doctor.py@@ -17,6 +17,11 @@ from app.services.doctor_service import (LAST_REPORT_KEY as DOCTOR_REPORT_KEY,)+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+# --------------------------------------------------------------------------- ## Fake drift check plumbingdiff --git a/backend/tests/test_email_extraction.py b/backend/tests/test_email_extraction.pyindex 269ea303..ce9ae38a 100644--- a/backend/tests/test_email_extraction.py+++ b/backend/tests/test_email_extraction.py@@ -12,6 +12,11 @@ import app as app_pkgfrom app.models.plugin import InstalledPluginfrom app.services import plugin_service+# Installs plugins, and plugin_service hot-loads their blueprints onto the+# live app. Flask refuses register_blueprint once an app has served a+# request, so these need a private app (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+SLUG = 'serverkit-email'_PKG = f'app.plugins.{SLUG}'diff --git a/backend/tests/test_extension_migration.py b/backend/tests/test_extension_migration.pyindex c19095af..fa5d5604 100644--- a/backend/tests/test_extension_migration.py+++ b/backend/tests/test_extension_migration.py@@ -14,6 +14,11 @@ from app.models.plugin import InstalledPluginfrom app.models.user import Userfrom app.services import plugin_service, extension_migration+# Installs plugins, and plugin_service hot-loads their blueprints onto the+# live app. Flask refuses register_blueprint once an app has served a+# request, so these need a private app (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+@pytest.fixturedef demo_builtin(tmp_path, monkeypatch):diff --git a/backend/tests/test_extension_platform.py b/backend/tests/test_extension_platform.pyindex 1c7a60c9..dd22bd74 100644--- a/backend/tests/test_extension_platform.py+++ b/backend/tests/test_extension_platform.py@@ -19,6 +19,11 @@ from app.services import plugin_service, extension_lifecyclefrom app.plugins_sdk import permissions, sockets as sdk_socketsimport app.plugins_sdk as sdk+# Installs plugins, and plugin_service hot-loads their blueprints onto the+# live app. Flask refuses register_blueprint once an app has served a+# request, so these need a private app (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+SLUG = 'testext'diff --git a/backend/tests/test_extension_signing.py b/backend/tests/test_extension_signing.pyindex 0b548212..491fd6ff 100644--- a/backend/tests/test_extension_signing.py+++ b/backend/tests/test_extension_signing.py@@ -19,6 +19,11 @@ from app import dbfrom app.models.plugin import InstalledPluginfrom app.services import plugin_service, registry_service, signing_service+# Installs plugins, and plugin_service hot-loads their blueprints onto the+# live app. Flask refuses register_blueprint once an app has served a+# request, so these need a private app (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+# --------------------------------------------------------------------------- ## Fixtures / helpersdiff --git a/backend/tests/test_ftp_extraction.py b/backend/tests/test_ftp_extraction.pyindex e28b8d29..33197dab 100644--- a/backend/tests/test_ftp_extraction.py+++ b/backend/tests/test_ftp_extraction.py@@ -12,6 +12,11 @@ import app as app_pkgfrom app.models.plugin import InstalledPluginfrom app.services import plugin_service+# Installs plugins, and plugin_service hot-loads their blueprints onto the+# live app. Flask refuses register_blueprint once an app has served a+# request, so these need a private app (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+SLUG = 'serverkit-ftp'_PKG = f'app.plugins.{SLUG}'diff --git a/backend/tests/test_htaccess_converter.py b/backend/tests/test_htaccess_converter.pyindex 980c4992..56d36ac8 100644--- a/backend/tests/test_htaccess_converter.py+++ b/backend/tests/test_htaccess_converter.py@@ -5,6 +5,11 @@ import pytestfrom app.services.htaccess_converter import MAX_INPUT_BYTES, convert+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+def _nginx(text):return convert(text)['nginx']diff --git a/backend/tests/test_managed_db_users.py b/backend/tests/test_managed_db_users.pyindex d4b95910..7fbdf4ce 100644--- a/backend/tests/test_managed_db_users.py+++ b/backend/tests/test_managed_db_users.py@@ -13,6 +13,11 @@ from app.models.managed_database_user import ManagedDatabaseUserfrom app.services.managed_database_service import ManagedDatabaseServicefrom app.services.managed_db_user_service import ManagedDbUserService+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+@pytest.fixturedef managed(app):diff --git a/backend/tests/test_plugin_store_sdk.py b/backend/tests/test_plugin_store_sdk.pyindex a395532b..fbc68e2a 100644--- a/backend/tests/test_plugin_store_sdk.py+++ b/backend/tests/test_plugin_store_sdk.py@@ -15,6 +15,11 @@ from app import dbfrom app.models.plugin_store import PluginStorefrom app.plugins_sdk.store_sdk import MAX_VALUE_BYTES, PluginStoreError, store+# Installs plugins, and plugin_service hot-loads their blueprints onto the+# live app. Flask refuses register_blueprint once an app has served a+# request, so these need a private app (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+@pytest.fixturedef mine(app):diff --git a/backend/tests/test_plugins_pipeline.py b/backend/tests/test_plugins_pipeline.pyindex 2984c070..991b0ffe 100644--- a/backend/tests/test_plugins_pipeline.py+++ b/backend/tests/test_plugins_pipeline.py@@ -14,6 +14,11 @@ from app import dbfrom app.models.plugin import InstalledPluginfrom app.services import plugin_service+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+# --------------------------------------------------------------------------- ## Fixturesdiff --git a/backend/tests/test_remote_access_extraction.py b/backend/tests/test_remote_access_extraction.pyindex 2590792a..d6424e02 100644--- a/backend/tests/test_remote_access_extraction.py+++ b/backend/tests/test_remote_access_extraction.py@@ -19,6 +19,11 @@ from app.models.plugin import InstalledPluginfrom app.services import agent_registry as ar_modfrom app.services import plugin_service+# Installs plugins, and plugin_service hot-loads their blueprints onto the+# live app. Flask refuses register_blueprint once an app has served a+# request, so these need a private app (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+SLUG = 'serverkit-remote-access'_PKG = f'app.plugins.{SLUG}'diff --git a/backend/tests/test_serverkit_gui_agent_gate.py b/backend/tests/test_serverkit_gui_agent_gate.pyindex 31dc9ac1..a14a4aca 100644--- a/backend/tests/test_serverkit_gui_agent_gate.py+++ b/backend/tests/test_serverkit_gui_agent_gate.py@@ -24,6 +24,11 @@ from app.models.server import Serverfrom app.models.user import Userfrom app.services import agent_registry as agent_registry_module+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+SLUG = 'serverkit-gui'SERVER_ID = 'srv-gui-1'PREFIX = '/api/v1/server-gui'diff --git a/backend/tests/test_shared_resources.py b/backend/tests/test_shared_resources.pyindex a81fe451..46dd19d3 100644--- a/backend/tests/test_shared_resources.py+++ b/backend/tests/test_shared_resources.py@@ -11,6 +11,11 @@ import pytest# body, i.e. AFTER create_all). The panel wires these via `models/__init__.py`.import app.models.shared_resource # noqa: F401,E402+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+# A concurrent, in-flight slice adds `applications.environment_id` (FK →# `environments`) but has not yet wired its models into `models/__init__.py`.# Until it does, `db.create_all()` cannot resolve that FK and the whole suite'sdiff --git a/backend/tests/test_site_import.py b/backend/tests/test_site_import.pyindex b4d7542c..9ab326d5 100644--- a/backend/tests/test_site_import.py+++ b/backend/tests/test_site_import.py@@ -25,6 +25,11 @@ from app.services.site_importers import ()from app.services.site_importers.cpanel import CpanelImporter+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+NATIVE_HASH = '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29'BACKUP_NAME = 'backup-7.3.2026_12-00-00_alice'diff --git a/backend/tests/test_speed_test.py b/backend/tests/test_speed_test.pyindex 00e34d11..3f2b7449 100644--- a/backend/tests/test_speed_test.py+++ b/backend/tests/test_speed_test.py@@ -13,6 +13,11 @@ from app.services.speed_test_service import (SpeedTestService,)+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+class _FakeProc:def __init__(self, returncode=0, stdout='', stderr=''):diff --git a/backend/tests/test_status_extraction.py b/backend/tests/test_status_extraction.pyindex 4651bcf6..e59bf276 100644--- a/backend/tests/test_status_extraction.py+++ b/backend/tests/test_status_extraction.py@@ -18,6 +18,11 @@ from app.models.user import Userfrom app.models.plugin import InstalledPluginfrom app.services import plugin_service+# Installs plugins, and plugin_service hot-loads their blueprints onto the+# live app. Flask refuses register_blueprint once an app has served a+# request, so these need a private app (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+SLUG = 'serverkit-status'_PKG = f'app.plugins.{SLUG}'diff --git a/backend/tests/test_support_bundle.py b/backend/tests/test_support_bundle.pyindex e9416308..f9b738f9 100644--- a/backend/tests/test_support_bundle.py+++ b/backend/tests/test_support_bundle.py@@ -8,6 +8,11 @@ import pytestfrom app.services import support_bundle_service+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+EXPECTED_MEMBERS = {'README.txt', 'meta.json', 'db.json', 'counts.json', 'services.json',diff --git a/backend/tests/test_trusted_client_ip.py b/backend/tests/test_trusted_client_ip.pyindex 8a137903..1bd24720 100644--- a/backend/tests/test_trusted_client_ip.py+++ b/backend/tests/test_trusted_client_ip.py@@ -16,6 +16,11 @@ import pytestfrom app import create_app+# Registers blueprints / url rules on the app fixture. Flask cannot unregister+# those, so these tests need a private app rather than the session-wide one+# (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+def _build_app(monkeypatch, trust, hops=1):"""Create a testing app with the trusted-proxy config forced on/off.diff --git a/backend/tests/test_wp_hook_inversions.py b/backend/tests/test_wp_hook_inversions.pyindex 87a6bd6e..9ce6cf4e 100644--- a/backend/tests/test_wp_hook_inversions.py+++ b/backend/tests/test_wp_hook_inversions.py@@ -29,6 +29,11 @@ from app.services.event_service import EventServicefrom app.services.backup_policy_service import BackupPolicyError, BackupPolicyServicefrom app.services.template_service import TemplateService+# Installs plugins, and plugin_service hot-loads their blueprints onto the+# live app. Flask refuses register_blueprint once an app has served a+# request, so these need a private app (plan 64 Phase 1).+pytestmark = pytest.mark.fresh_app+SLUG = 'serverkit-wordpress'