Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /stateset_stateset-agents /tests /unit /fake_modal.py
| """A behavioural stand-in for the ``modal`` SDK. | |
| This is not a mock. It *executes* the function it is given and *stores* the | |
| files that function writes, so tests can assert on real effects — an adapter | |
| directory actually arriving on local disk — rather than on recorded call | |
| kwargs. Asserting on recorded kwargs is what let an earlier version of | |
| ``ModalExecutor`` report SUCCEEDED without running anything. | |
| It models only the surface ``ModalExecutor`` uses, per Modal's reference docs: | |
| ``Image.debian_slim().pip_install()``, ``Volume.from_name(create_if_missing=)`` | |
| with batched upload/commit/reload/list/read, named Secrets, | |
| ``App.function(...)`` as a decorator, ``app.run()`` as a context manager, and | |
| ``Function.remote()``. | |
| """ | |
| from __future__ import annotations | |
| import contextlib | |
| import shutil | |
| from collections.abc import Callable, Iterator | |
| from dataclasses import dataclass, field | |
| from pathlib import Path | |
| from typing import Any | |
| class FakeImage: | |
| """Chainable image builder that records what was installed.""" | |
| def __init__(self) -> None: | |
| self.installed: list[str] = [] | |
| self.local_dirs_added: list[Any] = [] | |
| def debian_slim(self, **kwargs: Any) -> FakeImage: | |
| return self | |
| def pip_install(self, *packages: str, **kwargs: Any) -> FakeImage: | |
| self.installed.extend(packages) | |
| return self | |
| # Present so tests can assert they are never called — a working-tree sync | |
| # is the failure mode this design exists to avoid. | |
| def add_local_dir(self, *args: Any, **kwargs: Any) -> FakeImage: | |
| self.local_dirs_added.append(args) | |
| return self | |
| def add_local_python_source(self, *args: Any, **kwargs: Any) -> FakeImage: | |
| self.local_dirs_added.append(args) | |
| return self | |
| class FakeEntry: | |
| """One file in a volume, mirroring modal's FileEntry.""" | |
| path: str | |
| class FakeVolume: | |
| """A volume backed by a real directory on local disk.""" | |
| _instances: dict[str, FakeVolume] = {} | |
| deleted_names: list[str] = [] | |
| def __init__(self, name: str, root: Path) -> None: | |
| self.name = name | |
| self.root = root | |
| self.storage_root = root | |
| self.root.mkdir(parents=True, exist_ok=True) | |
| self.reload_count = 0 | |
| self.commit_count = 0 | |
| def bind(cls, root: Path) -> Callable[..., FakeVolume]: | |
| """Return a ``from_name`` that materialises volumes under ``root``.""" | |
| def from_name( | |
| name: str, *, create_if_missing: bool = False, **kwargs: Any | |
| ) -> FakeVolume: | |
| if name not in cls._instances: | |
| if not create_if_missing: | |
| raise KeyError(f"volume {name!r} does not exist") | |
| cls._instances[name] = cls(name, root / name) | |
| return cls._instances[name] | |
| return from_name | |
| def reset(cls) -> None: | |
| cls._instances.clear() | |
| cls.deleted_names.clear() | |
| def reload(self) -> None: | |
| self.reload_count += 1 | |
| def commit(self) -> None: | |
| self.commit_count += 1 | |
| def batch_upload(self) -> Iterator[FakeVolumeUpload]: | |
| yield FakeVolumeUpload(self) | |
| def iterdir(self, path: str, *, recursive: bool = True) -> Iterator[FakeEntry]: | |
| base = self.root / path.lstrip("/") | |
| if not base.exists(): | |
| return | |
| for item in sorted(base.rglob("*") if recursive else base.iterdir()): | |
| if item.is_file(): | |
| # Modal returns container-relative POSIX paths regardless of | |
| # the client OS running the SDK. | |
| yield FakeEntry(path=item.relative_to(self.root).as_posix()) | |
| def read_file(self, path: str) -> Iterator[bytes]: | |
| yield (self.root / path).read_bytes() | |
| class FakeVolumeUpload: | |
| """Write local files into a fake Volume using Modal's upload surface.""" | |
| volume: FakeVolume | |
| def put_file(self, local_path: str, remote_path: str) -> None: | |
| target = self.volume.root / remote_path.lstrip("/") | |
| target.parent.mkdir(parents=True, exist_ok=True) | |
| shutil.copyfile(local_path, target) | |
| class FakeFunction: | |
| """A registered function that really runs when ``.remote()`` is called.""" | |
| fn: Callable[..., Any] | |
| kwargs: dict[str, Any] | |
| app: FakeApp | |
| def _rewrite_paths(value: Any, source: str, target: str) -> Any: | |
| """Translate container paths to the fake host mount and back.""" | |
| if isinstance(value, str): | |
| return value.replace(source, target) | |
| if isinstance(value, dict): | |
| return { | |
| key: FakeFunction._rewrite_paths(item, source, target) | |
| for key, item in value.items() | |
| } | |
| if isinstance(value, list): | |
| return [FakeFunction._rewrite_paths(item, source, target) for item in value] | |
| if isinstance(value, tuple): | |
| return tuple( | |
| FakeFunction._rewrite_paths(item, source, target) for item in value | |
| ) | |
| return value | |
| def remote(self, *args: Any, **kwargs: Any) -> Any: | |
| if not self.app.running: | |
| raise RuntimeError("function called outside of app.run()") | |
| self.app.calls.append((args, kwargs)) | |
| translated_args = args | |
| translated_kwargs = kwargs | |
| translations: list[tuple[str, str]] = [] | |
| # Container mounts are POSIX paths even when this fake runs on a | |
| # Windows host. Translate them to the temporary host-backed Volume | |
| # while executing, then translate returned paths and logs back. | |
| for mount, volume in (self.kwargs.get("volumes") or {}).items(): | |
| host_mount = str(volume.root) | |
| translated_args = self._rewrite_paths(translated_args, mount, host_mount) | |
| translated_kwargs = self._rewrite_paths( | |
| translated_kwargs, mount, host_mount | |
| ) | |
| translations.append((host_mount, mount)) | |
| result = self.fn(*translated_args, **translated_kwargs) | |
| for host_mount, mount in translations: | |
| result = self._rewrite_paths(result, host_mount, mount) | |
| return result | |
| def spawn(self, *args: Any, **kwargs: Any) -> Any: | |
| raise NotImplementedError("spawn is not modelled") | |
| class FakeApp: | |
| """Records registered functions and models the ephemeral run context.""" | |
| name: str = "" | |
| running: bool = False | |
| functions: list[FakeFunction] = field(default_factory=list) | |
| calls: list[Any] = field(default_factory=list) | |
| def function(self, **kwargs: Any) -> Callable[[Callable[..., Any]], FakeFunction]: | |
| def decorator(fn: Callable[..., Any]) -> FakeFunction: | |
| registered = FakeFunction(fn=fn, kwargs=kwargs, app=self) | |
| self.functions.append(registered) | |
| return registered | |
| return decorator | |
| def run(self, *args: Any, **kwargs: Any) -> Iterator[FakeApp]: | |
| self.running = True | |
| try: | |
| yield self | |
| finally: | |
| self.running = False | |
| def build(volume_root: Path) -> Any: | |
| """Construct a fake ``modal`` module whose volumes live under ``volume_root``.""" | |
| import types | |
| module = types.ModuleType("modal") | |
| module.Image = FakeImage() | |
| async def delete(name: str, *, allow_missing: bool = False, **kwargs: Any) -> None: | |
| volume = FakeVolume._instances.pop(name, None) | |
| if volume is None: | |
| if allow_missing: | |
| return | |
| raise KeyError(name) | |
| shutil.rmtree(volume.root, ignore_errors=True) | |
| if volume.storage_root != volume.root: | |
| shutil.rmtree(volume.storage_root, ignore_errors=True) | |
| FakeVolume.deleted_names.append(name) | |
| module.Volume = types.SimpleNamespace( | |
| from_name=FakeVolume.bind(volume_root), | |
| objects=types.SimpleNamespace(delete=delete), | |
| ) | |
| class FakeSecret: | |
| name: str | |
| module.Secret = types.SimpleNamespace(from_name=lambda name: FakeSecret(name)) | |
| apps: list[FakeApp] = [] | |
| def make_app(name: str = "", **kwargs: Any) -> FakeApp: | |
| app = FakeApp(name=name) | |
| apps.append(app) | |
| return app | |
| module.App = make_app | |
| module.apps = apps | |
| FakeVolume.reset() | |
| return module | |
Xet Storage Details
- Size:
- 8.37 kB
- Xet hash:
- 1ff1aadbe5f135e697e83d5c183f17007dcb587f7c470f9a123a689369d5a3cc
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.