Merge branch 'labthings-debug-logging-config' into 'v3'
Add labthings debug config when spinning up a server See merge request openflexure/openflexure-microscope-server!523
This commit is contained in:
commit
01a19fab9b
2 changed files with 44 additions and 3 deletions
|
|
@ -72,9 +72,9 @@ def set_shutdown_function(shutdown_function: Callable[[], None]) -> None:
|
|||
|
||||
|
||||
def customise_server(
|
||||
server: lt.ThingServer, application_config: OFMApplicationData
|
||||
server: lt.ThingServer, application_config: OFMApplicationData, debug: bool = False
|
||||
) -> None:
|
||||
"""Customise the server with additional endpoints, etc."""
|
||||
"""Customise the server with additional endpoints, debug mode etc."""
|
||||
if DEVELOPER_MODE:
|
||||
# Allow CORS in developer mode for easier testing with the webapp
|
||||
server.app.add_middleware(
|
||||
|
|
@ -88,6 +88,10 @@ def customise_server(
|
|||
add_v2_endpoints(server)
|
||||
add_static_files(server.app, application_config.data_folder)
|
||||
|
||||
# Configure logging to DEBUG if requested in CLI args.
|
||||
if debug:
|
||||
lt.logs.configure_thing_logger(logging.DEBUG)
|
||||
|
||||
# Add an endpoint to get the logs - (directly calling the FastAPI decorator)
|
||||
server.app.get("/log/")(retrieve_log)
|
||||
server.app.get("/logfile/")(retrieve_log_from_file)
|
||||
|
|
@ -114,7 +118,8 @@ def serve_from_cli(argv: Optional[list[str]] = None) -> None:
|
|||
configure_logging(application_config.log_folder)
|
||||
|
||||
server = lt.ThingServer.from_config(lt_config)
|
||||
customise_server(server, application_config)
|
||||
debug = bool(args.debug)
|
||||
customise_server(server, application_config, debug)
|
||||
|
||||
def shutdown_call() -> None:
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -108,3 +108,39 @@ def test_failed_customise(mocker):
|
|||
assert isinstance(fallback_app, FastAPI)
|
||||
# An that it has the error to display
|
||||
assert str(fallback_app._context.error) == "Can't touch this"
|
||||
|
||||
|
||||
def test_debug_mode(mocker):
|
||||
"""Test that --debug flag triggers lt.logs.configure_thing_logger."""
|
||||
# Mock the LabThings function that returns the server
|
||||
mocker.patch(
|
||||
"openflexure_microscope_server.server.lt.ThingServer.from_config",
|
||||
return_value=mocker.Mock(),
|
||||
)
|
||||
# Mock customisation dependencies to avoid side effects
|
||||
mocker.patch.object(ofm_server, "add_v2_endpoints")
|
||||
mocker.patch.object(ofm_server, "add_static_files")
|
||||
# Mock logging to avoid side effects and allow checking calls
|
||||
mocker.patch.object(ofm_server, "configure_logging")
|
||||
mocker.patch.object(ofm_server, "retrieve_log")
|
||||
mocker.patch.object(ofm_server, "retrieve_log_from_file")
|
||||
# Mock uvicorn.run to avoid starting a server
|
||||
mocker.patch("openflexure_microscope_server.server.uvicorn.run")
|
||||
|
||||
# Mock the target function
|
||||
mock_configure_thing_logger = mocker.patch(
|
||||
"openflexure_microscope_server.server.lt.logs.configure_thing_logger"
|
||||
)
|
||||
|
||||
# 1. Run with --debug
|
||||
ofm_server.serve_from_cli(["-c", SIM_CONFIG, "--debug"])
|
||||
|
||||
# Verify it was called with DEBUG level
|
||||
mock_configure_thing_logger.assert_called_once_with(logging.DEBUG)
|
||||
|
||||
# 2. Run without --debug
|
||||
mock_configure_thing_logger.reset_mock()
|
||||
ofm_server.serve_from_cli(["-c", SIM_CONFIG])
|
||||
|
||||
# Verify it was NOT called
|
||||
mock_configure_thing_logger.assert_not_called()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue