mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-27 19:35:06 +00:00
refactor(proxies): apply a service proxy_map before the proxy resolves
This commit is contained in:
@@ -12,7 +12,9 @@ from unshackle.core.remote_protocol import APIErrorCode
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
def run_dl(monkeypatch: pytest.MonkeyPatch, tmp_path: Path, proxy: str) -> click.Context:
|
||||
def run_dl(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, proxy: str, providers: list | None = None, remote: bool = False
|
||||
) -> click.Context:
|
||||
from unshackle.commands import dl as dl_module
|
||||
|
||||
monkeypatch.setattr(dl_module.config, "key_vaults", [])
|
||||
@@ -22,7 +24,8 @@ def run_dl(monkeypatch: pytest.MonkeyPatch, tmp_path: Path, proxy: str) -> click
|
||||
monkeypatch.setattr(dl_module.dl.cli, "_result_callback", dl_module.dl.cli._result_callback)
|
||||
ctx = click.Context(dl_module.dl.cli)
|
||||
ctx.invoked_subcommand = "EXAMPLE"
|
||||
dl_module.dl(ctx=ctx, no_proxy=False, proxy=proxy, proxy_providers=[])
|
||||
ctx.params["remote"] = remote
|
||||
dl_module.dl(ctx=ctx, no_proxy=False, proxy=proxy, proxy_providers=providers or [])
|
||||
return ctx
|
||||
|
||||
|
||||
@@ -33,7 +36,7 @@ def test_dl_starts_a_forwarder_for_a_control_d_resolver(monkeypatch: pytest.Monk
|
||||
monkeypatch.setattr(controld, "local_proxy", lambda uri: "http://127.0.0.1:4000")
|
||||
params = run_dl(monkeypatch, tmp_path, "controld://efgh456@dns.controld.com").params
|
||||
assert params["proxy"] == "http://127.0.0.1:4000"
|
||||
assert (params["proxy_query"], params["proxy_provider"]) == (None, None)
|
||||
assert params["proxy_query"] is None
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("example_service")
|
||||
@@ -49,3 +52,25 @@ def test_a_dl_proxy_error_is_a_proxy_error(monkeypatch: pytest.MonkeyPatch, tmp_
|
||||
with pytest.raises(PlanError) as raised:
|
||||
run_dl(monkeypatch, tmp_path, proxy)
|
||||
assert getattr(raised.value, "error_code", None) == APIErrorCode.INVALID_PROXY
|
||||
|
||||
|
||||
class Recorder:
|
||||
def __init__(self) -> None:
|
||||
self.asked: list[str] = []
|
||||
|
||||
def get_proxy(self, query: str) -> str:
|
||||
self.asked.append(query)
|
||||
return f"http://{query}.proxy.example:8080"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("example_service")
|
||||
@pytest.mark.parametrize(("remote", "asked"), [(False, ["ca"]), (True, ["us"])])
|
||||
def test_dl_resolves_a_mapped_query_once_and_only_on_a_local_run(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, remote: bool, asked: list
|
||||
) -> None:
|
||||
from unshackle.commands import dl as dl_module
|
||||
|
||||
monkeypatch.setattr(dl_module.config, "services", {"example": {"proxy_map": {"us": "ca"}}})
|
||||
recorder = Recorder()
|
||||
params = run_dl(monkeypatch, tmp_path, "us", [recorder], remote).params
|
||||
assert (recorder.asked, params["proxy"]) == (asked, f"http://{asked[0]}.proxy.example:8080")
|
||||
|
||||
Reference in New Issue
Block a user