Add independent MIT account-rate500 policy source supplement
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
"""Build an isolated legacy project and run real Django/DB checks, never live DB."""
|
||||
import argparse
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--stage", choices=("red", "green"), required=True)
|
||||
parser.add_argument("--run-id")
|
||||
args = parser.parse_args()
|
||||
candidate = Path(__file__).resolve().parents[1]
|
||||
def candidate_inputs():
|
||||
return {str(path.relative_to(candidate)).replace("\\", "/"): hashlib.sha256(path.read_bytes()).hexdigest()
|
||||
for directory in (candidate / "overlay", candidate / "tests") for path in directory.rglob("*")
|
||||
if path.is_file() and path.suffix in (".py", ".html")}
|
||||
inputs_before = candidate_inputs()
|
||||
baseline = candidate.parent / "legacy-platform-fixture"
|
||||
run = candidate / "evidence" / (args.run_id or args.stage)
|
||||
run.mkdir(parents=True, exist_ok=False)
|
||||
project = run / "project"
|
||||
shutil.copytree(baseline, project, ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
|
||||
if args.stage == "green":
|
||||
for source in (candidate / "overlay").rglob("*"):
|
||||
if source.is_file():
|
||||
target = project / source.relative_to(candidate / "overlay")
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(source, target)
|
||||
os.environ.update(DJANGO_SETTINGS_MODULE="rate500_test_settings", DJANGO_SECRET_KEY="isolated-not-production",
|
||||
DJANGO_DEBUG="false", DJANGO_ALLOWED_HOSTS="localhost,127.0.0.1,testserver",
|
||||
ACCOUNT_BANDWIDTH_BUDGET_V2_ENABLED="false", ACCOUNT_BANDWIDTH_BUDGET_ENABLED="false")
|
||||
(project / "rate500_test_settings.py").write_text(
|
||||
"from toplc_vpn.test_settings import *\nDATABASES['default']['NAME'] = " + repr(str(run / "test.sqlite3")) + "\n", encoding="utf-8")
|
||||
sys.path.insert(0, str(project))
|
||||
import django
|
||||
django.setup()
|
||||
from django.core.management import call_command
|
||||
from django.db import connection
|
||||
from django.db.migrations.executor import MigrationExecutor
|
||||
call_command("migrate", "shared_budget", "0001", verbosity=0)
|
||||
state = MigrationExecutor(connection).loader.project_state([("shared_budget", "0001_initial")]).apps
|
||||
User = state.get_model("accounts", "User")
|
||||
Policy = state.get_model("shared_budget", "AccountBandwidthPolicy")
|
||||
Exemption = state.get_model("shared_budget", "AccountBandwidthExemption")
|
||||
for key in (1, 2, 5, 7):
|
||||
User.objects.create(id=key, username="fixture-" + str(key), email="owner@example.invalid" if key == 5 else f"fixture-{key}@example.invalid")
|
||||
if key != 7:
|
||||
Policy.objects.create(user_id=key, upload_limit_kib_per_second=111 if key == 5 else 200,
|
||||
download_limit_kib_per_second=123 if key == 5 else 150)
|
||||
Exemption.objects.create(policy_key="owner-approved-20261022", user_id=5)
|
||||
spec = importlib.util.spec_from_file_location("clone_validation", candidate / "tests/clone_validation.py")
|
||||
validation = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(validation)
|
||||
before = validation.protected_snapshot()
|
||||
def business_snapshot():
|
||||
result = {}
|
||||
with connection.cursor() as cursor:
|
||||
for table in sorted(connection.introspection.table_names()):
|
||||
if table.startswith(("accounts_", "control_", "shared_budget_")) and table != "shared_budget_accountbandwidthpolicy":
|
||||
cursor.execute("SELECT * FROM " + connection.ops.quote_name(table))
|
||||
rows = sorted(json.dumps(row, default=str, sort_keys=True) for row in cursor.fetchall())
|
||||
result[table] = hashlib.sha256("\n".join(rows).encode()).hexdigest()
|
||||
return result
|
||||
business_before = business_snapshot()
|
||||
has_migration = (project / "shared_budget/migrations/0002_account_rate_500.py").exists()
|
||||
plan = []
|
||||
if has_migration:
|
||||
plan = [(migration.app_label, migration.name, backwards) for migration, backwards in MigrationExecutor(connection).migration_plan([
|
||||
("shared_budget", "0002_account_rate_500")])]
|
||||
call_command("migrate", "shared_budget", "0002_account_rate_500", verbosity=0)
|
||||
business_after = business_snapshot()
|
||||
call_command("migrate", verbosity=0)
|
||||
result = validation.validate(before)
|
||||
result["checks"]["additive_0002_migration_present"] = has_migration
|
||||
result["checks"]["migration_plan_only_shared_budget_0002"] = plan == [("shared_budget", "0002_account_rate_500", False)]
|
||||
result["checks"]["migration_preserves_all_other_business_rows"] = business_before == business_after
|
||||
result["business_before_sha256"] = business_before
|
||||
result["business_after_sha256"] = business_after
|
||||
result["migration_plan"] = plan
|
||||
result["candidate_inputs_sha256"] = inputs_before
|
||||
result["checks"]["candidate_inputs_unchanged_during_test"] = inputs_before == candidate_inputs()
|
||||
result["passed"] = all(result["checks"].values())
|
||||
result["stage"] = args.stage
|
||||
result["django_version"] = django.get_version()
|
||||
result["database_vendor"] = connection.vendor
|
||||
result["inputs_sha256"] = {str(path.relative_to(project)).replace("\\", "/"): hashlib.sha256(path.read_bytes()).hexdigest()
|
||||
for path in project.rglob("*") if path.is_file() and path.suffix in (".py", ".html") and "__pycache__" not in path.parts}
|
||||
(run / "result.json").write_text(json.dumps(result, ensure_ascii=False, indent=2, default=str), encoding="utf-8")
|
||||
print(json.dumps({key: value for key, value in result.items() if key != "inputs_sha256"}, ensure_ascii=False, indent=2))
|
||||
raise SystemExit(0 if result["passed"] else 1)
|
||||
Reference in New Issue
Block a user