154 lines
5.1 KiB
Python
154 lines
5.1 KiB
Python
#!/usr/bin/env python3
|
||
"""TEST-ONLY fake docker/podman runtime for the WP-P4-13 contract smoke.
|
||
|
||
这不是容器,也不提供任何隔离!它只做三件事:
|
||
1. 记录宿主/测试传入的 runtime argv;
|
||
2. 校验 §7.3 命令形状(--rm/--name/--network none/--read-only/--tmpfs/单卷挂载/固定
|
||
request-response 路径),把 /vde/work 映射回 --volume 的宿主目录;
|
||
3. 用 --env 白名单(加少量基础变量)执行 VDE_FAKE_ENTRY 指向的本地服务。
|
||
|
||
真实 docker/podman e2e 见 container/real_container_e2e.sh/.ps1;本脚本的存在是为了在
|
||
没有容器运行时的机器上确定性地验证「命令构造 + 挂载 + 文件交换」契约,不能替代隔离测试。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import os
|
||
import subprocess
|
||
import sys
|
||
|
||
WORK_ROOT = "/vde/work"
|
||
BASE_ENV = ("PATH", "SystemRoot", "SYSTEMROOT", "TEMP", "TMP", "HOME", "LANG")
|
||
|
||
|
||
def log_record(record: dict) -> None:
|
||
path = os.environ.get("VDE_FAKE_RUNTIME_LOG", "")
|
||
if not path:
|
||
return
|
||
try:
|
||
with open(path, "a", encoding="utf-8") as handle:
|
||
handle.write(json.dumps(record, ensure_ascii=False) + "\n")
|
||
except OSError:
|
||
pass
|
||
|
||
|
||
def fail(message: str, code: int = 2) -> int:
|
||
print("[fake-runtime] error: " + message, file=sys.stderr)
|
||
log_record({"error": message})
|
||
return code
|
||
|
||
|
||
def map_container_path(host_root: str, value: str) -> str:
|
||
if value == WORK_ROOT:
|
||
return host_root
|
||
prefix = WORK_ROOT + "/"
|
||
if value.startswith(prefix):
|
||
return os.path.join(host_root, value[len(prefix):].replace("/", os.sep))
|
||
return value
|
||
|
||
|
||
def main(argv: list[str]) -> int:
|
||
log_record({"argv": argv})
|
||
if len(argv) < 2:
|
||
return fail("missing runtime subcommand")
|
||
mode = argv[1]
|
||
if mode == "rm":
|
||
return 0
|
||
if mode != "run":
|
||
return fail("only 'run' and 'rm' are simulated, got '%s'" % mode)
|
||
|
||
index = 2
|
||
volumes: list[str] = []
|
||
env_entries: list[str] = []
|
||
image = ""
|
||
entry = ""
|
||
while index < len(argv):
|
||
arg = argv[index]
|
||
if arg == "--rm":
|
||
index += 1
|
||
elif arg in ("--name", "--network", "--tmpfs"):
|
||
if index + 1 >= len(argv):
|
||
return fail(arg + " needs a value")
|
||
index += 2
|
||
elif arg == "--read-only":
|
||
index += 1
|
||
elif arg == "--volume":
|
||
if index + 1 >= len(argv):
|
||
return fail("--volume needs a value")
|
||
volumes.append(argv[index + 1])
|
||
index += 2
|
||
elif arg == "--env":
|
||
if index + 1 >= len(argv):
|
||
return fail("--env needs a value")
|
||
env_entries.append(argv[index + 1])
|
||
index += 2
|
||
elif arg.startswith("-"):
|
||
return fail("unsupported runtime option '%s'" % arg)
|
||
else:
|
||
image = arg
|
||
index += 1
|
||
break
|
||
if not image:
|
||
return fail("missing image")
|
||
if index >= len(argv):
|
||
return fail("missing in-image entry")
|
||
entry = argv[index]
|
||
index += 1
|
||
inner_args = argv[index:]
|
||
|
||
if len(volumes) != 1:
|
||
return fail("expected exactly one --volume, got %d" % len(volumes))
|
||
volume = volumes[0]
|
||
if ":" not in volume:
|
||
return fail("invalid --volume (expected host:container): " + volume)
|
||
host_root, container_root = volume.rsplit(":", 1)
|
||
if container_root != WORK_ROOT:
|
||
return fail("volume must mount at " + WORK_ROOT + ", got " + container_root)
|
||
if not os.path.isdir(host_root):
|
||
return fail("volume host directory does not exist: " + host_root)
|
||
|
||
mapped_inner = [map_container_path(host_root, value) for value in inner_args]
|
||
fake_entry = os.environ.get("VDE_FAKE_ENTRY", "")
|
||
fake_interpreter = os.environ.get("VDE_FAKE_INTERPRETER", "")
|
||
if not fake_entry:
|
||
return fail("VDE_FAKE_ENTRY is not set (test harness configuration)")
|
||
command = ([fake_interpreter] if fake_interpreter else []) + [fake_entry] + mapped_inner
|
||
|
||
child_env = {name: os.environ[name] for name in BASE_ENV if name in os.environ}
|
||
for item in env_entries:
|
||
if "=" not in item:
|
||
return fail("invalid --env (expected NAME=VALUE): " + item)
|
||
name, value = item.split("=", 1)
|
||
child_env[name] = value
|
||
|
||
log_record(
|
||
{
|
||
"image": image,
|
||
"entry": entry,
|
||
"mounted_at": container_root,
|
||
"inner_args": inner_args,
|
||
"mapped_inner_args": mapped_inner,
|
||
"env_names": sorted(env_entries),
|
||
"host_command": command,
|
||
}
|
||
)
|
||
completed = subprocess.run(
|
||
command,
|
||
cwd=host_root,
|
||
env=child_env,
|
||
capture_output=True,
|
||
text=True,
|
||
encoding="utf-8",
|
||
errors="replace",
|
||
)
|
||
log_record({"exit": completed.returncode, "stdout": completed.stdout, "stderr": completed.stderr})
|
||
if completed.stdout:
|
||
sys.stdout.write(completed.stdout)
|
||
if completed.stderr:
|
||
sys.stderr.write(completed.stderr)
|
||
return completed.returncode
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main(sys.argv))
|