mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-26 19:05:13 +00:00
fix(core): load every service config through one launch-context function
This commit is contained in:
@@ -59,7 +59,7 @@ def built_ctx(monkeypatch):
|
||||
seen["ctx"] = parent_ctx
|
||||
return object()
|
||||
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda service: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda service: {})
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda *args: None)
|
||||
monkeypatch.setattr(handlers.Services, "load", lambda service: None)
|
||||
monkeypatch.setattr(handlers, "instantiate_service", fake_instantiate)
|
||||
|
||||
@@ -832,7 +832,7 @@ def test_a_remote_session_puts_the_flag_in_the_service_ctx(monkeypatch: pytest.M
|
||||
seen["ctx"] = parent_ctx
|
||||
return object()
|
||||
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda service: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda service: {})
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda *args: None)
|
||||
monkeypatch.setattr(handlers.Services, "load", lambda service: None)
|
||||
monkeypatch.setattr(handlers, "instantiate_service", fake_instantiate)
|
||||
|
||||
@@ -62,7 +62,7 @@ def rest_handlers(monkeypatch: pytest.MonkeyPatch) -> tuple[Any, dict]:
|
||||
seen["ctx"] = parent_ctx
|
||||
return SimpleNamespace(authenticate=lambda cookies, credential: None)
|
||||
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda tag: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda tag: {})
|
||||
monkeypatch.setattr(handlers, "resolve_handler_proxy", lambda *a: (None, []))
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
|
||||
monkeypatch.setattr(handlers.Services, "load", lambda tag: SimpleNamespace())
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
"""The service config that a Service starts with: its config.yaml, with the user's ``services.<TAG>`` block over it."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Callable
|
||||
|
||||
import click
|
||||
import pytest
|
||||
|
||||
from tests.core.test_launch_context import cli_dl, cli_search
|
||||
from unshackle.core import launch
|
||||
from unshackle.core.config import config
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def service_dir(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path:
|
||||
from unshackle.core.services import Services
|
||||
|
||||
monkeypatch.setattr(Services, "get_path", staticmethod(lambda tag: tmp_path))
|
||||
return tmp_path
|
||||
|
||||
|
||||
def test_an_empty_config_yaml_takes_the_user_block(monkeypatch: pytest.MonkeyPatch, service_dir: Path) -> None:
|
||||
(service_dir / config.filenames.config).write_text("")
|
||||
monkeypatch.setattr(config, "services", {"EXAMPLE": {"region": "us"}})
|
||||
assert launch.load_service_config("EXAMPLE") == {"region": "us"}
|
||||
|
||||
|
||||
def test_a_user_block_matches_the_tag_in_any_case(monkeypatch: pytest.MonkeyPatch, service_dir: Path) -> None:
|
||||
(service_dir / config.filenames.config).write_text("region: gb\nendpoints:\n api: a\n")
|
||||
monkeypatch.setattr(config, "services", {"example": {"region": "us"}})
|
||||
assert launch.load_service_config("EXAMPLE") == {"region": "us", "endpoints": {"api": "a"}}
|
||||
|
||||
|
||||
def test_a_remote_service_gets_the_user_block_only(monkeypatch: pytest.MonkeyPatch, service_dir: Path) -> None:
|
||||
(service_dir / config.filenames.config).write_text("region: gb\n")
|
||||
monkeypatch.setattr(config, "services", {"EXAMPLE": {"profile": "a"}})
|
||||
assert launch.load_service_config("EXAMPLE", local=False) == {"profile": "a"}
|
||||
|
||||
|
||||
def test_each_caller_gets_its_own_copy(monkeypatch: pytest.MonkeyPatch, service_dir: Path) -> None:
|
||||
(service_dir / config.filenames.config).write_text("endpoints:\n api: a\n")
|
||||
monkeypatch.setattr(config, "services", {"EXAMPLE": {"hosts": ["h1"]}})
|
||||
first = launch.load_service_config("EXAMPLE")
|
||||
first["endpoints"]["api"] = "changed"
|
||||
first["hosts"].append("h2")
|
||||
assert launch.load_service_config("EXAMPLE") == {"endpoints": {"api": "a"}, "hosts": ["h1"]}
|
||||
assert config.services == {"EXAMPLE": {"hosts": ["h1"]}}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("build", [cli_dl, cli_search], ids=["dl", "search"])
|
||||
def test_the_cli_starts_a_service_with_an_empty_config_yaml_and_a_user_block(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, build: Callable[[pytest.MonkeyPatch, Path], click.Context]
|
||||
) -> None:
|
||||
(tmp_path / config.filenames.config).write_text("")
|
||||
monkeypatch.setattr(config, "services", {"example": {"region": "us"}})
|
||||
ctx = build(monkeypatch, tmp_path)
|
||||
assert ctx.obj.config == {"region": "us"}
|
||||
@@ -58,7 +58,7 @@ def session_params(monkeypatch: pytest.MonkeyPatch, data: dict) -> dict:
|
||||
seen["params"] = parent_ctx.params
|
||||
return object()
|
||||
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda service: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda service: {})
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda *args: None)
|
||||
monkeypatch.setattr(handlers.Services, "load", lambda service: None)
|
||||
monkeypatch.setattr(handlers, "instantiate_service", fake_instantiate)
|
||||
|
||||
@@ -170,7 +170,7 @@ def test_a_relaying_client_gets_a_stand_in_with_its_device_facts(monkeypatch: py
|
||||
class Declares:
|
||||
CLIENT_CONFIG = ("esn",)
|
||||
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda tag: {"esn": "SERVER"})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda tag: {"esn": "SERVER"})
|
||||
monkeypatch.setattr(
|
||||
handlers,
|
||||
"served_context",
|
||||
@@ -331,7 +331,7 @@ def test_a_lent_device_gives_the_service_the_server_config_back(monkeypatch: pyt
|
||||
CLIENT_CONFIG = ("esn", "esn_map")
|
||||
|
||||
monkeypatch.setattr(
|
||||
handlers, "load_service_yaml", lambda tag: {"esn": "SERVER", "esn_map": {7000: "s"}, "other": 1}
|
||||
handlers, "load_service_config", lambda tag: {"esn": "SERVER", "esn_map": {7000: "s"}, "other": 1}
|
||||
)
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: server_device)
|
||||
monkeypatch.setattr(handlers, "served_context", lambda obj, *a: built.update(cdm=obj.cdm, config=obj.config))
|
||||
|
||||
@@ -181,7 +181,7 @@ def test_setup_list_service_threads_request(monkeypatch):
|
||||
def authenticate(self, cookies, credential):
|
||||
seen["authenticated"] = True
|
||||
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda service: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda service: {})
|
||||
monkeypatch.setattr(handlers, "resolve_handler_proxy", fake_resolve)
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda *args: None)
|
||||
monkeypatch.setattr(handlers.Services, "load", lambda service: None)
|
||||
@@ -199,7 +199,7 @@ def test_search_reports_failed_proxy_check_as_invalid_proxy(monkeypatch):
|
||||
def failed_check(*args):
|
||||
raise ConnectionError("Proxy check failed: no IP lookup got through the proxy.")
|
||||
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda service: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda service: {})
|
||||
monkeypatch.setattr(handlers, "resolve_handler_proxy", lambda *args: ("http://127.0.0.1:9", []))
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda *args: None)
|
||||
monkeypatch.setattr(handlers.Services, "load", lambda service: None)
|
||||
|
||||
@@ -44,7 +44,7 @@ def test_a_session_keeps_the_user_config_it_was_created_with(server, monkeypatch
|
||||
|
||||
def test_the_key_block_beats_the_server_config_and_profile_sections(server, monkeypatch):
|
||||
base = {"device_id": "server", "device_key": "server", "p1": {"device_id": "p1", "other": 1}, "urls": {"a": "b"}}
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda service: json.loads(json.dumps(base)))
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda service: json.loads(json.dumps(base)))
|
||||
user = {"service_config": {"example": {"device_id": "key", "device_key": "key"}}}
|
||||
|
||||
merged = handlers.key_service_yaml("EXAMPLE", user)
|
||||
@@ -172,7 +172,7 @@ def test_a_reload_checks_key_devices_against_the_new_config(server, monkeypatch)
|
||||
|
||||
def test_a_block_value_reaches_only_plain_values_of_other_sections(server, monkeypatch):
|
||||
base = {"p1": {"device_id": "p1", "headers": {"a": "b"}}, "device_key": "server"}
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda service: json.loads(json.dumps(base)))
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda service: json.loads(json.dumps(base)))
|
||||
block = {"device_id": "key", "headers": {"c": "d"}, "device_key": {"nested": 1}}
|
||||
|
||||
merged = handlers.key_service_yaml("EXAMPLE", {"service_config": {"EXAMPLE": block}})
|
||||
|
||||
@@ -59,7 +59,7 @@ def seen(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(config.directories, "cache", tmp_path)
|
||||
monkeypatch.setattr(stats, "mode", "remote_only")
|
||||
monkeypatch.setattr(handlers, "validate_service", lambda tag, request=None: tag)
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda service: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda service: {})
|
||||
monkeypatch.setattr(handlers, "resolve_handler_proxy", lambda *args: (None, []))
|
||||
monkeypatch.setattr(handlers, "resolve_server_account", lambda *args: None)
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda *args: None)
|
||||
|
||||
@@ -71,7 +71,7 @@ def test_single_account_always_same(accounts):
|
||||
|
||||
|
||||
def test_client_managed_gets_nothing(monkeypatch, accounts):
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda s: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda s: {})
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
|
||||
monkeypatch.setattr(handlers.Services, "load", staticmethod(lambda s: object()))
|
||||
monkeypatch.setattr(handlers, "instantiate_service", lambda *a: "svc")
|
||||
@@ -101,7 +101,7 @@ def test_rotation_per_region_pool(accounts):
|
||||
|
||||
|
||||
def test_server_account_ignores_client_credentials(monkeypatch, accounts):
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda s: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda s: {})
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
|
||||
monkeypatch.setattr(handlers.Services, "load", staticmethod(lambda s: object()))
|
||||
monkeypatch.setattr(handlers, "instantiate_service", lambda *a: "svc")
|
||||
@@ -125,7 +125,7 @@ def test_server_cdm_ignores_client_cdm_type(monkeypatch):
|
||||
"""A server_cdm tier licenses with the server's own device, so the client's CDM type must
|
||||
not swap it for a stub of the other DRM system."""
|
||||
seen = []
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda s: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda s: {})
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda s, p, t=None, app_config=None: seen.append(t))
|
||||
monkeypatch.setattr(handlers.Services, "load", staticmethod(lambda s: object()))
|
||||
monkeypatch.setattr(handlers, "instantiate_service", lambda *a: "svc")
|
||||
@@ -155,7 +155,7 @@ def _lang_option_module():
|
||||
|
||||
|
||||
def _create_with_real_ctx(monkeypatch, data):
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda s: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda s: {})
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
|
||||
monkeypatch.setattr(handlers.Services, "load", staticmethod(lambda s: _lang_option_module()))
|
||||
svc, _, _ = handlers.create_service_instance("OTHER", "t", data, None, [], None)
|
||||
@@ -230,7 +230,7 @@ def test_malformed_dl_params_are_ignored(monkeypatch, dl_params):
|
||||
|
||||
|
||||
def test_profile_reaches_the_parent_ctx_params(monkeypatch):
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda s: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda s: {})
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
|
||||
monkeypatch.setattr(handlers.Services, "load", staticmethod(lambda s: _lang_option_module()))
|
||||
svc, _, _ = handlers.create_service_instance("OTHER", "t", {}, None, [], "work")
|
||||
|
||||
@@ -478,7 +478,7 @@ def test_a_client_device_session_builds_the_service_on_a_stub_of_that_device(mon
|
||||
def no_server_device(*a, **k):
|
||||
raise AssertionError("a client-device session must not load the server's device")
|
||||
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda tag: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda tag: {})
|
||||
monkeypatch.setattr(handlers, "load_full_cdm", no_server_device)
|
||||
monkeypatch.setattr(handlers, "served_context", lambda obj, *a: built.update(cdm=obj.cdm))
|
||||
monkeypatch.setattr(handlers.Services, "load", lambda tag: object)
|
||||
|
||||
@@ -237,7 +237,7 @@ def test_the_server_device_follows_the_client_only_when_both_systems_are_mapped(
|
||||
):
|
||||
asked: list = []
|
||||
monkeypatch.setattr(handlers.config, "cdm", mapping, raising=False)
|
||||
monkeypatch.setattr(handlers, "load_service_yaml", lambda tag: {})
|
||||
monkeypatch.setattr(handlers, "load_service_config", lambda tag: {})
|
||||
monkeypatch.setattr(
|
||||
handlers, "load_full_cdm", lambda service, profile, cdm_type, app_config=None: asked.append(cdm_type)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user