56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
"""Shared fixtures for credential-isolated Hermes SCM broker tests."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import io
|
|
import sys
|
|
from email.message import Message
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).parents[2]
|
|
SCRIPTS = ROOT / "services/hermes/scm-common/scripts"
|
|
sys.path.insert(0, str(SCRIPTS))
|
|
|
|
|
|
def _load(name: str):
|
|
path = SCRIPTS / f"{name}.py"
|
|
spec = importlib.util.spec_from_file_location(f"test_{name}", path)
|
|
assert spec and spec.loader
|
|
module = importlib.util.module_from_spec(spec)
|
|
sys.modules[spec.name] = module
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
def _load_path(name: str, path: Path):
|
|
spec = importlib.util.spec_from_file_location(name, path)
|
|
assert spec and spec.loader
|
|
module = importlib.util.module_from_spec(spec)
|
|
sys.modules[spec.name] = module
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
class Response:
|
|
def __init__(self, body: bytes, *, status: int = 200, content_type: str):
|
|
self.body = body
|
|
self.stream = io.BytesIO(body)
|
|
self.status = status
|
|
self.headers = Message()
|
|
self.headers["Content-Type"] = content_type
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *_args):
|
|
return False
|
|
|
|
def read(self, limit=-1):
|
|
return self.stream.read(limit)
|
|
|
|
|
|
def _receive_command(old: bytes, new: bytes, ref: bytes) -> bytes:
|
|
command = old + b" " + new + b" " + ref + b"\x00report-status\n"
|
|
return f"{len(command) + 4:04x}".encode() + command + b"0000PACK"
|