From 692a024d08d3ae0193037915c6aefe77c202874d Mon Sep 17 00:00:00 2001 From: Erik Rose Date: Wed, 25 Mar 2026 11:31:21 -0400 Subject: [PATCH] Move `test_server` module into `testing`. Where it was before, it looked like a test. Even renamed, it would have been strange to have it as a special, favored peer of `testing`, where all the other test machinery lived. Let's pull it and the Viceroy stuff into a `testing` package we can expand as necessary. Making `_ViceroyException` and `_ViceroyReturn` public so the autogenerated test-running wasm module can import them from `testing` seemed slightly preferable to having it reach invasively into `testing.viceroy`. But I'm open to persuasion, as customers shouldn't need them. --- fastly_compute/testing/__init__.py | 14 ++++++++++++++ .../mock_http_server.py} | 2 +- fastly_compute/{testing.py => testing/viceroy.py} | 12 ++++++------ fastly_compute/tests/test_backend_requests.py | 13 ++++++------- 4 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 fastly_compute/testing/__init__.py rename fastly_compute/{test_server.py => testing/mock_http_server.py} (99%) rename fastly_compute/{testing.py => testing/viceroy.py} (98%) diff --git a/fastly_compute/testing/__init__.py b/fastly_compute/testing/__init__.py new file mode 100644 index 0000000..07fc269 --- /dev/null +++ b/fastly_compute/testing/__init__.py @@ -0,0 +1,14 @@ +"""Utilities for customers to use in their own tests""" + +# This module exists only to re-export public things. +# ruff: noqa F401 + +from .mock_http_server import MockHttpServer, make_test_request_handler +from .viceroy import ( + AutoViceroyTestBase, + ViceroyServer, + ViceroyReturn, + ViceroyException, + ViceroyTestBase, + on_viceroy, +) diff --git a/fastly_compute/test_server.py b/fastly_compute/testing/mock_http_server.py similarity index 99% rename from fastly_compute/test_server.py rename to fastly_compute/testing/mock_http_server.py index 89defb5..45793fb 100644 --- a/fastly_compute/test_server.py +++ b/fastly_compute/testing/mock_http_server.py @@ -126,7 +126,7 @@ def make_test_request_handler( ) -class LocalTestServer: +class MockHttpServer: """Local HTTP server for backend testing. This server can be used to mock external backends during testing. diff --git a/fastly_compute/testing.py b/fastly_compute/testing/viceroy.py similarity index 98% rename from fastly_compute/testing.py rename to fastly_compute/testing/viceroy.py index b6bbfad..faae714 100644 --- a/fastly_compute/testing.py +++ b/fastly_compute/testing/viceroy.py @@ -341,7 +341,7 @@ def ephemeral_wasm(cls): import bottle from bottle import Bottle, post -from fastly_compute.testing import AutoViceroyTestBase, _ViceroyException, _ViceroyReturn +from fastly_compute.testing import AutoViceroyTestBase, ViceroyException, ViceroyReturn AutoViceroyTestBase._is_on_viceroy = True import {cls.__module__} @@ -355,7 +355,7 @@ def ephemeral_wasm(cls): @app.post("/") def run_viceroy_chunk(func_path: str) -> bytes: """Run an `@on_viceroy`-decorated method from a test class in Viceroy, and - return its result, wrapped in a pickled _ViceroyReturn or _ViceroyException, + return its result, wrapped in a pickled ViceroyReturn or ViceroyException, over HTTP. :arg func_path: Fully qualified name of the function to run, typically like @@ -374,9 +374,9 @@ def run_viceroy_chunk(func_path: str) -> bytes: try: return_value = method(class_, *shipped_args, **shipped_kwargs) except Exception as exc: - result = _ViceroyException(exc) + result = ViceroyException(exc) else: - result = _ViceroyReturn(return_value) + result = ViceroyReturn(return_value) return pickle.dumps(result) @@ -419,7 +419,7 @@ def _as_class_method(method) -> classmethod: return classmethod(method) if isinstance(method, MethodType) else method -class _ViceroyException: +class ViceroyException: """An exception passed back from Viceroy-dwelling code""" def __init__(self, exception: Exception): @@ -430,7 +430,7 @@ def raise_or_return_value(self): raise self.exception -class _ViceroyReturn: +class ViceroyReturn: """A function return value passed back from Viceroy-dwelling code""" def __init__(self, return_value: Any): diff --git a/fastly_compute/tests/test_backend_requests.py b/fastly_compute/tests/test_backend_requests.py index fc8ec95..e186dc5 100644 --- a/fastly_compute/tests/test_backend_requests.py +++ b/fastly_compute/tests/test_backend_requests.py @@ -6,8 +6,7 @@ import pytest import requests -from fastly_compute.test_server import LocalTestServer -from fastly_compute.testing import ViceroyTestBase +from fastly_compute.testing import MockHttpServer, ViceroyTestBase class BackendRequestsTestBase(ViceroyTestBase): @@ -105,7 +104,7 @@ def setup_class(cls): } # Set up mock server - cls.test_server = LocalTestServer( + cls.test_server = MockHttpServer( host="127.0.0.1", port=0, responses=mock_responses ) cls.test_server_url = cls.test_server.start() @@ -225,7 +224,7 @@ def setup_class(cls): }, } - cls.test_server = LocalTestServer(host="127.0.0.1", port=0, responses=responses) + cls.test_server = MockHttpServer(host="127.0.0.1", port=0, responses=responses) cls.test_server_url = cls.test_server.start() # Set up backends for viceroy @@ -395,7 +394,7 @@ class TestRequestErrorHandling(BackendRequestsTestBase): def setup_class(cls): """Set up test backend.""" # Create a local test server for backend testing - cls.test_server = LocalTestServer(host="127.0.0.1", port=0) + cls.test_server = MockHttpServer(host="127.0.0.1", port=0) base_url = cls.test_server.start() # Set up backends for viceroy (keep the full URL with scheme) @@ -539,7 +538,7 @@ class TestBackendResolution(BackendRequestsTestBase): @classmethod def setup_class(cls): """Set up multiple backends for testing.""" - cls.test_server1 = LocalTestServer(host="127.0.0.1", port=0) + cls.test_server1 = MockHttpServer(host="127.0.0.1", port=0) base_url1 = cls.test_server1.start() cls.set_up_backends( @@ -597,7 +596,7 @@ class TestHTTPMethodHandling(BackendRequestsTestBase): @classmethod def setup_class(cls): """Set up test backend.""" - cls.test_server = LocalTestServer(host="127.0.0.1", port=0) + cls.test_server = MockHttpServer(host="127.0.0.1", port=0) base_url = cls.test_server.start() cls.set_up_backends({"test-be": base_url})