| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- """Offline single-artifact install, factory loading, hot reload and move parity."""
- import ast
- import builtins
- from contextlib import ExitStack, redirect_stdout
- from dataclasses import asdict
- from datetime import date
- import hashlib
- from io import StringIO
- import json
- import os
- from pathlib import Path
- import sys
- from tempfile import TemporaryDirectory
- from types import SimpleNamespace
- import unittest
- from unittest.mock import Mock, patch
- from plugins import ita_plugin # Preload shared host dependencies, not a loader substitute.
- from test.ita_source_sections import section
- from test.test_ita_calendar_book import DAY, SERVICE, FakePage, packets
- from test.test_ita_protocol import live_route
- from tools import consolidate_ita_plugin as migration
- from tools import ita_query_book_local as local
- from utils import plugin_updater
- from vs_plg_factory import VSPlgFactory
- from vs_types import AppointmentType, BookingOutcomeUnknownError, VSQueryResult
- ROOT = Path(__file__).resolve().parents[1]
- class NormalizeOriginal(ast.NodeTransformer):
- """Independent structural check: only the explicitly allowed wiring edits."""
- def __init__(self, module):
- self.module = module
- self.renames = migration.RENAMES.get(module, {})
- def visit_ImportFrom(self, node):
- if node.module == "__future__":
- return None
- if node.module and node.module.startswith("plugins.ita_"):
- return [ast.Assign(targets=[ast.Name(id=a.asname, ctx=ast.Store())],
- value=ast.Name(id=a.name, ctx=ast.Load()))
- for a in node.names if a.asname]
- if node.module == "plugins":
- assert self.module == "ita_workflow"
- return None
- if self.module == "ita_protocol" and node.module == "datetime":
- node.names[-1].asname = "datetime_time"
- return node
- def visit_Assign(self, node):
- if any(isinstance(t, ast.Name) and t.id == "__all__" for t in node.targets):
- return None
- if any(isinstance(t, ast.Name) and t.id == "_OTP_INPUT_LOCK" for t in node.targets):
- return ast.parse('import vs_plg as _ita_host\n'
- '_OTP_INPUT_LOCK = vars(_ita_host).setdefault("_coordinate_ita_otp_input_lock", Lock())').body
- if (self.module == "ita_plugin" and any(isinstance(t, ast.Name) and t.id == "version" for t in node.targets)):
- node.value = ast.Constant(value="1.0.1")
- return self.generic_visit(node)
- def visit_Name(self, node):
- node.id = self.renames.get(node.id, node.id)
- return node
- def visit_FunctionDef(self, node):
- node.name = self.renames.get(node.name, node.name)
- return self.generic_visit(node)
- def visit_Attribute(self, node):
- if self.module == "ita_workflow" and isinstance(node.value, ast.Name) and node.value.id == "preparation":
- return ast.Name(id=node.attr, ctx=node.ctx)
- return self.generic_visit(node)
- class RelocationParityTests(unittest.TestCase):
- def test_every_original_section_is_structurally_unchanged_except_wiring(self):
- for module in migration.MODULES:
- with self.subTest(module=module):
- expected = NormalizeOriginal(module).visit(ast.parse(migration.original(module)))
- actual = ast.parse(section(module.removeprefix("ita_")))
- self.assertEqual(ast.dump(expected), ast.dump(actual))
- def test_relocation_and_manifest_are_reproducible(self):
- source, manifest = migration.assemble()
- self.assertEqual(source, (ROOT / "plugins/ita_plugin.py").read_text())
- self.assertEqual(manifest, json.loads((ROOT / "docs/ita-single-file-migration.json").read_text()))
- def test_runtime_has_no_private_italy_or_tool_imports_or_dynamic_packaging(self):
- tree = ast.parse((ROOT / "plugins/ita_plugin.py").read_text())
- for node in ast.walk(tree):
- if isinstance(node, (ast.Import, ast.ImportFrom)):
- names = [node.module or ""] if isinstance(node, ast.ImportFrom) else [a.name for a in node.names]
- self.assertFalse(any(name == "plugins" or name.startswith(("plugins.", "tools.")) for name in names), names)
- if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
- self.assertNotIn(node.func.id, {"exec", "eval", "__import__"})
- self.assertEqual(sorted(p.name for p in (ROOT / "plugins").glob("ita_*.py")), ["ita_plugin.py"])
- class SingleFileInstallationTests(unittest.TestCase):
- def setUp(self):
- self.stack = ExitStack()
- self.addCleanup(self.stack.close)
- self.directory = Path(self.stack.enter_context(TemporaryDirectory()))
- self.source = (ROOT / "plugins/ita_plugin.py").read_text()
- self.path = self.directory / "ita_plugin.py"
- old_cwd = Path.cwd()
- os.chdir(self.directory)
- self.stack.callback(os.chdir, old_cwd)
- self.stack.enter_context(redirect_stdout(StringIO()))
- old_module = sys.modules.get("ita_plugin")
- def restore_module():
- sys.modules.pop("ita_plugin", None)
- if old_module is not None:
- sys.modules["ita_plugin"] = old_module
- self.stack.callback(restore_module)
- original_import = builtins.__import__
- def guarded_import(name, *args, **kwargs):
- if name == "plugins" or name.startswith(("plugins.", "tools.")):
- raise AssertionError("Single artifact attempted a private runtime import: " + name)
- return original_import(name, *args, **kwargs)
- self.stack.enter_context(patch("builtins.__import__", side_effect=guarded_import))
- for target in ("socket.socket.connect", "socket.create_connection", "requests.sessions.Session.request",
- "subprocess.Popen"):
- self.stack.enter_context(patch(target, side_effect=AssertionError("Unexpected external I/O")))
- self.stack.enter_context(patch.object(sys, "dont_write_bytecode", True))
- self.factory = VSPlgFactory()
- self.factory.register_plugin("ita_plugin", str(self.path), "ItaPlugin")
- self.install(self.source, "1.0.1")
- def install(self, source, version):
- self.assertTrue(plugin_updater.download_and_install_plugin(
- "ita_plugin", version, code_content=source, plugins_dir=str(self.directory),
- expected_sha256=hashlib.sha256(source.encode()).hexdigest()))
- def create(self):
- plugin = self.factory.create("offline-single-file", "ita_plugin")
- self.stack.callback(plugin.cleanup)
- return plugin
- def test_only_one_uploaded_file_loads_all_runtime_sections(self):
- self.assertEqual([p.name for p in self.directory.glob("*.py")], ["ita_plugin.py"])
- plugin = self.create()
- runtime = type(plugin).book.__globals__
- for name in ("ItaRouteConfig", "CalendarRequestAudit", "FinalReceiptReadPort", "FinalBookingAttempt",
- "parse_otp_body", "prepare_calendar", "recheck_calendar_before_final", "submit_final"):
- self.assertEqual(runtime[name].__module__, "ita_plugin", name)
- self.assertIs(runtime["BookingOutcomeUnknownError"], BookingOutcomeUnknownError)
- self.assertIsInstance(runtime["FinalOutcomeUnknown"](), BookingOutcomeUnknownError)
- self.assertEqual(runtime["parse_otp_body"]("OTP Code:001234"), "001234")
- self.assertEqual(plugin_updater.get_installed_plugins(str(self.directory))["ita_plugin"], "1.0.1")
- self.assertIsNot(runtime["ItaCapability"], ita_plugin.ItaCapability)
- def test_public_query_and_book_bind_the_loaded_file_not_canonical_import(self):
- plugin = self.create()
- plugin.page = SimpleNamespace(url="https://prenotami.esteri.it/UserArea", quit=Mock())
- plugin.free_config = {"otp_mode": "cloud", "query_mode": "calendar"}
- negative = VSQueryResult(success=False)
- runtime = type(plugin).book.__globals__
- with patch.dict(runtime, prepare_query_if_needed=Mock(return_value=negative), book=Mock(return_value="loaded-book")):
- self.assertIs(plugin.query(AppointmentType(routing_key="fixture")), negative)
- self.assertEqual(plugin.book(negative, {"username": "fixture@example.test"}), "loaded-book")
- runtime["prepare_query_if_needed"].assert_called_once()
- runtime["book"].assert_called_once()
- def test_local_final_fence_uses_instance_types_and_functions_after_cache_replacement(self):
- plugin = self.create()
- runtime = type(plugin).book.__globals__
- plugin.page = FakePage()
- plugin.page.quit = Mock()
- availability, slots = packets()
- approval = runtime["select_slot_before_final"](
- plugin.page, target_day=DAY, availability_packet=availability, time_slots_packet=slots)
- plugin.last_booking_precommit = approval
- self.assertEqual(local.verify_plugin_selection(plugin), approval)
- key = "slot.fixture.it.tourist"
- config = {key: live_route()}
- route = runtime["resolve_route_config"](config, key)
- configured_route = ita_plugin.resolve_route_config(config, key)
- self.assertNotEqual(route, configured_route) # Distinct loaded dataclass types.
- self.assertEqual(asdict(route), asdict(configured_route))
- applicant = SimpleNamespace(username="applicant@example.test")
- plugin.config = SimpleNamespace(account=applicant)
- target = runtime["FinalTarget"](applicant.username, "fixture-session", "fixture-tab", key,
- str(route.service_id), SERVICE, approval)
- audit = Mock()
- audit.settled_snapshot.return_value = {"finalRequests": 0}
- observed = {"book": SimpleNamespace(plugin=plugin, audit=audit)}
- control = local.LocalFinalSubmission(applicant, configured_route, root=self.directory / "claims")
- native = Mock(return_value="synthetic-dispatch")
- def production(actual_plugin, actual_route, *, request_audit):
- self.assertIs(actual_plugin, plugin)
- self.assertIs(actual_route, route)
- self.assertIs(request_audit, audit)
- return runtime["NativeFinalSubmitter"](plugin.page, target, lambda: True)()
- self.create() # The host overwrites sys.modules['ita_plugin'] for Query.
- self.assertIsNot(sys.modules["ita_plugin"].ItaPlugin, type(plugin))
- with patch.dict(runtime, NativeFinalSubmitter=Mock(return_value=native)):
- self.assertEqual(control.submit(plugin, route, observed, production), "synthetic-dispatch")
- self.assertTrue(control.claim_path.is_file())
- self.assertTrue(control.possibly_submitted)
- native.assert_called_once_with()
- with self.assertRaises(local.LocalAcceptanceError):
- control.submit(plugin, route, observed, production)
- native.assert_called_once_with()
- def test_local_observer_preserves_loaded_unknown_error_for_shared_booker(self):
- plugin = self.create()
- runtime = type(plugin).book.__globals__
- observer = local.ObservedPlugin(plugin, "book")
- plugin.book = Mock(side_effect=runtime["FinalOutcomeUnknown"]())
- with self.assertRaises(BookingOutcomeUnknownError):
- observer.book(VSQueryResult(), {})
- self.assertEqual(observer.error, "FinalOutcomeUnknown")
- def test_real_reload_replaces_code_and_preserves_process_wide_otp_lock(self):
- first = self.create()
- old = type(first).book.__globals__
- updated = self.source.replace('version = "1.0.1"', 'version = "1.0.200"')
- self.install(updated, "1.0.200")
- owner = self
- events = []
- cfg = SimpleNamespace(plugin_config=SimpleNamespace(plugin_name="ita_plugin"))
- previous = SimpleNamespace(m_cfg=cfg, stop=lambda: events.append("stop"))
- class Wrapper:
- def __init__(self, cls, config, redis):
- self.m_cfg = config
- def load(self):
- events.append("load")
- self.plugin = owner.create()
- def start(self):
- events.append("start")
- # Execute the unmodified host function without importing/starting main.
- node = next(n for n in ast.parse((ROOT / "main.py").read_text()).body
- if isinstance(n, ast.FunctionDef) and n.name == "reload_wrappers_for_plugin")
- namespace = {"Dict": dict, "GCOWrapper": Wrapper, "sys": sys, "REDIS_CFG": {}}
- exec(compile(ast.Module(body=[node], type_ignores=[]), str(ROOT / "main.py"), "exec"), namespace)
- wrappers = {"fixture": previous}
- namespace["reload_wrappers_for_plugin"]("ita_plugin", wrappers, {"fixture": object}, Mock())
- new = type(wrappers["fixture"].plugin).book.__globals__
- self.assertEqual(events, ["stop", "load", "start"])
- self.assertEqual(wrappers["fixture"].plugin.version, "1.0.200")
- for name in ("ItaPlugin", "ItaCapability", "OtpMailConfig", "CalendarRequestAudit", "FinalOutcomeUnknown",
- "fetch_otp_from_cloud", "book", "submit_final"):
- self.assertIsNot(new[name], old[name], name)
- self.assertIs(new["_OTP_INPUT_LOCK"], old["_OTP_INPUT_LOCK"])
- self.assertIs(new["_OTP_INPUT_LOCK"], ita_plugin._OTP_INPUT_LOCK)
- with old["_OTP_INPUT_LOCK"]:
- self.assertFalse(new["_OTP_INPUT_LOCK"].acquire(blocking=False))
- self.assertIs(type(first).book.__globals__, old)
- self.assertIs(new["BookingOutcomeUnknownError"], old["BookingOutcomeUnknownError"])
- if __name__ == "__main__":
- unittest.main()
|