Merge pull request 'add tests for machines api' (#169) from Mic92-main into main
This commit is contained in:
@@ -3,11 +3,15 @@ import argparse
|
|||||||
from .folders import machine_folder
|
from .folders import machine_folder
|
||||||
|
|
||||||
|
|
||||||
def create_command(args: argparse.Namespace) -> None:
|
def create_machine(name: str) -> None:
|
||||||
folder = machine_folder(args.host)
|
folder = machine_folder(name)
|
||||||
folder.mkdir(parents=True, exist_ok=True)
|
folder.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
|
def create_command(args: argparse.Namespace) -> None:
|
||||||
|
create_machine(args.host)
|
||||||
|
|
||||||
|
|
||||||
def register_create_parser(parser: argparse.ArgumentParser) -> None:
|
def register_create_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
parser.add_argument("host", type=str)
|
parser.add_argument("host", type=str)
|
||||||
parser.set_defaults(func=create_command)
|
parser.set_defaults(func=create_command)
|
||||||
|
|||||||
@@ -1,14 +1,19 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter, Body
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
from ...machines.create import create_machine as _create_machine
|
||||||
|
from ...machines.list import list_machines as _list_machines
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
ONLINE = "online"
|
ONLINE = "online"
|
||||||
OFFLINE = "offline"
|
OFFLINE = "offline"
|
||||||
|
UNKNOWN = "unknown"
|
||||||
|
|
||||||
|
|
||||||
class Machine(BaseModel):
|
class Machine(BaseModel):
|
||||||
@@ -16,6 +21,10 @@ class Machine(BaseModel):
|
|||||||
status: Status
|
status: Status
|
||||||
|
|
||||||
|
|
||||||
|
class MachineCreate(BaseModel):
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
class MachinesResponse(BaseModel):
|
class MachinesResponse(BaseModel):
|
||||||
machines: list[Machine]
|
machines: list[Machine]
|
||||||
|
|
||||||
@@ -42,24 +51,38 @@ class SchemaResponse(BaseModel):
|
|||||||
|
|
||||||
@router.get("/api/machines")
|
@router.get("/api/machines")
|
||||||
async def list_machines() -> MachinesResponse:
|
async def list_machines() -> MachinesResponse:
|
||||||
return MachinesResponse(machines=[])
|
machines = []
|
||||||
|
for m in _list_machines():
|
||||||
|
machines.append(Machine(name=m, status=Status.UNKNOWN))
|
||||||
|
return MachinesResponse(machines=machines)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/api/machines/{machine}")
|
@router.post("/api/machines", status_code=201)
|
||||||
async def get_machine(machine: str) -> MachineResponse:
|
async def create_machine(machine: Annotated[MachineCreate, Body()]) -> MachineResponse:
|
||||||
return MachineResponse(machine=Machine(name=machine, status=Status.ONLINE))
|
_create_machine(machine.name)
|
||||||
|
return MachineResponse(machine=Machine(name=machine.name, status=Status.UNKNOWN))
|
||||||
|
|
||||||
|
|
||||||
@router.get("/api/machines/{machine}/config")
|
@router.get("/api/machines/{name}")
|
||||||
async def get_machine_config(machine: str) -> ConfigResponse:
|
async def get_machine(name: str) -> MachineResponse:
|
||||||
|
print("TODO")
|
||||||
|
return MachineResponse(machine=Machine(name=name, status=Status.UNKNOWN))
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/api/machines/{name}/config")
|
||||||
|
async def get_machine_config(name: str) -> ConfigResponse:
|
||||||
return ConfigResponse(config=Config())
|
return ConfigResponse(config=Config())
|
||||||
|
|
||||||
|
|
||||||
@router.post("/api/machines/{machine}/config")
|
@router.put("/api/machines/{name}/config")
|
||||||
async def set_machine_config(machine: str, config: Config) -> ConfigResponse:
|
async def set_machine_config(
|
||||||
|
name: str, config: Annotated[Config, Body()]
|
||||||
|
) -> ConfigResponse:
|
||||||
|
print("TODO")
|
||||||
return ConfigResponse(config=config)
|
return ConfigResponse(config=config)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/api/machines/{machine}/schema")
|
@router.get("/api/machines/{name}/schema")
|
||||||
async def get_machine_schema(machine: str) -> SchemaResponse:
|
async def get_machine_schema(name: str) -> SchemaResponse:
|
||||||
|
print("TODO")
|
||||||
return SchemaResponse(schema=Schema())
|
return SchemaResponse(schema=Schema())
|
||||||
|
|||||||
9
pkgs/clan-cli/tests/api.py
Normal file
9
pkgs/clan-cli/tests/api.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from clan_cli.webui.app import app
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session")
|
||||||
|
def api() -> TestClient:
|
||||||
|
return TestClient(app)
|
||||||
@@ -4,6 +4,7 @@ import sys
|
|||||||
sys.path.append(os.path.join(os.path.dirname(__file__), "helpers"))
|
sys.path.append(os.path.join(os.path.dirname(__file__), "helpers"))
|
||||||
|
|
||||||
pytest_plugins = [
|
pytest_plugins = [
|
||||||
|
"api",
|
||||||
"temporary_dir",
|
"temporary_dir",
|
||||||
"clan_flake",
|
"clan_flake",
|
||||||
"root",
|
"root",
|
||||||
|
|||||||
21
pkgs/clan-cli/tests/test_api_machines.py
Normal file
21
pkgs/clan-cli/tests/test_api_machines.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from api import TestClient
|
||||||
|
|
||||||
|
|
||||||
|
def test_machines(api: TestClient, clan_flake: Path) -> None:
|
||||||
|
response = api.get("/api/machines")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"machines": []}
|
||||||
|
|
||||||
|
response = api.post("/api/machines", json={"name": "test"})
|
||||||
|
assert response.status_code == 201
|
||||||
|
assert response.json() == {"machine": {"name": "test", "status": "unknown"}}
|
||||||
|
|
||||||
|
response = api.get("/api/machines/test")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"machine": {"name": "test", "status": "unknown"}}
|
||||||
|
|
||||||
|
response = api.get("/api/machines")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"machines": [{"name": "test", "status": "unknown"}]}
|
||||||
Reference in New Issue
Block a user