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")
|
||||
|
||||
@@ -30,3 +30,33 @@ def test_search_starts_a_forwarder_for_a_control_d_resolver(monkeypatch: pytest.
|
||||
assert ctx.params["proxy"] == "http://127.0.0.1:4000"
|
||||
assert ctx.params["proxy_arg"] == SENT
|
||||
assert ctx.params.get("proxy_query") is None
|
||||
|
||||
|
||||
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_search_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 search as search_module
|
||||
|
||||
recorder = Recorder()
|
||||
monkeypatch.setattr(search_module.config, "services", {"example": {"proxy_map": {"us": "ca"}}})
|
||||
monkeypatch.setattr(search_module, "load_proxy_providers", lambda: [recorder])
|
||||
monkeypatch.setattr(search_module.Services, "get_path", staticmethod(lambda tag: tmp_path))
|
||||
ctx = click.Context(search_module.search)
|
||||
ctx.invoked_subcommand = "EXAMPLE"
|
||||
ctx.params = {"proxy": "us", "no_proxy": False, "remote": remote}
|
||||
with ctx:
|
||||
assert search_module.search.callback
|
||||
search_module.search.callback(no_proxy=False, proxy="us", remote=remote)
|
||||
|
||||
assert (recorder.asked, ctx.params["proxy"]) == (asked, f"http://{asked[0]}.proxy.example:8080")
|
||||
|
||||
@@ -127,8 +127,8 @@ SERVED_LOGIN: LaunchKeys = {"served": True, "cookies_supplied": False}
|
||||
@pytest.mark.parametrize(
|
||||
("build", "expected"),
|
||||
[
|
||||
(cli_dl, {"proxy_arg": "us", "proxy_query": "us", "proxy_provider": None}),
|
||||
(cli_search, {"proxy_arg": "us", "proxy_query": "us", "proxy_provider": None}),
|
||||
(cli_dl, {"proxy_arg": "us", "proxy_query": "us"}),
|
||||
(cli_search, {"proxy_arg": "us", "proxy_query": "us"}),
|
||||
(rest_relay, {"served": True, "client_argv": "serve (REST API)"}),
|
||||
(rest_list, SERVED_LOGIN),
|
||||
(rest_search, SERVED_LOGIN),
|
||||
|
||||
@@ -230,18 +230,17 @@ class Gluetun(Alpha):
|
||||
(
|
||||
"US",
|
||||
ResolvedProxy(
|
||||
"http://a:b@alpha.example:80", None, "us", "Using Alpha Proxy: http://xxxxx:xxxxx@alpha.example:80"
|
||||
"http://a:b@alpha.example:80", "us", "Using Alpha Proxy: http://xxxxx:xxxxx@alpha.example:80"
|
||||
),
|
||||
),
|
||||
(
|
||||
"Beta:CA",
|
||||
ResolvedProxy("http://c:d@beta-ca.example:80", "Beta", "ca", "Using Beta Proxy via Dallas (server 12)"),
|
||||
ResolvedProxy("http://c:d@beta-ca.example:80", "ca", "Using Beta Proxy via Dallas (server 12)"),
|
||||
),
|
||||
(
|
||||
"alpha:beta:ca",
|
||||
ResolvedProxy(
|
||||
"http://a:b@alpha-beta.example:80",
|
||||
"alpha",
|
||||
"beta:ca",
|
||||
"Using Alpha Proxy: http://xxxxx:xxxxx@alpha-beta.example:80",
|
||||
),
|
||||
@@ -251,13 +250,12 @@ class Gluetun(Alpha):
|
||||
ResolvedProxy(
|
||||
"http://user:pass@proxy.example:8080",
|
||||
None,
|
||||
None,
|
||||
"Using explicit Proxy: http://xxxxx:xxxxx@proxy.example:8080",
|
||||
),
|
||||
),
|
||||
("localhost:8080", ResolvedProxy("localhost:8080", None, None, "Using a local proxy on port 8080")),
|
||||
("1.2.3.4:3128", ResolvedProxy("1.2.3.4:3128", None, None, "Using explicit Proxy: 1.2.3.4:3128")),
|
||||
("us:1080", ResolvedProxy("us:1080", None, None, "Using explicit Proxy: us:1080")),
|
||||
("localhost:8080", ResolvedProxy("localhost:8080", None, "Using a local proxy on port 8080")),
|
||||
("1.2.3.4:3128", ResolvedProxy("1.2.3.4:3128", None, "Using explicit Proxy: 1.2.3.4:3128")),
|
||||
("us:1080", ResolvedProxy("us:1080", None, "Using explicit Proxy: us:1080")),
|
||||
],
|
||||
)
|
||||
def test_resolve_proxy_value_reads_each_form(providers: list, value: str, expected: ResolvedProxy) -> None:
|
||||
@@ -270,7 +268,7 @@ def test_resolve_proxy_value_starts_a_forwarder_for_a_control_d_resolver(monkeyp
|
||||
|
||||
monkeypatch.setattr(controld, "local_proxy", lambda uri: "http://127.0.0.1:4000")
|
||||
resolved = resolve_proxy_value("controld://efgh456@dns.controld.com", [])
|
||||
assert resolved == ResolvedProxy("http://127.0.0.1:4000", None, None, "Using a local proxy on port 4000")
|
||||
assert resolved == ResolvedProxy("http://127.0.0.1:4000", None, "Using a local proxy on port 4000")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -292,3 +290,30 @@ def test_the_status_wraps_only_the_provider_lookup(providers: list, value: str,
|
||||
@pytest.mark.parametrize("value", ["localhost:8080", "1.2.3.4:3128"])
|
||||
def test_resolve_proxy_passes_a_host_and_port_through(providers: list, value: str) -> None:
|
||||
assert resolve_proxy(value, providers) == value
|
||||
|
||||
|
||||
class Recorder(Alpha):
|
||||
def __init__(self, **answers: str) -> None:
|
||||
super().__init__(**answers)
|
||||
self.asked: list[str] = []
|
||||
|
||||
def get_proxy(self, query: str) -> str | None:
|
||||
self.asked.append(query)
|
||||
return super().get_proxy(query)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "asked", "uri"),
|
||||
[
|
||||
("us", ["ca"], "http://ca.example:8080"),
|
||||
("recorder:us", ["uk"], "http://uk.example:8080"),
|
||||
("US", ["ca"], "http://ca.example:8080"),
|
||||
("Recorder:US", ["uk"], "http://uk.example:8080"),
|
||||
("fr", ["zz", "fr"], "http://fr.example:8080"),
|
||||
("de", ["de"], "http://de.example:8080"),
|
||||
],
|
||||
)
|
||||
def test_a_proxy_map_swaps_the_query_and_falls_back_to_it(value: str, asked: list, uri: str) -> None:
|
||||
recorder = Recorder(**{x: f"http://{x}.example:8080" for x in ("ca", "uk", "fr", "de")})
|
||||
resolved = resolve_proxy_value(value, [recorder], proxy_map={"US": "ca", "recorder:us": "uk", "fr": "zz"})
|
||||
assert (recorder.asked, resolved.uri) == (asked, uri)
|
||||
|
||||
Reference in New Issue
Block a user