Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions fastly_compute/testing/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions fastly_compute/testing.py → fastly_compute/testing/viceroy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}
Expand All @@ -355,7 +355,7 @@ def ephemeral_wasm(cls):
@app.post("/<func_path>")
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
Expand All @@ -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)


Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
13 changes: 6 additions & 7 deletions fastly_compute/tests/test_backend_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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})

Expand Down
Loading