Skip to content

Commit 5b1a7f9

Browse files
committed
test: add unit tests for nested_zip_loader
1 parent d9ddb3c commit 5b1a7f9

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

tests/test_nested_zip_loader.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import importlib
2+
import multiprocessing
3+
import shutil
4+
import sys
5+
import tempfile
6+
import zipfile
7+
from pathlib import Path
8+
9+
import pytest
10+
11+
# We have to use importlib here because nested_zip_loader calls load_nested_zip
12+
# at IMPORT TIME, which causes us a world of hurt in these tests if we try to
13+
# import it "normally" here.
14+
LOADER_PATH = Path(__file__).parent.parent / "package_python_function" / "nested_zip_loader.py"
15+
PKG_NAME = "_test_nested_zip"
16+
17+
def _make_deps_zip(path: Path) -> None:
18+
with zipfile.ZipFile(path, "w") as zf:
19+
zf.writestr(f"{PKG_NAME}/__init__.py", "LOADED = True\n")
20+
21+
@pytest.fixture()
22+
def lambda_env(tmp_path, monkeypatch):
23+
"""Simulate a Lambda-like layout: a task dir with a package whose __init__.py
24+
is the nested_zip_loader code, and a .dependencies.zip with the 'real' code."""
25+
task_dir = tmp_path / "task"
26+
pkg_dir = task_dir / PKG_NAME
27+
pkg_dir.mkdir(parents=True)
28+
shutil.copy(LOADER_PATH, pkg_dir / "__init__.py")
29+
_make_deps_zip(pkg_dir / ".dependencies.zip")
30+
31+
tmp_dir = tmp_path / "tmp"
32+
tmp_dir.mkdir()
33+
monkeypatch.setenv("TMPDIR", str(tmp_dir))
34+
tempfile.tempdir = None
35+
36+
monkeypatch.syspath_prepend(str(task_dir))
37+
38+
yield tmp_path
39+
40+
sys.modules.pop(PKG_NAME, None)
41+
tempfile.tempdir = None
42+
43+
def test_cold_start_extracts(lambda_env):
44+
mod = importlib.import_module(PKG_NAME)
45+
assert mod.LOADED is True
46+
assert (lambda_env / "tmp" / "package-python-function").exists()
47+
48+
def test_warm_start_skips_extraction(lambda_env):
49+
target_pkg = lambda_env / "tmp" / "package-python-function" / PKG_NAME
50+
target_pkg.mkdir(parents=True)
51+
(target_pkg / "__init__.py").write_text("LOADED = 'warm'\n")
52+
53+
mod = importlib.import_module(PKG_NAME)
54+
assert mod.LOADED == "warm"
55+
56+
def test_stale_staging_cleaned(lambda_env):
57+
staging = lambda_env / "tmp" / ".stage.package-python-function"
58+
staging.mkdir(parents=True)
59+
(staging / "stale.txt").write_text("leftover")
60+
61+
importlib.import_module(PKG_NAME)
62+
assert not staging.exists()
63+
64+
def _worker(task_dir):
65+
import importlib
66+
import sys
67+
68+
sys.path.insert(0, task_dir)
69+
assert importlib.import_module(PKG_NAME).LOADED is True
70+
71+
def test_concurrent_no_race(lambda_env):
72+
ctx = multiprocessing.get_context("forkserver")
73+
procs = [ctx.Process(target=_worker, args=(str(lambda_env / "task"),)) for _ in range(2)]
74+
for p in procs:
75+
p.start()
76+
for p in procs:
77+
p.join(timeout=10)
78+
assert p.exitcode == 0, "A race condition occured while extracting."
79+
assert (lambda_env / "tmp" / "package-python-function").exists()

0 commit comments

Comments
 (0)