feat: add daemon command + dummy server setup
This commit is contained in:
parent
be56f287ea
commit
72200752d6
4 changed files with 59 additions and 0 deletions
15
spud/cli.py
15
spud/cli.py
|
@ -8,6 +8,7 @@ import json
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
import uvicorn
|
||||||
|
|
||||||
from spud.base import Configuration
|
from spud.base import Configuration
|
||||||
|
|
||||||
|
@ -65,8 +66,22 @@ def print_config(context):
|
||||||
click.echo(config.model_dump_json(indent=2))
|
click.echo(config.model_dump_json(indent=2))
|
||||||
|
|
||||||
|
|
||||||
|
@click.command()
|
||||||
|
@click.option(
|
||||||
|
"--reload",
|
||||||
|
type=bool,
|
||||||
|
default=False,
|
||||||
|
is_flag=True,
|
||||||
|
help="Hot reloads on code updates.",
|
||||||
|
)
|
||||||
|
def daemon(reload):
|
||||||
|
"""Starts the daemon process."""
|
||||||
|
uvicorn.run("spud.daemon:app", reload=reload)
|
||||||
|
|
||||||
|
|
||||||
cli.add_command(init)
|
cli.add_command(init)
|
||||||
cli.add_command(print_config)
|
cli.add_command(print_config)
|
||||||
|
cli.add_command(daemon)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
cli(None, None)
|
cli(None, None)
|
||||||
|
|
9
spud/daemon.py
Normal file
9
spud/daemon.py
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import fastapi
|
||||||
|
|
||||||
|
app = fastapi.FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
def alive():
|
||||||
|
"""Live check."""
|
||||||
|
return 200
|
|
@ -1,4 +1,8 @@
|
||||||
import pathlib
|
import pathlib
|
||||||
|
from unittest.mock import Mock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import uvicorn
|
||||||
|
|
||||||
|
|
||||||
def test_init_raises_if_config_file_exists_default_path(invoke_cli, tmpdir):
|
def test_init_raises_if_config_file_exists_default_path(invoke_cli, tmpdir):
|
||||||
|
@ -50,3 +54,19 @@ def test_print_config_raises_if_no_config_file_custom_path(invoke_cli, tmpdir):
|
||||||
assert (
|
assert (
|
||||||
str(result.exception) == f"Configuration file not found at {str(config_file)}."
|
str(result.exception) == f"Configuration file not found at {str(config_file)}."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("reload_flag", [True, False])
|
||||||
|
def test_daemon_starts_server_with_reload_option(invoke_cli, monkeypatch, reload_flag):
|
||||||
|
run_mock = Mock()
|
||||||
|
monkeypatch.setattr(uvicorn, "run", run_mock)
|
||||||
|
|
||||||
|
args = ["daemon"]
|
||||||
|
|
||||||
|
if reload_flag:
|
||||||
|
args += ["--reload"]
|
||||||
|
result = invoke_cli(args)
|
||||||
|
|
||||||
|
assert result.exit_code == 0
|
||||||
|
|
||||||
|
run_mock.assert_called_once_with("spud.daemon:app", reload=reload_flag)
|
||||||
|
|
15
tests/test_daemon.py
Normal file
15
tests/test_daemon.py
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
import spud.daemon
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="client")
|
||||||
|
def client_fixture():
|
||||||
|
return TestClient(spud.daemon.app)
|
||||||
|
|
||||||
|
|
||||||
|
def test_alive_returns_200(client):
|
||||||
|
response = client.get("/")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
Reference in a new issue