Skip to content

Commit

Permalink
Add a --validate option to tuberd server
Browse files Browse the repository at this point in the history
This removes the proxy configuration from the testing infrastructure in favor of
a compact server-side option that is accessible to the user.
  • Loading branch information
arahlin committed Sep 11, 2024
1 parent 97e38db commit 3d4ca88
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 205 deletions.
133 changes: 13 additions & 120 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import cbor2
import http.server
import json
import jsonschema
import os
import pytest
import requests
import socketserver
import subprocess
import sys
import threading
import urllib
import warnings

from tuber import schema, codecs
from tuber import codecs

pytest_plugins = ("pytest_asyncio",)

Expand All @@ -32,9 +25,8 @@ def pytest_addoption(parser):
# changes test behaviour.
parser.addoption("--orjson", action="store_true", default=False)

# Allow tuberd and proxy ports to be specified
# Allow tuberd port to be specified
parser.addoption("--tuberd-port", default=8080)
parser.addoption("--proxy-port", default=8081)


# Some tests require orjson - the following skips them unless we're in
Expand All @@ -49,103 +41,12 @@ def pytest_collection_modifyitems(config, items):


@pytest.fixture(scope="module")
def proxy_uri(pytestconfig):
return f"http://localhost:{pytestconfig.getoption('proxy_port')}/tuber"
def tuberd_host(pytestconfig):
return f"localhost:{pytestconfig.getoption('tuberd_port')}"


@pytest.fixture(scope="module")
def tuberd_uri(pytestconfig):
return f"http://localhost:{pytestconfig.getoption('tuberd_port')}/tuber"


@pytest.fixture(scope="module")
def tuberd(tuberd_noproxy, proxy_uri, tuberd_uri):
# Create a proxy server that does nothing but validate schemas

def proxy_server():
adapter = requests.adapters.HTTPAdapter(
max_retries=requests.packages.urllib3.util.retry.Retry(total=10, backoff_factor=1)
)
session = requests.Session()
session.mount(tuberd_uri, adapter)

class ProxyHandler(http.server.SimpleHTTPRequestHandler):
def log_message(self, *a, **kw):
# be quiet
pass

def do_POST(self):
content_length = int(self.headers["Content-Length"])
request_data = self.rfile.read(content_length)
req = urllib.request.Request(
tuberd_uri,
data=request_data,
headers=self.headers,
method="POST",
)

# validate request to server
request_obj = json.loads(request_data)
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
jsonschema.validate(request_obj, schema.request)
except jsonschema.ValidationError as e:
print(e)
self.send_response(500)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write(f"Request validation failed: {e}\n\n{traceback.format_exc()}".encode("utf-8"))
return

# dispatch request to server
response = session.post(tuberd_uri, request_data, headers=self.headers)

self.send_response(response.status_code)
for key, value in response.headers.items():
self.send_header(key, value)
self.end_headers()
response_data = response.content

# validate response from server
response_type = response.headers["Content-Type"]
if response_type == "application/json":
response_obj = json.loads(response_data)
elif response_type == "application/cbor":
response_obj = cbor2.loads(response_data)
else:
raise RuntimeError(f"Unexpected content-type: {response_type}")

try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
jsonschema.validate(response_obj, schema.response)
except jsonschema.ValidationError as e:
print(e)
self.send_response(500)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write(f"Response validation failed: {e}\n\n{traceback.format_exc()}".encode("utf-8"))
return

self.wfile.write(response_data)

class ProxyServer(socketserver.TCPServer):
allow_reuse_address = True

# Start the proxy server
proxy_uri_parsed = urllib.parse.urlparse(proxy_uri)
with ProxyServer((proxy_uri_parsed.hostname, proxy_uri_parsed.port), ProxyHandler) as httpd:
httpd.serve_forever()

p = threading.Thread(target=proxy_server, daemon=True)
p.start()

yield tuberd_noproxy


@pytest.fixture(scope="module")
def tuberd_noproxy(request, pytestconfig):
@pytest.fixture(scope="module", autouse=True)
def tuberd(request, pytestconfig):
"""Spawn (and kill) a tuberd"""

TUBERD_PORT = pytestconfig.getoption("tuberd_port")
Expand All @@ -160,6 +61,7 @@ def tuberd_noproxy(request, pytestconfig):
argv = tuberd + [
f"-p{TUBERD_PORT}",
f"--registry={registry}",
f"--validate",
]

argv.extend(pytestconfig.getoption("tuberd_option"))
Expand All @@ -180,20 +82,11 @@ def tuberd_noproxy(request, pytestconfig):
# normally provided by tuber.py. It's coded directly - which makes it less
# flexible, less performant, and easier to understand here.
@pytest.fixture(scope="module", params=["json", "cbor"])
def tuber_call(request, tuberd, pytestconfig):

PROXY_PORT = int(pytestconfig.getoption("tuberd_port"))
URI_PROXY = f"http://localhost:{PROXY_PORT}/tuber"

# Although the tuberd argument is not used here, it creates a dependency on
# the daemon so it's launched and terminated.
def tuber_call(request, tuberd_host):
URI = f"http://{tuberd_host}/tuber"

if request.param == "json":
accept = "application/json"
loads = json.loads
elif request.param == "cbor":
accept = "application/cbor"
loads = lambda data: cbor2.loads(data, tag_hook=codecs.cbor_tag_decode)
accept = f"application/{request.param}"
loads = codecs.Codecs[request.param].decode

# The tuber daemon can take a little while to start (in particular, it
# sources this script as a registry) - rather than adding a magic sleep to
Expand All @@ -202,7 +95,7 @@ def tuber_call(request, tuberd, pytestconfig):
max_retries=requests.packages.urllib3.util.retry.Retry(total=10, backoff_factor=1)
)
session = requests.Session()
session.mount(URI_PROXY, adapter)
session.mount(URI, adapter)

def tuber_call(json=None, **kwargs):
# The most explicit call style passes POST content via an explicit
Expand All @@ -211,7 +104,7 @@ def tuber_call(json=None, **kwargs):
# this results in a more readable code style.
return loads(
session.post(
URI_PROXY,
URI,
json=kwargs if json is None else json,
headers={"Accept": accept},
).content
Expand Down
Loading

0 comments on commit 3d4ca88

Please sign in to comment.