test_ita_single_file.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. """Offline single-artifact install, factory loading, hot reload and move parity."""
  2. import ast
  3. import builtins
  4. from contextlib import ExitStack, redirect_stdout
  5. from dataclasses import asdict
  6. from datetime import date
  7. import hashlib
  8. from io import StringIO
  9. import json
  10. import os
  11. from pathlib import Path
  12. import sys
  13. from tempfile import TemporaryDirectory
  14. from types import SimpleNamespace
  15. import unittest
  16. from unittest.mock import Mock, patch
  17. from plugins import ita_plugin # Preload shared host dependencies, not a loader substitute.
  18. from test.ita_source_sections import section
  19. from test.test_ita_calendar_book import DAY, SERVICE, FakePage, packets
  20. from test.test_ita_protocol import live_route
  21. from tools import consolidate_ita_plugin as migration
  22. from tools import ita_query_book_local as local
  23. from utils import plugin_updater
  24. from vs_plg_factory import VSPlgFactory
  25. from vs_types import AppointmentType, BookingOutcomeUnknownError, VSQueryResult
  26. ROOT = Path(__file__).resolve().parents[1]
  27. class NormalizeOriginal(ast.NodeTransformer):
  28. """Independent structural check: only the explicitly allowed wiring edits."""
  29. def __init__(self, module):
  30. self.module = module
  31. self.renames = migration.RENAMES.get(module, {})
  32. def visit_ImportFrom(self, node):
  33. if node.module == "__future__":
  34. return None
  35. if node.module and node.module.startswith("plugins.ita_"):
  36. return [ast.Assign(targets=[ast.Name(id=a.asname, ctx=ast.Store())],
  37. value=ast.Name(id=a.name, ctx=ast.Load()))
  38. for a in node.names if a.asname]
  39. if node.module == "plugins":
  40. assert self.module == "ita_workflow"
  41. return None
  42. if self.module == "ita_protocol" and node.module == "datetime":
  43. node.names[-1].asname = "datetime_time"
  44. return node
  45. def visit_Assign(self, node):
  46. if any(isinstance(t, ast.Name) and t.id == "__all__" for t in node.targets):
  47. return None
  48. if any(isinstance(t, ast.Name) and t.id == "_OTP_INPUT_LOCK" for t in node.targets):
  49. return ast.parse('import vs_plg as _ita_host\n'
  50. '_OTP_INPUT_LOCK = vars(_ita_host).setdefault("_coordinate_ita_otp_input_lock", Lock())').body
  51. if (self.module == "ita_plugin" and any(isinstance(t, ast.Name) and t.id == "version" for t in node.targets)):
  52. node.value = ast.Constant(value="1.0.1")
  53. return self.generic_visit(node)
  54. def visit_Name(self, node):
  55. node.id = self.renames.get(node.id, node.id)
  56. return node
  57. def visit_FunctionDef(self, node):
  58. node.name = self.renames.get(node.name, node.name)
  59. return self.generic_visit(node)
  60. def visit_Attribute(self, node):
  61. if self.module == "ita_workflow" and isinstance(node.value, ast.Name) and node.value.id == "preparation":
  62. return ast.Name(id=node.attr, ctx=node.ctx)
  63. return self.generic_visit(node)
  64. class RelocationParityTests(unittest.TestCase):
  65. def test_every_original_section_is_structurally_unchanged_except_wiring(self):
  66. for module in migration.MODULES:
  67. with self.subTest(module=module):
  68. expected = NormalizeOriginal(module).visit(ast.parse(migration.original(module)))
  69. actual = ast.parse(section(module.removeprefix("ita_")))
  70. self.assertEqual(ast.dump(expected), ast.dump(actual))
  71. def test_relocation_and_manifest_are_reproducible(self):
  72. source, manifest = migration.assemble()
  73. self.assertEqual(source, (ROOT / "plugins/ita_plugin.py").read_text())
  74. self.assertEqual(manifest, json.loads((ROOT / "docs/ita-single-file-migration.json").read_text()))
  75. def test_runtime_has_no_private_italy_or_tool_imports_or_dynamic_packaging(self):
  76. tree = ast.parse((ROOT / "plugins/ita_plugin.py").read_text())
  77. for node in ast.walk(tree):
  78. if isinstance(node, (ast.Import, ast.ImportFrom)):
  79. names = [node.module or ""] if isinstance(node, ast.ImportFrom) else [a.name for a in node.names]
  80. self.assertFalse(any(name == "plugins" or name.startswith(("plugins.", "tools.")) for name in names), names)
  81. if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
  82. self.assertNotIn(node.func.id, {"exec", "eval", "__import__"})
  83. self.assertEqual(sorted(p.name for p in (ROOT / "plugins").glob("ita_*.py")), ["ita_plugin.py"])
  84. class SingleFileInstallationTests(unittest.TestCase):
  85. def setUp(self):
  86. self.stack = ExitStack()
  87. self.addCleanup(self.stack.close)
  88. self.directory = Path(self.stack.enter_context(TemporaryDirectory()))
  89. self.source = (ROOT / "plugins/ita_plugin.py").read_text()
  90. self.path = self.directory / "ita_plugin.py"
  91. old_cwd = Path.cwd()
  92. os.chdir(self.directory)
  93. self.stack.callback(os.chdir, old_cwd)
  94. self.stack.enter_context(redirect_stdout(StringIO()))
  95. old_module = sys.modules.get("ita_plugin")
  96. def restore_module():
  97. sys.modules.pop("ita_plugin", None)
  98. if old_module is not None:
  99. sys.modules["ita_plugin"] = old_module
  100. self.stack.callback(restore_module)
  101. original_import = builtins.__import__
  102. def guarded_import(name, *args, **kwargs):
  103. if name == "plugins" or name.startswith(("plugins.", "tools.")):
  104. raise AssertionError("Single artifact attempted a private runtime import: " + name)
  105. return original_import(name, *args, **kwargs)
  106. self.stack.enter_context(patch("builtins.__import__", side_effect=guarded_import))
  107. for target in ("socket.socket.connect", "socket.create_connection", "requests.sessions.Session.request",
  108. "subprocess.Popen"):
  109. self.stack.enter_context(patch(target, side_effect=AssertionError("Unexpected external I/O")))
  110. self.stack.enter_context(patch.object(sys, "dont_write_bytecode", True))
  111. self.factory = VSPlgFactory()
  112. self.factory.register_plugin("ita_plugin", str(self.path), "ItaPlugin")
  113. self.install(self.source, "1.0.1")
  114. def install(self, source, version):
  115. self.assertTrue(plugin_updater.download_and_install_plugin(
  116. "ita_plugin", version, code_content=source, plugins_dir=str(self.directory),
  117. expected_sha256=hashlib.sha256(source.encode()).hexdigest()))
  118. def create(self):
  119. plugin = self.factory.create("offline-single-file", "ita_plugin")
  120. self.stack.callback(plugin.cleanup)
  121. return plugin
  122. def test_only_one_uploaded_file_loads_all_runtime_sections(self):
  123. self.assertEqual([p.name for p in self.directory.glob("*.py")], ["ita_plugin.py"])
  124. plugin = self.create()
  125. runtime = type(plugin).book.__globals__
  126. for name in ("ItaRouteConfig", "CalendarRequestAudit", "FinalReceiptReadPort", "FinalBookingAttempt",
  127. "parse_otp_body", "prepare_calendar", "recheck_calendar_before_final", "submit_final"):
  128. self.assertEqual(runtime[name].__module__, "ita_plugin", name)
  129. self.assertIs(runtime["BookingOutcomeUnknownError"], BookingOutcomeUnknownError)
  130. self.assertIsInstance(runtime["FinalOutcomeUnknown"](), BookingOutcomeUnknownError)
  131. self.assertEqual(runtime["parse_otp_body"]("OTP Code:001234"), "001234")
  132. self.assertEqual(plugin_updater.get_installed_plugins(str(self.directory))["ita_plugin"], "1.0.1")
  133. self.assertIsNot(runtime["ItaCapability"], ita_plugin.ItaCapability)
  134. def test_public_query_and_book_bind_the_loaded_file_not_canonical_import(self):
  135. plugin = self.create()
  136. plugin.page = SimpleNamespace(url="https://prenotami.esteri.it/UserArea", quit=Mock())
  137. plugin.free_config = {"otp_mode": "cloud", "query_mode": "calendar"}
  138. negative = VSQueryResult(success=False)
  139. runtime = type(plugin).book.__globals__
  140. with patch.dict(runtime, prepare_query_if_needed=Mock(return_value=negative), book=Mock(return_value="loaded-book")):
  141. self.assertIs(plugin.query(AppointmentType(routing_key="fixture")), negative)
  142. self.assertEqual(plugin.book(negative, {"username": "fixture@example.test"}), "loaded-book")
  143. runtime["prepare_query_if_needed"].assert_called_once()
  144. runtime["book"].assert_called_once()
  145. def test_local_final_fence_uses_instance_types_and_functions_after_cache_replacement(self):
  146. plugin = self.create()
  147. runtime = type(plugin).book.__globals__
  148. plugin.page = FakePage()
  149. plugin.page.quit = Mock()
  150. availability, slots = packets()
  151. approval = runtime["select_slot_before_final"](
  152. plugin.page, target_day=DAY, availability_packet=availability, time_slots_packet=slots)
  153. plugin.last_booking_precommit = approval
  154. self.assertEqual(local.verify_plugin_selection(plugin), approval)
  155. key = "slot.fixture.it.tourist"
  156. config = {key: live_route()}
  157. route = runtime["resolve_route_config"](config, key)
  158. configured_route = ita_plugin.resolve_route_config(config, key)
  159. self.assertNotEqual(route, configured_route) # Distinct loaded dataclass types.
  160. self.assertEqual(asdict(route), asdict(configured_route))
  161. applicant = SimpleNamespace(username="applicant@example.test")
  162. plugin.config = SimpleNamespace(account=applicant)
  163. target = runtime["FinalTarget"](applicant.username, "fixture-session", "fixture-tab", key,
  164. str(route.service_id), SERVICE, approval)
  165. audit = Mock()
  166. audit.settled_snapshot.return_value = {"finalRequests": 0}
  167. observed = {"book": SimpleNamespace(plugin=plugin, audit=audit)}
  168. control = local.LocalFinalSubmission(applicant, configured_route, root=self.directory / "claims")
  169. native = Mock(return_value="synthetic-dispatch")
  170. def production(actual_plugin, actual_route, *, request_audit):
  171. self.assertIs(actual_plugin, plugin)
  172. self.assertIs(actual_route, route)
  173. self.assertIs(request_audit, audit)
  174. return runtime["NativeFinalSubmitter"](plugin.page, target, lambda: True)()
  175. self.create() # The host overwrites sys.modules['ita_plugin'] for Query.
  176. self.assertIsNot(sys.modules["ita_plugin"].ItaPlugin, type(plugin))
  177. with patch.dict(runtime, NativeFinalSubmitter=Mock(return_value=native)):
  178. self.assertEqual(control.submit(plugin, route, observed, production), "synthetic-dispatch")
  179. self.assertTrue(control.claim_path.is_file())
  180. self.assertTrue(control.possibly_submitted)
  181. native.assert_called_once_with()
  182. with self.assertRaises(local.LocalAcceptanceError):
  183. control.submit(plugin, route, observed, production)
  184. native.assert_called_once_with()
  185. def test_local_observer_preserves_loaded_unknown_error_for_shared_booker(self):
  186. plugin = self.create()
  187. runtime = type(plugin).book.__globals__
  188. observer = local.ObservedPlugin(plugin, "book")
  189. plugin.book = Mock(side_effect=runtime["FinalOutcomeUnknown"]())
  190. with self.assertRaises(BookingOutcomeUnknownError):
  191. observer.book(VSQueryResult(), {})
  192. self.assertEqual(observer.error, "FinalOutcomeUnknown")
  193. def test_real_reload_replaces_code_and_preserves_process_wide_otp_lock(self):
  194. first = self.create()
  195. old = type(first).book.__globals__
  196. updated = self.source.replace('version = "1.0.1"', 'version = "1.0.200"')
  197. self.install(updated, "1.0.200")
  198. owner = self
  199. events = []
  200. cfg = SimpleNamespace(plugin_config=SimpleNamespace(plugin_name="ita_plugin"))
  201. previous = SimpleNamespace(m_cfg=cfg, stop=lambda: events.append("stop"))
  202. class Wrapper:
  203. def __init__(self, cls, config, redis):
  204. self.m_cfg = config
  205. def load(self):
  206. events.append("load")
  207. self.plugin = owner.create()
  208. def start(self):
  209. events.append("start")
  210. # Execute the unmodified host function without importing/starting main.
  211. node = next(n for n in ast.parse((ROOT / "main.py").read_text()).body
  212. if isinstance(n, ast.FunctionDef) and n.name == "reload_wrappers_for_plugin")
  213. namespace = {"Dict": dict, "GCOWrapper": Wrapper, "sys": sys, "REDIS_CFG": {}}
  214. exec(compile(ast.Module(body=[node], type_ignores=[]), str(ROOT / "main.py"), "exec"), namespace)
  215. wrappers = {"fixture": previous}
  216. namespace["reload_wrappers_for_plugin"]("ita_plugin", wrappers, {"fixture": object}, Mock())
  217. new = type(wrappers["fixture"].plugin).book.__globals__
  218. self.assertEqual(events, ["stop", "load", "start"])
  219. self.assertEqual(wrappers["fixture"].plugin.version, "1.0.200")
  220. for name in ("ItaPlugin", "ItaCapability", "OtpMailConfig", "CalendarRequestAudit", "FinalOutcomeUnknown",
  221. "fetch_otp_from_cloud", "book", "submit_final"):
  222. self.assertIsNot(new[name], old[name], name)
  223. self.assertIs(new["_OTP_INPUT_LOCK"], old["_OTP_INPUT_LOCK"])
  224. self.assertIs(new["_OTP_INPUT_LOCK"], ita_plugin._OTP_INPUT_LOCK)
  225. with old["_OTP_INPUT_LOCK"]:
  226. self.assertFalse(new["_OTP_INPUT_LOCK"].acquire(blocking=False))
  227. self.assertIs(type(first).book.__globals__, old)
  228. self.assertIs(new["BookingOutcomeUnknownError"], old["BookingOutcomeUnknownError"])
  229. if __name__ == "__main__":
  230. unittest.main()