Buckets:

download
raw
9.44 kB
"""Tests for the ``dispatch:`` block of the build-profile schema.
Covers the :class:`DispatchConfig` dataclass, its per-field validation in
:meth:`BuildProfile.validate`, the triggers-file resolution (profile-relative
or bundled preset name), the ``shared`` + multi-worker advisory warning, the
``_parse_profile`` round-trip, the ``osprey.``-prefixed bundled-template skip,
and the ``_KNOWN_PROFILE_KEYS`` membership.
"""
from __future__ import annotations
from pathlib import Path
from typing import Any
import pytest
from osprey.cli import build_profile as bp
from osprey.cli import build_profile_model
from osprey.cli.build_profile import (
BuildProfile,
DispatchConfig,
ServiceDef,
_parse_profile,
)
from osprey.errors import BuildProfileError
@pytest.fixture(autouse=True)
def _facility_data_tree(tmp_path: Path) -> None:
"""The tree every profile's ``data:`` key names, beside the profile.
``data:`` is required of every repo profile and must resolve to a real
directory, so without this each profile below would report one extra
failure about a key none of these tests is about.
"""
(tmp_path / "data").mkdir(exist_ok=True)
def _profile(**fields: Any) -> BuildProfile:
"""A :class:`BuildProfile` carrying the keys every deployment must spell.
Only ``data:`` so far: it names the facility data tree a build copies, and
a profile without it is refused before any of the per-field rules below is
reached. ``tests/cli/test_profile_data_key.py`` owns the ``data:`` rules
themselves.
"""
fields.setdefault("data", "data")
return BuildProfile(**fields)
def _write_triggers(tmp_path: Path, name: str = "trig.yml") -> str:
"""Write a minimal triggers file into ``tmp_path`` and return its name."""
(tmp_path / name).write_text("triggers: []", encoding="utf-8")
return name
def test_no_dispatch_validates(tmp_path: Path) -> None:
"""A profile with no dispatch block validates without raising."""
_profile(name="x").validate(tmp_path)
def test_valid_dispatch_validates(tmp_path: Path) -> None:
"""A dispatch with a profile-relative triggers file validates."""
triggers = _write_triggers(tmp_path)
profile = _profile(name="x", dispatch=DispatchConfig(triggers=triggers))
profile.validate(tmp_path)
def test_events_panel_with_dispatch_validates_without_url(tmp_path: Path) -> None:
"""A dispatch-backed ``events`` panel needs no manual ``web.panels.events.url``.
The URL is derived post-build from ``dispatch.dispatcher_port`` (after this
validator runs), so the validator must accept the url-less events panel when
a dispatch block is present rather than aborting the build.
"""
triggers = _write_triggers(tmp_path)
profile = _profile(
name="x",
web_panels=["events"],
dispatch=DispatchConfig(triggers=triggers),
)
profile.validate(tmp_path) # must not raise
def test_events_panel_without_dispatch_still_requires_url(tmp_path: Path) -> None:
"""The escape hatch is narrow: an ``events`` panel with no dispatch block and
no url override is still rejected (nothing would derive its URL)."""
profile = _profile(name="x", web_panels=["events"])
with pytest.raises(BuildProfileError, match="events"):
profile.validate(tmp_path)
def test_non_events_custom_panel_with_dispatch_still_requires_url(tmp_path: Path) -> None:
"""The dispatch escape hatch applies only to ``events`` — any other url-less
custom panel is still rejected even when a dispatch block is present."""
triggers = _write_triggers(tmp_path)
profile = _profile(
name="x",
web_panels=["grafana"],
dispatch=DispatchConfig(triggers=triggers),
)
with pytest.raises(BuildProfileError, match="grafana"):
profile.validate(tmp_path)
def test_worker_count_below_one_raises(tmp_path: Path) -> None:
triggers = _write_triggers(tmp_path)
profile = _profile(name="x", dispatch=DispatchConfig(triggers=triggers, worker_count=0))
with pytest.raises(BuildProfileError, match="worker_count"):
profile.validate(tmp_path)
def test_workspace_mode_invalid_raises(tmp_path: Path) -> None:
triggers = _write_triggers(tmp_path)
profile = _profile(
name="x",
dispatch=DispatchConfig(triggers=triggers, workspace_mode="weird"), # type: ignore[arg-type]
)
with pytest.raises(BuildProfileError, match="workspace_mode"):
profile.validate(tmp_path)
def test_port_overflow_raises(tmp_path: Path) -> None:
triggers = _write_triggers(tmp_path)
profile = _profile(
name="x",
dispatch=DispatchConfig(triggers=triggers, worker_port_base=65530, worker_count=10),
)
with pytest.raises(BuildProfileError, match="65535"):
profile.validate(tmp_path)
def test_triggers_missing_file_raises(tmp_path: Path) -> None:
profile = _profile(name="x", dispatch=DispatchConfig(triggers="does-not-exist.yml"))
with pytest.raises(BuildProfileError, match="triggers"):
profile.validate(tmp_path)
def test_triggers_empty_string_raises(tmp_path: Path) -> None:
profile = _profile(name="x", dispatch=DispatchConfig(triggers=""))
with pytest.raises(BuildProfileError, match="triggers"):
profile.validate(tmp_path)
def test_shared_multiworker_emits_warning(tmp_path: Path) -> None:
"""shared workspace + worker_count>1 warns but does not raise."""
triggers = _write_triggers(tmp_path)
profile = _profile(
name="x",
dispatch=DispatchConfig(triggers=triggers, workspace_mode="shared", worker_count=2),
)
with pytest.warns(UserWarning, match="shared"):
profile.validate(tmp_path)
def test_bundled_triggers_name_resolves(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""A bundled triggers name resolves via _triggers_dir, independent of profile_dir."""
triggers = tmp_path / "triggers"
triggers.mkdir()
(triggers / "tutorial_triggers.yml").write_text("triggers: []", encoding="utf-8")
monkeypatch.setattr(build_profile_model, "_triggers_dir", lambda: triggers)
profile_dir = tmp_path / "empty_profile"
profile_dir.mkdir()
(profile_dir / "data").mkdir()
profile = _profile(name="x", dispatch=DispatchConfig(triggers="tutorial_triggers.yml"))
profile.validate(profile_dir)
def test_parse_round_trip() -> None:
profile = _parse_profile({"name": "x", "dispatch": {"triggers": "t.yml", "worker_count": 3}})
assert profile.dispatch is not None
assert profile.dispatch.worker_count == 3
assert profile.dispatch.triggers == "t.yml"
assert profile.dispatch.dispatcher_port == 10010
assert profile.dispatch.workspace_mode == "isolated"
def test_inactivity_sec_below_one_raises(tmp_path: Path) -> None:
triggers = _write_triggers(tmp_path)
profile = _profile(name="x", dispatch=DispatchConfig(triggers=triggers, inactivity_sec=0))
with pytest.raises(BuildProfileError, match="inactivity_sec"):
profile.validate(tmp_path)
def test_inactivity_sec_parses_and_defaults() -> None:
# An explicit value round-trips through _parse_profile.
p = _parse_profile({"name": "x", "dispatch": {"triggers": "t.yml", "inactivity_sec": 600}})
assert p.dispatch is not None
assert p.dispatch.inactivity_sec == 600
# Absent -> matches the worker's built-in 120s inactivity-watchdog default.
p2 = _parse_profile({"name": "x", "dispatch": {"triggers": "t.yml"}})
assert p2.dispatch is not None
assert p2.dispatch.inactivity_sec == 120
def test_dispatch_not_a_mapping_raises() -> None:
with pytest.raises(BuildProfileError, match="dispatch"):
_parse_profile({"name": "x", "dispatch": "nope"})
def test_dispatch_is_known_key() -> None:
assert "dispatch" in bp._KNOWN_PROFILE_KEYS
def test_servicedef_osprey_prefix_skips_filesystem_check(tmp_path: Path) -> None:
"""An ``osprey.``-prefixed template skips the profile-dir check; others error."""
bundled = _profile(name="x", services={"ed": ServiceDef(template="osprey.event_dispatcher")})
bundled.validate(tmp_path) # no filesystem error despite no such dir
missing = _profile(name="x", services={"ed": ServiceDef(template="nonexistent-dir")})
with pytest.raises(BuildProfileError, match="template dir not found"):
missing.validate(tmp_path)
def test_control_assistant_readwrite_persona_ships_events_panel() -> None:
"""The control-assistant family exposes the event-dispatcher dashboard as
an ``events`` web panel on its write-capable tier: the readwrite persona
declares the panel id and nothing else — its URL is projected into the
persona's render from the hosting deployment's own, where the dispatch
injector derived it (``osprey.deployment.reach``), so no preset carries a
second copy the injector could drift from. The base preset carries
neither the id nor a URL."""
presets_dir = bp._presets_dir()
profile, _dir = bp.resolve_build_profile(None, preset="control-assistant-readwrite")
profile.validate(presets_dir) # raises BuildProfileError on any issue
assert "events" in profile.web_panels
assert "web.panels.events.url" not in profile.config
base, _dir = bp.resolve_build_profile(None, preset="control-assistant")
assert "events" not in base.web_panels
assert "web.panels.events.url" not in base.config

Xet Storage Details

Size:
9.44 kB
·
Xet hash:
7dc22bb4e9876b45758b79d4ba26c9dfed8dd6bd83a3ec14c43894407dfad92b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.