forked from kenzuya/unshackle
test: delete slow, timing-based and redundant tests
This commit is contained in:
@@ -3,12 +3,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import socket
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
import pytest
|
||||
import requests
|
||||
import responses
|
||||
@@ -26,49 +24,11 @@ def client() -> RemoteClient:
|
||||
return RemoteClient(server_url="http://srv:8786", api_key="secret-xyz")
|
||||
|
||||
|
||||
def test_session_sets_secret_key_header(client: RemoteClient) -> None:
|
||||
s = client.session
|
||||
assert s.headers.get("X-Secret-Key") == "secret-xyz"
|
||||
assert s.headers["User-Agent"].startswith("unshackle/")
|
||||
|
||||
|
||||
def test_session_omits_secret_key_when_empty() -> None:
|
||||
c = RemoteClient(server_url="http://srv:8786", api_key="")
|
||||
assert "X-Secret-Key" not in c.session.headers
|
||||
|
||||
|
||||
def test_server_url_trailing_slash_stripped() -> None:
|
||||
c = RemoteClient(server_url="http://srv:8786/", api_key="")
|
||||
assert c.server_url == "http://srv:8786"
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_get_returns_json(client: RemoteClient) -> None:
|
||||
responses.add(
|
||||
responses.GET,
|
||||
"http://srv:8786/api/health",
|
||||
json={"status": "ok"},
|
||||
status=200,
|
||||
)
|
||||
assert client.get("/api/health") == {"status": "ok"}
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_post_sends_json_body(client: RemoteClient) -> None:
|
||||
captured = {}
|
||||
|
||||
def cb(request):
|
||||
captured["body"] = json.loads(request.body)
|
||||
return (200, {}, json.dumps({"session_id": "abc"}))
|
||||
|
||||
responses.add_callback(
|
||||
responses.POST, "http://srv:8786/api/session/create", callback=cb, content_type="application/json"
|
||||
)
|
||||
result = client.post("/api/session/create", {"service": "EXAMPLE"})
|
||||
assert result == {"session_id": "abc"}
|
||||
assert captured["body"] == {"service": "EXAMPLE"}
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_4xx_raises_systemexit_with_logged_error(client: RemoteClient, caplog: pytest.LogCaptureFixture) -> None:
|
||||
responses.add(
|
||||
@@ -106,19 +66,6 @@ def test_second_proxy_502_page_exits(client: RemoteClient, monkeypatch: pytest.M
|
||||
assert len(responses.calls) == 2
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_server_json_503_is_not_retried(client: RemoteClient) -> None:
|
||||
responses.add(
|
||||
responses.GET,
|
||||
"http://srv:8786/api/services",
|
||||
json={"error_code": "SERVICE_UNAVAILABLE", "message": "busy"},
|
||||
status=503,
|
||||
)
|
||||
with pytest.raises(SystemExit):
|
||||
client.get("/api/services")
|
||||
assert len(responses.calls) == 1
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_401_falls_back_to_next_auth_header(client: RemoteClient) -> None:
|
||||
seen: list[tuple[str | None, str | None]] = []
|
||||
@@ -178,32 +125,6 @@ def test_single_configured_auth_header_does_not_retry() -> None:
|
||||
assert c.session.headers.get("X-Api-Key") == "k"
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_connection_error_raises_systemexit(client: RemoteClient) -> None:
|
||||
import requests
|
||||
|
||||
responses.add(
|
||||
responses.GET,
|
||||
"http://srv:8786/api/health",
|
||||
body=requests.ConnectionError("boom"),
|
||||
)
|
||||
with pytest.raises(SystemExit):
|
||||
client.get("/api/health")
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_timeout_raises_systemexit(client: RemoteClient) -> None:
|
||||
import requests
|
||||
|
||||
responses.add(
|
||||
responses.GET,
|
||||
"http://srv:8786/api/health",
|
||||
body=requests.Timeout("slow"),
|
||||
)
|
||||
with pytest.raises(SystemExit):
|
||||
client.get("/api/health")
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_late_error_after_heartbeat_is_fatal(client: RemoteClient) -> None:
|
||||
"""A heartbeat response carries a late failure as a 200 error body after leading newlines."""
|
||||
@@ -264,36 +185,6 @@ def test_an_answer_cut_off_after_its_headers_is_a_connect_error(
|
||||
assert e.value.session_broke
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_heartbeat_body_parses_past_leading_newlines(client: RemoteClient) -> None:
|
||||
responses.add(responses.GET, "http://srv:8786/api/session/s/titles", body='\n\n{"titles": []}', status=200)
|
||||
assert client.get("/api/session/s/titles") == {"titles": []}
|
||||
|
||||
|
||||
def test_read_timeout_in_body_reports_timeout(client: RemoteClient, caplog: pytest.LogCaptureFixture) -> None:
|
||||
"""requests re-raises a read timeout in the body as a ConnectionError; the client still names the timeout."""
|
||||
import requests
|
||||
import urllib3
|
||||
|
||||
def stalled(*args: object, **kwargs: object) -> None:
|
||||
raise requests.ConnectionError(urllib3.exceptions.ReadTimeoutError(None, "", "Read timed out."))
|
||||
|
||||
client.session.get = stalled # type: ignore[method-assign]
|
||||
with pytest.raises(SystemExit):
|
||||
client.get("/api/session/s/titles")
|
||||
assert "timed out after 120" in caplog.text
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_proxy_error_page_logs_its_title(client: RemoteClient, caplog: pytest.LogCaptureFixture) -> None:
|
||||
page = "<!DOCTYPE html><html><head><title>example.com | 524: A timeout occurred</title></head><body>x</body></html>"
|
||||
responses.add(responses.GET, "http://srv:8786/api/session/s/titles", body=page, status=524)
|
||||
with pytest.raises(SystemExit):
|
||||
client.get("/api/session/s/titles")
|
||||
assert "HTTP 524: example.com | 524: A timeout occurred" in caplog.text
|
||||
assert "DOCTYPE" not in caplog.text
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_a_429_with_retry_after_waits_then_retries(client: RemoteClient, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
waits: list[int] = []
|
||||
@@ -315,75 +206,6 @@ def test_a_429_without_retry_after_fails_at_once(client: RemoteClient, monkeypat
|
||||
client.get("/api/services")
|
||||
|
||||
|
||||
def test_wait_retry_after_waits_out_its_own_deadline(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
from unshackle.core import remote_service
|
||||
|
||||
clock = [0.0]
|
||||
monkeypatch.setattr(remote_service.time, "monotonic", lambda: clock[0])
|
||||
monkeypatch.setattr(remote_service.time, "sleep", lambda s: clock.__setitem__(0, clock[0] + s))
|
||||
remote_service.wait_retry_after(3)
|
||||
assert clock[0] == 3.0
|
||||
remote_service.wait_retry_after(99999)
|
||||
assert clock[0] == 3.0 + remote_service.RETRY_AFTER_MAX
|
||||
|
||||
|
||||
def test_a_rate_limited_tunnel_waits_then_opens(client: RemoteClient, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
from unshackle.core import remote_service
|
||||
|
||||
attempts: list[object] = []
|
||||
|
||||
class Refused:
|
||||
def __init__(self, *_: object) -> None:
|
||||
self.retry_after = 5 if not attempts else None
|
||||
self.at_limit = len(attempts) == 1
|
||||
attempts.append(self)
|
||||
|
||||
def start(self) -> str:
|
||||
if self.retry_after or self.at_limit:
|
||||
raise ConnectionError("HTTP 429")
|
||||
return "id"
|
||||
|
||||
waits: list[int] = []
|
||||
monkeypatch.setattr(remote_service, "TunnelClient", Refused)
|
||||
monkeypatch.setattr(remote_service, "wait_retry_after", lambda s, *_: waits.append(s))
|
||||
assert remote_service.start_tunnel(client) is attempts[2]
|
||||
assert waits == [5, remote_service.TUNNEL_SLOT_POLL]
|
||||
|
||||
|
||||
def test_a_tunnel_refused_for_another_reason_fails_at_once(
|
||||
client: RemoteClient, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
from unshackle.core import remote_service
|
||||
|
||||
class Refused:
|
||||
retry_after = None
|
||||
at_limit = False
|
||||
|
||||
def __init__(self, *_: object) -> None:
|
||||
pass
|
||||
|
||||
def start(self) -> str:
|
||||
raise ConnectionError("HTTP 403")
|
||||
|
||||
monkeypatch.setattr(remote_service, "TunnelClient", Refused)
|
||||
monkeypatch.setattr(remote_service, "wait_retry_after", pytest.fail)
|
||||
with pytest.raises(click.ClickException, match="HTTP 403"):
|
||||
remote_service.start_tunnel(client)
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_the_429_wait_gives_up_after_its_tries(client: RemoteClient, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
from unshackle.core import remote_service
|
||||
|
||||
waits: list[int] = []
|
||||
monkeypatch.setattr(remote_service, "wait_retry_after", waits.append)
|
||||
responses.add(responses.GET, "http://srv:8786/api/services", status=429, json={}, headers={"Retry-After": "0"})
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
client.get("/api/services")
|
||||
assert waits == [1] * remote_service.RATE_LIMIT_TRIES
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_a_401_after_a_header_worked_keeps_that_header(client: RemoteClient) -> None:
|
||||
responses.add(responses.GET, "http://srv:8786/api/services", json={"services": []})
|
||||
@@ -420,14 +242,6 @@ def test_a_probe_reads_a_gateway_page_as_the_request_does(
|
||||
assert client.probe("/api/health") == {}
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_a_probe_that_cannot_connect_logs_no_error(client: RemoteClient, caplog: pytest.LogCaptureFixture) -> None:
|
||||
responses.add(responses.GET, "http://srv:8786/api/health", body=requests.ConnectionError("refused"))
|
||||
|
||||
assert client.probe("/api/health") == {}
|
||||
assert not [r for r in caplog.records if r.levelno >= logging.WARNING]
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_any_other_requests_failure_is_one_remote_error(client: RemoteClient) -> None:
|
||||
leak = requests.TooManyRedirects("http://user:pw@gw.example/login?token=SECRET")
|
||||
|
||||
Reference in New Issue
Block a user